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