]> git.basschouten.com Git - openhab-addons.git/blob
e24fdc3303ed361bd007a3eef559370c7b881bbe
[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 /**
36  * The {@link HostHandler} is responsible for all network equipments hosted on the network
37  *
38  * @author GaĆ«l L'hopital - Initial contribution
39  */
40 @NonNullByDefault
41 public class HostHandler extends ApiConsumerHandler {
42     private final Logger logger = LoggerFactory.getLogger(HostHandler.class);
43
44     // We start in pull mode and switch to push after a first update...
45     protected boolean pushSubscribed = false;
46
47     public HostHandler(Thing thing) {
48         super(thing);
49         statusDrivenByBridge = false;
50     }
51
52     @Override
53     void initializeProperties(Map<String, String> properties) throws FreeboxException {
54         LanHost host = getLanHost();
55         properties.put(Thing.PROPERTY_VENDOR, host.vendorName());
56         host.getName(Source.UPNP).ifPresent(upnpName -> properties.put(Source.UPNP.name(), upnpName));
57     }
58
59     @Override
60     public void dispose() {
61         cancelPushSubscription();
62         super.dispose();
63     }
64
65     protected void cancelPushSubscription() {
66         if (pushSubscribed) {
67             try {
68                 getManager(WebSocketManager.class).unregisterListener(getMac());
69             } catch (FreeboxException e) {
70                 logger.warn("Error unregistering host from the websocket: {}", e.getMessage());
71             }
72             pushSubscribed = false;
73         }
74     }
75
76     @Override
77     protected void internalPoll() throws FreeboxException {
78         if (pushSubscribed) {
79             return;
80         }
81
82         LanHost host = getLanHost();
83         updateConnectivityChannels(host);
84         logger.debug("Switching to push mode - refreshInterval will now be ignored for Connectivity data");
85         pushSubscribed = getManager(WebSocketManager.class).registerListener(host.getMac(), this);
86     }
87
88     @Override
89     protected void internalForcePoll() throws FreeboxException {
90         LanHost host = getLanHost();
91         updateConnectivityChannels(host);
92     }
93
94     protected LanHost getLanHost() throws FreeboxException {
95         return getManager(LanBrowserManager.class).getHost(getMac()).map(hostIntf -> hostIntf.host())
96                 .orElseThrow(() -> new FreeboxException("Host data not found"));
97     }
98
99     public void updateConnectivityChannels(LanHost host) {
100         updateChannelOnOff(CONNECTIVITY, REACHABLE, host.reachable());
101         updateChannelDateTimeState(CONNECTIVITY, LAST_SEEN, host.getLastSeen());
102         updateChannelString(CONNECTIVITY, IP_ADDRESS, host.getIpv4());
103         updateStatus(host.reachable() ? ThingStatus.ONLINE : ThingStatus.OFFLINE);
104     }
105
106     public void wol() {
107         try {
108             getManager(LanBrowserManager.class).wakeOnLan(getMac(),
109                     getConfigAs(ApiConsumerConfiguration.class).password);
110         } catch (FreeboxException e) {
111             logger.warn("Error waking up host: {}", e.getMessage());
112         }
113     }
114
115     @Override
116     public Collection<Class<? extends ThingHandlerService>> getServices() {
117         return Set.of(HostActions.class);
118     }
119 }