2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
14 package org.openhab.binding.ipcamera.internal;
16 import static org.openhab.binding.ipcamera.internal.IpCameraBindingConstants.*;
18 import java.util.ArrayList;
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;
33 import io.netty.channel.ChannelDuplexHandler;
34 import io.netty.channel.ChannelHandlerContext;
35 import io.netty.util.ReferenceCountUtil;
38 * The {@link FoscamHandler} is responsible for handling commands, which are
39 * sent to one of the channels.
41 * @author Matthew Skinner - Initial contribution
45 public class FoscamHandler extends ChannelDuplexHandler {
46 private IpCameraHandler ipCameraHandler;
47 private String password, username;
49 public FoscamHandler(ThingHandler handler, String username, String password) {
50 ipCameraHandler = (IpCameraHandler) handler;
51 this.username = username;
52 this.password = password;
55 // This handles the incoming http replies back from the camera.
57 public void channelRead(@Nullable ChannelHandlerContext ctx, @Nullable Object msg) throws Exception {
58 if (msg == null || ctx == null) {
61 String content = msg.toString();
63 if (!content.isEmpty()) {
64 ipCameraHandler.logger.trace("HTTP Result back from camera is \t:{}:", content);
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);
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);
87 if (content.contains("<soundAlarm>1</soundAlarm>")) {
88 ipCameraHandler.setChannelState(CHANNEL_ENABLE_AUDIO_ALARM, OnOffType.ON);
89 ipCameraHandler.noAudioDetected();
91 if (content.contains("<soundAlarm>2</soundAlarm>")) {
92 ipCameraHandler.setChannelState(CHANNEL_ENABLE_AUDIO_ALARM, OnOffType.ON);
93 ipCameraHandler.audioDetected();
96 ////////////// Sound Threshold //////////////
97 if (content.contains("<sensitivity>0</sensitivity>")) {
98 ipCameraHandler.setChannelState(CHANNEL_THRESHOLD_AUDIO_ALARM, PercentType.ZERO);
100 if (content.contains("<sensitivity>1</sensitivity>")) {
101 ipCameraHandler.setChannelState(CHANNEL_THRESHOLD_AUDIO_ALARM, PercentType.valueOf("50"));
103 if (content.contains("<sensitivity>2</sensitivity>")) {
104 ipCameraHandler.setChannelState(CHANNEL_THRESHOLD_AUDIO_ALARM, PercentType.HUNDRED);
107 //////////////// Infrared LED /////////////////////
108 if (content.contains("<infraLedState>0</infraLedState>")) {
109 ipCameraHandler.setChannelState(CHANNEL_ENABLE_LED, OnOffType.OFF);
111 if (content.contains("<infraLedState>1</infraLedState>")) {
112 ipCameraHandler.setChannelState(CHANNEL_ENABLE_LED, OnOffType.ON);
115 if (content.contains("</CGI_Result>")) {
117 ipCameraHandler.logger.debug("End of FOSCAM handler reached, so closing the channel to the camera now");
121 ReferenceCountUtil.release(msg);
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);
133 case CHANNEL_ENABLE_AUDIO_ALARM:
134 ipCameraHandler.sendHttpGET(
135 "/cgi-bin/CGIProxy.fcgi?cmd=getAudioAlarmConfig&usr=" + username + "&pwd=" + password);
137 case CHANNEL_ENABLE_MOTION_ALARM:
139 .sendHttpGET("/cgi-bin/CGIProxy.fcgi?cmd=getDevState&usr=" + username + "&pwd=" + password);
142 return; // Return as we have handled the refresh command above and don't need to
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);
155 ipCameraHandler.sendHttpGET(
156 "/cgi-bin/CGIProxy.fcgi?cmd=openInfraLed&usr=" + username + "&pwd=" + password);
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);
165 ipCameraHandler.sendHttpGET(
166 "/cgi-bin/CGIProxy.fcgi?cmd=setInfraLedConfig&mode=1&usr=" + username + "&pwd=" + password);
169 case CHANNEL_THRESHOLD_AUDIO_ALARM:
170 int value = Math.round(Float.valueOf(command.toString()));
172 ipCameraHandler.sendHttpGET("/cgi-bin/CGIProxy.fcgi?cmd=setAudioAlarmConfig&isEnable=0&usr="
173 + username + "&pwd=" + password);
174 } else if (value <= 33) {
176 .sendHttpGET("/cgi-bin/CGIProxy.fcgi?cmd=setAudioAlarmConfig&isEnable=1&sensitivity=0&usr="
177 + username + "&pwd=" + password);
178 } else if (value <= 66) {
180 .sendHttpGET("/cgi-bin/CGIProxy.fcgi?cmd=setAudioAlarmConfig&isEnable=1&sensitivity=1&usr="
181 + username + "&pwd=" + password);
184 .sendHttpGET("/cgi-bin/CGIProxy.fcgi?cmd=setAudioAlarmConfig&isEnable=1&sensitivity=2&usr="
185 + username + "&pwd=" + password);
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);
194 ipCameraHandler.sendHttpGET(ipCameraHandler.cameraConfig.getCustomAudioAlarmUrl());
197 ipCameraHandler.sendHttpGET("/cgi-bin/CGIProxy.fcgi?cmd=setAudioAlarmConfig&isEnable=0&usr="
198 + username + "&pwd=" + password);
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);
209 ipCameraHandler.sendHttpGET(ipCameraHandler.cameraConfig.getCustomMotionAlarmUrl());
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);
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);
225 ipCameraHandler.motionAlarmEnabled = true;
226 ipCameraHandler.motionThreshold = Double.valueOf(command.toString());
227 ipCameraHandler.motionThreshold = ipCameraHandler.motionThreshold / 10000;
229 ipCameraHandler.setupFfmpegFormat(FFmpegFormat.RTSP_ALARMS);
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;