]> git.basschouten.com Git - openhab-addons.git/blob
7212a075f9d15b95007140c7832f632c40ed2263
[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
17 import java.util.HashMap;
18 import java.util.Map;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 import com.google.gson.JsonObject;
25
26 /**
27  * The {@link AsuswrtRouterInfo} class stores the router data
28  *
29  * @author Christian Wild - Initial contribution
30  */
31 @NonNullByDefault
32 public class AsuswrtRouterInfo {
33     private final Logger logger = LoggerFactory.getLogger(AsuswrtRouterInfo.class);
34     private String productId = "";
35     private String fwVersion = "";
36     private String fwBuild = "";
37     private String macAddress = "";
38     private Map<String, AsuswrtUsage> usageStats = new HashMap<>();
39
40     public AsuswrtRouterInfo() {
41     }
42
43     public AsuswrtRouterInfo(JsonObject jsonObject) {
44         setSysInfo(jsonObject);
45     }
46
47     /*
48      * Setters
49      */
50
51     public void setAllData(JsonObject jsonObject) {
52         setSysInfo(jsonObject);
53         setUsageStats(jsonObject);
54     }
55
56     public void setSysInfo(JsonObject jsonObject) {
57         try {
58             productId = jsonObject.get(JSON_MEMBER_PRODUCTID).toString();
59             fwVersion = jsonObject.get(JSON_MEMBER_FIRMWARE).toString();
60             fwBuild = jsonObject.get(JSON_MEMBER_BUILD).toString();
61             macAddress = jsonObject.get(JSON_MEMBER_MAC).toString();
62         } catch (Exception e) {
63             logger.trace("incomplete SysInfo");
64         }
65     }
66
67     public void setUsageStats(JsonObject jsonObject) {
68         JsonObject jsnMemUsage = jsonObject.getAsJsonObject(JSON_MEMBER_MEM_USAGE);
69         JsonObject jsnCpuUsage = jsonObject.getAsJsonObject(JSON_MEMBER_CPU_USAGE);
70         // Get memory usage
71         if (jsnMemUsage != null) {
72             usageStats.put(JSON_MEMBER_MEM_USAGE,
73                     new AsuswrtUsage(jsnMemUsage, JSON_MEMBER_MEM_TOTAL, JSON_MEMBER_MEM_USED));
74         }
75         // Loop cpu usages
76         if (jsnCpuUsage != null) {
77             for (Integer i = 1; i <= USAGE_CPU_COUNT; i++) {
78                 String member = JSON_MEMBER_CPU_USAGE + "_" + i;
79                 String total = JSON_MEMBER_CPU_TOTAL.replace("{x}", "" + i);
80                 String used = JSON_MEMBER_CPU_USED.replace("{x}", "" + i);
81                 if (jsnCpuUsage.has(total) && jsnCpuUsage.has(used)) {
82                     usageStats.put(member, new AsuswrtUsage(jsnCpuUsage, total, used));
83                 }
84             }
85         }
86     }
87
88     /*
89      * Getters
90      */
91
92     public String getProductId() {
93         return productId;
94     }
95
96     public String getFirmwareVersion() {
97         return fwVersion + " (" + fwBuild + ")";
98     }
99
100     public String getMAC() {
101         return macAddress;
102     }
103
104     public AsuswrtUsage getMemUsage() {
105         if (usageStats.containsKey(JSON_MEMBER_MEM_USAGE)) {
106             AsuswrtUsage usage = usageStats.get(JSON_MEMBER_MEM_USAGE);
107             if (usage != null) {
108                 return usage;
109             }
110         }
111         return new AsuswrtUsage();
112     }
113
114     /**
115      * Gets the CPU usage for a core.
116      *
117      * @param coreNum the core number
118      * @return the {@link AsuswrtUsage} for the given core
119      */
120     public AsuswrtUsage getCpuUsage(Integer coreNum) {
121         String coreKey = JSON_MEMBER_CPU_USAGE + "_" + coreNum;
122         if (usageStats.containsKey(coreKey)) {
123             AsuswrtUsage usage = usageStats.get(coreKey);
124             if (usage != null) {
125                 return usage;
126             }
127         }
128         return new AsuswrtUsage();
129     }
130
131     /**
132      * Get CPU usage average over all cores.
133      *
134      * @return the {@link AsuswrtUsage} with CPU usage average over all cores
135      */
136     public AsuswrtUsage getCpuAverage() {
137         String coreKey;
138         AsuswrtUsage coreStatsX;
139         Integer total = 0, used = 0, coreNum;
140         for (coreNum = 1; coreNum <= USAGE_CPU_COUNT; coreNum++) {
141             coreKey = JSON_MEMBER_CPU_USAGE + "_" + coreNum;
142             coreStatsX = usageStats.get(coreKey);
143             if (coreStatsX != null) {
144                 total += coreStatsX.getTotal();
145                 used += coreStatsX.getUsed();
146             }
147         }
148         if (coreNum > 1) {
149             total = total / coreNum - 1;
150             used = used / coreNum - 1;
151         }
152         return new AsuswrtUsage(total, used);
153     }
154 }