]> git.basschouten.com Git - openhab-addons.git/blob
b48a34d7494644cc520639d6d180b3a9411bf9ef
[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.thing.binding.ThingHandler;
28 import org.openhab.core.types.Command;
29 import org.openhab.core.types.RefreshType;
30 import org.openhab.core.types.UnDefType;
31
32 import io.netty.channel.ChannelDuplexHandler;
33 import io.netty.channel.ChannelHandlerContext;
34 import io.netty.util.ReferenceCountUtil;
35
36 /**
37  * The {@link AmcrestHandler} is responsible for handling commands, which are
38  * sent to one of the channels.
39  *
40  * @author Matthew Skinner - Initial contribution
41  */
42
43 @NonNullByDefault
44 public class AmcrestHandler extends ChannelDuplexHandler {
45     private String requestUrl = "Empty";
46     private IpCameraHandler ipCameraHandler;
47
48     public AmcrestHandler(ThingHandler handler) {
49         ipCameraHandler = (IpCameraHandler) handler;
50     }
51
52     public void setURL(String url) {
53         requestUrl = url;
54     }
55
56     // This handles the incoming http replies back from the camera.
57     @Override
58     public void channelRead(@Nullable ChannelHandlerContext ctx, @Nullable Object msg) throws Exception {
59         if (msg == null || ctx == null) {
60             return;
61         }
62         try {
63             String content = msg.toString();
64             ipCameraHandler.logger.trace("HTTP Result back from camera is \t:{}:", content);
65             if (content.contains("Error: No Events")) {
66                 if ("/cgi-bin/eventManager.cgi?action=getEventIndexes&code=VideoMotion".equals(requestUrl)) {
67                     ipCameraHandler.noMotionDetected(CHANNEL_MOTION_ALARM);
68                 } else if ("/cgi-bin/eventManager.cgi?action=getEventIndexes&code=AudioMutation".equals(requestUrl)) {
69                     ipCameraHandler.noAudioDetected();
70                 }
71             } else if (content.contains("channels[0]=0")) {
72                 if ("/cgi-bin/eventManager.cgi?action=getEventIndexes&code=VideoMotion".equals(requestUrl)) {
73                     ipCameraHandler.motionDetected(CHANNEL_MOTION_ALARM);
74                 } else if ("/cgi-bin/eventManager.cgi?action=getEventIndexes&code=AudioMutation".equals(requestUrl)) {
75                     ipCameraHandler.audioDetected();
76                 }
77             }
78
79             if (content.contains("table.MotionDetect[0].Enable=false")) {
80                 ipCameraHandler.setChannelState(CHANNEL_ENABLE_MOTION_ALARM, OnOffType.OFF);
81             } else if (content.contains("table.MotionDetect[0].Enable=true")) {
82                 ipCameraHandler.setChannelState(CHANNEL_ENABLE_MOTION_ALARM, OnOffType.ON);
83             }
84             // determine if the audio alarm is turned on or off.
85             if (content.contains("table.AudioDetect[0].MutationDetect=true")) {
86                 ipCameraHandler.setChannelState(CHANNEL_ENABLE_AUDIO_ALARM, OnOffType.ON);
87             } else if (content.contains("table.AudioDetect[0].MutationDetect=false")) {
88                 ipCameraHandler.setChannelState(CHANNEL_ENABLE_AUDIO_ALARM, OnOffType.OFF);
89             }
90             // Handle AudioMutationThreshold alarm
91             if (content.contains("table.AudioDetect[0].MutationThreold=")) {
92                 String value = ipCameraHandler.returnValueFromString(content, "table.AudioDetect[0].MutationThreold=");
93                 ipCameraHandler.setChannelState(CHANNEL_THRESHOLD_AUDIO_ALARM, PercentType.valueOf(value));
94             }
95             // Privacy Mode on/off
96             if (content.contains("Code=LensMaskOpen;") || content.contains("table.LeLensMask[0].Enable=true")) {
97                 ipCameraHandler.setChannelState(CHANNEL_ENABLE_PRIVACY_MODE, OnOffType.ON);
98             } else if (content.contains("Code=LensMaskClose;")
99                     || content.contains("table.LeLensMask[0].Enable=false")) {
100                 ipCameraHandler.setChannelState(CHANNEL_ENABLE_PRIVACY_MODE, OnOffType.OFF);
101             }
102         } finally {
103             ReferenceCountUtil.release(msg);
104             ctx.close();
105         }
106     }
107
108     // This handles the commands that come from the Openhab event bus.
109     public void handleCommand(ChannelUID channelUID, Command command) {
110         if (command instanceof RefreshType) {
111             switch (channelUID.getId()) {
112                 case CHANNEL_THRESHOLD_AUDIO_ALARM:
113                     ipCameraHandler.sendHttpGET("/cgi-bin/configManager.cgi?action=getConfig&name=AudioDetect[0]");
114                     return;
115                 case CHANNEL_ENABLE_AUDIO_ALARM:
116                     ipCameraHandler.sendHttpGET("/cgi-bin/configManager.cgi?action=getConfig&name=AudioDetect[0]");
117                     return;
118                 case CHANNEL_ENABLE_LINE_CROSSING_ALARM:
119                     ipCameraHandler
120                             .sendHttpGET("/cgi-bin/configManager.cgi?action=getConfig&name=CrossLineDetection[0]");
121                     return;
122                 case CHANNEL_ENABLE_MOTION_ALARM:
123                     ipCameraHandler.sendHttpGET("/cgi-bin/configManager.cgi?action=getConfig&name=MotionDetect[0]");
124                     return;
125                 case CHANNEL_ENABLE_PRIVACY_MODE:
126                     ipCameraHandler.sendHttpGET("/cgi-bin/configManager.cgi?action=getConfig&name=LeLensMask[0]");
127                     return;
128             }
129             return; // Return as we have handled the refresh command above and don't need to
130                     // continue further.
131         } // end of "REFRESH"
132         switch (channelUID.getId()) {
133             case CHANNEL_TEXT_OVERLAY:
134                 String text = Helper.encodeSpecialChars(command.toString());
135                 if (text.isEmpty()) {
136                     ipCameraHandler.sendHttpGET(
137                             "/cgi-bin/configManager.cgi?action=setConfig&VideoWidget[0].CustomTitle[1].EncodeBlend=false");
138                 } else {
139                     ipCameraHandler.sendHttpGET(
140                             "/cgi-bin/configManager.cgi?action=setConfig&VideoWidget[0].CustomTitle[1].EncodeBlend=true&VideoWidget[0].CustomTitle[1].Text="
141                                     + text);
142                 }
143                 return;
144             case CHANNEL_ENABLE_LED:
145                 ipCameraHandler.setChannelState(CHANNEL_AUTO_LED, OnOffType.OFF);
146                 if (DecimalType.ZERO.equals(command) || OnOffType.OFF.equals(command)) {
147                     ipCameraHandler.sendHttpGET("/cgi-bin/configManager.cgi?action=setConfig&Lighting[0][0].Mode=Off");
148                 } else if (OnOffType.ON.equals(command)) {
149                     ipCameraHandler
150                             .sendHttpGET("/cgi-bin/configManager.cgi?action=setConfig&Lighting[0][0].Mode=Manual");
151                 } else {
152                     ipCameraHandler.sendHttpGET(
153                             "/cgi-bin/configManager.cgi?action=setConfig&Lighting[0][0].Mode=Manual&Lighting[0][0].MiddleLight[0].Light="
154                                     + command.toString());
155                 }
156                 return;
157             case CHANNEL_AUTO_LED:
158                 if (OnOffType.ON.equals(command)) {
159                     ipCameraHandler.setChannelState(CHANNEL_ENABLE_LED, UnDefType.UNDEF);
160                     ipCameraHandler.sendHttpGET("/cgi-bin/configManager.cgi?action=setConfig&Lighting[0][0].Mode=Auto");
161                 }
162                 return;
163             case CHANNEL_THRESHOLD_AUDIO_ALARM:
164                 int threshold = Math.round(Float.valueOf(command.toString()));
165
166                 if (threshold == 0) {
167                     ipCameraHandler.sendHttpGET(
168                             "/cgi-bin/configManager.cgi?action=setConfig&AudioDetect[0].MutationThreold=1");
169                 } else {
170                     ipCameraHandler.sendHttpGET(
171                             "/cgi-bin/configManager.cgi?action=setConfig&AudioDetect[0].MutationThreold=" + threshold);
172                 }
173                 return;
174             case CHANNEL_ENABLE_AUDIO_ALARM:
175                 if (OnOffType.ON.equals(command)) {
176                     ipCameraHandler.sendHttpGET(
177                             "/cgi-bin/configManager.cgi?action=setConfig&AudioDetect[0].MutationDetect=true&AudioDetect[0].EventHandler.Dejitter=1");
178                 } else {
179                     ipCameraHandler.sendHttpGET(
180                             "/cgi-bin/configManager.cgi?action=setConfig&AudioDetect[0].MutationDetect=false");
181                 }
182                 return;
183             case CHANNEL_ENABLE_LINE_CROSSING_ALARM:
184                 if (OnOffType.ON.equals(command)) {
185                     ipCameraHandler.sendHttpGET(
186                             "/cgi-bin/configManager.cgi?action=setConfig&VideoAnalyseRule[0][1].Enable=true");
187                 } else {
188                     ipCameraHandler.sendHttpGET(
189                             "/cgi-bin/configManager.cgi?action=setConfig&VideoAnalyseRule[0][1].Enable=false");
190                 }
191                 return;
192             case CHANNEL_ENABLE_MOTION_ALARM:
193                 if (OnOffType.ON.equals(command)) {
194                     ipCameraHandler.sendHttpGET(
195                             "/cgi-bin/configManager.cgi?action=setConfig&MotionDetect[0].Enable=true&MotionDetect[0].EventHandler.Dejitter=1");
196                 } else {
197                     ipCameraHandler
198                             .sendHttpGET("/cgi-bin/configManager.cgi?action=setConfig&MotionDetect[0].Enable=false");
199                 }
200                 return;
201             case CHANNEL_ACTIVATE_ALARM_OUTPUT:
202                 if (OnOffType.ON.equals(command)) {
203                     ipCameraHandler.sendHttpGET("/cgi-bin/configManager.cgi?action=setConfig&AlarmOut[0].Mode=1");
204                 } else {
205                     ipCameraHandler.sendHttpGET("/cgi-bin/configManager.cgi?action=setConfig&AlarmOut[0].Mode=0");
206                 }
207                 return;
208             case CHANNEL_ACTIVATE_ALARM_OUTPUT2:
209                 if (OnOffType.ON.equals(command)) {
210                     ipCameraHandler.sendHttpGET("/cgi-bin/configManager.cgi?action=setConfig&AlarmOut[1].Mode=1");
211                 } else {
212                     ipCameraHandler.sendHttpGET("/cgi-bin/configManager.cgi?action=setConfig&AlarmOut[1].Mode=0");
213                 }
214                 return;
215             case CHANNEL_ENABLE_PRIVACY_MODE:
216                 if (OnOffType.OFF.equals(command)) {
217                     ipCameraHandler
218                             .sendHttpGET("/cgi-bin/configManager.cgi?action=setConfig&LeLensMask[0].Enable=false");
219                 } else if (OnOffType.ON.equals(command)) {
220                     ipCameraHandler
221                             .sendHttpGET("/cgi-bin/configManager.cgi?action=setConfig&LeLensMask[0].Enable=true");
222                 }
223                 return;
224         }
225     }
226
227     // If a camera does not need to poll a request as often as snapshots, it can be
228     // added here. Binding steps through the list.
229     public ArrayList<String> getLowPriorityRequests() {
230         ArrayList<String> lowPriorityRequests = new ArrayList<String>(1);
231         return lowPriorityRequests;
232     }
233 }