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