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.time.ZonedDateTime;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.List;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.openhab.binding.freeboxos.internal.action.FreeplugActions;
26 import org.openhab.binding.freeboxos.internal.api.FreeboxException;
27 import org.openhab.binding.freeboxos.internal.api.rest.FreeplugManager;
28 import org.openhab.core.library.types.QuantityType;
29 import org.openhab.core.library.unit.Units;
30 import org.openhab.core.thing.Channel;
31 import org.openhab.core.thing.Thing;
32 import org.openhab.core.thing.binding.ThingHandlerService;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * The {@link FreeplugHandler} is responsible for handling everything associated to a
38 * powerline gateway managed by the freebox server
40 * @author Gaƫl L'hopital - Initial contribution
43 public class FreeplugHandler extends ApiConsumerHandler {
44 private final Logger logger = LoggerFactory.getLogger(FreeplugHandler.class);
46 public FreeplugHandler(Thing thing) {
51 void initializeProperties(Map<String, String> properties) throws FreeboxException {
52 getManager(FreeplugManager.class).getPlug(getMac()).ifPresent(plug -> {
53 properties.put(Thing.PROPERTY_MODEL_ID, plug.model());
54 properties.put(ROLE, plug.netRole().name());
55 properties.put(NET_ID, plug.netId());
56 properties.put(ETHERNET_SPEED, "%d Mb/s".formatted(plug.ethSpeed()));
57 properties.put(LOCAL, Boolean.toString(plug.local()));
58 properties.put(FULL_DUPLEX, Boolean.toString(plug.ethFullDuplex()));
60 if (plug.local()) { // Plug connected to the freebox does not provide rate up or down
61 List<Channel> channels = new ArrayList<>(getThing().getChannels());
62 channels.removeIf(channel -> channel.getUID().getId().contains(RATE));
63 updateThing(editThing().withChannels(channels).build());
69 protected void internalPoll() throws FreeboxException {
70 getManager(FreeplugManager.class).getPlug(getMac()).ifPresent(plug -> {
71 updateChannelDateTimeState(LAST_SEEN, ZonedDateTime.now().minusSeconds(plug.inactive()));
73 updateChannelString(LINE_STATUS, plug.ethPortStatus());
74 updateChannelOnOff(REACHABLE, plug.hasNetwork());
76 updateRateChannel(RATE + "-down", plug.rxRate());
77 updateRateChannel(RATE + "-up", plug.txRate());
81 private void updateRateChannel(String channel, int rate) {
82 // According to https://dev.freebox.fr/bugs/task/35895
83 updateChannelQuantity(channel, new QuantityType<>(rate > 0 ? rate : 9, Units.MEGABIT_PER_SECOND));
88 getManager(FreeplugManager.class).reboot(getMac());
89 logger.debug("Freeplug {} succesfully restarted", getMac());
90 } catch (FreeboxException e) {
91 logger.warn("Error restarting freeplug {}: {}", getMac(), e.getMessage());
96 public Collection<Class<? extends ThingHandlerService>> getServices() {
97 return Set.of(FreeplugActions.class);