]> git.basschouten.com Git - openhab-addons.git/blob
75d6b287149e3ae27e5c2ecc18366a59d77dc9c1
[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 package org.openhab.binding.avmfritz.internal.handler;
14
15 import java.util.HashSet;
16 import java.util.Set;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.eclipse.jetty.client.HttpClient;
21 import org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants;
22 import org.openhab.binding.avmfritz.internal.AVMFritzDynamicCommandDescriptionProvider;
23 import org.openhab.binding.avmfritz.internal.callmonitor.CallMonitor;
24 import org.openhab.binding.avmfritz.internal.config.AVMFritzBoxConfiguration;
25 import org.openhab.binding.avmfritz.internal.hardware.FritzAhaWebInterface;
26 import org.openhab.core.thing.Bridge;
27 import org.openhab.core.thing.ThingStatus;
28 import org.openhab.core.thing.ThingStatusDetail;
29 import org.openhab.core.types.State;
30
31 /**
32  * Handler for a FRITZ!Box device. Handles polling of values from AHA devices.
33  *
34  * @author Robert Bausdorf - Initial contribution
35  * @author Christoph Weitkamp - Added support for groups
36  * @author Kai Kreuzer - Added call monitor support
37  */
38 @NonNullByDefault
39 public class BoxHandler extends AVMFritzBaseBridgeHandler {
40
41     protected static final Set<String> CALL_CHANNELS = new HashSet<>();
42     static {
43         // TODO: We are still on Java 8 and cannot use Set.of
44         CALL_CHANNELS.add(AVMFritzBindingConstants.CHANNEL_CALL_ACTIVE);
45         CALL_CHANNELS.add(AVMFritzBindingConstants.CHANNEL_CALL_INCOMING);
46         CALL_CHANNELS.add(AVMFritzBindingConstants.CHANNEL_CALL_OUTGOING);
47         CALL_CHANNELS.add(AVMFritzBindingConstants.CHANNEL_CALL_STATE);
48     }
49
50     private @Nullable CallMonitor callMonitor;
51
52     /**
53      * Constructor
54      *
55      * @param bridge Bridge object representing a FRITZ!Box
56      */
57     public BoxHandler(Bridge bridge, HttpClient httpClient,
58             AVMFritzDynamicCommandDescriptionProvider commandDescriptionProvider) {
59         super(bridge, httpClient, commandDescriptionProvider);
60     }
61
62     @Override
63     protected void manageConnections() {
64         AVMFritzBoxConfiguration config = getConfigAs(AVMFritzBoxConfiguration.class);
65         if (this.callMonitor == null && callChannelsLinked()) {
66             this.callMonitor = new CallMonitor(config.ipAddress, this, scheduler);
67         } else if (this.callMonitor != null && !callChannelsLinked()) {
68             CallMonitor cm = this.callMonitor;
69             cm.dispose();
70             this.callMonitor = null;
71         }
72         if (this.connection == null) {
73             if (config.password != null) {
74                 this.connection = new FritzAhaWebInterface(config, this, httpClient);
75                 stopPolling();
76                 startPolling();
77             } else {
78                 if (!callChannelsLinked()) {
79                     updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
80                             "The 'password' parameter must be configured to use the AHA features.");
81                 }
82             }
83         }
84     }
85
86     private boolean callChannelsLinked() {
87         return getThing().getChannels().stream()
88                 .filter(c -> isLinked(c.getUID()) && CALL_CHANNELS.contains(c.getUID().getId())).count() > 0;
89     }
90
91     @Override
92     public void dispose() {
93         if (callMonitor != null) {
94             callMonitor.dispose();
95             callMonitor = null;
96         }
97         super.dispose();
98     }
99
100     @Override
101     public void updateState(String channelID, State state) {
102         super.updateState(channelID, state);
103     }
104 }