]> git.basschouten.com Git - openhab-addons.git/blob
d7966549eca5caded484aa5efb070013529ac6fe
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.Collections;
19 import java.util.Map;
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.HostIntf;
26 import org.openhab.binding.freeboxos.internal.api.rest.LanBrowserManager.LanHost;
27 import org.openhab.binding.freeboxos.internal.api.rest.LanBrowserManager.Source;
28 import org.openhab.binding.freeboxos.internal.api.rest.WebSocketManager;
29 import org.openhab.binding.freeboxos.internal.config.ApiConsumerConfiguration;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.thing.ThingStatus;
32 import org.openhab.core.thing.binding.ThingHandlerService;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * The {@link HostHandler} is responsible for all network equipments hosted on the network
38  *
39  * @author GaĆ«l L'hopital - Initial contribution
40  */
41 @NonNullByDefault
42 public class HostHandler extends ApiConsumerHandler {
43     private final Logger logger = LoggerFactory.getLogger(HostHandler.class);
44
45     // We start in pull mode and switch to push after a first update
46     private boolean pushSubscribed = false;
47
48     public HostHandler(Thing thing) {
49         super(thing);
50     }
51
52     @Override
53     void initializeProperties(Map<String, String> properties) throws FreeboxException {
54         getManager(LanBrowserManager.class).getHost(getMac()).ifPresent(result -> {
55             LanHost host = result.host();
56             properties.put(Thing.PROPERTY_VENDOR, host.vendorName());
57             host.getUPnPName().ifPresent(upnpName -> properties.put(Source.UPNP.name(), upnpName));
58         });
59     }
60
61     @Override
62     public void dispose() {
63         try {
64             getManager(WebSocketManager.class).unregisterListener(getMac());
65         } catch (FreeboxException e) {
66             logger.warn("Error unregistering host from the websocket: {}", e.getMessage());
67         }
68         super.dispose();
69     }
70
71     @Override
72     protected void internalPoll() throws FreeboxException {
73         if (pushSubscribed) {
74             return;
75         }
76         HostIntf data = getManager(LanBrowserManager.class).getHost(getMac())
77                 .orElseThrow(() -> new FreeboxException("Host data not found"));
78
79         updateConnectivityChannels(data.host());
80         logger.debug("Switching to push mode - refreshInterval will now be ignored for Connectivity data");
81         getManager(WebSocketManager.class).registerListener(data.host().getMac(), this);
82         pushSubscribed = true;
83     }
84
85     public void updateConnectivityChannels(LanHost host) {
86         updateChannelOnOff(CONNECTIVITY, REACHABLE, host.reachable());
87         updateChannelDateTimeState(CONNECTIVITY, LAST_SEEN, host.getLastSeen());
88         updateChannelString(CONNECTIVITY, IP_ADDRESS, host.getIpv4());
89         updateStatus(host.reachable() ? ThingStatus.ONLINE : ThingStatus.OFFLINE);
90     }
91
92     public void wol() {
93         try {
94             getManager(LanBrowserManager.class).wakeOnLan(getMac(),
95                     getConfigAs(ApiConsumerConfiguration.class).password);
96         } catch (FreeboxException e) {
97             logger.warn("Error waking up host: {}", e.getMessage());
98         }
99     }
100
101     @Override
102     public Collection<Class<? extends ThingHandlerService>> getServices() {
103         return Collections.singletonList(HostActions.class);
104     }
105 }