]> git.basschouten.com Git - openhab-addons.git/blob
5def6df3c5e31b4a23086d472089d1e4ac1383ed
[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.structures;
14
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.*;
18
19 import java.time.LocalDate;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import com.google.gson.JsonObject;
26
27 /**
28  * The {@link AsuswrtTraffic} class handles traffic statistics
29  *
30  * @author Christian Wild - Initial contribution
31  */
32 @NonNullByDefault
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 = "";
44
45     public AsuswrtTraffic() {
46     }
47
48     /**
49      * Constructor.
50      *
51      * @param representationProperty representationProperty of the device (i.e. interfaceName)
52      */
53     public AsuswrtTraffic(String representationProperty) {
54         this.representationProperty = representationProperty.toLowerCase();
55     }
56
57     /**
58      * Constructor.
59      *
60      * @param jsonObject stores the data
61      * @param representationProperty representationProperty of the device (i.e. interfaceName)
62      */
63     public AsuswrtTraffic(JsonObject jsonObject, String representationProperty) {
64         this.representationProperty = representationProperty;
65         setData(jsonObject);
66     }
67
68     public void setData(JsonObject jsonObject) {
69         Integer intRX;
70         Integer intTX;
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);
93             }
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);
99         } else {
100             logger.trace("({}) can't set Trafficdata", representationProperty);
101         }
102         lastUpdate = System.currentTimeMillis();
103         setZeroHourTraffic(totalRX, totalTX);
104     }
105
106     /**
107      * Saves the traffic values at the start of a new day.
108      */
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;
115         }
116     }
117
118     /**
119      * Gets the traffic as {@link Integer} value from a hexadecimal value in a {@link JsonObject}.
120      *
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
124      */
125     private Integer getTrafficFromHex(JsonObject jsonObject, String jsonMember) {
126         Long lngVal;
127         if (jsonObject.has(jsonMember)) {
128             String hex = jsonObjectToString(jsonObject, jsonMember);
129             try {
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);
135             }
136         }
137         return -1;
138     }
139
140     /**
141      * Calculates the traffic from the actual and old total traffic using the time span.
142      *
143      * @param actVal the actual value
144      * @param oldVal the old value
145      * @return the current traffic value
146      */
147     private Double calculateCurrentTraffic(Integer actVal, Integer oldVal) {
148         if (lastUpdate > 0) {
149             Long timeSpan = (System.currentTimeMillis() - lastUpdate) / 1000;
150             Integer div = 0;
151             try {
152                 if (actVal >= 0) {
153                     div = actVal - oldVal;
154                     return Double.valueOf(div / timeSpan.intValue());
155                 }
156             } catch (Exception e) {
157                 logger.debug("({}) error calculating traffic from timeSpan '{}/{}'", representationProperty, div,
158                         timeSpan);
159             }
160         }
161         return -1.0;
162     }
163
164     /*
165      * Getters
166      */
167
168     public Double getCurrentRX() {
169         return curRX;
170     }
171
172     public Double getCurrentTX() {
173         return curTX;
174     }
175
176     public Integer getTotalRX() {
177         return totalRX;
178     }
179
180     public Integer getTotalTX() {
181         return totalTX;
182     }
183
184     public Integer getTodayRX() {
185         return totalRX - zeroHourRX;
186     }
187
188     public Integer getTodayTX() {
189         return totalTX - zeroHourTX;
190     }
191 }