-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpanel-json-to-csv.py
executable file
·46 lines (34 loc) · 1.09 KB
/
panel-json-to-csv.py
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
#!/usr/bin/env python3
""" panel-json-to-csv.py
Read a Envoy panel.json file and write a CSV version of it.
This can be helpful when assigning row/col labels for your panels such as what
is used by Envoy Logger.
"""
import csv
import json
from collections import OrderedDict
with open("panel.json", "r") as f:
panel_json = json.load(f)
config = {}
for module in panel_json["arrays"][0]["modules"]:
config[module["inverter"]["serial_num"]] = {
"tags": {
"module_id": module["module_id"],
"x": module["x"],
"y": module["y"],
"inverter_id": module["inverter"]["inverter_id"],
},
}
# Sort by x then y
# This returns a list of tuples for some reason
sorted_config = sorted(
config.items(), key=lambda i: (i[1]["tags"]["y"], i[1]["tags"]["x"])
)
config = OrderedDict()
for c in sorted_config:
config[c[0]] = c[1]
with open("panel.csv", "w") as f:
c = csv.writer(f)
c.writerow(["Serial", "X", "Y"])
for panel in config.items():
c.writerow([f"'{panel[0]}", panel[1]["tags"]["x"], panel[1]["tags"]["y"]])