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.api.rest;
15 import java.time.ZonedDateTime;
16 import java.util.ArrayList;
17 import java.util.List;
18 import java.util.Optional;
20 import javax.ws.rs.core.UriBuilder;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.freeboxos.internal.api.FreeboxException;
25 import org.openhab.binding.freeboxos.internal.api.Response;
26 import org.openhab.binding.freeboxos.internal.api.rest.LanBrowserManager.LanHost;
28 import inet.ipaddr.mac.MACAddress;
31 * The {@link APManager} is the Java class used to handle api requests related to wifi access points
32 * provided by the Freebox Server
34 * @author Gaƫl L'hopital - Initial contribution
37 public class APManager extends ListableRest<APManager.WifiAp, APManager.APResponse> {
38 private static final String PATH = "ap";
39 private static final String STATIONS_PATH = "stations";
41 protected static record WifiInformation(String ssid, String band, int signal) { // Valid RSSI goes from -120 to 0
44 public static record LanAccessPoint(String mac, String type, String uid, @Nullable String connectivityType,
45 long rxBytes, // received bytes (from station to Freebox)
46 long txBytes, // transmitted bytes (from Freebox to station)
47 long txRate, // reception data rate (in bytes/s)
48 long rxRate, // transmission data rate (in bytes/s)
49 WifiInformation wifiInformation) {
51 public int getSignal() {
52 return wifiInformation.signal();
55 public @Nullable String getSsid() {
56 return wifiInformation().ssid();
60 private static enum State {
66 public static record Station(String id, MACAddress mac, String bssid, @Nullable String hostname, LanHost host,
67 State state, int inactive, int connDuration, //
68 long rxBytes, // received bytes (from station to Freebox)
69 long txBytes, // transmitted bytes (from Freebox to station)
70 long txRate, // reception data rate (in bytes/s)
71 long rxRate, // transmission data rate (in bytes/s)
72 int signal) { // signal attenuation (in dB)
74 public @Nullable String getSsid() {
75 LanAccessPoint accessPoint = host.accessPoint();
76 return accessPoint != null ? accessPoint.getSsid() : null;
79 public @Nullable ZonedDateTime getLastSeen() {
80 return host.getLastSeen();
84 protected static record ApStatus(ApState state, int channelWidth, int primaryChannel, int secondaryChannel,
85 int dfsCacRemainingTime, boolean dfsDisabled) {
86 private static enum ApState {
87 SCANNING, // Ap is probing wifi channels
88 NO_PARAM, // Ap is not configured
89 BAD_PARAM, // Ap has an invalid configuration
90 DISABLED, // Ap is permanently disabled
91 DISABLED_PLANNING, // Ap is currently disabled according to planning
92 NO_ACTIVE_BSS, // Ap has no active BSS
93 STARTING, // Ap is starting
94 ACS, // Ap is selecting the best available channel
95 HT_SCAN, // Ap is scanning for other access point
96 DFS, // Ap is performing dynamic frequency selection
97 ACTIVE, // Ap is active
98 FAILED, // Ap has failed to start
103 protected static record WifiAp(int id, String name, ApStatus status) {
106 private class ApHostsResponse extends Response<Station> {
109 protected class APResponse extends Response<WifiAp> {
112 public APManager(FreeboxOsSession session, UriBuilder uriBuilder) throws FreeboxException {
113 super(session, LoginManager.Permission.NONE, APResponse.class, uriBuilder.path(PATH));
116 private List<Station> getApStations(int apId) throws FreeboxException {
117 return get(ApHostsResponse.class, Integer.toString(apId), STATIONS_PATH);
120 public List<Station> getStations() throws FreeboxException {
121 List<Station> hosts = new ArrayList<>();
122 for (WifiAp ap : getDevices()) {
123 hosts.addAll(getApStations(ap.id));
128 public Optional<Station> getStation(MACAddress mac) throws FreeboxException {
129 return getStations().stream().filter(host -> host.mac().equals(mac)).findFirst();