2 * Copyright (c) 2010-2023 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.Collections;
21 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.binding.freeboxos.internal.api.rest.FreeplugManager.NetRole;
29 import org.openhab.core.library.types.QuantityType;
30 import org.openhab.core.library.unit.Units;
31 import org.openhab.core.thing.Channel;
32 import org.openhab.core.thing.Thing;
33 import org.openhab.core.thing.binding.ThingHandlerService;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * The {@link FreeplugHandler} is responsible for handling everything associated to a CPL gateway managed by the freebox
41 * @author Gaƫl L'hopital - Initial contribution
44 public class FreeplugHandler extends ApiConsumerHandler {
45 private final Logger logger = LoggerFactory.getLogger(FreeplugHandler.class);
47 public FreeplugHandler(Thing thing) {
52 void initializeProperties(Map<String, String> properties) throws FreeboxException {
53 getManager(FreeplugManager.class).getPlug(getMac()).ifPresent(plug -> {
54 NetRole role = plug.netRole();
55 properties.put(Thing.PROPERTY_MODEL_ID, plug.model());
56 properties.put(ROLE, role.name());
57 properties.put(NET_ID, plug.netId());
58 properties.put(ETHERNET_SPEED, String.format("%d Mb/s", plug.ethSpeed()));
59 properties.put(LOCAL, Boolean.valueOf(plug.local()).toString());
60 properties.put(FULL_DUPLEX, Boolean.valueOf(plug.ethFullDuplex()).toString());
62 if (role.equals(NetRole.CCO)) { // Coordinator does not provide rate up or down
63 List<Channel> channels = new ArrayList<>(getThing().getChannels());
64 channels.removeIf(channel -> channel.getUID().getId().contains("rate"));
65 updateThing(editThing().withChannels(channels).build());
71 protected void internalPoll() throws FreeboxException {
72 getManager(FreeplugManager.class).getPlug(getMac()).ifPresent(plug -> {
73 ZonedDateTime lastSeen = ZonedDateTime.now().minusSeconds(plug.inactive());
74 updateChannelDateTimeState(LAST_SEEN, lastSeen);
76 updateChannelString(LINE_STATUS, plug.ethPortStatus());
77 updateChannelOnOff(REACHABLE, plug.hasNetwork());
79 updateRateChannel(RATE + "-down", plug.rxRate());
80 updateRateChannel(RATE + "-up", plug.txRate());
84 private void updateRateChannel(String channel, int rate) {
85 QuantityType<?> qtty = rate != -1 ? new QuantityType<>(rate, Units.MEGABIT_PER_SECOND) : null;
86 updateChannelQuantity(channel, qtty);
91 getManager(FreeplugManager.class).reboot(getMac());
92 logger.debug("Freeplug {} succesfully restarted", getMac());
93 } catch (FreeboxException e) {
94 logger.warn("Error restarting freeplug: {}", e.getMessage());
99 public Collection<Class<? extends ThingHandlerService>> getServices() {
100 return Collections.singleton(FreeplugActions.class);