Repository navigation
Expand file tree
/
Copy pathserialize.py
More file actions
79 lines (65 loc) · 2.26 KB
/
Copy pathserialize.py
File metadata and controls
79 lines (65 loc) · 2.26 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
"""Serialization routines for the Simple Register Lookup project.
Serialization routines to support the web-based Simple Register Lookup project
for LabJack MODBUS devices.
@author Sam Pottinger
@license GNU GPL v2
"""
import copy
DEVICE_MODBUS_MAP_COLS = [
# Regular columns
"name", "address", "type", "access", "tags",
# detail columns
"description", "default", "streamable", "isBuffer", "devices", "constants", "altnames", "usesRAM"
]
def serialize_device_modbus_map(target, cols=DEVICE_MODBUS_MAP_COLS):
"""Serializes the given device modbus map to a list of lists.
This will, for example, take:
{
"name": str,
"address": int,
"type": str,
"devices": [
{
"device": str,
"fwmin": float
"description": str,
"default": int / float
}
],
"read": bool,
"write": bool,
"tags": list of str,
}
and produce
[str, int, str, int, float / int, str, csv str]
according to DEVICE_MODBUS_MAP_COLS
@param target: The device modbus map to serialize in form described in
ljmmm.get_device_modbus_maps
@type target: list of dict
@return: List of lists with the first list being a header.
@rtype: list of list
"""
ret_list = [cols]
for entry in target:
entry = copy.deepcopy(entry)
entry["tags"] = ", ".join(entry["tags"])
# Serialize read / write values from (bool, bool) to "R", "RW", or "W"
read_write_str = ""
if entry["readwrite"]["read"] and entry["readwrite"]["write"]:
read_write_str = "R / W"
elif entry["readwrite"]["read"]:
read_write_str = "R"
elif entry["readwrite"]["write"]:
read_write_str = "W"
entry["access"] = read_write_str
entry["default"] = entry.get("default", "")
if len(entry.get("altnames", [])):
entry["name"] = '%s (also known as: %s)' % (
entry["name"],
", ".join(entry["altnames"])
)
else:
entry["altnames"] = []
serialized_entry = [entry[x] if x in entry else None for x in cols]
ret_list.append(serialized_entry)
return ret_list