2 * Copyright (c) 2010-2021 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.thing.binding.ThingHandler;
27 import org.openhab.core.types.Command;
28 import org.openhab.core.types.RefreshType;
30 import io.netty.channel.ChannelDuplexHandler;
31 import io.netty.channel.ChannelHandlerContext;
32 import io.netty.util.ReferenceCountUtil;
35 * The {@link InstarHandler} is responsible for handling commands, which are
36 * sent to one of the channels.
38 * @author Matthew Skinner - Initial contribution
42 public class InstarHandler extends ChannelDuplexHandler {
43 private IpCameraHandler ipCameraHandler;
44 private String requestUrl = "Empty";
46 public InstarHandler(ThingHandler thingHandler) {
47 ipCameraHandler = (IpCameraHandler) thingHandler;
50 public void setURL(String url) {
54 // This handles the incoming http replies back from the camera.
56 public void channelRead(@Nullable ChannelHandlerContext ctx, @Nullable Object msg) throws Exception {
57 if (msg == null || ctx == null) {
62 String content = msg.toString();
63 ipCameraHandler.logger.trace("HTTP Result back from camera is \t:{}:", content);
65 case "/param.cgi?cmd=getinfrared":
66 if (content.contains("var infraredstat=\"auto")) {
67 ipCameraHandler.setChannelState(CHANNEL_AUTO_LED, OnOffType.ON);
69 ipCameraHandler.setChannelState(CHANNEL_AUTO_LED, OnOffType.OFF);
72 case "/param.cgi?cmd=getoverlayattr&-region=1":// Text Overlays
73 if (content.contains("var show_1=\"0\"")) {
74 ipCameraHandler.setChannelState(CHANNEL_TEXT_OVERLAY, StringType.EMPTY);
76 value1 = Helper.searchString(content, "var name_1=\"");
77 if (!value1.isEmpty()) {
78 ipCameraHandler.setChannelState(CHANNEL_TEXT_OVERLAY, StringType.valueOf(value1));
82 case "/cgi-bin/hi3510/param.cgi?cmd=getmdattr":// Motion Alarm
84 if (content.contains("var m1_enable=\"1\"")) {
85 ipCameraHandler.setChannelState(CHANNEL_ENABLE_MOTION_ALARM, OnOffType.ON);
87 ipCameraHandler.setChannelState(CHANNEL_ENABLE_MOTION_ALARM, OnOffType.OFF);
90 case "/cgi-bin/hi3510/param.cgi?cmd=getaudioalarmattr":// Audio Alarm
91 if (content.contains("var aa_enable=\"1\"")) {
92 ipCameraHandler.setChannelState(CHANNEL_ENABLE_AUDIO_ALARM, OnOffType.ON);
93 value1 = Helper.searchString(content, "var aa_value=\"");
94 if (!value1.isEmpty()) {
95 ipCameraHandler.setChannelState(CHANNEL_THRESHOLD_AUDIO_ALARM, PercentType.valueOf(value1));
98 ipCameraHandler.setChannelState(CHANNEL_ENABLE_AUDIO_ALARM, OnOffType.OFF);
101 case "param.cgi?cmd=getpirattr":// PIR Alarm
102 if (content.contains("var pir_enable=\"1\"")) {
103 ipCameraHandler.setChannelState(CHANNEL_ENABLE_PIR_ALARM, OnOffType.ON);
105 ipCameraHandler.setChannelState(CHANNEL_ENABLE_PIR_ALARM, OnOffType.OFF);
107 // Reset the Alarm, need to find better place to put this.
108 ipCameraHandler.noMotionDetected(CHANNEL_PIR_ALARM);
110 case "/param.cgi?cmd=getioattr":// External Alarm Input
111 if (content.contains("var io_enable=\"1\"")) {
112 ipCameraHandler.setChannelState(CHANNEL_ENABLE_EXTERNAL_ALARM_INPUT, OnOffType.ON);
114 ipCameraHandler.setChannelState(CHANNEL_ENABLE_EXTERNAL_ALARM_INPUT, OnOffType.OFF);
119 ReferenceCountUtil.release(msg);
123 // This handles the commands that come from the Openhab event bus.
124 public void handleCommand(ChannelUID channelUID, Command command) {
125 if (command instanceof RefreshType) {
127 } // end of "REFRESH"
128 switch (channelUID.getId()) {
129 case CHANNEL_THRESHOLD_AUDIO_ALARM:
130 int value = Math.round(Float.valueOf(command.toString()));
132 ipCameraHandler.sendHttpGET("/cgi-bin/hi3510/param.cgi?cmd=setaudioalarmattr&-aa_enable=0");
134 ipCameraHandler.sendHttpGET("/cgi-bin/hi3510/param.cgi?cmd=setaudioalarmattr&-aa_enable=1");
136 .sendHttpGET("/cgi-bin/hi3510/param.cgi?cmd=setaudioalarmattr&-aa_enable=1&-aa_value="
137 + command.toString());
140 case CHANNEL_ENABLE_AUDIO_ALARM:
141 if (OnOffType.ON.equals(command)) {
142 ipCameraHandler.sendHttpGET("/cgi-bin/hi3510/param.cgi?cmd=setaudioalarmattr&-aa_enable=1");
144 ipCameraHandler.sendHttpGET("/cgi-bin/hi3510/param.cgi?cmd=setaudioalarmattr&-aa_enable=0");
147 case CHANNEL_ENABLE_MOTION_ALARM:
148 if (OnOffType.ON.equals(command)) {
149 ipCameraHandler.sendHttpGET(
150 "/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");
152 ipCameraHandler.sendHttpGET(
153 "/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");
156 case CHANNEL_TEXT_OVERLAY:
157 String text = Helper.encodeSpecialChars(command.toString());
158 if (text.isEmpty()) {
159 ipCameraHandler.sendHttpGET("/param.cgi?cmd=setoverlayattr&-region=1&-show=0");
161 ipCameraHandler.sendHttpGET("/param.cgi?cmd=setoverlayattr&-region=1&-show=1&-name=" + text);
164 case CHANNEL_AUTO_LED:
165 if (OnOffType.ON.equals(command)) {
166 ipCameraHandler.sendHttpGET("/param.cgi?cmd=setinfrared&-infraredstat=auto");
168 ipCameraHandler.sendHttpGET("/param.cgi?cmd=setinfrared&-infraredstat=close");
171 case CHANNEL_ENABLE_PIR_ALARM:
172 if (OnOffType.ON.equals(command)) {
173 ipCameraHandler.sendHttpGET("/param.cgi?cmd=setpirattr&-pir_enable=1");
175 ipCameraHandler.sendHttpGET("/param.cgi?cmd=setpirattr&-pir_enable=0");
178 case CHANNEL_ENABLE_EXTERNAL_ALARM_INPUT:
179 if (OnOffType.ON.equals(command)) {
180 ipCameraHandler.sendHttpGET("/param.cgi?cmd=setioattr&-io_enable=1");
182 ipCameraHandler.sendHttpGET("/param.cgi?cmd=setioattr&-io_enable=0");
188 void alarmTriggered(String alarm) {
189 ipCameraHandler.logger.debug("Alarm has been triggered:{}", alarm);
191 case "/instar?&active=1":// The motion area boxes 1-4
192 case "/instar?&active=2":
193 case "/instar?&active=3":
194 case "/instar?&active=4":
195 ipCameraHandler.motionDetected(CHANNEL_MOTION_ALARM);
197 case "/instar?&active=5":// PIR
198 ipCameraHandler.motionDetected(CHANNEL_PIR_ALARM);
200 case "/instar?&active=6":// Audio Alarm
201 ipCameraHandler.audioDetected();
203 case "/instar?&active=7":// Motion Area 1
204 case "/instar?&active=8":// Motion Area 2
205 case "/instar?&active=9":// Motion Area 3
206 case "/instar?&active=10":// Motion Area 4
207 ipCameraHandler.motionDetected(CHANNEL_MOTION_ALARM);
212 // If a camera does not need to poll a request as often as snapshots, it can be
213 // added here. Binding steps through the list.
214 public ArrayList<String> getLowPriorityRequests() {
215 ArrayList<String> lowPriorityRequests = new ArrayList<String>(2);
216 lowPriorityRequests.add("/cgi-bin/hi3510/param.cgi?cmd=getaudioalarmattr");
217 lowPriorityRequests.add("/cgi-bin/hi3510/param.cgi?cmd=getmdattr");
218 lowPriorityRequests.add("/param.cgi?cmd=getinfrared");
219 lowPriorityRequests.add("/param.cgi?cmd=getoverlayattr&-region=1");
220 lowPriorityRequests.add("/param.cgi?cmd=getpirattr");
221 lowPriorityRequests.add("/param.cgi?cmd=getioattr"); // ext alarm input on/off
222 // lowPriorityRequests.add("/param.cgi?cmd=getserverinfo");
223 return lowPriorityRequests;