2 * Copyright (c) 2010-2024 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.handler;
15 import static org.openhab.binding.freeboxos.internal.FreeboxOsBindingConstants.*;
17 import java.util.Collection;
18 import java.util.List;
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;
38 * The {@link RepeaterHandler} is responsible for interface to a freebox
41 * @author Gaƫl L'hopital - Initial contribution
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;
49 public RepeaterHandler(Thing thing) {
51 eventChannelUID = new ChannelUID(getThing().getUID(), REPEATER_MISC, BOX_EVENT);
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);
67 protected void internalPoll() throws FreeboxException {
70 if (!thing.getStatus().equals(ThingStatus.ONLINE)) {
74 logger.debug("Polling Repeater status");
75 RepeaterManager repeaterManager = getManager(RepeaterManager.class);
77 Repeater repeater = repeaterManager.getDevice(getClientId());
78 updateChannelOnOff(REPEATER_MISC, LED, repeater.ledActivated());
79 updateChannelString(REPEATER_MISC, CONNECTION_STATUS, repeater.connection());
81 List<LanHost> hosts = repeaterManager.getRepeaterHosts(getClientId());
82 updateChannelDecimal(REPEATER_MISC, HOST_COUNT, hosts.size());
84 uptime = checkUptimeAndFirmware(repeater.getUptimeVal(), uptime, repeater.firmwareVersion());
85 updateChannelQuantity(REPEATER_MISC, UPTIME, uptime, Units.SECOND);
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()));
94 return super.internalHandleCommand(channelId, command);
97 public void reboot() {
100 getManager(RepeaterManager.class).reboot(getClientId());
101 } catch (FreeboxException e) {
102 logger.warn("Error rebooting: {}", e.getMessage());
108 public Collection<Class<? extends ThingHandlerService>> getServices() {
109 return Set.of(RepeaterActions.class);
113 public ChannelUID getEventChannelUID() {
114 return eventChannelUID;
118 public void triggerChannel(ChannelUID channelUID, String event) {
119 super.triggerChannel(channelUID, event);