]> git.basschouten.com Git - openhab-addons.git/blob
233046da0a82bfdcb738cece5eef7fa1e8b99dd9
[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.api.rest;
14
15 import java.time.ZonedDateTime;
16 import java.util.ArrayList;
17 import java.util.List;
18 import java.util.Optional;
19
20 import javax.ws.rs.core.UriBuilder;
21
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;
27
28 import inet.ipaddr.mac.MACAddress;
29
30 /**
31  * The {@link APManager} is the Java class used to handle api requests related to wifi access points
32  * provided by the Freebox Server
33  *
34  * @author GaĆ«l L'hopital - Initial contribution
35  */
36 @NonNullByDefault
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";
40
41     protected static record WifiInformation(String ssid, String band, int signal) { // Valid RSSI goes from -120 to 0
42     }
43
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) {
50
51         public int getSignal() {
52             return wifiInformation.signal();
53         }
54
55         public @Nullable String getSsid() {
56             return wifiInformation().ssid();
57         }
58     }
59
60     private static enum State {
61         ASSOCIATED,
62         AUTHENTICATED,
63         UNKNOWN;
64     }
65
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)
73
74         public @Nullable String getSsid() {
75             LanAccessPoint accessPoint = host.accessPoint();
76             return accessPoint != null ? accessPoint.getSsid() : null;
77         }
78
79         public @Nullable ZonedDateTime getLastSeen() {
80             return host.getLastSeen();
81         }
82     }
83
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
99             UNKNOWN;
100         }
101     }
102
103     protected static record WifiAp(int id, String name, ApStatus status) {
104     }
105
106     private class ApHostsResponse extends Response<Station> {
107     }
108
109     protected class APResponse extends Response<WifiAp> {
110     }
111
112     public APManager(FreeboxOsSession session, UriBuilder uriBuilder) throws FreeboxException {
113         super(session, LoginManager.Permission.NONE, APResponse.class, uriBuilder.path(PATH));
114     }
115
116     private List<Station> getApStations(int apId) throws FreeboxException {
117         return get(ApHostsResponse.class, Integer.toString(apId), STATIONS_PATH);
118     }
119
120     public List<Station> getStations() throws FreeboxException {
121         List<Station> hosts = new ArrayList<>();
122         for (WifiAp ap : getDevices()) {
123             hosts.addAll(getApStations(ap.id));
124         }
125         return hosts;
126     }
127
128     public Optional<Station> getStation(MACAddress mac) throws FreeboxException {
129         return getStations().stream().filter(host -> host.mac().equals(mac)).findFirst();
130     }
131 }