]> git.basschouten.com Git - openhab-addons.git/blob
73ea261e9223676d3c4ba39a03f24488115d2489
[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.CHANNEL_THRESHOLD_AUDIO_ALARM;
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.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;
31
32 import io.netty.channel.ChannelDuplexHandler;
33 import io.netty.channel.ChannelHandlerContext;
34 import io.netty.util.ReferenceCountUtil;
35
36 /**
37  * The {@link HttpOnlyHandler} is responsible for handling commands for generic and onvif thing types.
38  *
39  * @author Matthew Skinner - Initial contribution
40  */
41
42 @NonNullByDefault
43 public class HttpOnlyHandler extends ChannelDuplexHandler {
44     private final Logger logger = LoggerFactory.getLogger(getClass());
45     private IpCameraHandler ipCameraHandler;
46
47     public HttpOnlyHandler(IpCameraHandler handler) {
48         ipCameraHandler = handler;
49     }
50
51     // This handles the incoming http replies back from the camera.
52     @Override
53     public void channelRead(@Nullable ChannelHandlerContext ctx, @Nullable Object msg) throws Exception {
54         ReferenceCountUtil.release(msg);
55     }
56
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
61                     // continue further.
62         } // end of "REFRESH"
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;
69                 } else {
70                     ipCameraHandler.audioAlarmEnabled = true;
71                     try {
72                         ipCameraHandler.audioThreshold = Integer.valueOf(command.toString());
73                     } catch (NumberFormatException e) {
74                         logger.warn("Audio Threshold recieved an unexpected command, was it a number?");
75                     }
76                 }
77                 ipCameraHandler.setupFfmpegFormat(FFmpegFormat.RTSP_ALARMS);
78                 return;
79         }
80     }
81
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);
86     }
87 }