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.types.Command;
28 import org.openhab.core.types.RefreshType;
29 import org.openhab.core.types.UnDefType;
31 import io.netty.channel.ChannelDuplexHandler;
32 import io.netty.channel.ChannelHandlerContext;
33 import io.netty.util.ReferenceCountUtil;
36 * The {@link DahuaHandler} is responsible for handling commands, which are
37 * sent to one of the channels.
39 * @author Matthew Skinner - Initial contribution
43 public class DahuaHandler extends ChannelDuplexHandler {
44 private IpCameraHandler ipCameraHandler;
45 private int nvrChannel;
47 public DahuaHandler(IpCameraHandler handler, int nvrChannel) {
48 ipCameraHandler = handler;
49 this.nvrChannel = nvrChannel;
52 // This handles the incoming http replies back from the camera.
54 public void channelRead(@Nullable ChannelHandlerContext ctx, @Nullable Object msg) throws Exception {
55 if (msg == null || ctx == null) {
58 String content = msg.toString();
60 if (!content.isEmpty()) {
61 ipCameraHandler.logger.trace("HTTP Result back from camera is \t:{}:", content);
63 // determine if the motion detection is turned on or off.
64 if (content.contains("table.MotionDetect[0].Enable=true")) {
65 ipCameraHandler.setChannelState(CHANNEL_ENABLE_MOTION_ALARM, OnOffType.ON);
66 } else if (content.contains("table.MotionDetect[" + nvrChannel + "].Enable=false")) {
67 ipCameraHandler.setChannelState(CHANNEL_ENABLE_MOTION_ALARM, OnOffType.OFF);
69 // Handle motion alarm
70 if (content.contains("Code=VideoMotion;action=Start;index=0")) {
71 ipCameraHandler.motionDetected(CHANNEL_MOTION_ALARM);
72 } else if (content.contains("Code=VideoMotion;action=Stop;index=0")) {
73 ipCameraHandler.noMotionDetected(CHANNEL_MOTION_ALARM);
75 // Handle item taken alarm
76 if (content.contains("Code=TakenAwayDetection;action=Start;index=0")) {
77 ipCameraHandler.motionDetected(CHANNEL_ITEM_TAKEN);
78 } else if (content.contains("Code=TakenAwayDetection;action=Stop;index=0")) {
79 ipCameraHandler.noMotionDetected(CHANNEL_ITEM_TAKEN);
81 // Handle item left alarm
82 if (content.contains("Code=LeftDetection;action=Start;index=0")) {
83 ipCameraHandler.motionDetected(CHANNEL_ITEM_LEFT);
84 } else if (content.contains("Code=LeftDetection;action=Stop;index=0")) {
85 ipCameraHandler.noMotionDetected(CHANNEL_ITEM_LEFT);
87 // Handle CrossLineDetection alarm
88 if (content.contains("Code=CrossLineDetection;action=Start;index=0")) {
89 ipCameraHandler.motionDetected(CHANNEL_LINE_CROSSING_ALARM);
90 } else if (content.contains("Code=CrossLineDetection;action=Stop;index=0")) {
91 ipCameraHandler.noMotionDetected(CHANNEL_LINE_CROSSING_ALARM);
93 // determine if the audio alarm is turned on or off.
94 if (content.contains("table.AudioDetect[0].MutationDetect=true")) {
95 ipCameraHandler.setChannelState(CHANNEL_ENABLE_AUDIO_ALARM, OnOffType.ON);
96 } else if (content.contains("table.AudioDetect[0].MutationDetect=false")) {
97 ipCameraHandler.setChannelState(CHANNEL_ENABLE_AUDIO_ALARM, OnOffType.OFF);
99 // Handle AudioMutation alarm
100 if (content.contains("Code=AudioMutation;action=Start;index=0")) {
101 ipCameraHandler.audioDetected();
102 } else if (content.contains("Code=AudioMutation;action=Stop;index=0")) {
103 ipCameraHandler.noAudioDetected();
105 // Handle AudioMutationThreshold alarm
106 if (content.contains("table.AudioDetect[0].MutationThreold=")) {
107 String value = ipCameraHandler.returnValueFromString(content, "table.AudioDetect[0].MutationThreold=");
108 ipCameraHandler.setChannelState(CHANNEL_THRESHOLD_AUDIO_ALARM, PercentType.valueOf(value));
110 // Handle FaceDetection alarm
111 if (content.contains("Code=FaceDetection;action=Start;index=0")) {
112 ipCameraHandler.motionDetected(CHANNEL_FACE_DETECTED);
113 } else if (content.contains("Code=FaceDetection;action=Stop;index=0")) {
114 ipCameraHandler.noMotionDetected(CHANNEL_FACE_DETECTED);
116 // Handle ParkingDetection alarm
117 if (content.contains("Code=ParkingDetection;action=Start;index=0")) {
118 ipCameraHandler.motionDetected(CHANNEL_PARKING_ALARM);
119 } else if (content.contains("Code=ParkingDetection;action=Stop;index=0")) {
120 ipCameraHandler.noMotionDetected(CHANNEL_PARKING_ALARM);
122 // Handle CrossRegionDetection alarm
123 if (content.contains("Code=CrossRegionDetection;action=Start;index=0")) {
124 ipCameraHandler.motionDetected(CHANNEL_FIELD_DETECTION_ALARM);
125 } else if (content.contains("Code=CrossRegionDetection;action=Stop;index=0")) {
126 ipCameraHandler.noMotionDetected(CHANNEL_FIELD_DETECTION_ALARM);
128 // Handle External Input alarm
129 if (content.contains("Code=AlarmLocal;action=Start;index=0")) {
130 ipCameraHandler.setChannelState(CHANNEL_EXTERNAL_ALARM_INPUT, OnOffType.ON);
131 } else if (content.contains("Code=AlarmLocal;action=Stop;index=0")) {
132 ipCameraHandler.setChannelState(CHANNEL_EXTERNAL_ALARM_INPUT, OnOffType.OFF);
134 // Handle External Input alarm2
135 if (content.contains("Code=AlarmLocal;action=Start;index=1")) {
136 ipCameraHandler.setChannelState(CHANNEL_EXTERNAL_ALARM_INPUT2, OnOffType.ON);
137 } else if (content.contains("Code=AlarmLocal;action=Stop;index=1")) {
138 ipCameraHandler.setChannelState(CHANNEL_EXTERNAL_ALARM_INPUT2, OnOffType.OFF);
140 // CrossLineDetection alarm on/off
141 if (content.contains("table.VideoAnalyseRule[0][1].Enable=true")) {
142 ipCameraHandler.setChannelState(CHANNEL_ENABLE_LINE_CROSSING_ALARM, OnOffType.ON);
143 } else if (content.contains("table.VideoAnalyseRule[0][1].Enable=false")) {
144 ipCameraHandler.setChannelState(CHANNEL_ENABLE_LINE_CROSSING_ALARM, OnOffType.OFF);
147 ReferenceCountUtil.release(msg);
151 // This handles the commands that come from the Openhab event bus.
152 public void handleCommand(ChannelUID channelUID, Command command) {
153 if (command instanceof RefreshType) {
154 switch (channelUID.getId()) {
155 case CHANNEL_THRESHOLD_AUDIO_ALARM:
156 // ipCameraHandler.sendHttpGET("/cgi-bin/configManager.cgi?action=getConfig&name=AudioDetect[0]");
158 case CHANNEL_ENABLE_AUDIO_ALARM:
159 ipCameraHandler.sendHttpGET("/cgi-bin/configManager.cgi?action=getConfig&name=AudioDetect[0]");
161 case CHANNEL_ENABLE_LINE_CROSSING_ALARM:
162 ipCameraHandler.sendHttpGET("/cgi-bin/configManager.cgi?action=getConfig&name=VideoAnalyseRule");
164 case CHANNEL_ENABLE_MOTION_ALARM:
165 ipCameraHandler.sendHttpGET("/cgi-bin/configManager.cgi?action=getConfig&name=MotionDetect[0]");
168 return; // Return as we have handled the refresh command above and don't need to
170 } // end of "REFRESH"
171 switch (channelUID.getId()) {
172 case CHANNEL_TEXT_OVERLAY:
173 String text = Helper.encodeSpecialChars(command.toString());
174 if (text.isEmpty()) {
175 ipCameraHandler.sendHttpGET(
176 "/cgi-bin/configManager.cgi?action=setConfig&VideoWidget[0].CustomTitle[1].EncodeBlend=false");
178 ipCameraHandler.sendHttpGET(
179 "/cgi-bin/configManager.cgi?action=setConfig&VideoWidget[0].CustomTitle[1].EncodeBlend=true&VideoWidget[0].CustomTitle[1].Text="
183 case CHANNEL_ENABLE_LED:
184 ipCameraHandler.setChannelState(CHANNEL_AUTO_LED, OnOffType.OFF);
185 if (DecimalType.ZERO.equals(command) || OnOffType.OFF.equals(command)) {
186 ipCameraHandler.sendHttpGET("/cgi-bin/configManager.cgi?action=setConfig&Lighting[0][0].Mode=Off");
187 } else if (OnOffType.ON.equals(command)) {
189 .sendHttpGET("/cgi-bin/configManager.cgi?action=setConfig&Lighting[0][0].Mode=Manual");
191 ipCameraHandler.sendHttpGET(
192 "/cgi-bin/configManager.cgi?action=setConfig&Lighting[0][0].Mode=Manual&Lighting[0][0].MiddleLight[0].Light="
193 + command.toString());
196 case CHANNEL_AUTO_LED:
197 if (OnOffType.ON.equals(command)) {
198 ipCameraHandler.setChannelState(CHANNEL_ENABLE_LED, UnDefType.UNDEF);
199 ipCameraHandler.sendHttpGET("/cgi-bin/configManager.cgi?action=setConfig&Lighting[0][0].Mode=Auto");
202 case CHANNEL_THRESHOLD_AUDIO_ALARM:
203 int threshold = Math.round(Float.valueOf(command.toString()));
205 if (threshold == 0) {
206 ipCameraHandler.sendHttpGET(
207 "/cgi-bin/configManager.cgi?action=setConfig&AudioDetect[0].MutationThreold=1");
209 ipCameraHandler.sendHttpGET(
210 "/cgi-bin/configManager.cgi?action=setConfig&AudioDetect[0].MutationThreold=" + threshold);
213 case CHANNEL_ENABLE_AUDIO_ALARM:
214 if (OnOffType.ON.equals(command)) {
215 ipCameraHandler.sendHttpGET(
216 "/cgi-bin/configManager.cgi?action=setConfig&AudioDetect[0].MutationDetect=true&AudioDetect[0].EventHandler.Dejitter=1");
218 ipCameraHandler.sendHttpGET(
219 "/cgi-bin/configManager.cgi?action=setConfig&AudioDetect[0].MutationDetect=false");
222 case CHANNEL_ENABLE_LINE_CROSSING_ALARM:
223 if (OnOffType.ON.equals(command)) {
224 ipCameraHandler.sendHttpGET(
225 "/cgi-bin/configManager.cgi?action=setConfig&VideoAnalyseRule[0][1].Enable=true");
227 ipCameraHandler.sendHttpGET(
228 "/cgi-bin/configManager.cgi?action=setConfig&VideoAnalyseRule[0][1].Enable=false");
231 case CHANNEL_ENABLE_MOTION_ALARM:
232 if (OnOffType.ON.equals(command)) {
233 ipCameraHandler.sendHttpGET(
234 "/cgi-bin/configManager.cgi?action=setConfig&MotionDetect[0].Enable=true&MotionDetect[0].EventHandler.Dejitter=1");
237 .sendHttpGET("/cgi-bin/configManager.cgi?action=setConfig&MotionDetect[0].Enable=false");
240 case CHANNEL_ACTIVATE_ALARM_OUTPUT:
241 if (OnOffType.ON.equals(command)) {
242 ipCameraHandler.sendHttpGET("/cgi-bin/configManager.cgi?action=setConfig&AlarmOut[0].Mode=1");
244 ipCameraHandler.sendHttpGET("/cgi-bin/configManager.cgi?action=setConfig&AlarmOut[0].Mode=0");
247 case CHANNEL_ACTIVATE_ALARM_OUTPUT2:
248 if (OnOffType.ON.equals(command)) {
249 ipCameraHandler.sendHttpGET("/cgi-bin/configManager.cgi?action=setConfig&AlarmOut[1].Mode=1");
251 ipCameraHandler.sendHttpGET("/cgi-bin/configManager.cgi?action=setConfig&AlarmOut[1].Mode=0");
257 // If a camera does not need to poll a request as often as snapshots, it can be
258 // added here. Binding steps through the list.
259 public ArrayList<String> getLowPriorityRequests() {
260 ArrayList<String> lowPriorityRequests = new ArrayList<String>(1);
261 return lowPriorityRequests;