Skip to content

Commit

Permalink
[asuswrt] included traffic monitoring
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Wild <christian@wildclan.de>
  • Loading branch information
wildcs committed Dec 1, 2022
1 parent 3bfeddd commit c17f5ca
Show file tree
Hide file tree
Showing 20 changed files with 538 additions and 793 deletions.
18 changes: 11 additions & 7 deletions bundles/org.openhab.binding.asuswrt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

This binding adds support to control get informations from ASUS-Router (Copyright © ASUS).

# !! EARLY BETA !!

# BINDING AND REDME NOT FULLY WORKING/COMPLETED

## Supported Things

Binding supports ASUS-Router with AsusWRT or -AsusWRT-Merlin Firmware.
Expand Down Expand Up @@ -76,14 +72,22 @@ All devices support some of the following channels:
| | onlineClients | text (RO) | Online clients with name and MAC-Addresses | router |
| | onlineMACs | text (RO) | List with mac-addresses of online clients | router |
| | onlineClientsCount | Number:Dimensionless | Count of online clients | router |
| traffic | curRX | Number:DataTransferRate| Current DataTransferRate MBits/s (receive) | interface, client |
| | curTX | Number:DataTransferRate| Current DataTransferRate MBits/s (send) | interface, client |
| | todayRX | Number:DataAmount | Data received since 0:00 a clock in MB | interface, client |
| | todayTX | Number:DataAmount | Data sent since 0:00 a clock in MB | interface, client |
| | totalRX | Number:DataAmount | Data received since reboot in MB | interface, client |
| | totalTX | Number:DataAmount | Data sent since reboot in MB | interface, client |


## Events

All devices support some of the following Events:
| group | event |kind | description | things supporting this event |
|------------------|-----------------|------------|------------------------------------------------------------------------|---------------------------------|
| networkInfo | connectionEvent | Trigger | Fired if Client leaves ('gone') or enters ('connected') the network | interface, client |
| group | event |kind | description | things supporting this event |
|------------------|-------------------|------------|------------------------------------------------------------------------|---------------------------------|
| networkInfo | connectionEvent | Trigger | Fired if connection is established ('connected') or ('disconnected') | interface |
| | clientOnlineEvent | Trigger | Fired if Client leaves ('gone') or enters ('connected') the network | client |
| clientListGroup | clientOnlineEvent | Trigger | Fired if Client leaves ('gone') or enters ('connected') the network | router |



Expand Down
98 changes: 0 additions & 98 deletions bundles/org.openhab.binding.asuswrt/README.md.bak

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ public void handleClientScan(AsuswrtClientList clientList) {
public DiscoveryResult createInterfaceResult(AsuswrtIpInfo interfaceInfo) {
String ifName = interfaceInfo.getName();
String macAddress = interfaceInfo.getMAC();
String unformatedMac = unformatMac(macAddress);
String label = "AwrtInterface_" + ifName;

Map<String, Object> properties = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,21 @@ public class AsuswrtBindingConstants {
public static final String CHANNEL_CLIENTS_ONLINE = "onlineClients";
public static final String CHANNEL_CLIENTS_COUNT = "onlineClientsCount";
public static final String CHANNEL_CLIENTS_ONLINE_MAC = "onlineMACs";
public static final String EVENT_CLIENT_CONNECTION = "clientOnlineEvent";

// channel group client information
public static final String CHANNEL_GROUP_CLIENT = "client";
public static final String CHANNEL_CLIENT_NICKNAME = "clientName";

// channel group traffic
public static final String CHANNEL_GROUP_TRAFFIC = "traffic";
public static final String CHANNEL_TRAFFIC_TOTAL_RX = "totalRX";
public static final String CHANNEL_TRAFFIC_TOTAL_TX = "totalTX";
public static final String CHANNEL_TRAFFIC_TODAY_RX = "todayRX";
public static final String CHANNEL_TRAFFIC_TODAY_TX = "todayTX";
public static final String CHANNEL_TRAFFIC_CURRENT_RX = "curRX";
public static final String CHANNEL_TRAFFIC_CURRENT_TX = "curTX";

/**
* PROPERTIES
*/
Expand Down Expand Up @@ -173,4 +183,13 @@ public class AsuswrtBindingConstants {
public static final String JSON_MEMBER_MEM_USED = "mem_used";
public static final String JSON_MEMBER_MEM_FREE = "mem_free";
public static final Integer USAGE_CPU_COUNT = 4; // max count of cpu-cores

// traffic
public static final String JSON_MEMBER_TRAFFIC = "netdev";
public static final String JSON_MEMBER_INET_RX = "INTERNET_rx";
public static final String JSON_MEMBER_INET_TX = "INTERNET_tx";
public static final String JSON_MEMBER_LAN_RX = "WIRED_rx";
public static final String JSON_MEMBER_LAN_TX = "WIRED_tx";
public static final String JSON_MEMBER_WLAN_RX = "WIRELESS{}_rx";
public static final String JSON_MEMBER_WLAN_TX = "WIRELESS{}_tx";
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public class AsuswrtBindingSettings {
public static final Boolean HTTP_SSL_TRUST_ALL = true; // trust all ssl-certs

public static final Integer COOKIE_LIFETIME_S = 3600; // lifetime of login-cookie
public static final Integer POLLING_INTERVAL_S_MIN = 10; // minimum polling interval
public static final Integer POLLING_INTERVAL_S_DEFAULT = 30; // default polling interval
public static final Integer POLLING_INTERVAL_S_MIN = 5; // minimum polling interval
public static final Integer POLLING_INTERVAL_S_DEFAULT = 20; // default polling interval
public static final Integer RECONNECT_INTERVAL_S = 30; // interval trying try to reconnect to router
public static final Integer DISCOVERY_TIMEOUT_S = 10; // discovery service timeout in s
public static final Integer DISCOVERY_AUTOREMOVE_S = 1800; // discovery service remove things after x seconds
Expand All @@ -59,5 +59,6 @@ public class AsuswrtBindingSettings {
public static final String INTERFACE_WAN = "wan";
public static final String INTERFACE_LAN = "lan";
public static final String INTERFACE_WLAN = "wlan";
public static final String INTERFACE_CLIENT = "client";
public static final Set<String> INTERFACE_LIST = Set.of(INTERFACE_WAN, INTERFACE_LAN);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/
package org.openhab.binding.asuswrt.internal.helpers;

import java.util.Calendar;
import java.util.Date;
import java.util.regex.Pattern;

import javax.measure.Unit;
Expand Down Expand Up @@ -441,4 +443,23 @@ public static QuantityType<Energy> getEnergyType(@Nullable Number numVal, Unit<E
public static State getQuantityType(@Nullable Number numVal, Unit<?> unit) {
return new QuantityType<>((numVal != null ? numVal : 0), unit);
}

/************************************
* DATE UTILS
***********************************/
/**
* Checks if two date objects are on the same day ignoring time.
*
* @param date1 the first date, not altered, not null
* @param date2 the second date, not altered, not null
* @return true if they represent the same day
*/
public static boolean isSameDay(final Date date1, final Date date2) {
final Calendar cal1 = Calendar.getInstance();
cal1.setTime(date1);
final Calendar cal2 = Calendar.getInstance();
cal2.setTime(date2);
return cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA) && cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
&& cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.openhab.binding.asuswrt.internal.structures;

import static org.openhab.binding.asuswrt.internal.constants.AsuswrtBindingConstants.*;
import static org.openhab.binding.asuswrt.internal.constants.AsuswrtBindingSettings.*;
import static org.openhab.binding.asuswrt.internal.helpers.AsuswrtUtils.*;

import org.eclipse.jdt.annotation.NonNullByDefault;
Expand All @@ -26,8 +27,7 @@
*/
@NonNullByDefault
public class AsuswrtClientInfo {
private Integer curRx = 0;
private Integer curTx = 0;
private AsuswrtTraffic traffic = new AsuswrtTraffic();
private Integer defaultType = 0;
private String dpiDevice = "";
private String dpiType = "";
Expand Down Expand Up @@ -55,8 +55,6 @@ public class AsuswrtClientInfo {
private Integer rog = 0;
private Integer rssi = 0;
private String ssid = "";
private Integer totalRx = 0;
private Integer totalTx = 0;
private String vendor = "";
private String wlConnectTime = "";
private Integer wtfast = 0;
Expand All @@ -74,6 +72,7 @@ public AsuswrtClientInfo() {
* @param jsonObject with clientinfo
*/
public AsuswrtClientInfo(JsonObject jsonObject) {
this.traffic = new AsuswrtTraffic(INTERFACE_CLIENT);
setData(jsonObject);
}

Expand All @@ -84,8 +83,7 @@ public AsuswrtClientInfo(JsonObject jsonObject) {
* @param jsonObject
*/
public void setData(JsonObject jsonObject) {
this.curRx = jsonObjectToInt(jsonObject, JSON_MEMBER_CLIENT_RXCUR, this.curRx);
this.curTx = jsonObjectToInt(jsonObject, JSON_MEMBER_CLIENT_TXCUR, this.curTx);
this.traffic.setData(jsonObject);
this.defaultType = jsonObjectToInt(jsonObject, JSON_MEMBER_CLIENT_DEFTYPE, this.defaultType);
this.dpiDevice = jsonObjectToString(jsonObject, JSON_MEMBER_CLIENT_DPIDEVICE, this.dpiDevice);
this.dpiType = jsonObjectToString(jsonObject, JSON_MEMBER_CLIENT_DPITYPE, this.dpiType);
Expand Down Expand Up @@ -113,8 +111,6 @@ public void setData(JsonObject jsonObject) {
this.rog = jsonObjectToInt(jsonObject, JSON_MEMBER_CLIENT_ROG, this.rog);
this.rssi = jsonObjectToInt(jsonObject, JSON_MEMBER_CLIENT_RSSI, this.rssi);
this.ssid = jsonObjectToString(jsonObject, JSON_MEMBER_CLIENT_SSID, this.ssid);
this.totalRx = jsonObjectToInt(jsonObject, JSON_MEMBER_CLIENT_RXTOTAL, this.totalRx);
this.totalTx = jsonObjectToInt(jsonObject, JSON_MEMBER_CLIENT_TXTOTAL, this.totalTx);
this.vendor = jsonObjectToString(jsonObject, JSON_MEMBER_CLIENT_VENDOR, this.vendor);
this.wlConnectTime = jsonObjectToString(jsonObject, JSON_MEMBER_CLIENT_CONNECTTIME, this.wlConnectTime);
this.wtfast = jsonObjectToInt(jsonObject, JSON_MEMBER_CLIENT_WTFAST, this.wtfast);
Expand All @@ -126,12 +122,8 @@ public void setData(JsonObject jsonObject) {
*
************************************/

public Integer getRX() {
return this.curRx;
}

public Integer getTX() {
return this.curTx;
public AsuswrtTraffic getTraffic() {
return this.traffic;
}

public Integer getDefaultType() {
Expand Down Expand Up @@ -246,14 +238,6 @@ public String getSSID() {
return this.ssid;
}

public Integer getTotalRX() {
return this.totalRx;
}

public Integer getTotalTX() {
return this.totalTx;
}

public String getVendor() {
return this.vendor;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public Iterator<AsuswrtIpInfo> iterator() {
* @param ipInfoInfo AsuswrtIpInfo
*/
private void addInterface(AsuswrtIpInfo ipInfo) {

this.ipInfoList.add(ipInfo);
}

Expand Down
Loading

0 comments on commit c17f5ca

Please sign in to comment.