]> git.basschouten.com Git - openhab-addons.git/blob
70d6eb6ae06e9a154f5528e8d0402c084d794880
[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.asuswrt.internal.things;
14
15 import static org.openhab.binding.asuswrt.internal.constants.AsuswrtBindingConstants.*;
16 import static org.openhab.binding.asuswrt.internal.helpers.AsuswrtUtils.*;
17
18 import java.util.HashMap;
19 import java.util.Map;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.openhab.binding.asuswrt.internal.helpers.AsuswrtUtils;
23 import org.openhab.binding.asuswrt.internal.structures.AsuswrtClientInfo;
24 import org.openhab.binding.asuswrt.internal.structures.AsuswrtTraffic;
25 import org.openhab.core.config.core.Configuration;
26 import org.openhab.core.library.unit.Units;
27 import org.openhab.core.thing.ChannelUID;
28 import org.openhab.core.thing.Thing;
29 import org.openhab.core.thing.ThingStatus;
30 import org.openhab.core.thing.ThingTypeUID;
31 import org.openhab.core.thing.binding.BaseThingHandler;
32 import org.openhab.core.types.Command;
33 import org.openhab.core.types.RefreshType;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * The {@link AsuswrtClient} is used as {@link org.openhab.core.thing.binding.ThingHandler ThingHandler} for router
39  * clients.
40  *
41  * @author Christian Wild - Initial contribution
42  */
43 @NonNullByDefault
44 public class AsuswrtClient extends BaseThingHandler {
45     private final Logger logger = LoggerFactory.getLogger(AsuswrtClient.class);
46     private final AsuswrtRouter router;
47     private Map<String, Object> oldStates = new HashMap<>();
48     protected final String uid;
49
50     public AsuswrtClient(Thing thing, AsuswrtRouter router) {
51         super(thing);
52         this.router = router;
53         this.uid = getThing().getUID().getAsString();
54     }
55
56     @Override
57     public void initialize() {
58         logger.trace("({}) Initializing thing ", uid);
59         router.queryDeviceData(false);
60         refreshData();
61         updateStatus(ThingStatus.ONLINE);
62     }
63
64     /*
65      * Commands and events
66      */
67
68     @Override
69     public void handleCommand(ChannelUID channelUID, Command command) {
70         if (command instanceof RefreshType) {
71             refreshData();
72         }
73     }
74
75     public void updateClientProperties(AsuswrtClientInfo clientInfo) {
76         logger.trace("({}) clientPropertiesChanged ", uid);
77         Map<String, String> properties = editProperties();
78         properties.put(Thing.PROPERTY_MAC_ADDRESS, clientInfo.getMac());
79         properties.put(Thing.PROPERTY_VENDOR, clientInfo.getVendor());
80         properties.put(PROPERTY_CLIENT_NAME, clientInfo.getName());
81         updateProperties(properties);
82     }
83
84     public void updateClientChannels(AsuswrtClientInfo clientInfo) {
85         updateState(getChannelID(CHANNEL_GROUP_NETWORK, CHANNEL_NETWORK_STATE), getOnOffType(clientInfo.isOnline()));
86         updateState(getChannelID(CHANNEL_GROUP_NETWORK, CHANNEL_NETWORK_INTERNET),
87                 getOnOffType(clientInfo.getInternetState()));
88         updateState(getChannelID(CHANNEL_GROUP_NETWORK, CHANNEL_NETWORK_IP), getStringType(clientInfo.getIP()));
89         updateState(getChannelID(CHANNEL_GROUP_NETWORK, CHANNEL_NETWORK_METHOD),
90                 getStringType(clientInfo.getIpMethod()));
91     }
92
93     private void updateTrafficChannels(AsuswrtTraffic traffic) {
94         updateState(getChannelID(CHANNEL_GROUP_TRAFFIC, CHANNEL_TRAFFIC_CURRENT_RX),
95                 getQuantityType(traffic.getCurrentRX(), Units.MEGABIT_PER_SECOND));
96         updateState(getChannelID(CHANNEL_GROUP_TRAFFIC, CHANNEL_TRAFFIC_CURRENT_TX),
97                 getQuantityType(traffic.getCurrentTX(), Units.MEGABIT_PER_SECOND));
98         updateState(getChannelID(CHANNEL_GROUP_TRAFFIC, CHANNEL_TRAFFIC_TODAY_RX),
99                 getQuantityType(traffic.getTodayRX(), Units.MEGABYTE));
100         updateState(getChannelID(CHANNEL_GROUP_TRAFFIC, CHANNEL_TRAFFIC_TODAY_TX),
101                 getQuantityType(traffic.getTodayTX(), Units.MEGABYTE));
102         updateState(getChannelID(CHANNEL_GROUP_TRAFFIC, CHANNEL_TRAFFIC_TOTAL_RX),
103                 getQuantityType(traffic.getTotalRX(), Units.MEGABYTE));
104         updateState(getChannelID(CHANNEL_GROUP_TRAFFIC, CHANNEL_TRAFFIC_TOTAL_TX),
105                 getQuantityType(traffic.getTotalTX(), Units.MEGABYTE));
106     }
107
108     /**
109      * Fires events on {@link AsuswrtClientInfo} changes.
110      */
111     private void fireEvents(AsuswrtClientInfo clientInfo) {
112         if (checkForStateChange(CHANNEL_GROUP_NETWORK, clientInfo.isOnline())) {
113             if (clientInfo.isOnline()) {
114                 triggerChannel(getChannelID(CHANNEL_GROUP_NETWORK, EVENT_CLIENT_CONNECTION), EVENT_STATE_CONNECTED);
115                 router.fireEvent(getChannelID(CHANNEL_GROUP_CLIENTS, EVENT_CLIENT_CONNECTION), EVENT_STATE_CONNECTED);
116             } else {
117                 triggerChannel(getChannelID(CHANNEL_GROUP_NETWORK, EVENT_CLIENT_CONNECTION), EVENT_STATE_GONE);
118                 router.fireEvent(getChannelID(CHANNEL_GROUP_CLIENTS, EVENT_CLIENT_CONNECTION), EVENT_STATE_GONE);
119             }
120         }
121     }
122
123     private void refreshData() {
124         String mac = getMac();
125         AsuswrtClientInfo clientInfo = router.getClients().getClientByMAC(mac);
126         fireEvents(clientInfo);
127         updateClientProperties(clientInfo);
128         updateClientChannels(clientInfo);
129         updateTrafficChannels(clientInfo.getTraffic());
130     }
131
132     /*
133      * Functions
134      */
135
136     /**
137      * Gets the MAC address of a client from properties or settings.
138      */
139     public String getMac() {
140         String mac = "";
141         Map<String, String> properties = getThing().getProperties();
142         Configuration config = getThing().getConfiguration();
143
144         /* get mac from properties */
145         if (properties.containsKey(Thing.PROPERTY_MAC_ADDRESS)) {
146             mac = config.get(Thing.PROPERTY_MAC_ADDRESS).toString();
147         }
148
149         /* get mac from config */
150         if (mac.isBlank() && config.containsKey(Thing.PROPERTY_MAC_ADDRESS)) {
151             mac = config.get(Thing.PROPERTY_MAC_ADDRESS).toString();
152         }
153
154         if (mac.isBlank()) {
155             logger.debug("({}) cant find macAddress in properties and config", uid);
156         }
157         return AsuswrtUtils.formatMac(mac, ':');
158     }
159
160     /**
161      * Gets the channel ID including the group.
162      */
163     protected String getChannelID(String group, String channel) {
164         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
165         if (CHANNEL_GROUP_THING_SET.contains(thingTypeUID) && group.length() > 0) {
166             return group + "#" + channel;
167         }
168         return channel;
169     }
170
171     /**
172      * Gets a channel name from a channel ID.
173      */
174     protected String getChannelFromID(ChannelUID channelID) {
175         String channel = channelID.getIdWithoutGroup();
176         channel = channel.replace(CHANNEL_GROUP_CLIENT + "#", "");
177         return channel;
178     }
179
180     /**
181      * Checks if the state changed since the last channel update.
182      *
183      * @param stateName the name of the state (channel)
184      * @param comparator comparison value
185      * @return <code>true</code> if changed, <code>false</code> if not or no old value exists
186      */
187     private Boolean checkForStateChange(String stateName, Object comparator) {
188         if (oldStates.get(stateName) == null) {
189             oldStates.put(stateName, comparator);
190         } else if (!comparator.equals(oldStates.get(stateName))) {
191             oldStates.put(stateName, comparator);
192             return true;
193         }
194         return false;
195     }
196 }