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.CHANNEL_THRESHOLD_AUDIO_ALARM;
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.types.Command;
28 import org.openhab.core.types.RefreshType;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 import io.netty.channel.ChannelDuplexHandler;
33 import io.netty.channel.ChannelHandlerContext;
34 import io.netty.util.ReferenceCountUtil;
37 * The {@link HttpOnlyHandler} is responsible for handling commands for generic and onvif thing types.
39 * @author Matthew Skinner - Initial contribution
43 public class HttpOnlyHandler extends ChannelDuplexHandler {
44 private final Logger logger = LoggerFactory.getLogger(getClass());
45 private IpCameraHandler ipCameraHandler;
47 public HttpOnlyHandler(IpCameraHandler handler) {
48 ipCameraHandler = handler;
51 // This handles the incoming http replies back from the camera.
53 public void channelRead(@Nullable ChannelHandlerContext ctx, @Nullable Object msg) throws Exception {
54 ReferenceCountUtil.release(msg);
57 // This handles the commands that come from the Openhab event bus.
58 public void handleCommand(ChannelUID channelUID, Command command) {
59 if (command instanceof RefreshType) {
60 return; // Return as we have handled the refresh command above and don't need to
63 switch (channelUID.getId()) {
64 case CHANNEL_THRESHOLD_AUDIO_ALARM:
65 if (OnOffType.ON.equals(command)) {
66 ipCameraHandler.audioAlarmEnabled = true;
67 } else if (OnOffType.OFF.equals(command) || DecimalType.ZERO.equals(command)) {
68 ipCameraHandler.audioAlarmEnabled = false;
70 ipCameraHandler.audioAlarmEnabled = true;
72 ipCameraHandler.audioThreshold = Integer.valueOf(command.toString());
73 } catch (NumberFormatException e) {
74 logger.warn("Audio Threshold recieved an unexpected command, was it a number?");
77 ipCameraHandler.setupFfmpegFormat(FFmpegFormat.RTSP_ALARMS);
82 // If a camera does not need to poll a request as often as snapshots, it can be
83 // added here. Binding steps through the list and sends 1 every 8 seconds.
84 public ArrayList<String> getLowPriorityRequests() {
85 return new ArrayList<String>(0);