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.*;
17 import java.util.HashMap;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
24 import com.google.gson.JsonObject;
27 * The {@link AsuswrtRouterInfo} class stores the router data
29 * @author Christian Wild - Initial contribution
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<>();
40 public AsuswrtRouterInfo() {
43 public AsuswrtRouterInfo(JsonObject jsonObject) {
44 setSysInfo(jsonObject);
51 public void setAllData(JsonObject jsonObject) {
52 setSysInfo(jsonObject);
53 setUsageStats(jsonObject);
56 public void setSysInfo(JsonObject jsonObject) {
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");
67 public void setUsageStats(JsonObject jsonObject) {
68 JsonObject jsnMemUsage = jsonObject.getAsJsonObject(JSON_MEMBER_MEM_USAGE);
69 JsonObject jsnCpuUsage = jsonObject.getAsJsonObject(JSON_MEMBER_CPU_USAGE);
71 if (jsnMemUsage != null) {
72 usageStats.put(JSON_MEMBER_MEM_USAGE,
73 new AsuswrtUsage(jsnMemUsage, JSON_MEMBER_MEM_TOTAL, JSON_MEMBER_MEM_USED));
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));
92 public String getProductId() {
96 public String getFirmwareVersion() {
97 return fwVersion + " (" + fwBuild + ")";
100 public String getMAC() {
104 public AsuswrtUsage getMemUsage() {
105 if (usageStats.containsKey(JSON_MEMBER_MEM_USAGE)) {
106 AsuswrtUsage usage = usageStats.get(JSON_MEMBER_MEM_USAGE);
111 return new AsuswrtUsage();
115 * Gets the CPU usage for a core.
117 * @param coreNum the core number
118 * @return the {@link AsuswrtUsage} for the given core
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);
128 return new AsuswrtUsage();
132 * Get CPU usage average over all cores.
134 * @return the {@link AsuswrtUsage} with CPU usage average over all cores
136 public AsuswrtUsage getCpuAverage() {
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();
149 total = total / coreNum - 1;
150 used = used / coreNum - 1;
152 return new AsuswrtUsage(total, used);