From 913ca46f5deff31a0c938d28284b5f793fb43442 Mon Sep 17 00:00:00 2001 From: Matthew Skinner Date: Tue, 29 Nov 2022 22:27:59 +1100 Subject: [PATCH 1/5] Instar updates Signed-off-by: Matthew Skinner --- .../ipcamera/internal/InstarHandler.java | 92 ++++++++++++++----- .../internal/onvif/OnvifConnection.java | 5 +- 2 files changed, 74 insertions(+), 23 deletions(-) diff --git a/bundles/org.openhab.binding.ipcamera/src/main/java/org/openhab/binding/ipcamera/internal/InstarHandler.java b/bundles/org.openhab.binding.ipcamera/src/main/java/org/openhab/binding/ipcamera/internal/InstarHandler.java index 4c3ff3cc8613d..67be23ead7d76 100644 --- a/bundles/org.openhab.binding.ipcamera/src/main/java/org/openhab/binding/ipcamera/internal/InstarHandler.java +++ b/bundles/org.openhab.binding.ipcamera/src/main/java/org/openhab/binding/ipcamera/internal/InstarHandler.java @@ -14,6 +14,7 @@ import static org.openhab.binding.ipcamera.internal.IpCameraBindingConstants.*; +import java.math.BigDecimal; import java.util.ArrayList; import org.eclipse.jdt.annotation.NonNullByDefault; @@ -62,43 +63,61 @@ public void channelRead(@Nullable ChannelHandlerContext ctx, @Nullable Object ms ipCameraHandler.logger.trace("HTTP Result back from camera is \t:{}:", content); switch (requestUrl) { case "/param.cgi?cmd=getinfrared": - if (content.contains("var infraredstat=\"auto")) { + if (content.contains("var infraredstat=\"auto") || content.contains("infraredstat=\"2")) { ipCameraHandler.setChannelState(CHANNEL_AUTO_LED, OnOffType.ON); } else { ipCameraHandler.setChannelState(CHANNEL_AUTO_LED, OnOffType.OFF); } break; case "/param.cgi?cmd=getoverlayattr&-region=1":// Text Overlays - if (content.contains("var show_1=\"0\"")) { + if (content.contains("var show_1=\"0\"") || content.contains("show=\"0\"")) { ipCameraHandler.setChannelState(CHANNEL_TEXT_OVERLAY, StringType.EMPTY); } else { value1 = Helper.searchString(content, "var name_1=\""); if (!value1.isEmpty()) { ipCameraHandler.setChannelState(CHANNEL_TEXT_OVERLAY, StringType.valueOf(value1)); + } else { + value1 = Helper.searchString(content, "name=\""); + if (!value1.isEmpty()) { + ipCameraHandler.setChannelState(CHANNEL_TEXT_OVERLAY, StringType.valueOf(value1)); + } } } break; - case "/cgi-bin/hi3510/param.cgi?cmd=getmdattr":// Motion Alarm - // Motion Alarm + case "/cgi-bin/hi3510/param.cgi?cmd=getmdattr":// Motion Alarm old if (content.contains("var m1_enable=\"1\"")) { ipCameraHandler.setChannelState(CHANNEL_ENABLE_MOTION_ALARM, OnOffType.ON); } else { ipCameraHandler.setChannelState(CHANNEL_ENABLE_MOTION_ALARM, OnOffType.OFF); } break; - case "/cgi-bin/hi3510/param.cgi?cmd=getaudioalarmattr":// Audio Alarm - if (content.contains("var aa_enable=\"1\"")) { + case "/param.cgi?cmd=getalarmattr":// Motion Alarm new + if (content.contains("armed=\"1")) { + ipCameraHandler.setChannelState(CHANNEL_ENABLE_MOTION_ALARM, OnOffType.ON); + } else { + ipCameraHandler.setChannelState(CHANNEL_ENABLE_MOTION_ALARM, OnOffType.OFF); + } + break; + case "/param.cgi?cmd=getaudioalarmattr":// Audio Alarm + if (content.contains("enable=\"1\"")) { ipCameraHandler.setChannelState(CHANNEL_ENABLE_AUDIO_ALARM, OnOffType.ON); - value1 = Helper.searchString(content, "var aa_value=\""); - if (!value1.isEmpty()) { + value1 = Helper.searchString(content, "aa_value=\""); + if (!value1.isEmpty()) {// old cameras have threshold in percentage ipCameraHandler.setChannelState(CHANNEL_THRESHOLD_AUDIO_ALARM, PercentType.valueOf(value1)); + } else { + value1 = Helper.searchString(content, "threshold=\""); + if (!value1.isEmpty()) {// newer cameras have values up to 10 + ipCameraHandler.setChannelState(CHANNEL_THRESHOLD_AUDIO_ALARM, + PercentType.valueOf(value1 + "0")); + } } } else { ipCameraHandler.setChannelState(CHANNEL_ENABLE_AUDIO_ALARM, OnOffType.OFF); + ipCameraHandler.setChannelState(CHANNEL_THRESHOLD_AUDIO_ALARM, OnOffType.OFF); } break; - case "param.cgi?cmd=getpirattr":// PIR Alarm - if (content.contains("var pir_enable=\"1\"")) { + case "/param.cgi?cmd=getpirattr":// PIR Alarm + if (content.contains("enable=\"1\"")) { ipCameraHandler.setChannelState(CHANNEL_ENABLE_PIR_ALARM, OnOffType.ON); } else { ipCameraHandler.setChannelState(CHANNEL_ENABLE_PIR_ALARM, OnOffType.OFF); @@ -107,12 +126,14 @@ public void channelRead(@Nullable ChannelHandlerContext ctx, @Nullable Object ms ipCameraHandler.noMotionDetected(CHANNEL_PIR_ALARM); break; case "/param.cgi?cmd=getioattr":// External Alarm Input - if (content.contains("var io_enable=\"1\"")) { + if (content.contains("enable=\"1\"")) { ipCameraHandler.setChannelState(CHANNEL_ENABLE_EXTERNAL_ALARM_INPUT, OnOffType.ON); } else { ipCameraHandler.setChannelState(CHANNEL_ENABLE_EXTERNAL_ALARM_INPUT, OnOffType.OFF); } break; + default: + ipCameraHandler.logger.debug("Unknown reply from URI:{}", requestUrl); } } finally { ReferenceCountUtil.release(msg); @@ -126,14 +147,17 @@ public void handleCommand(ChannelUID channelUID, Command command) { } // end of "REFRESH" switch (channelUID.getId()) { case CHANNEL_THRESHOLD_AUDIO_ALARM: - int value = Math.round(Float.valueOf(command.toString())); - if (value == 0) { + if (OnOffType.OFF.equals(command) || PercentType.ZERO.equals(command)) { + ipCameraHandler.sendHttpGET("/param.cgi?cmd=setaudioalarmattr&enable=0"); ipCameraHandler.sendHttpGET("/cgi-bin/hi3510/param.cgi?cmd=setaudioalarmattr&-aa_enable=0"); - } else { + } else if (OnOffType.ON.equals(command)) { + ipCameraHandler.sendHttpGET("/param.cgi?cmd=setaudioalarmattr&enable=1"); ipCameraHandler.sendHttpGET("/cgi-bin/hi3510/param.cgi?cmd=setaudioalarmattr&-aa_enable=1"); - ipCameraHandler - .sendHttpGET("/cgi-bin/hi3510/param.cgi?cmd=setaudioalarmattr&-aa_enable=1&-aa_value=" - + command.toString()); + } else if (command instanceof PercentType) { + int value = ((PercentType) command).toBigDecimal().divide(BigDecimal.TEN).intValue(); + ipCameraHandler.sendHttpGET("/param.cgi?cmd=setaudioalarmattr&enable=1&threshold=" + value); + ipCameraHandler.sendHttpGET( + "/cgi-bin/hi3510/param.cgi?cmd=setaudioalarmattr&-aa_enable=1&-aa_value=" + value * 10); } return; case CHANNEL_ENABLE_AUDIO_ALARM: @@ -145,9 +169,11 @@ public void handleCommand(ChannelUID channelUID, Command command) { return; case CHANNEL_ENABLE_MOTION_ALARM: if (OnOffType.ON.equals(command)) { + ipCameraHandler.sendHttpGET("/param.cgi?cmd=setalarmattr&armed=1"); ipCameraHandler.sendHttpGET( "/cgi-bin/hi3510/param.cgi?cmd=setmdattr&-enable=1&-name=1&cmd=setmdattr&-enable=1&-name=2&cmd=setmdattr&-enable=1&-name=3&cmd=setmdattr&-enable=1&-name=4"); } else { + ipCameraHandler.sendHttpGET("/param.cgi?cmd=setalarmattr&armed=0"); ipCameraHandler.sendHttpGET( "/cgi-bin/hi3510/param.cgi?cmd=setmdattr&-enable=0&-name=1&cmd=setmdattr&-enable=0&-name=2&cmd=setmdattr&-enable=0&-name=3&cmd=setmdattr&-enable=0&-name=4"); } @@ -162,22 +188,28 @@ public void handleCommand(ChannelUID channelUID, Command command) { return; case CHANNEL_AUTO_LED: if (OnOffType.ON.equals(command)) { + ipCameraHandler.sendHttpGET("/param.cgi?cmd=setinfrared&-infraredstat=2"); ipCameraHandler.sendHttpGET("/param.cgi?cmd=setinfrared&-infraredstat=auto"); } else { + ipCameraHandler.sendHttpGET("/param.cgi?cmd=setinfrared&-infraredstat=0"); ipCameraHandler.sendHttpGET("/param.cgi?cmd=setinfrared&-infraredstat=close"); } return; case CHANNEL_ENABLE_PIR_ALARM: if (OnOffType.ON.equals(command)) { + ipCameraHandler.sendHttpGET("/param.cgi?cmd=setpirattr&enable=1"); ipCameraHandler.sendHttpGET("/param.cgi?cmd=setpirattr&-pir_enable=1"); } else { + ipCameraHandler.sendHttpGET("/param.cgi?cmd=setpirattr&enable=0"); ipCameraHandler.sendHttpGET("/param.cgi?cmd=setpirattr&-pir_enable=0"); } return; case CHANNEL_ENABLE_EXTERNAL_ALARM_INPUT: if (OnOffType.ON.equals(command)) { + ipCameraHandler.sendHttpGET("/param.cgi?cmd=setioattr&enable=1"); ipCameraHandler.sendHttpGET("/param.cgi?cmd=setioattr&-io_enable=1"); } else { + ipCameraHandler.sendHttpGET("/param.cgi?cmd=setioattr&enable=0"); ipCameraHandler.sendHttpGET("/param.cgi?cmd=setioattr&-io_enable=0"); } return; @@ -216,17 +248,33 @@ public void alarmTriggered(String alarm) { } if (!objectCode.isEmpty()) { switch (objectCode) { + // person=1, car=2, animal=4 so 1+2+4=7 means one of each. case "0":// no object break; - case "1":// person/human + case "1": ipCameraHandler.motionDetected(CHANNEL_HUMAN_ALARM); break; - case "2":// car/vehicles + case "2": + ipCameraHandler.motionDetected(CHANNEL_CAR_ALARM); + break; + case "3": + ipCameraHandler.motionDetected(CHANNEL_HUMAN_ALARM); ipCameraHandler.motionDetected(CHANNEL_CAR_ALARM); break; - case "3":// animals case "4": + ipCameraHandler.motionDetected(CHANNEL_ANIMAL_ALARM); + break; case "5": + ipCameraHandler.motionDetected(CHANNEL_HUMAN_ALARM); + ipCameraHandler.motionDetected(CHANNEL_ANIMAL_ALARM); + break; + case "6": + ipCameraHandler.motionDetected(CHANNEL_CAR_ALARM); + ipCameraHandler.motionDetected(CHANNEL_ANIMAL_ALARM); + break; + case "7": + ipCameraHandler.motionDetected(CHANNEL_HUMAN_ALARM); + ipCameraHandler.motionDetected(CHANNEL_CAR_ALARM); ipCameraHandler.motionDetected(CHANNEL_ANIMAL_ALARM); break; default: @@ -239,13 +287,13 @@ public void alarmTriggered(String alarm) { // added here. Binding steps through the list. public ArrayList getLowPriorityRequests() { ArrayList lowPriorityRequests = new ArrayList(2); - lowPriorityRequests.add("/cgi-bin/hi3510/param.cgi?cmd=getaudioalarmattr"); + lowPriorityRequests.add("/param.cgi?cmd=getaudioalarmattr"); lowPriorityRequests.add("/cgi-bin/hi3510/param.cgi?cmd=getmdattr"); + lowPriorityRequests.add("/param.cgi?cmd=getalarmattr"); lowPriorityRequests.add("/param.cgi?cmd=getinfrared"); lowPriorityRequests.add("/param.cgi?cmd=getoverlayattr&-region=1"); lowPriorityRequests.add("/param.cgi?cmd=getpirattr"); lowPriorityRequests.add("/param.cgi?cmd=getioattr"); // ext alarm input on/off - // lowPriorityRequests.add("/param.cgi?cmd=getserverinfo"); return lowPriorityRequests; } } diff --git a/bundles/org.openhab.binding.ipcamera/src/main/java/org/openhab/binding/ipcamera/internal/onvif/OnvifConnection.java b/bundles/org.openhab.binding.ipcamera/src/main/java/org/openhab/binding/ipcamera/internal/onvif/OnvifConnection.java index ed452194b13d0..94481e251cec8 100644 --- a/bundles/org.openhab.binding.ipcamera/src/main/java/org/openhab/binding/ipcamera/internal/onvif/OnvifConnection.java +++ b/bundles/org.openhab.binding.ipcamera/src/main/java/org/openhab/binding/ipcamera/internal/onvif/OnvifConnection.java @@ -409,7 +409,7 @@ HttpRequest requestBuilder(RequestType requestType, String xAddr) { request.headers().add("Content-Type", "application/soap+xml; charset=utf-8; action=\"" + actionString + "/" + requestType + "\""); request.headers().add("Charset", "utf-8"); - request.headers().set("Host", extractIPportFromUrl(xAddr)); + request.headers().set("Host", ipAddress + ":" + onvifPort); request.headers().set("Connection", HttpHeaderValues.CLOSE); request.headers().set("Accept-Encoding", "gzip, deflate"); String fullXml = "" @@ -578,8 +578,11 @@ void getIPandPortFromUrl(String url) { onvifPort = Integer.parseInt(url.substring(beginIndex + 1, endIndex)); } else {// 192.168.1.1 ipAddress = url; + deviceXAddr = "http://" + ipAddress + "/onvif/device_service"; logger.debug("No Onvif Port found when parsing:{}", url); + return; } + deviceXAddr = "http://" + ipAddress + ":" + onvifPort + "/onvif/device_service"; } public void gotoPreset(int index) { From 4c481404b69939bd77cb921f27d6a6f1b4b21771 Mon Sep 17 00:00:00 2001 From: Matthew Skinner Date: Wed, 30 Nov 2022 21:50:17 +1100 Subject: [PATCH 2/5] Detect which API to use from reply. Signed-off-by: Matthew Skinner --- .../ipcamera/internal/InstarHandler.java | 200 ++++++++++++------ .../internal/handler/IpCameraHandler.java | 1 + 2 files changed, 131 insertions(+), 70 deletions(-) diff --git a/bundles/org.openhab.binding.ipcamera/src/main/java/org/openhab/binding/ipcamera/internal/InstarHandler.java b/bundles/org.openhab.binding.ipcamera/src/main/java/org/openhab/binding/ipcamera/internal/InstarHandler.java index 67be23ead7d76..6bee405be0f53 100644 --- a/bundles/org.openhab.binding.ipcamera/src/main/java/org/openhab/binding/ipcamera/internal/InstarHandler.java +++ b/bundles/org.openhab.binding.ipcamera/src/main/java/org/openhab/binding/ipcamera/internal/InstarHandler.java @@ -133,7 +133,17 @@ public void channelRead(@Nullable ChannelHandlerContext ctx, @Nullable Object ms } break; default: - ipCameraHandler.logger.debug("Unknown reply from URI:{}", requestUrl); + if (requestUrl.startsWith("/param.cgi?cmd=setasaction&-server=1&enable=1") + && content.contains("response=\"200\";")) {// new + ipCameraHandler.newInstarApi = true; + ipCameraHandler.logger.debug("Alarm server sucessfully setup for a 2k+ Instar camera"); + } else if (requestUrl.startsWith("/param.cgi?cmd=setmdalarm&-aname=server2&-switch=on&-interval=1") + && content.startsWith("[Succeed]set ok")) { + ipCameraHandler.newInstarApi = false; + ipCameraHandler.logger.debug("Alarm server sucessfully setup for a 1080p Instar camera"); + } else { + ipCameraHandler.logger.debug("Unknown reply from URI:{}", requestUrl); + } } } finally { ReferenceCountUtil.release(msg); @@ -145,74 +155,121 @@ public void handleCommand(ChannelUID channelUID, Command command) { if (command instanceof RefreshType) { return; } // end of "REFRESH" - switch (channelUID.getId()) { - case CHANNEL_THRESHOLD_AUDIO_ALARM: - if (OnOffType.OFF.equals(command) || PercentType.ZERO.equals(command)) { - ipCameraHandler.sendHttpGET("/param.cgi?cmd=setaudioalarmattr&enable=0"); - ipCameraHandler.sendHttpGET("/cgi-bin/hi3510/param.cgi?cmd=setaudioalarmattr&-aa_enable=0"); - } else if (OnOffType.ON.equals(command)) { - ipCameraHandler.sendHttpGET("/param.cgi?cmd=setaudioalarmattr&enable=1"); - ipCameraHandler.sendHttpGET("/cgi-bin/hi3510/param.cgi?cmd=setaudioalarmattr&-aa_enable=1"); - } else if (command instanceof PercentType) { - int value = ((PercentType) command).toBigDecimal().divide(BigDecimal.TEN).intValue(); - ipCameraHandler.sendHttpGET("/param.cgi?cmd=setaudioalarmattr&enable=1&threshold=" + value); - ipCameraHandler.sendHttpGET( - "/cgi-bin/hi3510/param.cgi?cmd=setaudioalarmattr&-aa_enable=1&-aa_value=" + value * 10); - } - return; - case CHANNEL_ENABLE_AUDIO_ALARM: - if (OnOffType.ON.equals(command)) { - ipCameraHandler.sendHttpGET("/cgi-bin/hi3510/param.cgi?cmd=setaudioalarmattr&-aa_enable=1"); - } else { - ipCameraHandler.sendHttpGET("/cgi-bin/hi3510/param.cgi?cmd=setaudioalarmattr&-aa_enable=0"); - } - return; - case CHANNEL_ENABLE_MOTION_ALARM: - if (OnOffType.ON.equals(command)) { - ipCameraHandler.sendHttpGET("/param.cgi?cmd=setalarmattr&armed=1"); - ipCameraHandler.sendHttpGET( - "/cgi-bin/hi3510/param.cgi?cmd=setmdattr&-enable=1&-name=1&cmd=setmdattr&-enable=1&-name=2&cmd=setmdattr&-enable=1&-name=3&cmd=setmdattr&-enable=1&-name=4"); - } else { - ipCameraHandler.sendHttpGET("/param.cgi?cmd=setalarmattr&armed=0"); - ipCameraHandler.sendHttpGET( - "/cgi-bin/hi3510/param.cgi?cmd=setmdattr&-enable=0&-name=1&cmd=setmdattr&-enable=0&-name=2&cmd=setmdattr&-enable=0&-name=3&cmd=setmdattr&-enable=0&-name=4"); - } - return; - case CHANNEL_TEXT_OVERLAY: - String text = Helper.encodeSpecialChars(command.toString()); - if (text.isEmpty()) { - ipCameraHandler.sendHttpGET("/param.cgi?cmd=setoverlayattr&-region=1&-show=0"); - } else { - ipCameraHandler.sendHttpGET("/param.cgi?cmd=setoverlayattr&-region=1&-show=1&-name=" + text); - } - return; - case CHANNEL_AUTO_LED: - if (OnOffType.ON.equals(command)) { - ipCameraHandler.sendHttpGET("/param.cgi?cmd=setinfrared&-infraredstat=2"); - ipCameraHandler.sendHttpGET("/param.cgi?cmd=setinfrared&-infraredstat=auto"); - } else { - ipCameraHandler.sendHttpGET("/param.cgi?cmd=setinfrared&-infraredstat=0"); - ipCameraHandler.sendHttpGET("/param.cgi?cmd=setinfrared&-infraredstat=close"); - } - return; - case CHANNEL_ENABLE_PIR_ALARM: - if (OnOffType.ON.equals(command)) { - ipCameraHandler.sendHttpGET("/param.cgi?cmd=setpirattr&enable=1"); - ipCameraHandler.sendHttpGET("/param.cgi?cmd=setpirattr&-pir_enable=1"); - } else { - ipCameraHandler.sendHttpGET("/param.cgi?cmd=setpirattr&enable=0"); - ipCameraHandler.sendHttpGET("/param.cgi?cmd=setpirattr&-pir_enable=0"); - } - return; - case CHANNEL_ENABLE_EXTERNAL_ALARM_INPUT: - if (OnOffType.ON.equals(command)) { - ipCameraHandler.sendHttpGET("/param.cgi?cmd=setioattr&enable=1"); - ipCameraHandler.sendHttpGET("/param.cgi?cmd=setioattr&-io_enable=1"); - } else { - ipCameraHandler.sendHttpGET("/param.cgi?cmd=setioattr&enable=0"); - ipCameraHandler.sendHttpGET("/param.cgi?cmd=setioattr&-io_enable=0"); - } - return; + if (ipCameraHandler.newInstarApi) { + switch (channelUID.getId()) { + case CHANNEL_THRESHOLD_AUDIO_ALARM: + if (OnOffType.OFF.equals(command) || PercentType.ZERO.equals(command)) { + ipCameraHandler.sendHttpGET("/param.cgi?cmd=setaudioalarmattr&enable=0"); + } else if (OnOffType.ON.equals(command)) { + ipCameraHandler.sendHttpGET("/param.cgi?cmd=setaudioalarmattr&enable=1"); + } else if (command instanceof PercentType) { + int value = ((PercentType) command).toBigDecimal().divide(BigDecimal.TEN).intValue(); + ipCameraHandler.sendHttpGET("/param.cgi?cmd=setaudioalarmattr&enable=1&threshold=" + value); + } + return; + case CHANNEL_ENABLE_AUDIO_ALARM: + if (OnOffType.ON.equals(command)) { + ipCameraHandler.sendHttpGET("/param.cgi?cmd=setaudioalarmattr&enable=1"); + } else { + ipCameraHandler.sendHttpGET("/param.cgi?cmd=setaudioalarmattr&enable=0"); + } + return; + case CHANNEL_ENABLE_MOTION_ALARM: + if (OnOffType.ON.equals(command)) { + ipCameraHandler.sendHttpGET("/param.cgi?cmd=setalarmattr&armed=1"); + } else { + ipCameraHandler.sendHttpGET("/param.cgi?cmd=setalarmattr&armed=0"); + } + return; + case CHANNEL_TEXT_OVERLAY: + String text = Helper.encodeSpecialChars(command.toString()); + if (text.isEmpty()) { + ipCameraHandler.sendHttpGET("/param.cgi?cmd=setoverlayattr&-region=1&-show=0"); + } else { + ipCameraHandler.sendHttpGET("/param.cgi?cmd=setoverlayattr&-region=1&-show=1&-name=" + text); + } + return; + case CHANNEL_AUTO_LED: + if (OnOffType.ON.equals(command)) { + ipCameraHandler.sendHttpGET("/param.cgi?cmd=setinfrared&-infraredstat=2"); + } else { + ipCameraHandler.sendHttpGET("/param.cgi?cmd=setinfrared&-infraredstat=0"); + } + return; + case CHANNEL_ENABLE_PIR_ALARM: + if (OnOffType.ON.equals(command)) { + ipCameraHandler.sendHttpGET("/param.cgi?cmd=setpirattr&enable=1"); + } else { + ipCameraHandler.sendHttpGET("/param.cgi?cmd=setpirattr&enable=0"); + } + return; + case CHANNEL_ENABLE_EXTERNAL_ALARM_INPUT: + if (OnOffType.ON.equals(command)) { + ipCameraHandler.sendHttpGET("/param.cgi?cmd=setioattr&enable=1"); + } else { + ipCameraHandler.sendHttpGET("/param.cgi?cmd=setioattr&enable=0"); + } + return; + } + } else { + switch (channelUID.getId()) { + case CHANNEL_THRESHOLD_AUDIO_ALARM: + if (OnOffType.OFF.equals(command) || PercentType.ZERO.equals(command)) { + ipCameraHandler.sendHttpGET("/cgi-bin/hi3510/param.cgi?cmd=setaudioalarmattr&-aa_enable=0"); + } else if (OnOffType.ON.equals(command)) { + ipCameraHandler.sendHttpGET("/cgi-bin/hi3510/param.cgi?cmd=setaudioalarmattr&-aa_enable=1"); + } else if (command instanceof PercentType) { + int value = ((PercentType) command).toBigDecimal().divide(BigDecimal.TEN).intValue(); + ipCameraHandler.sendHttpGET( + "/cgi-bin/hi3510/param.cgi?cmd=setaudioalarmattr&-aa_enable=1&-aa_value=" + value * 10); + } + return; + case CHANNEL_ENABLE_AUDIO_ALARM: + if (OnOffType.ON.equals(command)) { + ipCameraHandler.sendHttpGET("/cgi-bin/hi3510/param.cgi?cmd=setaudioalarmattr&-aa_enable=1"); + } else { + ipCameraHandler.sendHttpGET("/cgi-bin/hi3510/param.cgi?cmd=setaudioalarmattr&-aa_enable=0"); + } + return; + case CHANNEL_ENABLE_MOTION_ALARM: + if (OnOffType.ON.equals(command)) { + ipCameraHandler.sendHttpGET( + "/cgi-bin/hi3510/param.cgi?cmd=setmdattr&-enable=1&-name=1&cmd=setmdattr&-enable=1&-name=2&cmd=setmdattr&-enable=1&-name=3&cmd=setmdattr&-enable=1&-name=4"); + } else { + ipCameraHandler.sendHttpGET( + "/cgi-bin/hi3510/param.cgi?cmd=setmdattr&-enable=0&-name=1&cmd=setmdattr&-enable=0&-name=2&cmd=setmdattr&-enable=0&-name=3&cmd=setmdattr&-enable=0&-name=4"); + } + return; + case CHANNEL_TEXT_OVERLAY: + String text = Helper.encodeSpecialChars(command.toString()); + if (text.isEmpty()) { + ipCameraHandler.sendHttpGET("/param.cgi?cmd=setoverlayattr&-region=1&-show=0"); + } else { + ipCameraHandler.sendHttpGET("/param.cgi?cmd=setoverlayattr&-region=1&-show=1&-name=" + text); + } + return; + case CHANNEL_AUTO_LED: + if (OnOffType.ON.equals(command)) { + ipCameraHandler.sendHttpGET("/param.cgi?cmd=setinfrared&-infraredstat=auto"); + } else { + ipCameraHandler.sendHttpGET("/param.cgi?cmd=setinfrared&-infraredstat=close"); + } + return; + case CHANNEL_ENABLE_PIR_ALARM: + if (OnOffType.ON.equals(command)) { + ipCameraHandler.sendHttpGET("/param.cgi?cmd=setpirattr&-pir_enable=1"); + } else { + ipCameraHandler.sendHttpGET("/param.cgi?cmd=setpirattr&-pir_enable=0"); + } + return; + case CHANNEL_ENABLE_EXTERNAL_ALARM_INPUT: + if (OnOffType.ON.equals(command)) { + ipCameraHandler.sendHttpGET("/param.cgi?cmd=setioattr&-io_enable=1"); + } else { + ipCameraHandler.sendHttpGET("/param.cgi?cmd=setioattr&-io_enable=0"); + } + return; + } } } @@ -278,6 +335,9 @@ public void alarmTriggered(String alarm) { ipCameraHandler.motionDetected(CHANNEL_ANIMAL_ALARM); break; default: + if (objectCode.startsWith("/instar?")) { + return;// has no object due to older Instar camera model + } ipCameraHandler.logger.debug("Unknown object detection code:{}", objectCode); } } @@ -286,7 +346,7 @@ public void alarmTriggered(String alarm) { // If a camera does not need to poll a request as often as snapshots, it can be // added here. Binding steps through the list. public ArrayList getLowPriorityRequests() { - ArrayList lowPriorityRequests = new ArrayList(2); + ArrayList lowPriorityRequests = new ArrayList(7); lowPriorityRequests.add("/param.cgi?cmd=getaudioalarmattr"); lowPriorityRequests.add("/cgi-bin/hi3510/param.cgi?cmd=getmdattr"); lowPriorityRequests.add("/param.cgi?cmd=getalarmattr"); diff --git a/bundles/org.openhab.binding.ipcamera/src/main/java/org/openhab/binding/ipcamera/internal/handler/IpCameraHandler.java b/bundles/org.openhab.binding.ipcamera/src/main/java/org/openhab/binding/ipcamera/internal/handler/IpCameraHandler.java index cc6b8f5d222bd..ff3a127352043 100644 --- a/bundles/org.openhab.binding.ipcamera/src/main/java/org/openhab/binding/ipcamera/internal/handler/IpCameraHandler.java +++ b/bundles/org.openhab.binding.ipcamera/src/main/java/org/openhab/binding/ipcamera/internal/handler/IpCameraHandler.java @@ -170,6 +170,7 @@ public class IpCameraHandler extends BaseThingHandler { private String basicAuth = ""; public boolean useBasicAuth = false; public boolean useDigestAuth = false; + public boolean newInstarApi = false; public String snapshotUri = ""; public String mjpegUri = ""; private byte[] currentSnapshot = new byte[] { (byte) 0x00 }; From d2ec791c70f5873894aded63a69f7eeae9767742 Mon Sep 17 00:00:00 2001 From: Matthew Skinner Date: Sat, 3 Dec 2022 14:32:20 +1100 Subject: [PATCH 3/5] Add handling of REFRESH to instar. Signed-off-by: Matthew Skinner --- .../ipcamera/internal/InstarHandler.java | 25 ++++++++++++++++++- .../internal/handler/IpCameraHandler.java | 3 ++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/bundles/org.openhab.binding.ipcamera/src/main/java/org/openhab/binding/ipcamera/internal/InstarHandler.java b/bundles/org.openhab.binding.ipcamera/src/main/java/org/openhab/binding/ipcamera/internal/InstarHandler.java index 6bee405be0f53..0c73adb66164b 100644 --- a/bundles/org.openhab.binding.ipcamera/src/main/java/org/openhab/binding/ipcamera/internal/InstarHandler.java +++ b/bundles/org.openhab.binding.ipcamera/src/main/java/org/openhab/binding/ipcamera/internal/InstarHandler.java @@ -150,9 +150,32 @@ public void channelRead(@Nullable ChannelHandlerContext ctx, @Nullable Object ms } } - // This handles the commands that come from the Openhab event bus. + // This handles the commands that come from the openHAB event bus. public void handleCommand(ChannelUID channelUID, Command command) { if (command instanceof RefreshType) { + switch (channelUID.getId()) { + case CHANNEL_THRESHOLD_AUDIO_ALARM: + case CHANNEL_ENABLE_AUDIO_ALARM: + ipCameraHandler.sendHttpGET("/param.cgi?cmd=getaudioalarmattr"); + return; + case CHANNEL_ENABLE_EXTERNAL_ALARM_INPUT: + ipCameraHandler.sendHttpGET("/param.cgi?cmd=getioattr"); + return; + case CHANNEL_ENABLE_MOTION_ALARM: + if (ipCameraHandler.newInstarApi) { + ipCameraHandler.sendHttpGET("/param.cgi?cmd=getalarmattr"); + } else { + ipCameraHandler.sendHttpGET("/cgi-bin/hi3510/param.cgi?cmd=getmdattr"); + } + return; + case CHANNEL_ENABLE_PIR_ALARM: + ipCameraHandler.sendHttpGET("/param.cgi?cmd=getpirattr"); + case CHANNEL_AUTO_LED: + ipCameraHandler.sendHttpGET("/param.cgi?cmd=getinfrared"); + case CHANNEL_TEXT_OVERLAY: + ipCameraHandler.sendHttpGET("/param.cgi?cmd=getoverlayattr&-region=1"); + return; + } return; } // end of "REFRESH" if (ipCameraHandler.newInstarApi) { diff --git a/bundles/org.openhab.binding.ipcamera/src/main/java/org/openhab/binding/ipcamera/internal/handler/IpCameraHandler.java b/bundles/org.openhab.binding.ipcamera/src/main/java/org/openhab/binding/ipcamera/internal/handler/IpCameraHandler.java index ff3a127352043..c424388da03c3 100644 --- a/bundles/org.openhab.binding.ipcamera/src/main/java/org/openhab/binding/ipcamera/internal/handler/IpCameraHandler.java +++ b/bundles/org.openhab.binding.ipcamera/src/main/java/org/openhab/binding/ipcamera/internal/handler/IpCameraHandler.java @@ -1643,7 +1643,8 @@ public void initialize() { if (mjpegUri.isEmpty()) { mjpegUri = "/mjpegstream.cgi?-chn=12"; } - // Newer Instar cameras use this to setup the Alarm Server + // Newer Instar cameras use this to setup the Alarm Server, plus it is used to work out which API is + // implemented based on the response to these two requests. sendHttpGET( "/param.cgi?cmd=setasaction&-server=1&enable=1&-interval=1&cmd=setasattr&-as_index=1&-as_server=" + hostIp + "&-as_port=" + SERVLET_PORT + "&-as_path=/ipcamera/" From bab16d71350d9fe6e00c171902485ef298b18d42 Mon Sep 17 00:00:00 2001 From: Matthew Skinner Date: Sat, 3 Dec 2022 15:37:44 +1100 Subject: [PATCH 4/5] Fix review suggestions. Signed-off-by: Matthew Skinner --- .../org/openhab/binding/ipcamera/internal/InstarHandler.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bundles/org.openhab.binding.ipcamera/src/main/java/org/openhab/binding/ipcamera/internal/InstarHandler.java b/bundles/org.openhab.binding.ipcamera/src/main/java/org/openhab/binding/ipcamera/internal/InstarHandler.java index 0c73adb66164b..a27cd7b264c3d 100644 --- a/bundles/org.openhab.binding.ipcamera/src/main/java/org/openhab/binding/ipcamera/internal/InstarHandler.java +++ b/bundles/org.openhab.binding.ipcamera/src/main/java/org/openhab/binding/ipcamera/internal/InstarHandler.java @@ -63,7 +63,7 @@ public void channelRead(@Nullable ChannelHandlerContext ctx, @Nullable Object ms ipCameraHandler.logger.trace("HTTP Result back from camera is \t:{}:", content); switch (requestUrl) { case "/param.cgi?cmd=getinfrared": - if (content.contains("var infraredstat=\"auto") || content.contains("infraredstat=\"2")) { + if (content.contains("var infraredstat=\"auto") || content.contains("infraredstat=\"2\"")) { ipCameraHandler.setChannelState(CHANNEL_AUTO_LED, OnOffType.ON); } else { ipCameraHandler.setChannelState(CHANNEL_AUTO_LED, OnOffType.OFF); @@ -92,7 +92,7 @@ public void channelRead(@Nullable ChannelHandlerContext ctx, @Nullable Object ms } break; case "/param.cgi?cmd=getalarmattr":// Motion Alarm new - if (content.contains("armed=\"1")) { + if (content.contains("armed=\"1\"")) { ipCameraHandler.setChannelState(CHANNEL_ENABLE_MOTION_ALARM, OnOffType.ON); } else { ipCameraHandler.setChannelState(CHANNEL_ENABLE_MOTION_ALARM, OnOffType.OFF); From 25d6399260cba14a01e2ff26602be70aa9f7b6d2 Mon Sep 17 00:00:00 2001 From: Matthew Skinner Date: Sat, 3 Dec 2022 19:51:47 +1100 Subject: [PATCH 5/5] Fix missing break; Signed-off-by: Matthew Skinner --- .../binding/ipcamera/internal/InstarHandler.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/bundles/org.openhab.binding.ipcamera/src/main/java/org/openhab/binding/ipcamera/internal/InstarHandler.java b/bundles/org.openhab.binding.ipcamera/src/main/java/org/openhab/binding/ipcamera/internal/InstarHandler.java index a27cd7b264c3d..bdacdc168bfdf 100644 --- a/bundles/org.openhab.binding.ipcamera/src/main/java/org/openhab/binding/ipcamera/internal/InstarHandler.java +++ b/bundles/org.openhab.binding.ipcamera/src/main/java/org/openhab/binding/ipcamera/internal/InstarHandler.java @@ -157,24 +157,26 @@ public void handleCommand(ChannelUID channelUID, Command command) { case CHANNEL_THRESHOLD_AUDIO_ALARM: case CHANNEL_ENABLE_AUDIO_ALARM: ipCameraHandler.sendHttpGET("/param.cgi?cmd=getaudioalarmattr"); - return; + break; case CHANNEL_ENABLE_EXTERNAL_ALARM_INPUT: ipCameraHandler.sendHttpGET("/param.cgi?cmd=getioattr"); - return; + break; case CHANNEL_ENABLE_MOTION_ALARM: if (ipCameraHandler.newInstarApi) { ipCameraHandler.sendHttpGET("/param.cgi?cmd=getalarmattr"); } else { ipCameraHandler.sendHttpGET("/cgi-bin/hi3510/param.cgi?cmd=getmdattr"); } - return; + break; case CHANNEL_ENABLE_PIR_ALARM: ipCameraHandler.sendHttpGET("/param.cgi?cmd=getpirattr"); + break; case CHANNEL_AUTO_LED: ipCameraHandler.sendHttpGET("/param.cgi?cmd=getinfrared"); + break; case CHANNEL_TEXT_OVERLAY: ipCameraHandler.sendHttpGET("/param.cgi?cmd=getoverlayattr&-region=1"); - return; + break; } return; } // end of "REFRESH"