]> git.basschouten.com Git - openhab-addons.git/blob
5b24399d6bea07e7085a7b9683d25f225fe4a9f0
[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     public HostHandler(Thing thing) {
50         super(thing);
51         statusDrivenByBridge = false;
52     }
53
54     @Override
55     void initializeProperties(Map<String, String> properties) throws FreeboxException {
56         LanHost host = getLanHost();
57         properties.put(Thing.PROPERTY_VENDOR, host.vendorName());
58         host.getName(Source.UPNP).ifPresent(upnpName -> properties.put(Source.UPNP.name(), upnpName));
59     }
60
61     @Override
62     public void dispose() {
63         cancelPushSubscription();
64         super.dispose();
65     }
66
67     protected void cancelPushSubscription() {
68         MACAddress mac = getMac();
69         if (pushSubscribed && mac != null) {
70             try {
71                 getManager(WebSocketManager.class).unregisterListener(mac);
72             } catch (FreeboxException e) {
73                 logger.warn("Error unregistering host from the websocket: {}", e.getMessage());
74             }
75             pushSubscribed = false;
76         }
77     }
78
79     @Override
80     protected void internalPoll() throws FreeboxException {
81         if (pushSubscribed) {
82             return;
83         }
84
85         LanHost host = getLanHost();
86         updateConnectivityChannels(host);
87         logger.debug("Switching to push mode - refreshInterval will now be ignored for Connectivity data");
88         pushSubscribed = getManager(WebSocketManager.class).registerListener(host.getMac(), this);
89     }
90
91     @Override
92     protected void internalForcePoll() throws FreeboxException {
93         LanHost host = getLanHost();
94         updateConnectivityChannels(host);
95     }
96
97     protected LanHost getLanHost() throws FreeboxException {
98         MACAddress mac = getMac();
99         if (mac == null) {
100             throw new FreeboxException(
101                     "getLanHost is not possible because MAC address is undefined for the thing " + thing.getUID());
102         }
103         return getManager(LanBrowserManager.class).getHost(mac).map(hostIntf -> hostIntf.host())
104                 .orElseThrow(() -> new FreeboxException("Host data not found"));
105     }
106
107     public void updateConnectivityChannels(LanHost host) {
108         updateChannelOnOff(CONNECTIVITY, REACHABLE, host.reachable());
109         updateChannelDateTimeState(CONNECTIVITY, LAST_SEEN, host.getLastSeen());
110         updateChannelString(CONNECTIVITY, IP_ADDRESS, host.getIpv4());
111         updateStatus(host.reachable() ? ThingStatus.ONLINE : ThingStatus.OFFLINE);
112     }
113
114     public void wol() {
115         MACAddress mac = getMac();
116         if (mac == null) {
117             logger.warn("Waking up host is not possible because MAC address is undefined for the thing {}",
118                     thing.getUID());
119             return;
120         }
121         try {
122             getManager(LanBrowserManager.class).wakeOnLan(mac, getConfigAs(ApiConsumerConfiguration.class).password);
123         } catch (FreeboxException e) {
124             logger.warn("Error waking up host: {}", e.getMessage());
125         }
126     }
127
128     @Override
129     public Collection<Class<? extends ThingHandlerService>> getServices() {
130         return Set.of(HostActions.class);
131     }
132 }