-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththingDescriptionCollection.py
More file actions
120 lines (86 loc) · 3.79 KB
/
thingDescriptionCollection.py
File metadata and controls
120 lines (86 loc) · 3.79 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import copy
class ThingDescriptionCollection:
tds = None
def __init__(self, tds: list[dict]):
self.tds = tds
def selectElement(self, type: str, name: str, id: str | None = None) -> dict | None:
for td in self.tds:
if (type in td) and (name in td[type]) and (id is None or td["id"] == id):
return td[type][name]
def selectTD(self, type: str, name: str, id: str | None = None) -> dict | None:
for td in self.tds:
if (name in td[type]) and (id is None or td["id"] == id):
return td
def getActionInput(self, actionName: str, id: str | None = None) -> dict | None:
for td in self.tds:
if "actions" not in td:
continue
if id is not None and td["id"] != id:
continue
for actName, actValue in td["actions"].items():
if actName != actionName:
continue
if "input" in actValue:
return copy.deepcopy(actValue["input"])
else:
return {}
def getEventData(self, eventName: str, id: str | None = None) -> dict | None:
for td in self.tds:
#print(td)
if "events" not in td:
continue
if id is not None and td["id"] != id:
continue
for evtName, evtValue in td["events"].items():
if evtName != eventName:
continue
if "data" not in evtValue:
return {}
if "description" in evtValue["data"]:
del evtValue["data"]["description"]
if "unit" in evtValue["data"]:
del evtValue["data"]["unit"]
if "enum" in evtValue["data"]:
del evtValue["data"]["enum"]
if "properties" not in evtValue["data"]:
return evtValue["data"]
for property in evtValue["data"]["properties"].values():
if "description" in property:
del property["description"]
if "unit" in property:
del property["unit"]
if "enum" in property:
del property["enum"]
return copy.deepcopy(evtValue["data"])
def getActionOutput(self, actionName: str, id: str | None = None) -> dict | None:
for td in self.tds:
if "actions" not in td:
continue
if id is not None and td["id"] != id:
continue
for actName, actValue in td["actions"].items():
if actName != actionName:
continue
if "output" not in actValue:
return {}
if "description" in actValue["output"]:
del actValue["output"]["description"]
if "enum" in actValue["output"]:
del actValue["output"]["enum"]
return copy.deepcopy(actValue["output"])
return None
def getPropertyValue(self, propertyName: str, id: str | None = None) -> dict | None:
for td in self.tds:
if "properties" not in td:
continue
if id is not None and td["id"] != id:
continue
for propName, propValue in td["properties"].items():
if propName != propertyName:
continue
propertyReturn = {}
propertyReturn["type"] = propValue["type"]
if "properties" in propValue:
propertyReturn["properties"] = propValue["properties"]
return copy.deepcopy(propertyReturn)
return None