]> git.basschouten.com Git - openhab-addons.git/blob
6766a7fbfec419a6ad3dde18a9e0f9b604f5791c
[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.IpCameraBindingConstants.FFmpegFormat;
23 import org.openhab.binding.ipcamera.internal.handler.IpCameraHandler;
24 import org.openhab.core.library.types.DecimalType;
25 import org.openhab.core.library.types.OnOffType;
26 import org.openhab.core.library.types.PercentType;
27 import org.openhab.core.thing.ChannelUID;
28 import org.openhab.core.thing.binding.ThingHandler;
29 import org.openhab.core.types.Command;
30 import org.openhab.core.types.RefreshType;
31 import org.openhab.core.types.UnDefType;
32
33 import io.netty.channel.ChannelDuplexHandler;
34 import io.netty.channel.ChannelHandlerContext;
35 import io.netty.util.ReferenceCountUtil;
36
37 /**
38  * The {@link FoscamHandler} is responsible for handling commands, which are
39  * sent to one of the channels.
40  *
41  * @author Matthew Skinner - Initial contribution
42  */
43
44 @NonNullByDefault
45 public class FoscamHandler extends ChannelDuplexHandler {
46     private IpCameraHandler ipCameraHandler;
47     private String password, username;
48
49     public FoscamHandler(ThingHandler handler, String username, String password) {
50         ipCameraHandler = (IpCameraHandler) handler;
51         this.username = username;
52         this.password = password;
53     }
54
55     // This handles the incoming http replies back from the camera.
56     @Override
57     public void channelRead(@Nullable ChannelHandlerContext ctx, @Nullable Object msg) throws Exception {
58         if (msg == null || ctx == null) {
59             return;
60         }
61         String content = msg.toString();
62         try {
63             if (!content.isEmpty()) {
64                 ipCameraHandler.logger.trace("HTTP Result back from camera is \t:{}:", content);
65             } else {
66                 return;
67             }
68
69             ////////////// Motion Alarm //////////////
70             if (content.contains("<motionDetectAlarm>")) {
71                 if (content.contains("<motionDetectAlarm>0</motionDetectAlarm>")) {
72                     ipCameraHandler.setChannelState(CHANNEL_ENABLE_MOTION_ALARM, OnOffType.OFF);
73                 } else if (content.contains("<motionDetectAlarm>1</motionDetectAlarm>")) { // Enabled but no alarm
74                     ipCameraHandler.setChannelState(CHANNEL_ENABLE_MOTION_ALARM, OnOffType.ON);
75                     ipCameraHandler.noMotionDetected(CHANNEL_MOTION_ALARM);
76                 } else if (content.contains("<motionDetectAlarm>2</motionDetectAlarm>")) {// Enabled, alarm on
77                     ipCameraHandler.setChannelState(CHANNEL_ENABLE_MOTION_ALARM, OnOffType.ON);
78                     ipCameraHandler.motionDetected(CHANNEL_MOTION_ALARM);
79                 }
80             }
81
82             ////////////// Sound Alarm //////////////
83             if (content.contains("<soundAlarm>0</soundAlarm>")) {
84                 ipCameraHandler.setChannelState(CHANNEL_ENABLE_AUDIO_ALARM, OnOffType.OFF);
85                 ipCameraHandler.setChannelState(CHANNEL_AUDIO_ALARM, OnOffType.OFF);
86             }
87             if (content.contains("<soundAlarm>1</soundAlarm>")) {
88                 ipCameraHandler.setChannelState(CHANNEL_ENABLE_AUDIO_ALARM, OnOffType.ON);
89                 ipCameraHandler.noAudioDetected();
90             }
91             if (content.contains("<soundAlarm>2</soundAlarm>")) {
92                 ipCameraHandler.setChannelState(CHANNEL_ENABLE_AUDIO_ALARM, OnOffType.ON);
93                 ipCameraHandler.audioDetected();
94             }
95
96             ////////////// Sound Threshold //////////////
97             if (content.contains("<sensitivity>0</sensitivity>")) {
98                 ipCameraHandler.setChannelState(CHANNEL_THRESHOLD_AUDIO_ALARM, PercentType.ZERO);
99             }
100             if (content.contains("<sensitivity>1</sensitivity>")) {
101                 ipCameraHandler.setChannelState(CHANNEL_THRESHOLD_AUDIO_ALARM, PercentType.valueOf("50"));
102             }
103             if (content.contains("<sensitivity>2</sensitivity>")) {
104                 ipCameraHandler.setChannelState(CHANNEL_THRESHOLD_AUDIO_ALARM, PercentType.HUNDRED);
105             }
106
107             //////////////// Infrared LED /////////////////////
108             if (content.contains("<infraLedState>0</infraLedState>")) {
109                 ipCameraHandler.setChannelState(CHANNEL_ENABLE_LED, OnOffType.OFF);
110             }
111             if (content.contains("<infraLedState>1</infraLedState>")) {
112                 ipCameraHandler.setChannelState(CHANNEL_ENABLE_LED, OnOffType.ON);
113             }
114
115             if (content.contains("</CGI_Result>")) {
116                 ctx.close();
117                 ipCameraHandler.logger.debug("End of FOSCAM handler reached, so closing the channel to the camera now");
118             }
119
120         } finally {
121             ReferenceCountUtil.release(msg);
122         }
123     }
124
125     // This handles the commands that come from the Openhab event bus.
126     public void handleCommand(ChannelUID channelUID, Command command) {
127         if (command instanceof RefreshType) {
128             switch (channelUID.getId()) {
129                 case CHANNEL_THRESHOLD_AUDIO_ALARM:
130                     ipCameraHandler.sendHttpGET(
131                             "/cgi-bin/CGIProxy.fcgi?cmd=getAudioAlarmConfig&usr=" + username + "&pwd=" + password);
132                     return;
133                 case CHANNEL_ENABLE_AUDIO_ALARM:
134                     ipCameraHandler.sendHttpGET(
135                             "/cgi-bin/CGIProxy.fcgi?cmd=getAudioAlarmConfig&usr=" + username + "&pwd=" + password);
136                     return;
137                 case CHANNEL_ENABLE_MOTION_ALARM:
138                     ipCameraHandler
139                             .sendHttpGET("/cgi-bin/CGIProxy.fcgi?cmd=getDevState&usr=" + username + "&pwd=" + password);
140                     return;
141             }
142             return; // Return as we have handled the refresh command above and don't need to
143                     // continue further.
144         } // end of "REFRESH"
145         switch (channelUID.getId()) {
146             case CHANNEL_ENABLE_LED:
147                 // Disable the auto mode first
148                 ipCameraHandler.sendHttpGET(
149                         "/cgi-bin/CGIProxy.fcgi?cmd=setInfraLedConfig&mode=1&usr=" + username + "&pwd=" + password);
150                 ipCameraHandler.setChannelState(CHANNEL_AUTO_LED, OnOffType.OFF);
151                 if (DecimalType.ZERO.equals(command) || OnOffType.OFF.equals(command)) {
152                     ipCameraHandler.sendHttpGET(
153                             "/cgi-bin/CGIProxy.fcgi?cmd=closeInfraLed&usr=" + username + "&pwd=" + password);
154                 } else {
155                     ipCameraHandler.sendHttpGET(
156                             "/cgi-bin/CGIProxy.fcgi?cmd=openInfraLed&usr=" + username + "&pwd=" + password);
157                 }
158                 return;
159             case CHANNEL_AUTO_LED:
160                 if (OnOffType.ON.equals(command)) {
161                     ipCameraHandler.setChannelState(CHANNEL_ENABLE_LED, UnDefType.UNDEF);
162                     ipCameraHandler.sendHttpGET(
163                             "/cgi-bin/CGIProxy.fcgi?cmd=setInfraLedConfig&mode=0&usr=" + username + "&pwd=" + password);
164                 } else {
165                     ipCameraHandler.sendHttpGET(
166                             "/cgi-bin/CGIProxy.fcgi?cmd=setInfraLedConfig&mode=1&usr=" + username + "&pwd=" + password);
167                 }
168                 return;
169             case CHANNEL_THRESHOLD_AUDIO_ALARM:
170                 int value = Math.round(Float.valueOf(command.toString()));
171                 if (value == 0) {
172                     ipCameraHandler.sendHttpGET("/cgi-bin/CGIProxy.fcgi?cmd=setAudioAlarmConfig&isEnable=0&usr="
173                             + username + "&pwd=" + password);
174                 } else if (value <= 33) {
175                     ipCameraHandler
176                             .sendHttpGET("/cgi-bin/CGIProxy.fcgi?cmd=setAudioAlarmConfig&isEnable=1&sensitivity=0&usr="
177                                     + username + "&pwd=" + password);
178                 } else if (value <= 66) {
179                     ipCameraHandler
180                             .sendHttpGET("/cgi-bin/CGIProxy.fcgi?cmd=setAudioAlarmConfig&isEnable=1&sensitivity=1&usr="
181                                     + username + "&pwd=" + password);
182                 } else {
183                     ipCameraHandler
184                             .sendHttpGET("/cgi-bin/CGIProxy.fcgi?cmd=setAudioAlarmConfig&isEnable=1&sensitivity=2&usr="
185                                     + username + "&pwd=" + password);
186                 }
187                 return;
188             case CHANNEL_ENABLE_AUDIO_ALARM:
189                 if (OnOffType.ON.equals(command)) {
190                     if (ipCameraHandler.cameraConfig.getCustomAudioAlarmUrl().isEmpty()) {
191                         ipCameraHandler.sendHttpGET("/cgi-bin/CGIProxy.fcgi?cmd=setAudioAlarmConfig&isEnable=1&usr="
192                                 + username + "&pwd=" + password);
193                     } else {
194                         ipCameraHandler.sendHttpGET(ipCameraHandler.cameraConfig.getCustomAudioAlarmUrl());
195                     }
196                 } else {
197                     ipCameraHandler.sendHttpGET("/cgi-bin/CGIProxy.fcgi?cmd=setAudioAlarmConfig&isEnable=0&usr="
198                             + username + "&pwd=" + password);
199                 }
200                 return;
201             case CHANNEL_ENABLE_MOTION_ALARM:
202                 if (OnOffType.ON.equals(command)) {
203                     if (ipCameraHandler.cameraConfig.getCustomMotionAlarmUrl().isEmpty()) {
204                         ipCameraHandler.sendHttpGET("/cgi-bin/CGIProxy.fcgi?cmd=setMotionDetectConfig&isEnable=1&usr="
205                                 + username + "&pwd=" + password);
206                         ipCameraHandler.sendHttpGET("/cgi-bin/CGIProxy.fcgi?cmd=setMotionDetectConfig1&isEnable=1&usr="
207                                 + username + "&pwd=" + password);
208                     } else {
209                         ipCameraHandler.sendHttpGET(ipCameraHandler.cameraConfig.getCustomMotionAlarmUrl());
210                     }
211                 } else {
212                     ipCameraHandler.sendHttpGET("/cgi-bin/CGIProxy.fcgi?cmd=setMotionDetectConfig&isEnable=0&usr="
213                             + username + "&pwd=" + password);
214                     ipCameraHandler.sendHttpGET("/cgi-bin/CGIProxy.fcgi?cmd=setMotionDetectConfig1&isEnable=0&usr="
215                             + username + "&pwd=" + password);
216                 }
217                 return;
218             case CHANNEL_FFMPEG_MOTION_CONTROL:
219                 if (OnOffType.ON.equals(command)) {
220                     ipCameraHandler.motionAlarmEnabled = true;
221                 } else if (OnOffType.OFF.equals(command) || DecimalType.ZERO.equals(command)) {
222                     ipCameraHandler.motionAlarmEnabled = false;
223                     ipCameraHandler.noMotionDetected(CHANNEL_MOTION_ALARM);
224                 } else {
225                     ipCameraHandler.motionAlarmEnabled = true;
226                     ipCameraHandler.motionThreshold = Double.valueOf(command.toString());
227                     ipCameraHandler.motionThreshold = ipCameraHandler.motionThreshold / 10000;
228                 }
229                 ipCameraHandler.setupFfmpegFormat(FFmpegFormat.RTSP_ALARMS);
230                 return;
231         }
232     }
233
234     // If a camera does not need to poll a request as often as snapshots, it can be
235     // added here. Binding steps through the list.
236     public ArrayList<String> getLowPriorityRequests() {
237         ArrayList<String> lowPriorityRequests = new ArrayList<String>(1);
238         lowPriorityRequests.add("/cgi-bin/CGIProxy.fcgi?cmd=getDevState&usr=" + username + "&pwd=" + password);
239         return lowPriorityRequests;
240     }
241 }