]> git.basschouten.com Git - openhab-addons.git/blob
c22c9ad33336c996683a54be088f6ac6bec7968a
[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.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.LanHost;
26 import org.openhab.binding.freeboxos.internal.api.rest.RepeaterManager;
27 import org.openhab.core.library.types.QuantityType;
28 import org.openhab.core.library.unit.Units;
29 import org.openhab.core.thing.Channel;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.types.UnDefType;
32
33 /**
34  * The {@link WifiStationHandler} is responsible for handling everything associated to
35  * any Freebox thing types except the bridge thing type.
36  *
37  * @author GaĆ«l L'hopital - Initial contribution
38  */
39 @NonNullByDefault
40 public class WifiStationHandler extends HostHandler {
41     private static final String SERVER_HOST = "Server";
42
43     public WifiStationHandler(Thing thing) {
44         super(thing);
45     }
46
47     @Override
48     protected void internalPoll() throws FreeboxException {
49         super.internalPoll();
50
51         // Search if the wifi-host is hosted on server access-points
52         Optional<Station> station = getManager(APManager.class).getStation(getMac());
53         if (station.isPresent()) {
54             Station data = station.get();
55             updateChannelDateTimeState(CONNECTIVITY, LAST_SEEN, data.getLastSeen());
56             updateChannelString(GROUP_WIFI, WIFI_HOST, SERVER_HOST);
57             updateWifiStationChannels(data.signal(), data.getSsid(), data.rxRate(), data.txRate());
58             return;
59         }
60
61         // Search if it is hosted by a repeater
62         Optional<LanHost> wifiHost = getManager(RepeaterManager.class).getHost(getMac());
63         if (wifiHost.isPresent()) {
64             updateChannelDateTimeState(CONNECTIVITY, LAST_SEEN, wifiHost.get().getLastSeen());
65             LanAccessPoint lanAp = wifiHost.get().accessPoint();
66             if (lanAp != null) {
67                 updateChannelString(GROUP_WIFI, WIFI_HOST, "%s-%s".formatted(lanAp.type(), lanAp.uid()));
68                 updateWifiStationChannels(lanAp.getSignal(), lanAp.getSsid(), lanAp.rxRate(), lanAp.txRate());
69                 return;
70             }
71         }
72         // Not found a wifi repeater/host, so update all wifi channels to NULL
73         getThing().getChannelsOfGroup(GROUP_WIFI).stream().map(Channel::getUID).filter(uid -> isLinked(uid))
74                 .forEach(uid -> updateState(uid, UnDefType.NULL));
75     }
76
77     private void updateWifiStationChannels(int rssi, @Nullable String ssid, long rxRate, long txRate) {
78         updateChannelString(GROUP_WIFI, SSID, ssid);
79         updateChannelQuantity(GROUP_WIFI, RSSI, rssi <= 0 ? new QuantityType<>(rssi, Units.DECIBEL_MILLIWATTS) : null);
80         updateChannelDecimal(GROUP_WIFI, WIFI_QUALITY, rssi <= 0 ? toQoS(rssi) : null);
81         updateRateChannel(RATE + "-down", rxRate);
82         updateRateChannel(RATE + "-up", txRate);
83     }
84
85     private void updateRateChannel(String channel, long rate) {
86         QuantityType<?> qtty = rate != -1 ? new QuantityType<>(rate * 8, Units.BIT_PER_SECOND) : null;
87         updateChannelQuantity(GROUP_WIFI, channel, qtty);
88     }
89
90     private int toQoS(int rssi) {
91         return rssi > -50 ? 4 : rssi > -60 ? 3 : rssi > -70 ? 2 : rssi > -85 ? 1 : 0;
92     }
93 }