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.structures.AsuswrtIpInfo;
23 import org.openhab.binding.asuswrt.internal.structures.AsuswrtTraffic;
24 import org.openhab.core.config.core.Configuration;
25 import org.openhab.core.library.unit.Units;
26 import org.openhab.core.thing.ChannelUID;
27 import org.openhab.core.thing.Thing;
28 import org.openhab.core.thing.ThingStatus;
29 import org.openhab.core.thing.ThingTypeUID;
30 import org.openhab.core.thing.binding.BaseThingHandler;
31 import org.openhab.core.types.Command;
32 import org.openhab.core.types.RefreshType;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * The {@link AsuswrtInterface} is used as {@link org.openhab.core.thing.binding.ThingHandler ThingHandler} for router
40 * @author Christian Wild - Initial contribution
43 public class AsuswrtInterface extends BaseThingHandler {
44 private final Logger logger = LoggerFactory.getLogger(AsuswrtInterface.class);
45 private final AsuswrtRouter router;
46 private String ifName = "";
47 private Map<String, Object> oldStates = new HashMap<>();
48 protected final String uid;
50 public AsuswrtInterface(Thing thing, AsuswrtRouter router) {
53 this.uid = getThing().getUID().getAsString();
57 public void initialize() {
58 logger.trace("({}) Initializing thing ", uid);
59 Configuration config = getThing().getConfiguration();
60 if (config.containsKey(PROPERTY_INTERFACE_NAME)) {
61 this.ifName = config.get(PROPERTY_INTERFACE_NAME).toString();
62 updateProperty(NETWORK_REPRESENTATION_PROPERTY, ifName);
64 updateStatus(ThingStatus.ONLINE);
66 logger.debug("({}) configurtation error", uid);
75 public void handleCommand(ChannelUID channelUID, Command command) {
76 if (command instanceof RefreshType) {
81 public void updateChannels() {
83 AsuswrtIpInfo interfaceInfo = router.getInterfaces().getByName(ifName);
84 fireEvents(interfaceInfo);
85 updateInterfaceChannels(interfaceInfo);
86 updateTrafficChannels(interfaceInfo.getTraffic());
87 } catch (Exception e) {
88 logger.debug("({}) unable to refresh data - property interfaceName not found ", uid);
92 private void updateInterfaceChannels(AsuswrtIpInfo interfaceInfo) {
93 updateState(getChannelID(CHANNEL_GROUP_NETWORK, CHANNEL_NETWORK_MAC), getStringType(interfaceInfo.getMAC()));
94 updateState(getChannelID(CHANNEL_GROUP_NETWORK, CHANNEL_NETWORK_IP),
95 getStringType(interfaceInfo.getIpAddress()));
96 updateState(getChannelID(CHANNEL_GROUP_NETWORK, CHANNEL_NETWORK_MASK),
97 getStringType(interfaceInfo.getSubnet()));
98 updateState(getChannelID(CHANNEL_GROUP_NETWORK, CHANNEL_NETWORK_GATEWAY),
99 getStringType(interfaceInfo.getGateway()));
100 updateState(getChannelID(CHANNEL_GROUP_NETWORK, CHANNEL_NETWORK_METHOD),
101 getStringType(interfaceInfo.getIpProto()));
102 updateState(getChannelID(CHANNEL_GROUP_NETWORK, CHANNEL_NETWORK_DNS),
103 getStringType(interfaceInfo.getDNSNServer()));
104 updateState(getChannelID(CHANNEL_GROUP_NETWORK, CHANNEL_NETWORK_STATE),
105 getOnOffType(interfaceInfo.isConnected()));
108 private void updateTrafficChannels(AsuswrtTraffic traffic) {
109 updateState(getChannelID(CHANNEL_GROUP_TRAFFIC, CHANNEL_TRAFFIC_CURRENT_RX),
110 getQuantityType(traffic.getCurrentRX(), Units.MEGABIT_PER_SECOND));
111 updateState(getChannelID(CHANNEL_GROUP_TRAFFIC, CHANNEL_TRAFFIC_CURRENT_TX),
112 getQuantityType(traffic.getCurrentTX(), Units.MEGABIT_PER_SECOND));
113 updateState(getChannelID(CHANNEL_GROUP_TRAFFIC, CHANNEL_TRAFFIC_TODAY_RX),
114 getQuantityType(traffic.getTodayRX(), Units.MEGABYTE));
115 updateState(getChannelID(CHANNEL_GROUP_TRAFFIC, CHANNEL_TRAFFIC_TODAY_TX),
116 getQuantityType(traffic.getTodayTX(), Units.MEGABYTE));
117 updateState(getChannelID(CHANNEL_GROUP_TRAFFIC, CHANNEL_TRAFFIC_TOTAL_RX),
118 getQuantityType(traffic.getTotalRX(), Units.MEGABYTE));
119 updateState(getChannelID(CHANNEL_GROUP_TRAFFIC, CHANNEL_TRAFFIC_TOTAL_TX),
120 getQuantityType(traffic.getTotalTX(), Units.MEGABYTE));
124 * Fires events on {@link AsuswrtIpInfo} changes.
126 public void fireEvents(AsuswrtIpInfo interfaceInfo) {
127 Boolean isConnected = interfaceInfo.isConnected();
128 if (checkForStateChange(CHANNEL_NETWORK_STATE, isConnected)) {
130 triggerChannel(getChannelID(CHANNEL_GROUP_NETWORK, EVENT_CONNECTION), EVENT_STATE_CONNECTED);
132 triggerChannel(getChannelID(CHANNEL_GROUP_NETWORK, EVENT_CONNECTION), EVENT_STATE_DISCONNECTED);
142 * Gets the channel ID including the group.
144 protected String getChannelID(String group, String channel) {
145 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
146 if (CHANNEL_GROUP_THING_SET.contains(thingTypeUID) && group.length() > 0) {
147 return group + "#" + channel;
153 * Gets a channel name from a channel ID.
155 protected String getChannelFromID(ChannelUID channelID) {
156 String channel = channelID.getIdWithoutGroup();
157 channel = channel.replace(CHANNEL_GROUP_NETWORK + "#", "");
162 * Checks if the state changed since the last channel update.
164 * @param stateName the name of the state (channel)
165 * @param comparator comparison value
166 * @return <code>true</code> if changed, <code>false</code> if not or no old value exists
168 private Boolean checkForStateChange(String stateName, Object comparator) {
169 if (oldStates.get(stateName) == null) {
170 oldStates.put(stateName, comparator);
171 } else if (!comparator.equals(oldStates.get(stateName))) {
172 oldStates.put(stateName, comparator);