File: sysinfo.py

package info (click to toggle)
python-asusrouter 1.21.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,856 kB
  • sloc: python: 20,497; makefile: 3
file content (108 lines) | stat: -rw-r--r-- 3,282 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
"""SysInfo endpoint module."""

from __future__ import annotations

from typing import Any

from asusrouter.modules.data import AsusData
from asusrouter.modules.wlan import WLAN_TYPE, Wlan
from asusrouter.tools.cleaners import clean_content
from asusrouter.tools.converters import safe_float, safe_int
from asusrouter.tools.readers import read_json_content


def read(content: str, **kwargs: Any) -> dict[str, Any]:
    """Read sysinfo data."""

    # Prepare the content
    content = clean_content(content).replace(" = ", '":').replace(";\n", ',"')
    content = '{"' + content[:-3] + "}"

    # Read the json content
    sysinfo: dict[str, Any] = read_json_content(content)

    return sysinfo


def process(data: dict[str, Any]) -> dict[AsusData, Any]:
    """Process sysinfo data."""

    state: dict[AsusData, Any] = {}

    sysinfo = {}

    # WLAN info
    wlan_info = {}
    i = 0
    while wlan_data := data.get(f"wlc_{i}_arr"):
        name = WLAN_TYPE.get(i, Wlan.UNKNOWN)
        wlan_info[name] = {
            "client_associated": safe_int(wlan_data[0]),
            "client_authorized": safe_int(wlan_data[1]),
            "client_authenticated": safe_int(wlan_data[2]),
        }
        i += 1
    sysinfo["wlan"] = wlan_info

    # Connections info
    connections_info = {}
    connections_data = data.get("conn_stats_arr")
    if connections_data:
        connections_info = {
            "total": safe_int(connections_data[0]),
            "active": safe_int(connections_data[1]),
        }
    sysinfo["connections"] = connections_info

    # Memory info
    memory_info = {}
    memory_data = data.get("mem_stats_arr")
    if memory_data:
        # Before 388.7
        # JFFS data is presented as a string of `XX.xx / YY.yy MB`
        # where `XX.xx` is the used space (float) and `YY.yy` is
        # the total space (float)
        jffs = memory_data[7]
        if "/" in jffs:
            jffs_data = jffs[:-3].split(" / ")
            jffs_used = safe_float(jffs_data[0])
            jffs_total = safe_float(jffs_data[1])
            jffs_free = (
                jffs_total - jffs_used if jffs_used and jffs_total else None
            )
        # From 388.7
        # JFFS is just a `free` single float
        else:
            jffs_free = safe_float(jffs)
            jffs_used = None
            jffs_total = None

        memory_info = {
            "total": safe_float(memory_data[0]),
            "free": safe_float(memory_data[1]),
            "buffers": safe_float(memory_data[2]),
            "cache": safe_float(memory_data[3]),
            "swap_1": safe_float(memory_data[4]),
            "swap_2": safe_float(memory_data[5]),
            "nvram": safe_int(memory_data[6]),
            "jffs_free": jffs_free,
            "jffs_used": jffs_used,
            "jffs_total": jffs_total,
        }
    sysinfo["memory"] = memory_info

    # Load average info
    load_avg_info = {}
    load_avg_data = data.get("cpu_stats_arr")
    if load_avg_data:
        load_avg_info = {
            1: safe_float(load_avg_data[0]),
            5: safe_float(load_avg_data[1]),
            15: safe_float(load_avg_data[2]),
        }
    sysinfo["load_avg"] = load_avg_info

    # Sysinfo as it is
    state[AsusData.SYSINFO] = sysinfo

    return state