forked from matter-js/python-matter-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbeautify_diagnostics.py
More file actions
94 lines (70 loc) · 2.87 KB
/
beautify_diagnostics.py
File metadata and controls
94 lines (70 loc) · 2.87 KB
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
"""Script to beautify diagnostics output."""
import json
import sys
from chip.clusters.ClusterObjects import ALL_ATTRIBUTES, ALL_CLUSTERS
import yaml
from matter_server.client.models.device_types import ALL_TYPES
def main():
"""Run the script."""
if len(sys.argv) != 2:
print("Usage: {} <diagnostics_file.json>".format(sys.argv[0]))
sys.exit(1)
with open(sys.argv[1]) as f:
data = json.load(f)
if "node" in data["data"]:
nodes = [data["data"]["node"]]
else:
nodes = data["data"]["server"]["nodes"]
for node in nodes:
process_node(node)
yaml.safe_dump(data, sys.stdout, indent=2, sort_keys=False)
def process_node(node):
"""Process a node."""
endpoints = {}
cluster_warn = set()
for attr_path, value in node["attributes"].items():
endpoint_id, cluster_id, attr_id = attr_path.split("/")
cluster_id = int(cluster_id)
endpoint_id = int(endpoint_id)
attr_id = int(attr_id)
if cluster_id in ALL_CLUSTERS:
cluster_name = f"{ALL_CLUSTERS[cluster_id].__name__} ({cluster_id} / 0x{cluster_id:04x})"
else:
if cluster_id not in cluster_warn:
print("Unknown cluster ID: {}".format(cluster_id))
cluster_warn.add(cluster_id)
cluster_name = f"{cluster_id} (unknown)"
if cluster_id in ALL_ATTRIBUTES and attr_id in ALL_ATTRIBUTES[cluster_id]:
attr_name = f"{ALL_ATTRIBUTES[cluster_id][attr_id].__name__} ({attr_id} / 0x{attr_id:04x})"
else:
if cluster_id not in cluster_warn:
print(
"Unknown attribute ID: {} in cluster {} ({})".format(
attr_id, cluster_name, cluster_id
)
)
attr_name = f"{attr_id} (unknown)"
if endpoint_id not in endpoints:
endpoints[endpoint_id] = {}
if cluster_name not in endpoints[endpoint_id]:
endpoints[endpoint_id][cluster_name] = {}
endpoints[endpoint_id][cluster_name][attr_name] = value
# Augment device types
for endpoint in endpoints.values():
if not (descriptor_cls := endpoint.get("Descriptor (29 / 0x001d)")):
continue
if not (device_types := descriptor_cls.get("DeviceTypeList (0 / 0x0000)")):
continue
for device_type in device_types:
device_type_id = device_type["deviceType"]
if device_type_id in ALL_TYPES:
device_type_name = ALL_TYPES[device_type_id].__name__
else:
device_type_name = f"{device_type} (unknown)"
device_type["name"] = device_type_name
device_type["hex"] = f"0x{device_type_id:04x}"
node["attributes"] = {
f"Endpoint {endpoint_id}": clusters
for endpoint_id, clusters in endpoints.items()
}
main()