-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtd-serialization-test.py
263 lines (207 loc) · 7.46 KB
/
td-serialization-test.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
from __future__ import division
from webthing import (Action, Property, MultipleThings, Thing, Value,
WebThingServer)
import logging
import time
import uuid
import json
from utils import setup_logging
setup_logging()
logger = logging.getLogger("tdtest")
"""
///////////////////////////////////////////////////////////////////////////////////
//// Wrapper for WebThing to enable custom serialize/deserialize operations ////
///////////////////////////////////////////////////////////////////////////////////
"""
TD_IRI = "https://www.w3.org/2019/wot/td#Thing"
class ThingWrapper(Thing):
def __init__(self, id_, title_, type_=TD_IRI, description_=''):
super(ThingWrapper, self).__init__(id_=id_, title=title_, type_=type_, description=description_)
def to_json_ld(self):
pass
def to_rdf(self):
pass
"""
//////////////////////////
//// HUE_LAMP CODE ////
//////////////////////////
"""
"""AVAILABLE ACTIONS"""
class OnAction(Action):
def __init__(self, thing, input_):
super(OnAction, self).__init__(uuid.uuid4().hex, thing, 'on', input_=input_)
def perform_action(self):
self.thing.set_property('on', True)
logger.info('ToggleAction performed')
class OffAction(Action):
def __init__(self, thing, input_):
super(OffAction, self).__init__(uuid.uuid4().hex, thing, 'off', input_=input_)
def perform_action(self):
self.thing.set_property('on', False)
logger.info('ToggleAction performed')
class ChangeColor(Action):
def __init__(self, thing, input_):
super(ChangeColor, self).__init__(uuid.uuid4().hex, thing, 'color', input_=input_)
def perform_action(self):
color = self.input['color']
logger.info('Color changed to: ' + color)
self.thing.set_property('color', color)
def make_hue_lamp_thing():
thing_id = "urn:dev:test:007-WoTLamp-1"
thing = ThingWrapper(id_=thing_id, title_="HueLampThing", type_=TD_IRI,
description_="Philips Hue Lamp in Lab 308")
thing.add_property(
Property(thing,
'on',
Value(False),
metadata={
'@type': 'OnOffProperty',
'title': 'On/Off',
'type': 'boolean',
'description': 'Whether the lamp is turned on',
}))
thing.add_property(
Property(thing,
'brightness',
Value(0),
metadata={
'@type': 'BrightnessProperty',
'title': 'Brightness',
'type': 'integer',
'description': 'The level of light from 0-100',
'minimum': 0,
'maximum': 100,
'unit': 'percent',
})
)
thing.add_property(
Property(thing,
'color',
Value('white'),
metadata={
'@type': 'Color',
'title': 'Light color',
'type': 'string',
'description': 'Lamp Color',
}))
# thing actions #
thing.add_available_action('on',
{
'title': 'OnAction',
'description': 'Turn the lamp on',
'metadata': {
'input': {
'type': 'None'
}
}
},
OnAction)
thing.add_available_action('off',
{
'title': 'OffAction',
'description': 'Turn the lamp off',
'metadata': {
'input': {
'type': 'None'
}
}
},
OffAction)
thing.add_available_action('color',
{
'title': 'ChangeColor',
'description': 'Change hue_light color',
'metadata': {
'input': {
'type': 'object',
'required': ['color']
}
}
},
ChangeColor)
return thing
"""
//////////////////////////
//// BLINDS CODE ////
//////////////////////////
"""
class BlindsUp(Action):
def __init__(self, thing, input_):
super(BlindsUp, self).__init__(uuid.uuid4().hex, thing, 'blinds_up', input_=input_)
def perform_action(self):
time.sleep(1)
logger.info('Se ridica jaluzelele')
class BlindsDown(Action):
def __init__(self, thing, input_):
super(BlindsDown, self).__init__(uuid.uuid4().hex, thing, 'blinds_down', input_=input_)
def perform_action(self):
time.sleep(1)
logger.info('Se coboara jaluzelele')
class BlindsStop(Action):
def __init__(self, thing, input_):
super(BlindsStop, self).__init__(uuid.uuid4().hex, thing, 'blinds_stop', input_=input_)
def perform_action(self):
time.sleep(1)
logger.info('Se opresc jaluzelele')
def set_blind_actions(thing: ThingWrapper):
thing.add_available_action(
'blinds_up',
{
'title': 'Blinds up',
'description': 'Rise the blinds',
'metadata': {
'input': 'None'
}
},
BlindsUp
)
thing.add_available_action(
'blinds_down',
{
'title': 'Blinds down',
'description': 'Lower the blinds',
'metadata': {
'input': 'None'
}
},
BlindsDown
)
thing.add_available_action(
'blinds_stop',
{
'title': 'Blinds stop',
'description': 'Stop the blinds',
'metadata': {
'input': 'None'
}
},
BlindsStop
)
return thing
def make_blinds1_thing():
thing_id = "urn:dev:test:007-WoTBlinds-1"
thing = ThingWrapper(id_=thing_id, title_="BlindsThing-1", type_=TD_IRI,
description_="South facing blinds in lab 308")
thing = set_blind_actions(thing)
return thing
def make_blinds2_thing():
thing_id = "urn:dev:test:007-WoTBlinds-2"
thing = ThingWrapper(id_=thing_id, title_="BlindsThing-2", type_=TD_IRI,
description_="East facing blinds in lab 308")
thing = set_blind_actions(thing)
return thing
def run_multiple_things_server():
hue_lamp = make_hue_lamp_thing()
blinds1 = make_blinds1_thing()
blinds2 = make_blinds2_thing()
things = [hue_lamp, blinds1, blinds2]
server = WebThingServer(MultipleThings(things, 'AI-MAS LAB'), port=8888)
try:
logger.info('starting the server')
server.start()
except KeyboardInterrupt:
logger.info('stopping the server')
server.stop()
logger.info('done')
if __name__ == '__main__':
run_multiple_things_server()