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