2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.freeboxos.internal.handler;
15 import static org.openhab.binding.freeboxos.internal.FreeboxOsBindingConstants.*;
17 import java.util.Optional;
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;
34 * The {@link WifiStationHandler} is responsible for handling everything associated to
35 * any Freebox thing types except the bridge thing type.
37 * @author Gaƫl L'hopital - Initial contribution
40 public class WifiStationHandler extends HostHandler {
41 private static final String SERVER_HOST = "Server";
43 public WifiStationHandler(Thing thing) {
48 protected void internalPoll() throws FreeboxException {
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());
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();
67 updateChannelString(GROUP_WIFI, WIFI_HOST, "%s-%s".formatted(lanAp.type(), lanAp.uid()));
68 updateWifiStationChannels(lanAp.getSignal(), lanAp.getSsid(), lanAp.rxRate(), lanAp.txRate());
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));
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);
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);
90 private int toQoS(int rssi) {
91 return rssi > -50 ? 4 : rssi > -60 ? 3 : rssi > -70 ? 2 : rssi > -85 ? 1 : 0;