]> git.basschouten.com Git - openhab-addons.git/blob
c600c8814789247740275764f3ff5ead9a982a60
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.CHANNEL_THRESHOLD_AUDIO_ALARM;
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.IpCameraBindingConstants.FFmpegFormat;
22 import org.openhab.binding.ipcamera.internal.handler.IpCameraHandler;
23 import org.openhab.core.library.types.DecimalType;
24 import org.openhab.core.library.types.OnOffType;
25 import org.openhab.core.thing.ChannelUID;
26 import org.openhab.core.types.Command;
27 import org.openhab.core.types.RefreshType;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import io.netty.channel.ChannelDuplexHandler;
32 import io.netty.channel.ChannelHandlerContext;
33 import io.netty.util.ReferenceCountUtil;
34
35 /**
36  * The {@link HttpOnlyHandler} is responsible for handling commands for generic and onvif thing types.
37  *
38  * @author Matthew Skinner - Initial contribution
39  */
40
41 @NonNullByDefault
42 public class HttpOnlyHandler extends ChannelDuplexHandler {
43     private final Logger logger = LoggerFactory.getLogger(getClass());
44     private IpCameraHandler ipCameraHandler;
45
46     public HttpOnlyHandler(IpCameraHandler handler) {
47         ipCameraHandler = handler;
48     }
49
50     // This handles the incoming http replies back from the camera.
51     @Override
52     public void channelRead(@Nullable ChannelHandlerContext ctx, @Nullable Object msg) throws Exception {
53         ReferenceCountUtil.release(msg);
54     }
55
56     // This handles the commands that come from the Openhab event bus.
57     public void handleCommand(ChannelUID channelUID, Command command) {
58         if (command instanceof RefreshType) {
59             return; // Return as we have handled the refresh command above and don't need to
60                     // continue further.
61         } // end of "REFRESH"
62         switch (channelUID.getId()) {
63             case CHANNEL_THRESHOLD_AUDIO_ALARM:
64                 if (OnOffType.ON.equals(command)) {
65                     ipCameraHandler.ffmpegAudioAlarmEnabled = true;
66                 } else if (OnOffType.OFF.equals(command) || DecimalType.ZERO.equals(command)) {
67                     ipCameraHandler.ffmpegAudioAlarmEnabled = false;
68                 } else {
69                     ipCameraHandler.ffmpegAudioAlarmEnabled = true;
70                     try {
71                         ipCameraHandler.audioThreshold = Integer.valueOf(command.toString());
72                     } catch (NumberFormatException e) {
73                         logger.warn("Audio Threshold recieved an unexpected command, was it a number?");
74                     }
75                 }
76                 ipCameraHandler.setupFfmpegFormat(FFmpegFormat.RTSP_ALARMS);
77                 return;
78         }
79     }
80
81     // If a camera does not need to poll a request as often as snapshots, it can be
82     // added here. Binding steps through the list and sends 1 every 8 seconds.
83     public ArrayList<String> getLowPriorityRequests() {
84         return new ArrayList<String>(0);
85     }
86 }