]> git.basschouten.com Git - openhab-addons.git/blob
0ae755337eea6a431221be98bb6c9a10c28714d7
[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         super.initializeProperties(properties);
57
58         Repeater repeater = getManager(RepeaterManager.class).getDevice(getClientId());
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     }
63
64     @Override
65     protected void internalPoll() throws FreeboxException {
66         super.internalPoll();
67
68         if (!thing.getStatus().equals(ThingStatus.ONLINE)) {
69             return;
70         }
71
72         logger.debug("Polling Repeater status");
73         RepeaterManager repeaterManager = getManager(RepeaterManager.class);
74
75         Repeater repeater = repeaterManager.getDevice(getClientId());
76         updateChannelOnOff(REPEATER_MISC, LED, repeater.ledActivated());
77         updateChannelString(REPEATER_MISC, CONNECTION_STATUS, repeater.connection());
78
79         List<LanHost> hosts = repeaterManager.getRepeaterHosts(getClientId());
80         updateChannelDecimal(REPEATER_MISC, HOST_COUNT, hosts.size());
81
82         uptime = checkUptimeAndFirmware(repeater.getUptimeVal(), uptime, repeater.firmwareVersion());
83         updateChannelQuantity(REPEATER_MISC, UPTIME, uptime, Units.SECOND);
84     }
85
86     @Override
87     protected boolean internalHandleCommand(String channelId, Command command) throws FreeboxException {
88         if (ON_OFF_CLASSES.contains(command.getClass()) && LED.equals(channelId)) {
89             getManager(RepeaterManager.class).led(getClientId(), TRUE_COMMANDS.contains(command))
90                     .ifPresent(repeater -> updateChannelOnOff(REPEATER_MISC, LED, repeater.ledActivated()));
91         }
92         return super.internalHandleCommand(channelId, command);
93     }
94
95     public void reboot() {
96         processReboot(() -> {
97             try {
98                 getManager(RepeaterManager.class).reboot(getClientId());
99             } catch (FreeboxException e) {
100                 logger.warn("Error rebooting: {}", e.getMessage());
101             }
102         });
103     }
104
105     @Override
106     public Collection<Class<? extends ThingHandlerService>> getServices() {
107         return Set.of(RepeaterActions.class);
108     }
109
110     @Override
111     public ChannelUID getEventChannelUID() {
112         return eventChannelUID;
113     }
114
115     @Override
116     public void triggerChannel(ChannelUID channelUID, String event) {
117         super.triggerChannel(channelUID, event);
118     }
119 }