]> git.basschouten.com Git - openhab-addons.git/blob
a37e5f88a7499fc557c3e46a48548a9687e5e1d5
[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.Optional;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.freeboxos.internal.api.FreeboxException;
22 import org.openhab.binding.freeboxos.internal.api.rest.APManager;
23 import org.openhab.binding.freeboxos.internal.api.rest.APManager.LanAccessPoint;
24 import org.openhab.binding.freeboxos.internal.api.rest.APManager.Station;
25 import org.openhab.binding.freeboxos.internal.api.rest.LanBrowserManager;
26 import org.openhab.binding.freeboxos.internal.api.rest.LanBrowserManager.HostName;
27 import org.openhab.binding.freeboxos.internal.api.rest.LanBrowserManager.LanHost;
28 import org.openhab.binding.freeboxos.internal.api.rest.RepeaterManager;
29 import org.openhab.binding.freeboxos.internal.config.WifiHostConfiguration;
30 import org.openhab.core.config.core.Configuration;
31 import org.openhab.core.library.types.QuantityType;
32 import org.openhab.core.library.unit.Units;
33 import org.openhab.core.thing.Channel;
34 import org.openhab.core.thing.Thing;
35 import org.openhab.core.types.UnDefType;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 import inet.ipaddr.mac.MACAddress;
40
41 /**
42  * The {@link WifiStationHandler} is responsible for handling everything associated to
43  * any Freebox thing types except the bridge thing type.
44  *
45  * @author GaĆ«l L'hopital - Initial contribution
46  */
47 @NonNullByDefault
48 public class WifiStationHandler extends HostHandler {
49     private static final String SERVER_HOST = "Server";
50
51     private final Logger logger = LoggerFactory.getLogger(WifiStationHandler.class);
52
53     public WifiStationHandler(Thing thing) {
54         super(thing);
55     }
56
57     @Override
58     protected void internalPoll() throws FreeboxException {
59         super.internalPoll();
60         poll();
61     }
62
63     @Override
64     protected void internalForcePoll() throws FreeboxException {
65         super.internalForcePoll();
66         poll();
67     }
68
69     private void poll() throws FreeboxException {
70         MACAddress mac = getMac();
71         if (mac == null) {
72             throw new FreeboxException(
73                     "internalPoll is not possible because MAC address is undefined for the thing " + thing.getUID());
74         }
75
76         // Search if the wifi-host is hosted on server access-points
77         Optional<Station> station = getManager(APManager.class).getStation(mac);
78         if (station.isPresent()) {
79             Station data = station.get();
80             updateChannelDateTimeState(CONNECTIVITY, LAST_SEEN, data.getLastSeen());
81             updateChannelString(GROUP_WIFI, WIFI_HOST, SERVER_HOST);
82             updateWifiStationChannels(data.signal(), data.getSsid(), data.rxRate(), data.txRate());
83             return;
84         }
85
86         // Search if it is hosted by a repeater
87         Optional<LanHost> wifiHost = getManager(RepeaterManager.class).getHost(mac);
88         if (wifiHost.isPresent()) {
89             updateChannelDateTimeState(CONNECTIVITY, LAST_SEEN, wifiHost.get().getLastSeen());
90             LanAccessPoint lanAp = wifiHost.get().accessPoint();
91             if (lanAp != null) {
92                 updateChannelString(GROUP_WIFI, WIFI_HOST, "%s-%s".formatted(lanAp.type(), lanAp.uid()));
93                 updateWifiStationChannels(lanAp.getRSSI(), lanAp.getSsid(), lanAp.rxRate(), lanAp.txRate());
94                 return;
95             }
96         }
97         // Not found a wifi repeater/host, so update all wifi channels to NULL
98         getThing().getChannelsOfGroup(GROUP_WIFI).stream().map(Channel::getUID).filter(uid -> isLinked(uid))
99                 .forEach(uid -> updateState(uid, UnDefType.NULL));
100     }
101
102     private void updateWifiStationChannels(int rssi, @Nullable String ssid, long rxRate, long txRate) {
103         updateChannelString(GROUP_WIFI, SSID, ssid);
104         updateChannelQuantity(GROUP_WIFI, RSSI, rssi <= 0 ? new QuantityType<>(rssi, Units.DECIBEL_MILLIWATTS) : null);
105         updateChannelDecimal(GROUP_WIFI, WIFI_QUALITY, rssi <= 0 ? toQoS(rssi) : null);
106         updateRateChannel(RATE + "-down", rxRate);
107         updateRateChannel(RATE + "-up", txRate);
108     }
109
110     private void updateRateChannel(String channel, long rate) {
111         QuantityType<?> qtty = rate != -1 ? new QuantityType<>(rate * 8, Units.BIT_PER_SECOND) : null;
112         updateChannelQuantity(GROUP_WIFI, channel, qtty);
113     }
114
115     private int toQoS(int rssi) {
116         return rssi > -50 ? 4 : rssi > -60 ? 3 : rssi > -70 ? 2 : rssi > -85 ? 1 : 0;
117     }
118
119     @Override
120     protected LanHost getLanHost() throws FreeboxException {
121         try {
122             return super.getLanHost();
123         } catch (FreeboxException e) {
124             HostName identifier = getConfigAs(WifiHostConfiguration.class).getIdentifier();
125             if (identifier != null) {
126                 cancelPushSubscription();
127                 Optional<LanHost> lanHost = getManager(LanBrowserManager.class).getHost(identifier);
128                 return lanHost.map(host -> {
129                     Configuration thingConfig = editConfiguration();
130                     thingConfig.put(Thing.PROPERTY_MAC_ADDRESS, host.getMac().toColonDelimitedString());
131                     updateConfiguration(thingConfig);
132                     logger.info("MAC address of the wifihost {} changed, configuration updated to {}", thing.getUID(),
133                             host.getMac());
134                     return host;
135                 }).orElseThrow(() -> new FreeboxException("Host data not found - mDNS failed also"));
136             }
137             throw new FreeboxException("Host not found - no mDNS alternative");
138         }
139     }
140 }