]> git.basschouten.com Git - openhab-addons.git/blob
0a2c54c6811a240f8e2814e4cc9abdb53dda7082
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13
14 package org.openhab.binding.ipcamera.internal;
15
16 import static org.openhab.binding.ipcamera.internal.IpCameraBindingConstants.*;
17
18 import java.util.ArrayList;
19
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;
30
31 import io.netty.channel.ChannelDuplexHandler;
32 import io.netty.channel.ChannelHandlerContext;
33 import io.netty.util.ReferenceCountUtil;
34
35 /**
36  * The {@link DahuaHandler} is responsible for handling commands, which are
37  * sent to one of the channels.
38  *
39  * @author Matthew Skinner - Initial contribution
40  */
41
42 @NonNullByDefault
43 public class DahuaHandler extends ChannelDuplexHandler {
44     private IpCameraHandler ipCameraHandler;
45     private int nvrChannel;
46
47     public DahuaHandler(IpCameraHandler handler, int nvrChannel) {
48         ipCameraHandler = handler;
49         this.nvrChannel = nvrChannel;
50     }
51
52     // This handles the incoming http replies back from the camera.
53     @Override
54     public void channelRead(@Nullable ChannelHandlerContext ctx, @Nullable Object msg) throws Exception {
55         if (msg == null || ctx == null) {
56             return;
57         }
58         String content = msg.toString();
59         try {
60             if (!content.isEmpty()) {
61                 ipCameraHandler.logger.trace("HTTP Result back from camera is \t:{}:", content);
62             }
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);
68             }
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);
74             }
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);
80             }
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);
86             }
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);
92             }
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);
98             }
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();
104             }
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));
109             }
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);
115             }
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);
121             }
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);
127             }
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);
133             }
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);
139             }
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);
145             }
146             // Privacy Mode on/off
147             if (content.contains("Code=LensMaskOpen;") || content.contains("table.LeLensMask[0].Enable=true")) {
148                 ipCameraHandler.setChannelState(CHANNEL_ENABLE_PRIVACY_MODE, OnOffType.ON);
149             } else if (content.contains("Code=LensMaskClose;")
150                     || content.contains("table.LeLensMask[0].Enable=false")) {
151                 ipCameraHandler.setChannelState(CHANNEL_ENABLE_PRIVACY_MODE, OnOffType.OFF);
152             }
153         } finally {
154             ReferenceCountUtil.release(msg);
155         }
156     }
157
158     // This handles the commands that come from the Openhab event bus.
159     public void handleCommand(ChannelUID channelUID, Command command) {
160         if (command instanceof RefreshType) {
161             switch (channelUID.getId()) {
162                 case CHANNEL_THRESHOLD_AUDIO_ALARM:
163                     // ipCameraHandler.sendHttpGET("/cgi-bin/configManager.cgi?action=getConfig&name=AudioDetect[0]");
164                     return;
165                 case CHANNEL_ENABLE_AUDIO_ALARM:
166                     ipCameraHandler.sendHttpGET("/cgi-bin/configManager.cgi?action=getConfig&name=AudioDetect[0]");
167                     return;
168                 case CHANNEL_ENABLE_LINE_CROSSING_ALARM:
169                     ipCameraHandler.sendHttpGET("/cgi-bin/configManager.cgi?action=getConfig&name=VideoAnalyseRule");
170                     return;
171                 case CHANNEL_ENABLE_MOTION_ALARM:
172                     ipCameraHandler.sendHttpGET("/cgi-bin/configManager.cgi?action=getConfig&name=MotionDetect[0]");
173                     return;
174                 case CHANNEL_ENABLE_PRIVACY_MODE:
175                     ipCameraHandler.sendHttpGET("/cgi-bin/configManager.cgi?action=getConfig&name=LeLensMask[0]");
176                     return;
177             }
178             return; // Return as we have handled the refresh command above and don't need to
179                     // continue further.
180         } // end of "REFRESH"
181         switch (channelUID.getId()) {
182             case CHANNEL_TEXT_OVERLAY:
183                 String text = Helper.encodeSpecialChars(command.toString());
184                 if (text.isEmpty()) {
185                     ipCameraHandler.sendHttpGET(
186                             "/cgi-bin/configManager.cgi?action=setConfig&VideoWidget[0].CustomTitle[1].EncodeBlend=false");
187                 } else {
188                     ipCameraHandler.sendHttpGET(
189                             "/cgi-bin/configManager.cgi?action=setConfig&VideoWidget[0].CustomTitle[1].EncodeBlend=true&VideoWidget[0].CustomTitle[1].Text="
190                                     + text);
191                 }
192                 return;
193             case CHANNEL_ENABLE_LED:
194                 ipCameraHandler.setChannelState(CHANNEL_AUTO_LED, OnOffType.OFF);
195                 if (DecimalType.ZERO.equals(command) || OnOffType.OFF.equals(command)) {
196                     ipCameraHandler.sendHttpGET("/cgi-bin/configManager.cgi?action=setConfig&Lighting[0][0].Mode=Off");
197                 } else if (OnOffType.ON.equals(command)) {
198                     ipCameraHandler
199                             .sendHttpGET("/cgi-bin/configManager.cgi?action=setConfig&Lighting[0][0].Mode=Manual");
200                 } else {
201                     ipCameraHandler.sendHttpGET(
202                             "/cgi-bin/configManager.cgi?action=setConfig&Lighting[0][0].Mode=Manual&Lighting[0][0].MiddleLight[0].Light="
203                                     + command.toString());
204                 }
205                 return;
206             case CHANNEL_AUTO_LED:
207                 if (OnOffType.ON.equals(command)) {
208                     ipCameraHandler.setChannelState(CHANNEL_ENABLE_LED, UnDefType.UNDEF);
209                     ipCameraHandler.sendHttpGET("/cgi-bin/configManager.cgi?action=setConfig&Lighting[0][0].Mode=Auto");
210                 }
211                 return;
212             case CHANNEL_THRESHOLD_AUDIO_ALARM:
213                 int threshold = Math.round(Float.valueOf(command.toString()));
214
215                 if (threshold == 0) {
216                     ipCameraHandler.sendHttpGET(
217                             "/cgi-bin/configManager.cgi?action=setConfig&AudioDetect[0].MutationThreold=1");
218                 } else {
219                     ipCameraHandler.sendHttpGET(
220                             "/cgi-bin/configManager.cgi?action=setConfig&AudioDetect[0].MutationThreold=" + threshold);
221                 }
222                 return;
223             case CHANNEL_ENABLE_AUDIO_ALARM:
224                 if (OnOffType.ON.equals(command)) {
225                     ipCameraHandler.sendHttpGET(
226                             "/cgi-bin/configManager.cgi?action=setConfig&AudioDetect[0].MutationDetect=true&AudioDetect[0].EventHandler.Dejitter=1");
227                 } else {
228                     ipCameraHandler.sendHttpGET(
229                             "/cgi-bin/configManager.cgi?action=setConfig&AudioDetect[0].MutationDetect=false");
230                 }
231                 return;
232             case CHANNEL_ENABLE_LINE_CROSSING_ALARM:
233                 if (OnOffType.ON.equals(command)) {
234                     ipCameraHandler.sendHttpGET(
235                             "/cgi-bin/configManager.cgi?action=setConfig&VideoAnalyseRule[0][1].Enable=true");
236                 } else {
237                     ipCameraHandler.sendHttpGET(
238                             "/cgi-bin/configManager.cgi?action=setConfig&VideoAnalyseRule[0][1].Enable=false");
239                 }
240                 return;
241             case CHANNEL_ENABLE_MOTION_ALARM:
242                 if (OnOffType.ON.equals(command)) {
243                     ipCameraHandler.sendHttpGET(
244                             "/cgi-bin/configManager.cgi?action=setConfig&MotionDetect[0].Enable=true&MotionDetect[0].EventHandler.Dejitter=1");
245                 } else {
246                     ipCameraHandler
247                             .sendHttpGET("/cgi-bin/configManager.cgi?action=setConfig&MotionDetect[0].Enable=false");
248                 }
249                 return;
250             case CHANNEL_ACTIVATE_ALARM_OUTPUT:
251                 if (OnOffType.ON.equals(command)) {
252                     ipCameraHandler.sendHttpGET("/cgi-bin/configManager.cgi?action=setConfig&AlarmOut[0].Mode=1");
253                 } else {
254                     ipCameraHandler.sendHttpGET("/cgi-bin/configManager.cgi?action=setConfig&AlarmOut[0].Mode=0");
255                 }
256                 return;
257             case CHANNEL_ACTIVATE_ALARM_OUTPUT2:
258                 if (OnOffType.ON.equals(command)) {
259                     ipCameraHandler.sendHttpGET("/cgi-bin/configManager.cgi?action=setConfig&AlarmOut[1].Mode=1");
260                 } else {
261                     ipCameraHandler.sendHttpGET("/cgi-bin/configManager.cgi?action=setConfig&AlarmOut[1].Mode=0");
262                 }
263                 return;
264             case CHANNEL_ENABLE_PRIVACY_MODE:
265                 if (OnOffType.OFF.equals(command)) {
266                     ipCameraHandler
267                             .sendHttpGET("/cgi-bin/configManager.cgi?action=setConfig&LeLensMask[0].Enable=false");
268                 } else if (OnOffType.ON.equals(command)) {
269                     ipCameraHandler
270                             .sendHttpGET("/cgi-bin/configManager.cgi?action=setConfig&LeLensMask[0].Enable=true");
271                 }
272                 return;
273         }
274     }
275
276     // If a camera does not need to poll a request as often as snapshots, it can be
277     // added here. Binding steps through the list.
278     public ArrayList<String> getLowPriorityRequests() {
279         ArrayList<String> lowPriorityRequests = new ArrayList<String>(1);
280         return lowPriorityRequests;
281     }
282 }