2 * Copyright (c) 2010-2023 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 static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.*;
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;
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 private static final Set<String> CALL_CHANNELS = Set.of(CHANNEL_CALL_ACTIVE, CHANNEL_CALL_INCOMING,
42 CHANNEL_CALL_OUTGOING, CHANNEL_CALL_STATE);
44 private @Nullable CallMonitor callMonitor;
49 * @param bridge Bridge object representing a FRITZ!Box
51 public BoxHandler(Bridge bridge, HttpClient httpClient,
52 AVMFritzDynamicCommandDescriptionProvider commandDescriptionProvider) {
53 super(bridge, httpClient, commandDescriptionProvider);
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()) {
64 this.callMonitor = null;
66 if (this.connection == null) {
67 if (config.password != null) {
68 this.connection = new FritzAhaWebInterface(config, this, httpClient);
72 if (!callChannelsLinked()) {
73 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
74 "The 'password' parameter must be configured to use the AHA features.");
80 private boolean callChannelsLinked() {
81 return getThing().getChannels().stream()
82 .filter(c -> isLinked(c.getUID()) && CALL_CHANNELS.contains(c.getUID().getId())).count() > 0;
86 public void dispose() {
87 if (callMonitor != null) {
88 callMonitor.dispose();
95 public void updateState(String channelID, State state) {
96 super.updateState(channelID, state);
100 public void handleRefreshCommand() {
101 refreshCallMonitorChannels();
102 super.handleRefreshCommand();
105 private void refreshCallMonitorChannels() {
106 CallMonitor cm = this.callMonitor;
108 // initialize states of call monitor channels