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.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;
29 import io.netty.channel.ChannelDuplexHandler;
30 import io.netty.channel.ChannelHandlerContext;
31 import io.netty.util.ReferenceCountUtil;
34 * The {@link DoorBirdHandler} is responsible for handling commands, which are
35 * sent to one of the channels.
37 * @author Matthew Skinner - Initial contribution
41 public class DoorBirdHandler extends ChannelDuplexHandler {
42 private IpCameraHandler ipCameraHandler;
44 public DoorBirdHandler(ThingHandler handler) {
45 ipCameraHandler = (IpCameraHandler) handler;
48 // This handles the incoming http replies back from the camera.
50 public void channelRead(@Nullable ChannelHandlerContext ctx, @Nullable Object msg) throws Exception {
51 if (msg == null || ctx == null) {
54 String content = msg.toString();
56 if (!content.isEmpty()) {
57 ipCameraHandler.logger.trace("HTTP Result back from camera is \t:{}:", content);
61 if (content.contains("doorbell:H")) {
62 ipCameraHandler.setChannelState(CHANNEL_DOORBELL, OnOffType.ON);
64 if (content.contains("doorbell:L")) {
65 ipCameraHandler.setChannelState(CHANNEL_DOORBELL, OnOffType.OFF);
67 if (content.contains("motionsensor:L")) {
68 ipCameraHandler.noMotionDetected(CHANNEL_MOTION_ALARM);
70 if (content.contains("motionsensor:H")) {
71 ipCameraHandler.motionDetected(CHANNEL_MOTION_ALARM);
75 ReferenceCountUtil.release(msg);
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) {
84 switch (channelUID.getId()) {
85 case CHANNEL_ACTIVATE_ALARM_OUTPUT:
86 if (OnOffType.ON.equals(command)) {
87 ipCameraHandler.sendHttpGET("/bha-api/open-door.cgi");
90 case CHANNEL_ACTIVATE_ALARM_OUTPUT2:
91 if (OnOffType.ON.equals(command)) {
92 ipCameraHandler.sendHttpGET("/bha-api/open-door.cgi?r=2");
95 case CHANNEL_EXTERNAL_LIGHT:
96 if (OnOffType.ON.equals(command)) {
97 ipCameraHandler.sendHttpGET("/bha-api/light-on.cgi");
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;