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