]> git.basschouten.com Git - openhab-addons.git/blob
77eddc8dddc69329ed03158e04aa0fd134856b58
[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.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;
35
36 /**
37  * The {@link AsuswrtInterface} is used as {@link org.openhab.core.thing.binding.ThingHandler ThingHandler} for router
38  * interfaces.
39  *
40  * @author Christian Wild - Initial contribution
41  */
42 @NonNullByDefault
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;
49
50     public AsuswrtInterface(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         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);
63             updateChannels();
64             updateStatus(ThingStatus.ONLINE);
65         } else {
66             logger.debug("({}) configurtation error", uid);
67         }
68     }
69
70     /*
71      * Commands and events
72      */
73
74     @Override
75     public void handleCommand(ChannelUID channelUID, Command command) {
76         if (command instanceof RefreshType) {
77             updateChannels();
78         }
79     }
80
81     public void updateChannels() {
82         try {
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);
89         }
90     }
91
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()));
106     }
107
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));
121     }
122
123     /**
124      * Fires events on {@link AsuswrtIpInfo} changes.
125      */
126     public void fireEvents(AsuswrtIpInfo interfaceInfo) {
127         Boolean isConnected = interfaceInfo.isConnected();
128         if (checkForStateChange(CHANNEL_NETWORK_STATE, isConnected)) {
129             if (isConnected) {
130                 triggerChannel(getChannelID(CHANNEL_GROUP_NETWORK, EVENT_CONNECTION), EVENT_STATE_CONNECTED);
131             } else {
132                 triggerChannel(getChannelID(CHANNEL_GROUP_NETWORK, EVENT_CONNECTION), EVENT_STATE_DISCONNECTED);
133             }
134         }
135     }
136
137     /*
138      * Functions
139      */
140
141     /**
142      * Gets the channel ID including the group.
143      */
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;
148         }
149         return channel;
150     }
151
152     /**
153      * Gets a channel name from a channel ID.
154      */
155     protected String getChannelFromID(ChannelUID channelID) {
156         String channel = channelID.getIdWithoutGroup();
157         channel = channel.replace(CHANNEL_GROUP_NETWORK + "#", "");
158         return channel;
159     }
160
161     /**
162      * Checks if the state changed since the last channel update.
163      *
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
167      */
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);
173             return true;
174         }
175         return false;
176     }
177 }