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