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 static org.openhab.binding.freeboxos.internal.FreeboxOsBindingConstants.THING_FREEPLUG;
17 import java.util.List;
18 import java.util.Optional;
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;
24 import inet.ipaddr.mac.MACAddress;
27 * The {@link FreeplugManager} is the Java class used to handle api requests related to freeplugs
28 * https://dev.freebox.fr/sdk/os/freeplug/
30 * @author Gaël L'hopital - Initial contribution
33 public class FreeplugManager extends RestManager {
34 private static final String RESET_ACTION = "reset";
36 private static class Networks extends Response<Network> {
40 STA, // Freeplug station
41 PCO, // Freeplug proxy coordinator
42 CCO, // Central Coordinator
47 UP, // The ethernet port is up
48 DOWN, // The ethernet port is down
49 UNKNOWN // The ethernet port state is unknown
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
64 private static record Network(MACAddress id, List<Freeplug> members) {
67 public FreeplugManager(FreeboxOsSession session) throws FreeboxException {
68 super(session, LoginManager.Permission.NONE, session.getUriBuilder().path(THING_FREEPLUG));
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();
76 public Optional<Freeplug> getPlug(MACAddress mac) throws FreeboxException {
77 return getPlugs().stream().filter(plug -> plug.id.equals(mac)).findFirst();
80 public void reboot(MACAddress mac) throws FreeboxException {
81 post(mac.toColonDelimitedString(), RESET_ACTION);