2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.ipcamera.internal;
15 import static org.openhab.binding.ipcamera.internal.IpCameraBindingConstants.*;
17 import java.util.ArrayList;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.ipcamera.internal.handler.IpCameraHandler;
22 import org.openhab.core.library.types.OnOffType;
23 import org.openhab.core.library.types.PercentType;
24 import org.openhab.core.library.types.StringType;
25 import org.openhab.core.thing.ChannelUID;
26 import org.openhab.core.types.Command;
27 import org.openhab.core.types.RefreshType;
29 import io.netty.channel.ChannelDuplexHandler;
30 import io.netty.channel.ChannelHandlerContext;
31 import io.netty.util.ReferenceCountUtil;
34 * The {@link InstarHandler} is responsible for handling commands, which are
35 * sent to one of the channels.
37 * @author Matthew Skinner - Initial contribution
41 public class InstarHandler extends ChannelDuplexHandler {
42 private IpCameraHandler ipCameraHandler;
43 private String requestUrl = "Empty";
45 public InstarHandler(IpCameraHandler thingHandler) {
46 ipCameraHandler = thingHandler;
49 public void setURL(String url) {
53 // This handles the incoming http replies back from the camera.
55 public void channelRead(@Nullable ChannelHandlerContext ctx, @Nullable Object msg) throws Exception {
56 if (msg == null || ctx == null) {
61 String content = msg.toString();
62 ipCameraHandler.logger.trace("HTTP Result back from camera is \t:{}:", content);
64 case "/param.cgi?cmd=getinfrared":
65 if (content.contains("var infraredstat=\"auto")) {
66 ipCameraHandler.setChannelState(CHANNEL_AUTO_LED, OnOffType.ON);
68 ipCameraHandler.setChannelState(CHANNEL_AUTO_LED, OnOffType.OFF);
71 case "/param.cgi?cmd=getoverlayattr&-region=1":// Text Overlays
72 if (content.contains("var show_1=\"0\"")) {
73 ipCameraHandler.setChannelState(CHANNEL_TEXT_OVERLAY, StringType.EMPTY);
75 value1 = Helper.searchString(content, "var name_1=\"");
76 if (!value1.isEmpty()) {
77 ipCameraHandler.setChannelState(CHANNEL_TEXT_OVERLAY, StringType.valueOf(value1));
81 case "/cgi-bin/hi3510/param.cgi?cmd=getmdattr":// Motion Alarm
83 if (content.contains("var m1_enable=\"1\"")) {
84 ipCameraHandler.setChannelState(CHANNEL_ENABLE_MOTION_ALARM, OnOffType.ON);
86 ipCameraHandler.setChannelState(CHANNEL_ENABLE_MOTION_ALARM, OnOffType.OFF);
89 case "/cgi-bin/hi3510/param.cgi?cmd=getaudioalarmattr":// Audio Alarm
90 if (content.contains("var aa_enable=\"1\"")) {
91 ipCameraHandler.setChannelState(CHANNEL_ENABLE_AUDIO_ALARM, OnOffType.ON);
92 value1 = Helper.searchString(content, "var aa_value=\"");
93 if (!value1.isEmpty()) {
94 ipCameraHandler.setChannelState(CHANNEL_THRESHOLD_AUDIO_ALARM, PercentType.valueOf(value1));
97 ipCameraHandler.setChannelState(CHANNEL_ENABLE_AUDIO_ALARM, OnOffType.OFF);
100 case "param.cgi?cmd=getpirattr":// PIR Alarm
101 if (content.contains("var pir_enable=\"1\"")) {
102 ipCameraHandler.setChannelState(CHANNEL_ENABLE_PIR_ALARM, OnOffType.ON);
104 ipCameraHandler.setChannelState(CHANNEL_ENABLE_PIR_ALARM, OnOffType.OFF);
106 // Reset the Alarm, need to find better place to put this.
107 ipCameraHandler.noMotionDetected(CHANNEL_PIR_ALARM);
109 case "/param.cgi?cmd=getioattr":// External Alarm Input
110 if (content.contains("var io_enable=\"1\"")) {
111 ipCameraHandler.setChannelState(CHANNEL_ENABLE_EXTERNAL_ALARM_INPUT, OnOffType.ON);
113 ipCameraHandler.setChannelState(CHANNEL_ENABLE_EXTERNAL_ALARM_INPUT, OnOffType.OFF);
118 ReferenceCountUtil.release(msg);
122 // This handles the commands that come from the Openhab event bus.
123 public void handleCommand(ChannelUID channelUID, Command command) {
124 if (command instanceof RefreshType) {
126 } // end of "REFRESH"
127 switch (channelUID.getId()) {
128 case CHANNEL_THRESHOLD_AUDIO_ALARM:
129 int value = Math.round(Float.valueOf(command.toString()));
131 ipCameraHandler.sendHttpGET("/cgi-bin/hi3510/param.cgi?cmd=setaudioalarmattr&-aa_enable=0");
133 ipCameraHandler.sendHttpGET("/cgi-bin/hi3510/param.cgi?cmd=setaudioalarmattr&-aa_enable=1");
135 .sendHttpGET("/cgi-bin/hi3510/param.cgi?cmd=setaudioalarmattr&-aa_enable=1&-aa_value="
136 + command.toString());
139 case CHANNEL_ENABLE_AUDIO_ALARM:
140 if (OnOffType.ON.equals(command)) {
141 ipCameraHandler.sendHttpGET("/cgi-bin/hi3510/param.cgi?cmd=setaudioalarmattr&-aa_enable=1");
143 ipCameraHandler.sendHttpGET("/cgi-bin/hi3510/param.cgi?cmd=setaudioalarmattr&-aa_enable=0");
146 case CHANNEL_ENABLE_MOTION_ALARM:
147 if (OnOffType.ON.equals(command)) {
148 ipCameraHandler.sendHttpGET(
149 "/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");
151 ipCameraHandler.sendHttpGET(
152 "/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");
155 case CHANNEL_TEXT_OVERLAY:
156 String text = Helper.encodeSpecialChars(command.toString());
157 if (text.isEmpty()) {
158 ipCameraHandler.sendHttpGET("/param.cgi?cmd=setoverlayattr&-region=1&-show=0");
160 ipCameraHandler.sendHttpGET("/param.cgi?cmd=setoverlayattr&-region=1&-show=1&-name=" + text);
163 case CHANNEL_AUTO_LED:
164 if (OnOffType.ON.equals(command)) {
165 ipCameraHandler.sendHttpGET("/param.cgi?cmd=setinfrared&-infraredstat=auto");
167 ipCameraHandler.sendHttpGET("/param.cgi?cmd=setinfrared&-infraredstat=close");
170 case CHANNEL_ENABLE_PIR_ALARM:
171 if (OnOffType.ON.equals(command)) {
172 ipCameraHandler.sendHttpGET("/param.cgi?cmd=setpirattr&-pir_enable=1");
174 ipCameraHandler.sendHttpGET("/param.cgi?cmd=setpirattr&-pir_enable=0");
177 case CHANNEL_ENABLE_EXTERNAL_ALARM_INPUT:
178 if (OnOffType.ON.equals(command)) {
179 ipCameraHandler.sendHttpGET("/param.cgi?cmd=setioattr&-io_enable=1");
181 ipCameraHandler.sendHttpGET("/param.cgi?cmd=setioattr&-io_enable=0");
187 public void alarmTriggered(String alarm) {
188 // older cameras placed the & for the first query, whilst newer cameras do not.
189 // examples are /instar?&active=6 vs /instar?active=6&object=0
190 ipCameraHandler.setChannelState(CHANNEL_LAST_EVENT_DATA, new StringType(alarm));
191 String alarmCode = alarm.replaceAll(".+active=", "");
192 alarmCode = alarmCode.replaceAll("&.+", "");
193 String objectCode = alarm.replaceAll(".+object=", "");
195 case "1":// The motion area boxes 1-4
199 ipCameraHandler.motionDetected(CHANNEL_MOTION_ALARM);
202 ipCameraHandler.motionDetected(CHANNEL_PIR_ALARM);
204 case "6":// Audio Alarm
205 ipCameraHandler.audioDetected();
207 case "7":// Motion area 1 + PIR
208 case "8":// Motion area 2 + PIR
209 case "9":// Motion area 3 + PIR
210 case "10":// Motion area 4 + PIR
211 ipCameraHandler.motionDetected(CHANNEL_MOTION_ALARM);
212 ipCameraHandler.motionDetected(CHANNEL_PIR_ALARM);
215 ipCameraHandler.logger.debug("Unknown alarm code:{}", alarmCode);
217 if (!objectCode.isEmpty()) {
218 switch (objectCode) {
219 case "0":// no object
221 case "1":// person/human
222 ipCameraHandler.motionDetected(CHANNEL_HUMAN_ALARM);
224 case "2":// car/vehicles
225 ipCameraHandler.motionDetected(CHANNEL_CAR_ALARM);
230 ipCameraHandler.motionDetected(CHANNEL_ANIMAL_ALARM);
233 ipCameraHandler.logger.debug("Unknown object detection code:{}", objectCode);
238 // If a camera does not need to poll a request as often as snapshots, it can be
239 // added here. Binding steps through the list.
240 public ArrayList<String> getLowPriorityRequests() {
241 ArrayList<String> lowPriorityRequests = new ArrayList<String>(2);
242 lowPriorityRequests.add("/cgi-bin/hi3510/param.cgi?cmd=getaudioalarmattr");
243 lowPriorityRequests.add("/cgi-bin/hi3510/param.cgi?cmd=getmdattr");
244 lowPriorityRequests.add("/param.cgi?cmd=getinfrared");
245 lowPriorityRequests.add("/param.cgi?cmd=getoverlayattr&-region=1");
246 lowPriorityRequests.add("/param.cgi?cmd=getpirattr");
247 lowPriorityRequests.add("/param.cgi?cmd=getioattr"); // ext alarm input on/off
248 // lowPriorityRequests.add("/param.cgi?cmd=getserverinfo");
249 return lowPriorityRequests;