2 * Copyright (c) 2010-2024 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.freeboxos.internal.handler;
15 import static org.openhab.binding.freeboxos.internal.FreeboxOsBindingConstants.*;
17 import java.util.Collection;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.openhab.binding.freeboxos.internal.action.HostActions;
23 import org.openhab.binding.freeboxos.internal.api.FreeboxException;
24 import org.openhab.binding.freeboxos.internal.api.rest.LanBrowserManager;
25 import org.openhab.binding.freeboxos.internal.api.rest.LanBrowserManager.LanHost;
26 import org.openhab.binding.freeboxos.internal.api.rest.LanBrowserManager.Source;
27 import org.openhab.binding.freeboxos.internal.api.rest.WebSocketManager;
28 import org.openhab.binding.freeboxos.internal.config.ApiConsumerConfiguration;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.thing.ThingStatus;
31 import org.openhab.core.thing.ThingStatusDetail;
32 import org.openhab.core.thing.binding.ThingHandlerService;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 import inet.ipaddr.mac.MACAddress;
39 * The {@link HostHandler} is responsible for all network equipments hosted on the network
41 * @author Gaƫl L'hopital - Initial contribution
44 public class HostHandler extends ApiConsumerHandler {
45 private final Logger logger = LoggerFactory.getLogger(HostHandler.class);
47 // We start in pull mode and switch to push after a first update...
48 protected boolean pushSubscribed = false;
50 protected boolean statusDrivenByLanConnectivity = true;
52 protected boolean reachable;
54 private int tryConfigureMediaSink = 1;
56 public HostHandler(Thing thing) {
58 statusDrivenByBridge = false;
62 void initializeProperties(Map<String, String> properties) throws FreeboxException {
63 LanHost host = getLanHost();
64 properties.put(Thing.PROPERTY_VENDOR, host.vendorName());
65 host.getName(Source.UPNP).ifPresent(upnpName -> properties.put(Source.UPNP.name(), upnpName));
69 public void dispose() {
70 cancelPushSubscription();
74 protected void cancelPushSubscription() {
75 MACAddress mac = getMac();
76 if (pushSubscribed && mac != null) {
78 getManager(WebSocketManager.class).unregisterListener(mac);
79 } catch (FreeboxException e) {
80 logger.warn("Error unregistering host from the websocket: {}", e.getMessage());
82 pushSubscribed = false;
87 protected void internalPoll() throws FreeboxException {
88 if (tryConfigureMediaSink > 0) {
90 tryConfigureMediaSink--;
97 LanHost host = getLanHost();
98 updateConnectivityChannels(host);
99 logger.debug("{}: switching to push mode - refreshInterval will now be ignored for Connectivity data",
101 pushSubscribed = getManager(WebSocketManager.class).registerListener(host.getMac(), this);
105 protected void internalForcePoll() throws FreeboxException {
106 LanHost host = getLanHost();
107 updateConnectivityChannels(host);
110 protected LanHost getLanHost() throws FreeboxException {
111 MACAddress mac = getMac();
113 throw new FreeboxException(
114 "getLanHost is not possible because MAC address is undefined for the thing " + thing.getUID());
116 return getManager(LanBrowserManager.class).getHost(mac).map(hostIntf -> hostIntf.host())
117 .orElseThrow(() -> new FreeboxException("Host data not found"));
120 public void updateConnectivityChannels(LanHost host) {
121 logger.debug("{}: updateConnectivityChannels with host.reachable() = {}", thing.getUID(), host.reachable());
122 updateChannelOnOff(CONNECTIVITY, REACHABLE, host.reachable());
123 updateChannelDateTimeState(CONNECTIVITY, LAST_SEEN, host.getLastSeen());
124 updateChannelString(CONNECTIVITY, IP_ADDRESS, host.getIpv4());
125 if (statusDrivenByLanConnectivity) {
126 if (host.reachable()) {
127 updateStatus(ThingStatus.ONLINE);
129 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE, "@text/info-host-not-reachable");
132 // We will check and configure audio sink only when the host reachability changed
133 if (reachable != host.reachable()) {
134 reachable = host.reachable();
135 // It can take time until the Media Receiver API returns the receiver after it becomes reachable.
136 // So this will be checked during the next 2 polls.
137 tryConfigureMediaSink = 2;
142 MACAddress mac = getMac();
144 logger.warn("Waking up host is not possible because MAC address is undefined for the thing {}",
149 getManager(LanBrowserManager.class).wakeOnLan(mac, getConfigAs(ApiConsumerConfiguration.class).password);
150 } catch (FreeboxException e) {
151 logger.warn("Error waking up host: {}", e.getMessage());
156 public Collection<Class<? extends ThingHandlerService>> getServices() {
157 return Set.of(HostActions.class);