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
29 * @author Gaël L'hopital - Initial contribution
32 public class FreeplugManager extends RestManager {
33 private static final String RESET_ACTION = "reset";
35 private static class Networks extends Response<Network> {
38 public static enum NetRole {
39 STA, // Freeplug station
40 PCO, // Freeplug proxy coordinator
41 CCO, // Central Coordinator
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
63 private static record Network(MACAddress id, List<Freeplug> members) {
66 public FreeplugManager(FreeboxOsSession session) throws FreeboxException {
67 super(session, LoginManager.Permission.NONE, session.getUriBuilder().path(THING_FREEPLUG));
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();
75 public Optional<Freeplug> getPlug(MACAddress mac) throws FreeboxException {
76 return getPlugs().stream().filter(plug -> plug.id.equals(mac)).findFirst();
79 public void reboot(MACAddress mac) throws FreeboxException {
80 post(mac.toColonDelimitedString(), RESET_ACTION);