| 1 |
"""SysInfo endpoint module.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
from typing import Any |
| 6 |
|
| 7 |
from asusrouter.modules.data import AsusData |
| 8 |
from asusrouter.modules.wlan import WLAN_TYPE, Wlan |
| 9 |
from asusrouter.tools.cleaners import clean_content |
| 10 |
from asusrouter.tools.converters import safe_float, safe_int |
| 11 |
from asusrouter.tools.readers import read_json_content |
| 12 |
|
| 13 |
|
| 14 |
def read(content: str, **kwargs: Any) -> dict[str, Any]: |
| 15 |
"""Read sysinfo data.""" |
| 16 |
|
| 17 |
# Prepare the content |
| 18 |
content = clean_content(content).replace(" = ", '":').replace(";\n", ',"') |
| 19 |
content = '{"' + content[:-3] + "}" |
| 20 |
|
| 21 |
# Read the json content |
| 22 |
sysinfo: dict[str, Any] = read_json_content(content) |
| 23 |
|
| 24 |
return sysinfo |
| 25 |
|
| 26 |
|
| 27 |
def process(data: dict[str, Any]) -> dict[AsusData, Any]: |
| 28 |
"""Process sysinfo data.""" |
| 29 |
|
| 30 |
state: dict[AsusData, Any] = {} |
| 31 |
|
| 32 |
sysinfo = {} |
| 33 |
|
| 34 |
# WLAN info |
| 35 |
wlan_info = {} |
| 36 |
i = 0 |
| 37 |
while wlan_data := data.get(f"wlc_{i}_arr"): |
| 38 |
name = WLAN_TYPE.get(i, Wlan.UNKNOWN) |
| 39 |
wlan_info[name] = { |
| 40 |
"client_associated": safe_int(wlan_data[0]), |
| 41 |
"client_authorized": safe_int(wlan_data[1]), |
| 42 |
"client_authenticated": safe_int(wlan_data[2]), |
| 43 |
} |
| 44 |
i += 1 |
| 45 |
sysinfo["wlan"] = wlan_info |
| 46 |
|
| 47 |
# Connections info |
| 48 |
connections_info = {} |
| 49 |
connections_data = data.get("conn_stats_arr") |
| 50 |
if connections_data: |
| 51 |
connections_info = { |
| 52 |
"total": safe_int(connections_data[0]), |
| 53 |
"active": safe_int(connections_data[1]), |
| 54 |
} |
| 55 |
sysinfo["connections"] = connections_info |
| 56 |
|
| 57 |
# Memory info |
| 58 |
memory_info = {} |
| 59 |
memory_data = data.get("mem_stats_arr") |
| 60 |
if memory_data: |
| 61 |
# Before 388.7 |
| 62 |
# JFFS data is presented as a string of `XX.xx / YY.yy MB` |
| 63 |
# where `XX.xx` is the used space (float) and `YY.yy` is |
| 64 |
# the total space (float) |
| 65 |
jffs = memory_data[7] |
| 66 |
if "/" in jffs: |
| 67 |
jffs_data = jffs[:-3].split(" / ") |
| 68 |
jffs_used = safe_float(jffs_data[0]) |
| 69 |
jffs_total = safe_float(jffs_data[1]) |
| 70 |
jffs_free = ( |
| 71 |
jffs_total - jffs_used if jffs_used and jffs_total else None |
| 72 |
) |
| 73 |
# From 388.7 |
| 74 |
# JFFS is just a `free` single float |
| 75 |
else: |
| 76 |
jffs_free = safe_float(jffs) |
| 77 |
jffs_used = None |
| 78 |
jffs_total = None |
| 79 |
|
| 80 |
memory_info = { |
| 81 |
"total": safe_float(memory_data[0]), |
| 82 |
"free": safe_float(memory_data[1]), |
| 83 |
"buffers": safe_float(memory_data[2]), |
| 84 |
"cache": safe_float(memory_data[3]), |
| 85 |
"swap_1": safe_float(memory_data[4]), |
| 86 |
"swap_2": safe_float(memory_data[5]), |
| 87 |
"nvram": safe_int(memory_data[6]), |
| 88 |
"jffs_free": jffs_free, |
| 89 |
"jffs_used": jffs_used, |
| 90 |
"jffs_total": jffs_total, |
| 91 |
} |
| 92 |
sysinfo["memory"] = memory_info |
| 93 |
|
| 94 |
# Load average info |
| 95 |
load_avg_info = {} |
| 96 |
load_avg_data = data.get("cpu_stats_arr") |
| 97 |
if load_avg_data: |
| 98 |
load_avg_info = { |
| 99 |
1: safe_float(load_avg_data[0]), |
| 100 |
5: safe_float(load_avg_data[1]), |
| 101 |
15: safe_float(load_avg_data[2]), |
| 102 |
} |
| 103 |
sysinfo["load_avg"] = load_avg_info |
| 104 |
|
| 105 |
# Sysinfo as it is |
| 106 |
state[AsusData.SYSINFO] = sysinfo |
| 107 |
|
| 108 |
return state |