]> git.basschouten.com Git - openhab-addons.git/blob
499712704bc148381341de0a8dde3567b6a4aa00
[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         try {
55             String content = msg.toString();
56             ipCameraHandler.logger.trace("HTTP Result back from camera is \t:{}:", content);
57             if (content.contains("doorbell:H")) {
58                 ipCameraHandler.setChannelState(CHANNEL_DOORBELL, OnOffType.ON);
59             }
60             if (content.contains("doorbell:L")) {
61                 ipCameraHandler.setChannelState(CHANNEL_DOORBELL, OnOffType.OFF);
62             }
63             if (content.contains("motionsensor:L")) {
64                 ipCameraHandler.noMotionDetected(CHANNEL_MOTION_ALARM);
65             }
66             if (content.contains("motionsensor:H")) {
67                 ipCameraHandler.motionDetected(CHANNEL_MOTION_ALARM);
68             }
69         } finally {
70             ReferenceCountUtil.release(msg);
71         }
72     }
73
74     // This handles the commands that come from the Openhab event bus.
75     public void handleCommand(ChannelUID channelUID, Command command) {
76         if (command instanceof RefreshType) {
77             return;
78         } // end of "REFRESH"
79         switch (channelUID.getId()) {
80             case CHANNEL_ACTIVATE_ALARM_OUTPUT:
81                 if (OnOffType.ON.equals(command)) {
82                     ipCameraHandler.sendHttpGET("/bha-api/open-door.cgi");
83                 }
84                 return;
85             case CHANNEL_ACTIVATE_ALARM_OUTPUT2:
86                 if (OnOffType.ON.equals(command)) {
87                     ipCameraHandler.sendHttpGET("/bha-api/open-door.cgi?r=2");
88                 }
89                 return;
90             case CHANNEL_EXTERNAL_LIGHT:
91                 if (OnOffType.ON.equals(command)) {
92                     ipCameraHandler.sendHttpGET("/bha-api/light-on.cgi");
93                 }
94                 return;
95         }
96     }
97
98     // If a camera does not need to poll a request as often as snapshots, it can be
99     // added here. Binding steps through the list.
100     public ArrayList<String> getLowPriorityRequests() {
101         ArrayList<String> lowPriorityRequests = new ArrayList<String>(1);
102         return lowPriorityRequests;
103     }
104 }