]> git.basschouten.com Git - openhab-addons.git/blob
0208037f825e61e83eb610854dcc43116a54a8a2
[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.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
31 import io.netty.channel.ChannelDuplexHandler;
32 import io.netty.channel.ChannelHandlerContext;
33 import io.netty.util.ReferenceCountUtil;
34
35 /**
36  * The {@link DoorBirdHandler} is responsible for handling commands, which are
37  * sent to one of the channels.
38  *
39  * @author Matthew Skinner - Initial contribution
40  */
41
42 @NonNullByDefault
43 public class DoorBirdHandler extends ChannelDuplexHandler {
44     private IpCameraHandler ipCameraHandler;
45
46     public DoorBirdHandler(ThingHandler handler) {
47         ipCameraHandler = (IpCameraHandler) handler;
48     }
49
50     // This handles the incoming http replies back from the camera.
51     @Override
52     public void channelRead(@Nullable ChannelHandlerContext ctx, @Nullable Object msg) throws Exception {
53         if (msg == null || ctx == null) {
54             return;
55         }
56         String content = msg.toString();
57         try {
58             if (!content.isEmpty()) {
59                 ipCameraHandler.logger.trace("HTTP Result back from camera is \t:{}:", content);
60             } else {
61                 return;
62             }
63             if (content.contains("doorbell:H")) {
64                 ipCameraHandler.setChannelState(CHANNEL_DOORBELL, OnOffType.ON);
65             }
66             if (content.contains("doorbell:L")) {
67                 ipCameraHandler.setChannelState(CHANNEL_DOORBELL, OnOffType.OFF);
68             }
69             if (content.contains("motionsensor:L")) {
70                 ipCameraHandler.noMotionDetected(CHANNEL_MOTION_ALARM);
71             }
72             if (content.contains("motionsensor:H")) {
73                 ipCameraHandler.motionDetected(CHANNEL_MOTION_ALARM);
74             }
75
76         } finally {
77             ReferenceCountUtil.release(msg);
78         }
79     }
80
81     // This handles the commands that come from the Openhab event bus.
82     public void handleCommand(ChannelUID channelUID, Command command) {
83         if (command instanceof RefreshType) {
84             return;
85         } // end of "REFRESH"
86         switch (channelUID.getId()) {
87             case CHANNEL_ACTIVATE_ALARM_OUTPUT:
88                 if (OnOffType.ON.equals(command)) {
89                     ipCameraHandler.sendHttpGET("/bha-api/open-door.cgi");
90                 }
91                 return;
92             case CHANNEL_ACTIVATE_ALARM_OUTPUT2:
93                 if (OnOffType.ON.equals(command)) {
94                     ipCameraHandler.sendHttpGET("/bha-api/open-door.cgi?r=2");
95                 }
96                 return;
97             case CHANNEL_EXTERNAL_LIGHT:
98                 if (OnOffType.ON.equals(command)) {
99                     ipCameraHandler.sendHttpGET("/bha-api/light-on.cgi");
100                 }
101                 return;
102             case CHANNEL_FFMPEG_MOTION_CONTROL:
103                 if (OnOffType.ON.equals(command)) {
104                     ipCameraHandler.motionAlarmEnabled = true;
105                 } else if (OnOffType.OFF.equals(command) || DecimalType.ZERO.equals(command)) {
106                     ipCameraHandler.motionAlarmEnabled = false;
107                     ipCameraHandler.noMotionDetected(CHANNEL_MOTION_ALARM);
108                 } else {
109                     ipCameraHandler.motionAlarmEnabled = true;
110                     ipCameraHandler.motionThreshold = Double.valueOf(command.toString());
111                     ipCameraHandler.motionThreshold = ipCameraHandler.motionThreshold / 10000;
112                 }
113                 ipCameraHandler.setupFfmpegFormat(FFmpegFormat.RTSP_ALARMS);
114                 return;
115         }
116     }
117
118     // If a camera does not need to poll a request as often as snapshots, it can be
119     // added here. Binding steps through the list.
120     public ArrayList<String> getLowPriorityRequests() {
121         ArrayList<String> lowPriorityRequests = new ArrayList<String>(1);
122         return lowPriorityRequests;
123     }
124 }