]> git.basschouten.com Git - openhab-addons.git/blob
a3fb85b8586b59f480e4deed76abeb3758ad4dbd
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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 static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.*;
16
17 import java.util.Set;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.eclipse.jetty.client.HttpClient;
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     private static final Set<String> CALL_CHANNELS = Set.of(CHANNEL_CALL_ACTIVE, CHANNEL_CALL_INCOMING,
42             CHANNEL_CALL_OUTGOING, CHANNEL_CALL_STATE);
43
44     private @Nullable CallMonitor callMonitor;
45
46     /**
47      * Constructor
48      *
49      * @param bridge Bridge object representing a FRITZ!Box
50      */
51     public BoxHandler(Bridge bridge, HttpClient httpClient,
52             AVMFritzDynamicCommandDescriptionProvider commandDescriptionProvider) {
53         super(bridge, httpClient, commandDescriptionProvider);
54     }
55
56     @Override
57     protected void manageConnections() {
58         AVMFritzBoxConfiguration config = getConfigAs(AVMFritzBoxConfiguration.class);
59         CallMonitor cm = this.callMonitor;
60         if (cm == null && callChannelsLinked()) {
61             this.callMonitor = new CallMonitor(config.ipAddress, this, scheduler);
62         } else if (cm != null && !callChannelsLinked()) {
63             cm.dispose();
64             this.callMonitor = null;
65         }
66         if (this.connection == null) {
67             if (config.password != null) {
68                 this.connection = new FritzAhaWebInterface(config, this, httpClient);
69                 stopPolling();
70                 startPolling();
71             } else {
72                 if (!callChannelsLinked()) {
73                     updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
74                             "The 'password' parameter must be configured to use the AHA features.");
75                 }
76             }
77         }
78     }
79
80     private boolean callChannelsLinked() {
81         return getThing().getChannels().stream()
82                 .filter(c -> isLinked(c.getUID()) && CALL_CHANNELS.contains(c.getUID().getId())).count() > 0;
83     }
84
85     @Override
86     public void dispose() {
87         if (callMonitor != null) {
88             callMonitor.dispose();
89             callMonitor = null;
90         }
91         super.dispose();
92     }
93
94     @Override
95     public void updateState(String channelID, State state) {
96         super.updateState(channelID, state);
97     }
98
99     @Override
100     public void handleRefreshCommand() {
101         refreshCallMonitorChannels();
102         super.handleRefreshCommand();
103     }
104
105     private void refreshCallMonitorChannels() {
106         CallMonitor cm = this.callMonitor;
107         if (cm != null) {
108             // initialize states of call monitor channels
109             cm.resetChannels();
110         }
111     }
112 }