]> git.basschouten.com Git - openhab-addons.git/blob
eaa62f9929005498b692c83144ad495e165729a2
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.handler;
14
15 import static org.openhab.binding.freeboxos.internal.FreeboxOsBindingConstants.*;
16
17 import java.util.Collection;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.Set;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.openhab.binding.freeboxos.internal.action.RepeaterActions;
24 import org.openhab.binding.freeboxos.internal.api.FreeboxException;
25 import org.openhab.binding.freeboxos.internal.api.rest.LanBrowserManager.LanHost;
26 import org.openhab.binding.freeboxos.internal.api.rest.RepeaterManager;
27 import org.openhab.binding.freeboxos.internal.api.rest.RepeaterManager.Repeater;
28 import org.openhab.core.library.unit.Units;
29 import org.openhab.core.thing.ChannelUID;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.thing.ThingStatus;
32 import org.openhab.core.thing.binding.ThingHandlerService;
33 import org.openhab.core.types.Command;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * The {@link RepeaterHandler} is responsible for interface to a freebox
39  * pop repeater.
40  *
41  * @author GaĆ«l L'hopital - Initial contribution
42  */
43 @NonNullByDefault
44 public class RepeaterHandler extends HostHandler implements FreeDeviceIntf {
45     private final Logger logger = LoggerFactory.getLogger(RepeaterHandler.class);
46     private long uptime = -1;
47     private final ChannelUID eventChannelUID;
48
49     public RepeaterHandler(Thing thing) {
50         super(thing);
51         eventChannelUID = new ChannelUID(getThing().getUID(), REPEATER_MISC, BOX_EVENT);
52     }
53
54     @Override
55     void initializeProperties(Map<String, String> properties) throws FreeboxException {
56         // We need to get and set the MAC address before calling super.initializeProperties
57         Repeater repeater = getManager(RepeaterManager.class).getDevice(getClientId());
58         properties.put(Thing.PROPERTY_MAC_ADDRESS, repeater.mainMac().toColonDelimitedString());
59         properties.put(Thing.PROPERTY_SERIAL_NUMBER, repeater.sn());
60         properties.put(Thing.PROPERTY_FIRMWARE_VERSION, repeater.firmwareVersion());
61         properties.put(Thing.PROPERTY_MODEL_ID, repeater.model().name());
62         updateProperties(properties);
63         super.initializeProperties(properties);
64     }
65
66     @Override
67     protected void internalPoll() throws FreeboxException {
68         super.internalPoll();
69         poll();
70     }
71
72     @Override
73     protected void internalForcePoll() throws FreeboxException {
74         super.internalForcePoll();
75         poll();
76     }
77
78     private void poll() throws FreeboxException {
79         if (!thing.getStatus().equals(ThingStatus.ONLINE)) {
80             return;
81         }
82
83         logger.debug("Polling Repeater status");
84         RepeaterManager repeaterManager = getManager(RepeaterManager.class);
85
86         Repeater repeater = repeaterManager.getDevice(getClientId());
87         updateChannelOnOff(REPEATER_MISC, LED, repeater.ledActivated());
88         updateChannelString(REPEATER_MISC, CONNECTION_STATUS, repeater.connection());
89
90         List<LanHost> hosts = repeaterManager.getRepeaterHosts(getClientId());
91         updateChannelDecimal(REPEATER_MISC, HOST_COUNT, hosts.size());
92
93         uptime = checkUptimeAndFirmware(repeater.getUptimeVal(), uptime, repeater.firmwareVersion());
94         updateChannelQuantity(REPEATER_MISC, UPTIME, uptime, Units.SECOND);
95     }
96
97     @Override
98     protected boolean internalHandleCommand(String channelId, Command command) throws FreeboxException {
99         if (ON_OFF_CLASSES.contains(command.getClass()) && LED.equals(channelId)) {
100             getManager(RepeaterManager.class).led(getClientId(), TRUE_COMMANDS.contains(command))
101                     .ifPresent(repeater -> updateChannelOnOff(REPEATER_MISC, LED, repeater.ledActivated()));
102         }
103         return super.internalHandleCommand(channelId, command);
104     }
105
106     public void reboot() {
107         processReboot(() -> {
108             try {
109                 getManager(RepeaterManager.class).reboot(getClientId());
110             } catch (FreeboxException e) {
111                 logger.warn("Error rebooting: {}", e.getMessage());
112             }
113         });
114     }
115
116     @Override
117     public Collection<Class<? extends ThingHandlerService>> getServices() {
118         return Set.of(RepeaterActions.class);
119     }
120
121     @Override
122     public ChannelUID getEventChannelUID() {
123         return eventChannelUID;
124     }
125
126     @Override
127     public void triggerChannel(ChannelUID channelUID, String event) {
128         super.triggerChannel(channelUID, event);
129     }
130 }