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.asuswrt.internal.things;
15 import static org.openhab.binding.asuswrt.internal.constants.AsuswrtBindingConstants.*;
16 import static org.openhab.binding.asuswrt.internal.helpers.AsuswrtUtils.*;
18 import java.util.HashMap;
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;
38 * The {@link AsuswrtClient} is used as {@link org.openhab.core.thing.binding.ThingHandler ThingHandler} for router
41 * @author Christian Wild - Initial contribution
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;
50 public AsuswrtClient(Thing thing, AsuswrtRouter router) {
53 this.uid = getThing().getUID().getAsString();
57 public void initialize() {
58 logger.trace("({}) Initializing thing ", uid);
59 router.queryDeviceData(false);
61 updateStatus(ThingStatus.ONLINE);
69 public void handleCommand(ChannelUID channelUID, Command command) {
70 if (command instanceof RefreshType) {
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);
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()));
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));
109 * Fires events on {@link AsuswrtClientInfo} changes.
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);
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);
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());
137 * Gets the MAC address of a client from properties or settings.
139 public String getMac() {
141 Map<String, String> properties = getThing().getProperties();
142 Configuration config = getThing().getConfiguration();
144 /* get mac from properties */
145 if (properties.containsKey(Thing.PROPERTY_MAC_ADDRESS)) {
146 mac = config.get(Thing.PROPERTY_MAC_ADDRESS).toString();
149 /* get mac from config */
150 if (mac.isBlank() && config.containsKey(Thing.PROPERTY_MAC_ADDRESS)) {
151 mac = config.get(Thing.PROPERTY_MAC_ADDRESS).toString();
155 logger.debug("({}) cant find macAddress in properties and config", uid);
157 return AsuswrtUtils.formatMac(mac, ':');
161 * Gets the channel ID including the group.
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;
172 * Gets a channel name from a channel ID.
174 protected String getChannelFromID(ChannelUID channelID) {
175 String channel = channelID.getIdWithoutGroup();
176 channel = channel.replace(CHANNEL_GROUP_CLIENT + "#", "");
181 * Checks if the state changed since the last channel update.
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
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);