]> git.basschouten.com Git - openhab-addons.git/blob
eacac76df48dba2c6381fb0a098bf3f996009f8a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.freeboxos.internal.handler;
14
15 import static org.openhab.binding.freeboxos.internal.FreeboxOsBindingConstants.*;
16
17 import java.util.Collection;
18 import java.util.Map;
19 import java.util.Set;
20
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.binding.ThingHandlerService;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 import inet.ipaddr.mac.MACAddress;
36
37 /**
38  * The {@link HostHandler} is responsible for all network equipments hosted on the network
39  *
40  * @author GaĆ«l L'hopital - Initial contribution
41  */
42 @NonNullByDefault
43 public class HostHandler extends ApiConsumerHandler {
44     private final Logger logger = LoggerFactory.getLogger(HostHandler.class);
45
46     // We start in pull mode and switch to push after a first update...
47     protected boolean pushSubscribed = false;
48
49     protected boolean reachable;
50
51     private int tryConfigureMediaSink = 1;
52
53     public HostHandler(Thing thing) {
54         super(thing);
55         statusDrivenByBridge = false;
56     }
57
58     @Override
59     void initializeProperties(Map<String, String> properties) throws FreeboxException {
60         LanHost host = getLanHost();
61         properties.put(Thing.PROPERTY_VENDOR, host.vendorName());
62         host.getName(Source.UPNP).ifPresent(upnpName -> properties.put(Source.UPNP.name(), upnpName));
63     }
64
65     @Override
66     public void dispose() {
67         cancelPushSubscription();
68         super.dispose();
69     }
70
71     protected void cancelPushSubscription() {
72         MACAddress mac = getMac();
73         if (pushSubscribed && mac != null) {
74             try {
75                 getManager(WebSocketManager.class).unregisterListener(mac);
76             } catch (FreeboxException e) {
77                 logger.warn("Error unregistering host from the websocket: {}", e.getMessage());
78             }
79             pushSubscribed = false;
80         }
81     }
82
83     @Override
84     protected void internalPoll() throws FreeboxException {
85         if (tryConfigureMediaSink > 0) {
86             configureMediaSink();
87             tryConfigureMediaSink--;
88         }
89
90         if (pushSubscribed) {
91             return;
92         }
93
94         LanHost host = getLanHost();
95         updateConnectivityChannels(host);
96         logger.debug("Switching to push mode - refreshInterval will now be ignored for Connectivity data");
97         pushSubscribed = getManager(WebSocketManager.class).registerListener(host.getMac(), this);
98     }
99
100     @Override
101     protected void internalForcePoll() throws FreeboxException {
102         LanHost host = getLanHost();
103         updateConnectivityChannels(host);
104     }
105
106     protected LanHost getLanHost() throws FreeboxException {
107         MACAddress mac = getMac();
108         if (mac == null) {
109             throw new FreeboxException(
110                     "getLanHost is not possible because MAC address is undefined for the thing " + thing.getUID());
111         }
112         return getManager(LanBrowserManager.class).getHost(mac).map(hostIntf -> hostIntf.host())
113                 .orElseThrow(() -> new FreeboxException("Host data not found"));
114     }
115
116     public void updateConnectivityChannels(LanHost host) {
117         updateChannelOnOff(CONNECTIVITY, REACHABLE, host.reachable());
118         updateChannelDateTimeState(CONNECTIVITY, LAST_SEEN, host.getLastSeen());
119         updateChannelString(CONNECTIVITY, IP_ADDRESS, host.getIpv4());
120         updateStatus(host.reachable() ? ThingStatus.ONLINE : ThingStatus.OFFLINE);
121         // We will check and configure audio sink only when the host reachability changed
122         if (reachable != host.reachable()) {
123             reachable = host.reachable();
124             // It can take time until the Media Receiver API returns the receiver after it becomes reachable.
125             // So this will be checked during the next 2 polls.
126             tryConfigureMediaSink = 2;
127         }
128     }
129
130     public void wol() {
131         MACAddress mac = getMac();
132         if (mac == null) {
133             logger.warn("Waking up host is not possible because MAC address is undefined for the thing {}",
134                     thing.getUID());
135             return;
136         }
137         try {
138             getManager(LanBrowserManager.class).wakeOnLan(mac, getConfigAs(ApiConsumerConfiguration.class).password);
139         } catch (FreeboxException e) {
140             logger.warn("Error waking up host: {}", e.getMessage());
141         }
142     }
143
144     @Override
145     public Collection<Class<? extends ThingHandlerService>> getServices() {
146         return Set.of(HostActions.class);
147     }
148 }