]> git.basschouten.com Git - openhab-addons.git/blob
7755666ada466414e566b169cbe63ec0500fc5fe
[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.time.ZonedDateTime;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Set;
23
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;
35
36 /**
37  * The {@link FreeplugHandler} is responsible for handling everything associated to a
38  * powerline gateway managed by the freebox server
39  *
40  * @author GaĆ«l L'hopital - Initial contribution
41  */
42 @NonNullByDefault
43 public class FreeplugHandler extends ApiConsumerHandler {
44     private final Logger logger = LoggerFactory.getLogger(FreeplugHandler.class);
45
46     public FreeplugHandler(Thing thing) {
47         super(thing);
48     }
49
50     @Override
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()));
59
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());
64             }
65         });
66     }
67
68     @Override
69     protected void internalPoll() throws FreeboxException {
70         getManager(FreeplugManager.class).getPlug(getMac()).ifPresent(plug -> {
71             updateChannelDateTimeState(LAST_SEEN, ZonedDateTime.now().minusSeconds(plug.inactive()));
72
73             updateChannelString(LINE_STATUS, plug.ethPortStatus());
74             updateChannelOnOff(REACHABLE, plug.hasNetwork());
75
76             updateRateChannel(RATE + "-down", plug.rxRate());
77             updateRateChannel(RATE + "-up", plug.txRate());
78         });
79     }
80
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));
84     }
85
86     public void reset() {
87         try {
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());
92         }
93     }
94
95     @Override
96     public Collection<Class<? extends ThingHandlerService>> getServices() {
97         return Set.of(FreeplugActions.class);
98     }
99 }