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