]> git.basschouten.com Git - openhab-addons.git/blob
9849ae5f42f759a9aa3e0d72c0c71f65da41600d
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.Collections;
21 import java.util.List;
22 import java.util.Map;
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.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;
36
37 /**
38  * The {@link FreeplugHandler} is responsible for handling everything associated to a CPL gateway managed by the freebox
39  * server
40  *
41  * @author GaĆ«l L'hopital - Initial contribution
42  */
43 @NonNullByDefault
44 public class FreeplugHandler extends ApiConsumerHandler {
45     private final Logger logger = LoggerFactory.getLogger(FreeplugHandler.class);
46
47     public FreeplugHandler(Thing thing) {
48         super(thing);
49     }
50
51     @Override
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());
61
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());
66             }
67         });
68     }
69
70     @Override
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);
75
76             updateChannelString(LINE_STATUS, plug.ethPortStatus());
77             updateChannelOnOff(REACHABLE, plug.hasNetwork());
78
79             updateRateChannel(RATE + "-down", plug.rxRate());
80             updateRateChannel(RATE + "-up", plug.txRate());
81         });
82     }
83
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);
87     }
88
89     public void reset() {
90         try {
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());
95         }
96     }
97
98     @Override
99     public Collection<Class<? extends ThingHandlerService>> getServices() {
100         return Collections.singleton(FreeplugActions.class);
101     }
102 }