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.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 ipCameraHandler.logger.debug("Alarm has been triggered:{}", alarm);
190 case "/instar?&active=1":// The motion area boxes 1-4
191 case "/instar?&active=2":
192 case "/instar?&active=3":
193 case "/instar?&active=4":
194 ipCameraHandler.motionDetected(CHANNEL_MOTION_ALARM);
196 case "/instar?&active=5":// PIR
197 ipCameraHandler.motionDetected(CHANNEL_PIR_ALARM);
199 case "/instar?&active=6":// Audio Alarm
200 ipCameraHandler.audioDetected();
202 case "/instar?&active=7":// Motion Area 1
203 case "/instar?&active=8":// Motion Area 2
204 case "/instar?&active=9":// Motion Area 3
205 case "/instar?&active=10":// Motion Area 4
206 ipCameraHandler.motionDetected(CHANNEL_MOTION_ALARM);
211 // If a camera does not need to poll a request as often as snapshots, it can be
212 // added here. Binding steps through the list.
213 public ArrayList<String> getLowPriorityRequests() {
214 ArrayList<String> lowPriorityRequests = new ArrayList<String>(2);
215 lowPriorityRequests.add("/cgi-bin/hi3510/param.cgi?cmd=getaudioalarmattr");
216 lowPriorityRequests.add("/cgi-bin/hi3510/param.cgi?cmd=getmdattr");
217 lowPriorityRequests.add("/param.cgi?cmd=getinfrared");
218 lowPriorityRequests.add("/param.cgi?cmd=getoverlayattr&-region=1");
219 lowPriorityRequests.add("/param.cgi?cmd=getpirattr");
220 lowPriorityRequests.add("/param.cgi?cmd=getioattr"); // ext alarm input on/off
221 // lowPriorityRequests.add("/param.cgi?cmd=getserverinfo");
222 return lowPriorityRequests;