| 1 |
"""Debug userdata json to see where things don't add up.""" |
| 2 |
|
| 3 |
import json |
| 4 |
import logging |
| 5 |
import os |
| 6 |
import sys |
| 7 |
from typing import Any |
| 8 |
|
| 9 |
_current_script_dir = os.path.dirname(os.path.abspath(__file__)) # noqa: PTH100, PTH120 |
| 10 |
_project_root_dir = os.path.abspath(os.path.join(_current_script_dir, os.pardir)) # noqa: PTH100, PTH118 |
| 11 |
|
| 12 |
if _project_root_dir not in sys.path: |
| 13 |
sys.path.append(_project_root_dir) |
| 14 |
|
| 15 |
from airos.airos6 import AirOS6 # noqa: E402 |
| 16 |
from airos.airos8 import AirOS8 # noqa: E402 |
| 17 |
from airos.data import ( # noqa: E402 |
| 18 |
AirOS6Data, |
| 19 |
AirOS8Data, |
| 20 |
Interface, |
| 21 |
Interface6, |
| 22 |
Remote, |
| 23 |
Station, |
| 24 |
Wireless, |
| 25 |
Wireless6, |
| 26 |
) |
| 27 |
|
| 28 |
logging.basicConfig(level=logging.DEBUG, stream=sys.stdout) |
| 29 |
_LOGGER = logging.getLogger(__name__) |
| 30 |
|
| 31 |
|
| 32 |
def main() -> None: |
| 33 |
"""Debug data.""" |
| 34 |
if len(sys.argv) <= 1: |
| 35 |
_LOGGER.info("Use with file to check") |
| 36 |
raise Exception("File to check not provided.") # noqa: TRY002 |
| 37 |
|
| 38 |
current_script_dir = os.path.dirname(os.path.abspath(__file__)) # noqa: PTH100, PTH120 |
| 39 |
project_root_dir = os.path.abspath(os.path.join(current_script_dir, os.pardir)) # noqa: PTH100, PTH118 |
| 40 |
|
| 41 |
if project_root_dir not in sys.path: |
| 42 |
sys.path.append(project_root_dir) |
| 43 |
|
| 44 |
# Load the JSON data |
| 45 |
with open(sys.argv[1], encoding="utf-8") as f: # noqa: PTH123 |
| 46 |
data = json.loads(f.read()) |
| 47 |
|
| 48 |
fwversion = (data.get("host") or {}).get("fwversion") |
| 49 |
if not fwversion: |
| 50 |
_LOGGER.error( |
| 51 |
"Unable to determine firmware version in '%s' (missing host.fwversion)", |
| 52 |
sys.argv[1], |
| 53 |
) |
| 54 |
raise ValueError("fwversion missing") from None |
| 55 |
|
| 56 |
try: |
| 57 |
fw_major = int(fwversion.lstrip("v").split(".", 1)[0]) |
| 58 |
except (ValueError, AttributeError) as exc: |
| 59 |
_LOGGER.error("Invalid firmware version '%s' in '%s'", fwversion, sys.argv[1]) |
| 60 |
raise ValueError("invalid fwversion") from exc |
| 61 |
|
| 62 |
if fw_major != 8: |
| 63 |
_LOGGER.warning("Non firmware 8 detected: %s", fwversion) |
| 64 |
|
| 65 |
try: |
| 66 |
_LOGGER.info("Attempting to deserialize Wireless object...") |
| 67 |
wireless_data: dict[str, Any] = data["wireless"] |
| 68 |
|
| 69 |
_LOGGER.info(" -> Checking Wireless enums...") |
| 70 |
if fw_major == 6: |
| 71 |
wireless_data_prepped = Wireless6.__pre_deserialize__(wireless_data.copy()) |
| 72 |
else: |
| 73 |
wireless_data_prepped = Wireless.__pre_deserialize__(wireless_data.copy()) |
| 74 |
_LOGGER.info( |
| 75 |
" Success! Wireless enums (mode, ieeemode, security) are valid." |
| 76 |
) |
| 77 |
|
| 78 |
_LOGGER.info( |
| 79 |
" -> Proving Wireless enums via ieeemode: %s", |
| 80 |
wireless_data_prepped["ieeemode"], |
| 81 |
) |
| 82 |
|
| 83 |
if fw_major >= 8: |
| 84 |
_LOGGER.info(" -> Checking list of Station objects...") |
| 85 |
station_list_data = wireless_data["sta"] |
| 86 |
station_obj_list = [] |
| 87 |
for i, station_data in enumerate(station_list_data): |
| 88 |
_LOGGER.info(" -> Checking Station object at index %s...", i) |
| 89 |
remote_data = station_data["remote"] |
| 90 |
_LOGGER.info(" -> Checking Remote object at index %s...", i) |
| 91 |
_LOGGER.info("Remote data = %s", remote_data) |
| 92 |
remote_obj = Remote.from_dict(remote_data) # noqa: F841 |
| 93 |
_LOGGER.info(" Success! Remote is valid.") |
| 94 |
|
| 95 |
station_obj = Station.from_dict(station_data) |
| 96 |
station_obj_list.append(station_obj) |
| 97 |
_LOGGER.info(" Success! Station at index %s is valid.", i) |
| 98 |
else: |
| 99 |
_LOGGER.warning(" fw lower than 8 -> no station information") |
| 100 |
|
| 101 |
_LOGGER.info(" -> Checking top-level Wireless object...") |
| 102 |
wireless_obj: Wireless | Wireless6 |
| 103 |
if fw_major == 6: |
| 104 |
wireless_obj = Wireless6.from_dict(wireless_data) |
| 105 |
else: |
| 106 |
wireless_obj = Wireless.from_dict(wireless_data) # noqa: F841 |
| 107 |
_LOGGER.info(" -> Success! The Wireless object is valid.") |
| 108 |
|
| 109 |
_LOGGER.info(" -> Checking list of Interface objects...") |
| 110 |
interfaces = data["interfaces"] |
| 111 |
for i, interface_data in enumerate(interfaces): |
| 112 |
_LOGGER.info(" -> Checking Interface object at index %s...", i) |
| 113 |
_LOGGER.info(" Interface should be %s.", interface_data["ifname"]) |
| 114 |
interface_obj: Interface | Interface6 |
| 115 |
if fw_major == 6: |
| 116 |
interface_obj = Interface6.from_dict(interface_data) |
| 117 |
else: |
| 118 |
interface_obj = Interface.from_dict(interface_data) # noqa: F841 |
| 119 |
_LOGGER.info(" Success! Interface is valid.") |
| 120 |
|
| 121 |
airos_data_obj: AirOS6Data | AirOS8Data |
| 122 |
if fw_major == 6: |
| 123 |
_LOGGER.info("Deriving AirOS6Data from object...") |
| 124 |
derived_data = AirOS6._derived_data_helper( # noqa: SLF001 |
| 125 |
data, |
| 126 |
AirOS6._derived_wireless_data, # noqa: SLF001 |
| 127 |
) |
| 128 |
_LOGGER.info("Attempting to deserialize full AirOS6Data object...") |
| 129 |
airos_data_obj = AirOS6Data.from_dict(derived_data) |
| 130 |
_LOGGER.info("Success! Full AirOS6Data object is valid.") |
| 131 |
else: |
| 132 |
_LOGGER.info("Deriving AirOS8Data from object...") |
| 133 |
derived_data = AirOS8._derived_data_helper( # noqa: SLF001 |
| 134 |
data, |
| 135 |
AirOS8._derived_wireless_data, # noqa: SLF001 |
| 136 |
) |
| 137 |
_LOGGER.info("Attempting to deserialize full AirOS8Data object...") |
| 138 |
airos_data_obj = AirOS8Data.from_dict(derived_data) |
| 139 |
_LOGGER.info("Success! Full AirOS8Data object is valid.") |
| 140 |
_LOGGER.info("SKU-check: %s", airos_data_obj.derived.sku) |
| 141 |
|
| 142 |
except Exception: |
| 143 |
_LOGGER.info("\n------------------") |
| 144 |
_LOGGER.info("CRITICAL ERROR FOUND!") |
| 145 |
_LOGGER.exception("The program failed") |
| 146 |
_LOGGER.info("------------------\n") |
| 147 |
|
| 148 |
# Reload the JSON data |
| 149 |
with open(sys.argv[1], encoding="utf-8") as f: # noqa: PTH123 |
| 150 |
data = json.loads(f.read()) |
| 151 |
|
| 152 |
_LOGGER.info("Adding derived data") |
| 153 |
if fw_major == 8: |
| 154 |
derived_data = AirOS8.derived_data(data) |
| 155 |
if fw_major == 6: |
| 156 |
derived_data = AirOS6.derived_data(data) |
| 157 |
_LOGGER.info("Full serialisation check: %s", derived_data) |
| 158 |
|
| 159 |
|
| 160 |
if __name__ == "__main__": |
| 161 |
main() |