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
13 package org.openhab.binding.avmfritz.internal.handler;
15 import java.util.HashSet;
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;
32 * Handler for a FRITZ!Box device. Handles polling of values from AHA devices.
34 * @author Robert Bausdorf - Initial contribution
35 * @author Christoph Weitkamp - Added support for groups
36 * @author Kai Kreuzer - Added call monitor support
39 public class BoxHandler extends AVMFritzBaseBridgeHandler {
41 protected static final Set<String> CALL_CHANNELS = new HashSet<>();
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);
50 private @Nullable CallMonitor callMonitor;
55 * @param bridge Bridge object representing a FRITZ!Box
57 public BoxHandler(Bridge bridge, HttpClient httpClient,
58 AVMFritzDynamicCommandDescriptionProvider commandDescriptionProvider) {
59 super(bridge, httpClient, commandDescriptionProvider);
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;
70 this.callMonitor = null;
72 if (this.connection == null) {
73 if (config.password != null) {
74 this.connection = new FritzAhaWebInterface(config, this, httpClient);
78 if (!callChannelsLinked()) {
79 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
80 "The 'password' parameter must be configured to use the AHA features.");
86 private boolean callChannelsLinked() {
87 return getThing().getChannels().stream()
88 .filter(c -> isLinked(c.getUID()) && CALL_CHANNELS.contains(c.getUID().getId())).count() > 0;
92 public void dispose() {
93 if (callMonitor != null) {
94 callMonitor.dispose();
101 public void updateState(String channelID, State state) {
102 super.updateState(channelID, state);