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.*;
17 import java.time.Duration;
18 import java.time.ZonedDateTime;
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.Optional;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
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.HostsResponse;
27 import org.openhab.binding.freeboxos.internal.api.rest.LanBrowserManager.LanHost;
29 import inet.ipaddr.mac.MACAddress;
32 * The {@link RepeaterManager} is the Java class used to handle api requests related to repeater
34 * @author Gaël L'hopital - Initial contribution
37 public class RepeaterManager extends ListableRest<RepeaterManager.Repeater, RepeaterManager.RepeaterResponse> {
39 protected static class RepeaterResponse extends Response<Repeater> {
42 protected static class RepeaterLedResponse extends Response<RepeaterLed> {
45 public static record RepeaterLed(int id, boolean ledActivated) {
48 private enum Connection {
65 FBXWMR, // Répéteur Wifi
69 public static record Repeater(int id, boolean ledActivated, boolean enabled, MACAddress mainMac,
70 Connection connection, ZonedDateTime bootTime, Status status, String name, String sn, String apiVer,
71 ZonedDateTime lastSeen, Model model, String firmwareVersion) {
73 public long getUptimeVal() {
74 return Duration.between(bootTime, ZonedDateTime.now()).toSeconds();
78 public RepeaterManager(FreeboxOsSession session) throws FreeboxException {
79 super(session, LoginManager.Permission.NONE, RepeaterResponse.class,
80 session.getUriBuilder().path(THING_REPEATER));
83 public List<LanHost> getRepeaterHosts(int id) throws FreeboxException {
84 return get(HostsResponse.class, Integer.toString(id), THING_HOST);
87 public synchronized List<LanHost> getHosts() throws FreeboxException {
88 List<LanHost> hosts = new ArrayList<>();
89 for (Repeater rep : getDevices()) {
90 if (Connection.CONNECTED.equals(rep.connection)) {
91 hosts.addAll(getRepeaterHosts(rep.id));
97 public Optional<LanHost> getHost(MACAddress mac) throws FreeboxException {
98 return getHosts().stream().filter(host -> host.getMac().equals(mac)).findFirst();
101 public void reboot(int id) throws FreeboxException {
102 post(Integer.toString(id), REBOOT_ACTION);
105 public Optional<RepeaterLed> led(int id, boolean enable) throws FreeboxException {
106 RepeaterLed result = put(RepeaterLedResponse.class, new RepeaterLed(id, enable), Integer.toString(id));
107 return Optional.ofNullable(result);