]> git.basschouten.com Git - openhab-addons.git/blob
35c418bb948ffd4eee8c12fc457f781113655050
[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         } finally {
147             ReferenceCountUtil.release(msg);
148         }
149     }
150
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]");
157                     return;
158                 case CHANNEL_ENABLE_AUDIO_ALARM:
159                     ipCameraHandler.sendHttpGET("/cgi-bin/configManager.cgi?action=getConfig&name=AudioDetect[0]");
160                     return;
161                 case CHANNEL_ENABLE_LINE_CROSSING_ALARM:
162                     ipCameraHandler.sendHttpGET("/cgi-bin/configManager.cgi?action=getConfig&name=VideoAnalyseRule");
163                     return;
164                 case CHANNEL_ENABLE_MOTION_ALARM:
165                     ipCameraHandler.sendHttpGET("/cgi-bin/configManager.cgi?action=getConfig&name=MotionDetect[0]");
166                     return;
167             }
168             return; // Return as we have handled the refresh command above and don't need to
169                     // continue further.
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");
177                 } else {
178                     ipCameraHandler.sendHttpGET(
179                             "/cgi-bin/configManager.cgi?action=setConfig&VideoWidget[0].CustomTitle[1].EncodeBlend=true&VideoWidget[0].CustomTitle[1].Text="
180                                     + text);
181                 }
182                 return;
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)) {
188                     ipCameraHandler
189                             .sendHttpGET("/cgi-bin/configManager.cgi?action=setConfig&Lighting[0][0].Mode=Manual");
190                 } else {
191                     ipCameraHandler.sendHttpGET(
192                             "/cgi-bin/configManager.cgi?action=setConfig&Lighting[0][0].Mode=Manual&Lighting[0][0].MiddleLight[0].Light="
193                                     + command.toString());
194                 }
195                 return;
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");
200                 }
201                 return;
202             case CHANNEL_THRESHOLD_AUDIO_ALARM:
203                 int threshold = Math.round(Float.valueOf(command.toString()));
204
205                 if (threshold == 0) {
206                     ipCameraHandler.sendHttpGET(
207                             "/cgi-bin/configManager.cgi?action=setConfig&AudioDetect[0].MutationThreold=1");
208                 } else {
209                     ipCameraHandler.sendHttpGET(
210                             "/cgi-bin/configManager.cgi?action=setConfig&AudioDetect[0].MutationThreold=" + threshold);
211                 }
212                 return;
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");
217                 } else {
218                     ipCameraHandler.sendHttpGET(
219                             "/cgi-bin/configManager.cgi?action=setConfig&AudioDetect[0].MutationDetect=false");
220                 }
221                 return;
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");
226                 } else {
227                     ipCameraHandler.sendHttpGET(
228                             "/cgi-bin/configManager.cgi?action=setConfig&VideoAnalyseRule[0][1].Enable=false");
229                 }
230                 return;
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");
235                 } else {
236                     ipCameraHandler
237                             .sendHttpGET("/cgi-bin/configManager.cgi?action=setConfig&MotionDetect[0].Enable=false");
238                 }
239                 return;
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");
243                 } else {
244                     ipCameraHandler.sendHttpGET("/cgi-bin/configManager.cgi?action=setConfig&AlarmOut[0].Mode=0");
245                 }
246                 return;
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");
250                 } else {
251                     ipCameraHandler.sendHttpGET("/cgi-bin/configManager.cgi?action=setConfig&AlarmOut[1].Mode=0");
252                 }
253                 return;
254         }
255     }
256
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;
262     }
263 }