2 * Copyright (c) 2010-2020 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
14 package org.openhab.binding.ipcamera.internal;
16 import static org.openhab.binding.ipcamera.internal.IpCameraBindingConstants.*;
18 import java.util.ArrayList;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.ipcamera.internal.handler.IpCameraHandler;
23 import org.openhab.core.library.types.DecimalType;
24 import org.openhab.core.library.types.OnOffType;
25 import org.openhab.core.library.types.PercentType;
26 import org.openhab.core.thing.ChannelUID;
27 import org.openhab.core.thing.binding.ThingHandler;
28 import org.openhab.core.types.Command;
29 import org.openhab.core.types.RefreshType;
30 import org.openhab.core.types.UnDefType;
32 import io.netty.channel.ChannelDuplexHandler;
33 import io.netty.channel.ChannelHandlerContext;
34 import io.netty.util.ReferenceCountUtil;
37 * The {@link AmcrestHandler} is responsible for handling commands, which are
38 * sent to one of the channels.
40 * @author Matthew Skinner - Initial contribution
44 public class AmcrestHandler extends ChannelDuplexHandler {
45 private String requestUrl = "Empty";
46 private IpCameraHandler ipCameraHandler;
48 public AmcrestHandler(ThingHandler handler) {
49 ipCameraHandler = (IpCameraHandler) handler;
52 public void setURL(String url) {
56 // This handles the incoming http replies back from the camera.
58 public void channelRead(@Nullable ChannelHandlerContext ctx, @Nullable Object msg) throws Exception {
59 if (msg == null || ctx == null) {
63 String content = msg.toString();
65 if (!content.isEmpty()) {
66 ipCameraHandler.logger.trace("HTTP Result back from camera is \t:{}:", content);
68 if (content.contains("Error: No Events")) {
69 if ("/cgi-bin/eventManager.cgi?action=getEventIndexes&code=VideoMotion".equals(requestUrl)) {
70 ipCameraHandler.noMotionDetected(CHANNEL_MOTION_ALARM);
71 } else if ("/cgi-bin/eventManager.cgi?action=getEventIndexes&code=AudioMutation".equals(requestUrl)) {
72 ipCameraHandler.noAudioDetected();
74 } else if (content.contains("channels[0]=0")) {
75 if ("/cgi-bin/eventManager.cgi?action=getEventIndexes&code=VideoMotion".equals(requestUrl)) {
76 ipCameraHandler.motionDetected(CHANNEL_MOTION_ALARM);
77 } else if ("/cgi-bin/eventManager.cgi?action=getEventIndexes&code=AudioMutation".equals(requestUrl)) {
78 ipCameraHandler.audioDetected();
82 if (content.contains("table.MotionDetect[0].Enable=false")) {
83 ipCameraHandler.setChannelState(CHANNEL_ENABLE_MOTION_ALARM, OnOffType.OFF);
84 } else if (content.contains("table.MotionDetect[0].Enable=true")) {
85 ipCameraHandler.setChannelState(CHANNEL_ENABLE_MOTION_ALARM, OnOffType.ON);
87 // determine if the audio alarm is turned on or off.
88 if (content.contains("table.AudioDetect[0].MutationDetect=true")) {
89 ipCameraHandler.setChannelState(CHANNEL_ENABLE_AUDIO_ALARM, OnOffType.ON);
90 } else if (content.contains("table.AudioDetect[0].MutationDetect=false")) {
91 ipCameraHandler.setChannelState(CHANNEL_ENABLE_AUDIO_ALARM, OnOffType.OFF);
93 // Handle AudioMutationThreshold alarm
94 if (content.contains("table.AudioDetect[0].MutationThreold=")) {
95 String value = ipCameraHandler.returnValueFromString(content, "table.AudioDetect[0].MutationThreold=");
96 ipCameraHandler.setChannelState(CHANNEL_THRESHOLD_AUDIO_ALARM, PercentType.valueOf(value));
98 // Privacy Mode on/off
99 if (content.contains("Code=LensMaskOpen;") || content.contains("table.LeLensMask[0].Enable=true")) {
100 ipCameraHandler.setChannelState(CHANNEL_ENABLE_PRIVACY_MODE, OnOffType.ON);
101 } else if (content.contains("Code=LensMaskClose;")
102 || content.contains("table.LeLensMask[0].Enable=false")) {
103 ipCameraHandler.setChannelState(CHANNEL_ENABLE_PRIVACY_MODE, OnOffType.OFF);
106 ReferenceCountUtil.release(msg);
111 // This handles the commands that come from the Openhab event bus.
112 public void handleCommand(ChannelUID channelUID, Command command) {
113 if (command instanceof RefreshType) {
114 switch (channelUID.getId()) {
115 case CHANNEL_THRESHOLD_AUDIO_ALARM:
116 ipCameraHandler.sendHttpGET("/cgi-bin/configManager.cgi?action=getConfig&name=AudioDetect[0]");
118 case CHANNEL_ENABLE_AUDIO_ALARM:
119 ipCameraHandler.sendHttpGET("/cgi-bin/configManager.cgi?action=getConfig&name=AudioDetect[0]");
121 case CHANNEL_ENABLE_LINE_CROSSING_ALARM:
123 .sendHttpGET("/cgi-bin/configManager.cgi?action=getConfig&name=CrossLineDetection[0]");
125 case CHANNEL_ENABLE_MOTION_ALARM:
126 ipCameraHandler.sendHttpGET("/cgi-bin/configManager.cgi?action=getConfig&name=MotionDetect[0]");
128 case CHANNEL_ENABLE_PRIVACY_MODE:
129 ipCameraHandler.sendHttpGET("/cgi-bin/configManager.cgi?action=getConfig&name=LeLensMask[0]");
132 return; // Return as we have handled the refresh command above and don't need to
134 } // end of "REFRESH"
135 switch (channelUID.getId()) {
136 case CHANNEL_TEXT_OVERLAY:
137 String text = Helper.encodeSpecialChars(command.toString());
138 if (text.isEmpty()) {
139 ipCameraHandler.sendHttpGET(
140 "/cgi-bin/configManager.cgi?action=setConfig&VideoWidget[0].CustomTitle[1].EncodeBlend=false");
142 ipCameraHandler.sendHttpGET(
143 "/cgi-bin/configManager.cgi?action=setConfig&VideoWidget[0].CustomTitle[1].EncodeBlend=true&VideoWidget[0].CustomTitle[1].Text="
147 case CHANNEL_ENABLE_LED:
148 ipCameraHandler.setChannelState(CHANNEL_AUTO_LED, OnOffType.OFF);
149 if (DecimalType.ZERO.equals(command) || OnOffType.OFF.equals(command)) {
150 ipCameraHandler.sendHttpGET("/cgi-bin/configManager.cgi?action=setConfig&Lighting[0][0].Mode=Off");
151 } else if (OnOffType.ON.equals(command)) {
153 .sendHttpGET("/cgi-bin/configManager.cgi?action=setConfig&Lighting[0][0].Mode=Manual");
155 ipCameraHandler.sendHttpGET(
156 "/cgi-bin/configManager.cgi?action=setConfig&Lighting[0][0].Mode=Manual&Lighting[0][0].MiddleLight[0].Light="
157 + command.toString());
160 case CHANNEL_AUTO_LED:
161 if (OnOffType.ON.equals(command)) {
162 ipCameraHandler.setChannelState(CHANNEL_ENABLE_LED, UnDefType.UNDEF);
163 ipCameraHandler.sendHttpGET("/cgi-bin/configManager.cgi?action=setConfig&Lighting[0][0].Mode=Auto");
166 case CHANNEL_THRESHOLD_AUDIO_ALARM:
167 int threshold = Math.round(Float.valueOf(command.toString()));
169 if (threshold == 0) {
170 ipCameraHandler.sendHttpGET(
171 "/cgi-bin/configManager.cgi?action=setConfig&AudioDetect[0].MutationThreold=1");
173 ipCameraHandler.sendHttpGET(
174 "/cgi-bin/configManager.cgi?action=setConfig&AudioDetect[0].MutationThreold=" + threshold);
177 case CHANNEL_ENABLE_AUDIO_ALARM:
178 if (OnOffType.ON.equals(command)) {
179 ipCameraHandler.sendHttpGET(
180 "/cgi-bin/configManager.cgi?action=setConfig&AudioDetect[0].MutationDetect=true&AudioDetect[0].EventHandler.Dejitter=1");
182 ipCameraHandler.sendHttpGET(
183 "/cgi-bin/configManager.cgi?action=setConfig&AudioDetect[0].MutationDetect=false");
186 case CHANNEL_ENABLE_LINE_CROSSING_ALARM:
187 if (OnOffType.ON.equals(command)) {
188 ipCameraHandler.sendHttpGET(
189 "/cgi-bin/configManager.cgi?action=setConfig&VideoAnalyseRule[0][1].Enable=true");
191 ipCameraHandler.sendHttpGET(
192 "/cgi-bin/configManager.cgi?action=setConfig&VideoAnalyseRule[0][1].Enable=false");
195 case CHANNEL_ENABLE_MOTION_ALARM:
196 if (OnOffType.ON.equals(command)) {
197 ipCameraHandler.sendHttpGET(
198 "/cgi-bin/configManager.cgi?action=setConfig&MotionDetect[0].Enable=true&MotionDetect[0].EventHandler.Dejitter=1");
201 .sendHttpGET("/cgi-bin/configManager.cgi?action=setConfig&MotionDetect[0].Enable=false");
204 case CHANNEL_ACTIVATE_ALARM_OUTPUT:
205 if (OnOffType.ON.equals(command)) {
206 ipCameraHandler.sendHttpGET("/cgi-bin/configManager.cgi?action=setConfig&AlarmOut[0].Mode=1");
208 ipCameraHandler.sendHttpGET("/cgi-bin/configManager.cgi?action=setConfig&AlarmOut[0].Mode=0");
211 case CHANNEL_ACTIVATE_ALARM_OUTPUT2:
212 if (OnOffType.ON.equals(command)) {
213 ipCameraHandler.sendHttpGET("/cgi-bin/configManager.cgi?action=setConfig&AlarmOut[1].Mode=1");
215 ipCameraHandler.sendHttpGET("/cgi-bin/configManager.cgi?action=setConfig&AlarmOut[1].Mode=0");
218 case CHANNEL_ENABLE_PRIVACY_MODE:
219 if (OnOffType.OFF.equals(command)) {
221 .sendHttpGET("/cgi-bin/configManager.cgi?action=setConfig&LeLensMask[0].Enable=false");
222 } else if (OnOffType.ON.equals(command)) {
224 .sendHttpGET("/cgi-bin/configManager.cgi?action=setConfig&LeLensMask[0].Enable=true");
230 // If a camera does not need to poll a request as often as snapshots, it can be
231 // added here. Binding steps through the list.
232 public ArrayList<String> getLowPriorityRequests() {
233 ArrayList<String> lowPriorityRequests = new ArrayList<String>(1);
234 return lowPriorityRequests;