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.structures;
15 import static org.openhab.binding.asuswrt.internal.constants.AsuswrtBindingConstants.*;
16 import static org.openhab.binding.asuswrt.internal.constants.AsuswrtBindingSettings.*;
17 import static org.openhab.binding.asuswrt.internal.helpers.AsuswrtUtils.*;
19 import java.time.LocalDate;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 import com.google.gson.JsonObject;
28 * The {@link AsuswrtTraffic} class handles traffic statistics
30 * @author Christian Wild - Initial contribution
33 public class AsuswrtTraffic {
34 private final Logger logger = LoggerFactory.getLogger(AsuswrtTraffic.class);
35 private Double curTX = 0.0;
36 private Double curRX = 0.0;
37 private Integer totalTX = 0;
38 private Integer totalRX = 0;
39 private Integer zeroHourTX = 0;
40 private Integer zeroHourRX = 0;
41 private LocalDate zeroHourDate = LocalDate.now();
42 private Long lastUpdate = 0L;
43 private String representationProperty = "";
45 public AsuswrtTraffic() {
51 * @param representationProperty representationProperty of the device (i.e. interfaceName)
53 public AsuswrtTraffic(String representationProperty) {
54 this.representationProperty = representationProperty.toLowerCase();
60 * @param jsonObject stores the data
61 * @param representationProperty representationProperty of the device (i.e. interfaceName)
63 public AsuswrtTraffic(JsonObject jsonObject, String representationProperty) {
64 this.representationProperty = representationProperty;
68 public void setData(JsonObject jsonObject) {
71 if (representationProperty.startsWith(INTERFACE_LAN)) {
72 intRX = getTrafficFromHex(jsonObject, JSON_MEMBER_LAN_RX);
73 intTX = getTrafficFromHex(jsonObject, JSON_MEMBER_LAN_TX);
74 curRX = calculateCurrentTraffic(intRX, totalRX);
75 curTX = calculateCurrentTraffic(intTX, totalTX);
76 totalRX = getTrafficFromHex(jsonObject, JSON_MEMBER_LAN_RX);
77 totalTX = getTrafficFromHex(jsonObject, JSON_MEMBER_LAN_TX);
78 } else if (representationProperty.startsWith(INTERFACE_WAN)) {
79 intRX = getTrafficFromHex(jsonObject, JSON_MEMBER_INET_RX);
80 intTX = getTrafficFromHex(jsonObject, JSON_MEMBER_INET_TX);
81 curRX = calculateCurrentTraffic(intRX, totalRX);
82 curTX = calculateCurrentTraffic(intTX, totalTX);
83 totalRX = getTrafficFromHex(jsonObject, JSON_MEMBER_INET_RX);
84 totalTX = getTrafficFromHex(jsonObject, JSON_MEMBER_INET_TX);
85 } else if (representationProperty.startsWith(INTERFACE_WLAN)) {
86 for (int i = 0; i < 1; i++) {
87 intRX = getTrafficFromHex(jsonObject, JSON_MEMBER_WLAN_RX.replace("{}", Integer.toString(i)));
88 intTX = getTrafficFromHex(jsonObject, JSON_MEMBER_WLAN_TX.replace("{}", Integer.toString(i)));
89 curRX = calculateCurrentTraffic(intRX, totalRX);
90 curTX = calculateCurrentTraffic(intTX, totalTX);
91 totalRX = getTrafficFromHex(jsonObject, JSON_MEMBER_INET_RX);
92 totalTX = getTrafficFromHex(jsonObject, JSON_MEMBER_INET_TX);
94 } else if (INTERFACE_CLIENT.equals(representationProperty)) {
95 curRX = Double.valueOf(jsonObjectToInt(jsonObject, JSON_MEMBER_CLIENT_RXCUR, -1));
96 curTX = Double.valueOf(jsonObjectToInt(jsonObject, JSON_MEMBER_CLIENT_TXCUR, -1));
97 totalRX = jsonObjectToInt(jsonObject, JSON_MEMBER_CLIENT_RXTOTAL, -1);
98 totalTX = jsonObjectToInt(jsonObject, JSON_MEMBER_CLIENT_TXTOTAL, -1);
100 logger.trace("({}) can't set Trafficdata", representationProperty);
102 lastUpdate = System.currentTimeMillis();
103 setZeroHourTraffic(totalRX, totalTX);
107 * Saves the traffic values at the start of a new day.
109 private void setZeroHourTraffic(Integer totalRX, Integer totalTX) {
110 LocalDate today = LocalDate.now();
111 if (today.isAfter(zeroHourDate) || zeroHourRX > totalRX) {
112 zeroHourRX = totalRX;
113 zeroHourTX = totalTX;
114 zeroHourDate = today;
119 * Gets the traffic as {@link Integer} value from a hexadecimal value in a {@link JsonObject}.
121 * @param jsonObject the object containing the values
122 * @param jsonMember the name of the key that stores the value
123 * @return the traffic value
125 private Integer getTrafficFromHex(JsonObject jsonObject, String jsonMember) {
127 if (jsonObject.has(jsonMember)) {
128 String hex = jsonObjectToString(jsonObject, jsonMember);
130 lngVal = Long.decode(hex);
131 lngVal = lngVal * 8 / 1024 / 1024 / 2;
132 return lngVal.intValue();
133 } catch (Exception e) {
134 logger.debug("({}) error calculating traffic from hex '{}'", representationProperty, hex);
141 * Calculates the traffic from the actual and old total traffic using the time span.
143 * @param actVal the actual value
144 * @param oldVal the old value
145 * @return the current traffic value
147 private Double calculateCurrentTraffic(Integer actVal, Integer oldVal) {
148 if (lastUpdate > 0) {
149 Long timeSpan = (System.currentTimeMillis() - lastUpdate) / 1000;
153 div = actVal - oldVal;
154 return Double.valueOf(div / timeSpan.intValue());
156 } catch (Exception e) {
157 logger.debug("({}) error calculating traffic from timeSpan '{}/{}'", representationProperty, div,
168 public Double getCurrentRX() {
172 public Double getCurrentTX() {
176 public Integer getTotalRX() {
180 public Integer getTotalTX() {
184 public Integer getTodayRX() {
185 return totalRX - zeroHourRX;
188 public Integer getTodayTX() {
189 return totalTX - zeroHourTX;