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.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;
31 import io.netty.channel.ChannelDuplexHandler;
32 import io.netty.channel.ChannelHandlerContext;
33 import io.netty.util.ReferenceCountUtil;
36 * The {@link DoorBirdHandler} is responsible for handling commands, which are
37 * sent to one of the channels.
39 * @author Matthew Skinner - Initial contribution
43 public class DoorBirdHandler extends ChannelDuplexHandler {
44 private IpCameraHandler ipCameraHandler;
46 public DoorBirdHandler(ThingHandler handler) {
47 ipCameraHandler = (IpCameraHandler) handler;
50 // This handles the incoming http replies back from the camera.
52 public void channelRead(@Nullable ChannelHandlerContext ctx, @Nullable Object msg) throws Exception {
53 if (msg == null || ctx == null) {
56 String content = msg.toString();
58 if (!content.isEmpty()) {
59 ipCameraHandler.logger.trace("HTTP Result back from camera is \t:{}:", content);
63 if (content.contains("doorbell:H")) {
64 ipCameraHandler.setChannelState(CHANNEL_DOORBELL, OnOffType.ON);
66 if (content.contains("doorbell:L")) {
67 ipCameraHandler.setChannelState(CHANNEL_DOORBELL, OnOffType.OFF);
69 if (content.contains("motionsensor:L")) {
70 ipCameraHandler.noMotionDetected(CHANNEL_MOTION_ALARM);
72 if (content.contains("motionsensor:H")) {
73 ipCameraHandler.motionDetected(CHANNEL_MOTION_ALARM);
77 ReferenceCountUtil.release(msg);
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) {
86 switch (channelUID.getId()) {
87 case CHANNEL_ACTIVATE_ALARM_OUTPUT:
88 if (OnOffType.ON.equals(command)) {
89 ipCameraHandler.sendHttpGET("/bha-api/open-door.cgi");
92 case CHANNEL_ACTIVATE_ALARM_OUTPUT2:
93 if (OnOffType.ON.equals(command)) {
94 ipCameraHandler.sendHttpGET("/bha-api/open-door.cgi?r=2");
97 case CHANNEL_EXTERNAL_LIGHT:
98 if (OnOffType.ON.equals(command)) {
99 ipCameraHandler.sendHttpGET("/bha-api/light-on.cgi");
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);
109 ipCameraHandler.motionAlarmEnabled = true;
110 ipCameraHandler.motionThreshold = Double.valueOf(command.toString());
111 ipCameraHandler.motionThreshold = ipCameraHandler.motionThreshold / 10000;
113 ipCameraHandler.setupFfmpegFormat(FFmpegFormat.RTSP_ALARMS);
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;