]> git.basschouten.com Git - openhab-addons.git/blob
4a238d001e1e5dbe8731d534c1799153d490b46e
[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.ThingStatusDetail;
32 import org.openhab.core.thing.binding.ThingHandlerService;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 import inet.ipaddr.mac.MACAddress;
37
38 /**
39  * The {@link HostHandler} is responsible for all network equipments hosted on the network
40  *
41  * @author GaĆ«l L'hopital - Initial contribution
42  */
43 @NonNullByDefault
44 public class HostHandler extends ApiConsumerHandler {
45     private final Logger logger = LoggerFactory.getLogger(HostHandler.class);
46
47     // We start in pull mode and switch to push after a first update...
48     protected boolean pushSubscribed = false;
49
50     protected boolean statusDrivenByLanConnectivity = true;
51
52     protected boolean reachable;
53
54     private int tryConfigureMediaSink = 1;
55
56     public HostHandler(Thing thing) {
57         super(thing);
58         statusDrivenByBridge = false;
59     }
60
61     @Override
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));
66     }
67
68     @Override
69     public void dispose() {
70         cancelPushSubscription();
71         super.dispose();
72     }
73
74     protected void cancelPushSubscription() {
75         MACAddress mac = getMac();
76         if (pushSubscribed && mac != null) {
77             try {
78                 getManager(WebSocketManager.class).unregisterListener(mac);
79             } catch (FreeboxException e) {
80                 logger.warn("Error unregistering host from the websocket: {}", e.getMessage());
81             }
82             pushSubscribed = false;
83         }
84     }
85
86     @Override
87     protected void internalPoll() throws FreeboxException {
88         if (tryConfigureMediaSink > 0) {
89             configureMediaSink();
90             tryConfigureMediaSink--;
91         }
92
93         if (pushSubscribed) {
94             return;
95         }
96
97         LanHost host = getLanHost();
98         updateConnectivityChannels(host);
99         logger.debug("{}: switching to push mode - refreshInterval will now be ignored for Connectivity data",
100                 thing.getUID());
101         pushSubscribed = getManager(WebSocketManager.class).registerListener(host.getMac(), this);
102     }
103
104     @Override
105     protected void internalForcePoll() throws FreeboxException {
106         LanHost host = getLanHost();
107         updateConnectivityChannels(host);
108     }
109
110     protected LanHost getLanHost() throws FreeboxException {
111         MACAddress mac = getMac();
112         if (mac == null) {
113             throw new FreeboxException(
114                     "getLanHost is not possible because MAC address is undefined for the thing " + thing.getUID());
115         }
116         return getManager(LanBrowserManager.class).getHost(mac).map(hostIntf -> hostIntf.host())
117                 .orElseThrow(() -> new FreeboxException("Host data not found"));
118     }
119
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);
128             } else {
129                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE, "@text/info-host-not-reachable");
130             }
131         }
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;
138         }
139     }
140
141     public void wol() {
142         MACAddress mac = getMac();
143         if (mac == null) {
144             logger.warn("Waking up host is not possible because MAC address is undefined for the thing {}",
145                     thing.getUID());
146             return;
147         }
148         try {
149             getManager(LanBrowserManager.class).wakeOnLan(mac, getConfigAs(ApiConsumerConfiguration.class).password);
150         } catch (FreeboxException e) {
151             logger.warn("Error waking up host: {}", e.getMessage());
152         }
153     }
154
155     @Override
156     public Collection<Class<? extends ThingHandlerService>> getServices() {
157         return Set.of(HostActions.class);
158     }
159 }