]> git.basschouten.com Git - openhab-addons.git/blob
2f7fd291ca16f1b8a071099a6037a9bf5f9de143
[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 static org.openhab.binding.freeboxos.internal.FreeboxOsBindingConstants.THING_FREEPLUG;
16
17 import java.util.List;
18 import java.util.Optional;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.binding.freeboxos.internal.api.FreeboxException;
22 import org.openhab.binding.freeboxos.internal.api.Response;
23
24 import inet.ipaddr.mac.MACAddress;
25
26 /**
27  * The {@link FreeplugManager} is the Java class used to handle api requests related to freeplugs
28  * https://dev.freebox.fr/sdk/os/freeplug/
29  *
30  * @author Gaël L'hopital - Initial contribution
31  */
32 @NonNullByDefault
33 public class FreeplugManager extends RestManager {
34     private static final String RESET_ACTION = "reset";
35
36     private static class Networks extends Response<Network> {
37     }
38
39     public enum NetRole {
40         STA, // Freeplug station
41         PCO, // Freeplug proxy coordinator
42         CCO, // Central Coordinator
43         UNKNOWN
44     }
45
46     private enum Status {
47         UP, // The ethernet port is up
48         DOWN, // The ethernet port is down
49         UNKNOWN // The ethernet port state is unknown
50     }
51
52     public static record Freeplug(MACAddress id, String netId, // Id of the network holding the plug
53             boolean local, // if true the Freeplug is connected directly to the Freebox
54             NetRole netRole, // Freeplug network role
55             String model, Status ethPortStatus, //
56             boolean ethFullDuplex, // ethernet link is full duplex
57             boolean hasNetwork, // is connected to the network
58             int ethSpeed, // ethernet port speed
59             int inactive, // seconds since last activity
60             int rxRate, // rx rate (from the freeplugs to the “cco” freeplug) (in Mb/s) -1 if not available
61             int txRate) { // tx rate (from the “cco” freeplug to the freeplugs) (in Mb/s) -1 if not available
62     }
63
64     private static record Network(MACAddress id, List<Freeplug> members) {
65     }
66
67     public FreeplugManager(FreeboxOsSession session) throws FreeboxException {
68         super(session, LoginManager.Permission.NONE, session.getUriBuilder().path(THING_FREEPLUG));
69     }
70
71     // Most of the users will host only one CPL network on their server, so we hide the network level in the manager
72     public List<Freeplug> getPlugs() throws FreeboxException {
73         return get(Networks.class).stream().map(Network::members).flatMap(List::stream).toList();
74     }
75
76     public Optional<Freeplug> getPlug(MACAddress mac) throws FreeboxException {
77         return getPlugs().stream().filter(plug -> plug.id.equals(mac)).findFirst();
78     }
79
80     public void reboot(MACAddress mac) throws FreeboxException {
81         post(mac.toColonDelimitedString(), RESET_ACTION);
82     }
83 }