]> git.basschouten.com Git - openhab-addons.git/blob
add9ac45029c68c9fddbab6840e2b79e80fb7e89
[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.*;
16
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;
22
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;
28
29 import inet.ipaddr.mac.MACAddress;
30
31 /**
32  * The {@link RepeaterManager} is the Java class used to handle api requests related to repeater
33  *
34  * @author Gaël L'hopital - Initial contribution
35  */
36 @NonNullByDefault
37 public class RepeaterManager extends ListableRest<RepeaterManager.Repeater, RepeaterManager.RepeaterResponse> {
38
39     protected static class RepeaterResponse extends Response<Repeater> {
40     }
41
42     protected static class RepeaterLedResponse extends Response<RepeaterLed> {
43     }
44
45     public static record RepeaterLed(int id, boolean ledActivated) {
46     }
47
48     private enum Connection {
49         CONNECTED,
50         DISCONNECTED,
51         UNKNOWN
52     }
53
54     private enum Status {
55         STARTING,
56         RUNNING,
57         REBOOTING,
58         UPDATING,
59         REBOOT_FAILURE,
60         UPDATE_FAILURE,
61         UNKNOWN
62     }
63
64     public enum Model {
65         FBXWMR, // Répéteur Wifi
66         UNKNOWN
67     }
68
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) {
72
73         public long getUptimeVal() {
74             return Duration.between(bootTime, ZonedDateTime.now()).toSeconds();
75         }
76     }
77
78     public RepeaterManager(FreeboxOsSession session) throws FreeboxException {
79         super(session, LoginManager.Permission.NONE, RepeaterResponse.class,
80                 session.getUriBuilder().path(THING_REPEATER));
81     }
82
83     public List<LanHost> getRepeaterHosts(int id) throws FreeboxException {
84         return get(HostsResponse.class, Integer.toString(id), THING_HOST);
85     }
86
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));
92             }
93         }
94         return hosts;
95     }
96
97     public Optional<LanHost> getHost(MACAddress mac) throws FreeboxException {
98         return getHosts().stream().filter(host -> host.getMac().equals(mac)).findFirst();
99     }
100
101     public void reboot(int id) throws FreeboxException {
102         post(Integer.toString(id), REBOOT_ACTION);
103     }
104
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);
108     }
109 }