forked from connectIOT/iottoolkit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCapsuleListenerLwm2m.py
428 lines (369 loc) · 17.5 KB
/
CapsuleListenerLwm2m.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
'''
Created on Sept 22, 2014
System Constructor for resource stub representing the Nespresso Capsule Type String
An agent listens on a socket and discovers the capsule type from the output of Barista
The Agent sets the resource value of 11101/0/5001 Capsule Type Resource to the current type
@author: mjkoster
'''
from core.RESTfulResource import RESTfulResource
from core.SmartObject import SmartObject
from rdflib.term import Literal, URIRef
from interfaces.HttpObjectService import HttpObjectService
from interfaces.CoapObjectService import CoapObjectService
from time import sleep
from urlparse import urlparse
import subprocess
import rdflib
import websocket
import json
#workaround to register rdf JSON plugins
from rdflib.plugin import Serializer, Parser
rdflib.plugin.register('json-ld', Serializer, 'rdflib_jsonld.serializer', 'JsonLDSerializer')
rdflib.plugin.register('json-ld', Parser, 'rdflib_jsonld.parser', 'JsonLDParser')
rdflib.plugin.register('rdf-json', Serializer, 'rdflib_rdfjson.rdfjson_serializer', 'RdfJsonSerializer')
rdflib.plugin.register('rdf-json', Parser, 'rdflib_rdfjson.rdfjson_parser', 'RdfJsonParser')
'''
model format for populating Description and creating SmartObject instances and service instances
'''
exampleConstructor = {
'service_metadata': {
'FQDN': '',
'IPV4': '',
'IPV6': ''
},
#replace with unique service URIs e.g. http://localhost:8000 when starting service instances
'services': {
'localHTTP' : {
'scheme': 'http',
'FQDN': 'localhost',
'port': 8000,
'IPV4': '',
'root': '/',
'discovery': '/'
},
'localCoAP': {
'scheme': 'coap',
'FQDN': 'localhost',
'port': 5683,
'IPV4': '',
'root': '/',
'discovery': '/'
}
},
'object_metadata': {
'objectPath': '',
},
'objects': {
'/': {
'resourceName': '/',
'resourceClass': 'SmartObject'
},
'/services': {
'resourceName': 'services',
'resourceClass': 'SmartObject'
},
'/11101': {
'resourceName': '11101',
'resourceClass': 'SmartObject' # LWM2M_Object
},
'/11101/0': {
'resourceName': '0',
'resourceClass': 'SmartObject' # LWM2M_Instance
},
'/11101/0/5001': {
'resourceName': '5001',
'resourceClass': 'ObservableProperty', # LWM2M_Resource
'resourceType': 'CapsuleID',
'interfaceType':'sensor',
'dataType':'string',
},
'/Agent/BLE_ColorLED_Handler': {
'resourceName': 'BLE_ColorLED_handler',
'resourceClass': 'BLE_ColorLED_handler',
'MACaddress': 'E0:DE:F3:62:42:D7',
'MACtype': 'random',
'charHandle': '0x000b'
},
'/11100': {
'resourceName': '11100',
'resourceClass': 'SmartObject' # LWM2M_Object
},
'/11100/0': {
'resourceName': '0',
'resourceClass': 'SmartObject' # LWM2M_Instance
},
'/11100/0/5900': {
'resourceName': '5900',
'resourceClass': 'ObservableProperty', # LWM2M_Resource
'resourceType': 'ColorLED',
'interfaceType':'actuator',
'dataType':'32_bit_hex_string_RRGGBBNN',
'publishesTo':['http://barista.cloudapp.net:8080/domain/endpoints/LED-booth-10-0-0-44/11100/0/5900?sync=true']
#'publishesTo':['http://192.168.1.200:8000/11100/0/5900']
#'handledBy': ['handler:///Agent/BLE_ColorLED_handler']
},
}
}
class SystemInstance(object):
'''
creates service instances and object instances from dictionary constructors
{
'service_metadata': {},
'services': {},
'object_metadata': {},
'objects': {}
}
'''
def __init__(self, systemConstructor):
self._service_metadata = systemConstructor['service_metadata']
self._services = systemConstructor['services']
self._object_metadata = systemConstructor['object_metadata']
self._objects = systemConstructor['objects']
self._baseObject = None
self._defaultResources = {
'SmartObject': ['Description', 'Agent'],
'ObservableProperty': ['Description', 'Observers']
}
self._observerTypes = ['subscribesTo', 'publishesTo', 'bridgesTo', 'handledBy']
self._observerSchemes = ['http', 'coap', 'mqtt', 'handler']
self._mqttObserverTemplate = {
'resourceName': 'mqttObserver',
'resourceClass': 'mqttObserver',
'connection': 'localhost',
'pubTopic': '',
'subTopic': '',
'keepAlive': 60,
'QoS': 0
}
self._httpPublisherTemplate = {
'resourceName': 'httpPublisher',
'resourceClass': 'httpPublisher',
'targetURI': 'http://localhost:8000/',
'username': 'admin',
'password': 'secret'
}
self._httpSubscriberTemplate = {
'resourceName': 'httpSubscriber',
'resourceClass': 'httpSubscriber',
'ObserverURI': 'http://localhost:8000/',
}
self._coapPublisherTemplate = {
'resourceName': 'coapPublisher',
'resourceClass': 'coapPublisher',
'targetURI': 'coap://localhost:5683/'
}
self._coapSubscriberTemplate = {
'resourceName': 'coapSubscriber',
'resourceClass': 'coapSubscriber',
'ObserverURI': 'coap://localhost:5683/'
}
self._callbackNotifierTemplate = {
'resourceName': 'callbackNotifier',
'resourceClass': 'callbackNotifier',
'handlerURI': 'handler://'
}
'''
make objects from object models first
make list sorted by path element count + length for import from graph,
could count a split list but this should be the same if we eat slashes somewhere
having the root object called '/' and '/' as the separator is extra work
'''
self._resourceList = sorted( self._objects.keys(), key=lambda s:s.count('/') )
self._resourceList = sorted( self._resourceList, key=lambda s:len(s))
for self._resourceLink in self._resourceList:
self._resourceDescriptor = self._objects[self._resourceLink]
# see if base object needs to be created.
if self._resourceLink is '/' and self._resourceDescriptor['resourceClass'] is 'SmartObject' and self._baseObject is None:
self._newResource = SmartObject()
self._baseObject = self._newResource
else:
self._parentLink = '/'.join(self._resourceLink.split('/')[:-1])
if self._parentLink == '': self._parentLink = '/'
self._parentObject = self._objectFromPath(self._parentLink, self._baseObject)
self._newResource = self._parentObject.create( self._resourceDescriptor)
if self._resourceDescriptor['resourceClass'] in self._defaultResources:
for self._defaultResource in self._defaultResources[self._resourceDescriptor['resourceClass']]:
self._newChildResource = self._newResource.create({
'resourceName': self._defaultResource,
'resourceClass': self._defaultResource
})
if self._defaultResource is 'Description':
self._newChildResource.create(self._graphFromModel(self._resourceLink, self._resourceDescriptor))
self._newResource.create({'resourceName':'.well-known', 'resourceClass':'Agent'})\
.create({'resourceName':'core', 'resourceClass':'LinkFormatProxy'})
# FIXME need to aggregate graphs upstream
# make observers from the list of URIs of each Observer type
for self._resourceProperty in self._resourceDescriptor:
if self._resourceProperty in self._observerTypes:
for self._observerURI in self._resourceDescriptor[self._resourceProperty]:
self._observerFromURI(self._newResource, self._resourceProperty, self._observerURI )
'''
make services
'''
# make this a service Object (RESTfulResource) with dict as constructor
self._serviceRegistry = self._objectFromPath('/services', self._baseObject)
self._serviceDescription = self._objectFromPath('/services/Description', self._baseObject)
for self._serviceName in self._services:
self._newService = ServiceObject(self._serviceName, self._services[self._serviceName], self._baseObject)
self._serviceRegistry.resources.update({self._serviceName:self._newService})
self._serviceDescription.set(self._graphFromModel(self._serviceName, self._services[self._serviceName]))
def _graphFromModel(self, link, meta):
# make rdf-json from the model and return RDF graph for loading into Description
g=rdflib.Graph()
subject=URIRef(link)
for relation in meta:
value = meta[relation]
g.add((subject, Literal(relation), Literal(value)))
return g
def _observerFromURI(self, currentResource, observerType, observerURI):
# split by scheme
URIObject=urlparse(observerURI)
# fill in constructor template
if URIObject.scheme == 'http':
if observerType is 'publishesTo':
resourceConstructor = self._httpPublisherTemplate.copy()
resourceConstructor['targetURI'] = observerURI
if observerType is 'subscribesTo':
resourceConstructor = self._httpSubscriberTemplate.copy()
resourceConstructor['observerURI'] = observerURI
elif URIObject.scheme == 'coap':
if observerType is 'publishesTo':
resourceConstructor = self._coapPublisherTemplate.copy()
resourceConstructor['targetURI'] = observerURI
if observerType is 'subscribesTo':
resourceConstructor = self._coapSubscriberTemplate.copy()
resourceConstructor['observerURI'] = observerURI
elif URIObject.scheme == 'mqtt':
resourceConstructor = self._mqttObserverTemplate.copy()
resourceConstructor['connection'] = URIObject.netloc
if observerType is 'publishesTo':
resourceConstructor['pubTopic'] = URIObject.path
if observerType is 'subscribesTo':
resourceConstructor['subTopic'] = URIObject.path
if observerType is 'bridgesTo':
resourceConstructor['pubTopic'] = URIObject.path
resourceConstructor['subTopic'] = URIObject.path
elif URIObject.scheme == 'handler':
resourceConstructor = self._callbackNotifierTemplate.copy()
resourceConstructor['handlerURI'] = observerURI
else:
print 'no scheme', URIObject.scheme
return
#create resource in currentResource.resources['Observers'] container
newObserver = currentResource.resources['Observers'].create(resourceConstructor)
def _objectFromPath(self, path, baseObject):
# fails if resource doesn't exist
currentObject=baseObject
pathList = path.split('/')[1:]
for pathElement in pathList:
if len(pathElement) > 0:
currentObject=currentObject.resources[pathElement]
return currentObject
class ServiceObject(RESTfulResource):
def __init__(self, serviceName, serviceConstructor, baseObject):
self._resourceConstructor = {
'resourceName': serviceName,
'resourceClass': serviceConstructor['scheme']
}
RESTfulResource.__init__(self, baseObject, self._resourceConstructor )
self._serviceConstructor = serviceConstructor
# TODO collect IP addresses and update the constructor
if self._serviceConstructor['scheme'] is 'http':
self._httpService = HttpObjectService\
(self._objectFromPath(self._serviceConstructor['root'], baseObject), port=self._serviceConstructor['port'])
URLObject= urlparse(self._httpService._baseObject.Properties.get('httpService'))
self._serviceConstructor['FQDN'] = URLObject.netloc.split(':')[0]
if self._serviceConstructor['scheme'] is 'coap':
self._coapService = CoapObjectService\
(self._objectFromPath(self._serviceConstructor['root'], baseObject), port=self._serviceConstructor['port'])
URLObject= urlparse(self._coapService._baseObject.Properties.get('coapService'))
self._serviceConstructor['FQDN'] = URLObject.netloc.split(':')[0]
if serviceConstructor['scheme'] is 'mqtt':
subprocess.call('mosquitto -d -p ', self._serviceConstructor['port'])
self._set(self._serviceConstructor)
def _objectFromPath(self, path, baseObject):
# fails if resource doesn't exist
currentObject=baseObject
pathList = path.split('/')[1:]
for pathElement in pathList:
if len(pathElement) > 0:
currentObject=currentObject.resources[pathElement]
return currentObject
if __name__ == '__main__' :
import httplib
import json
from urlparse import urlparse
import base64
httpServer = 'http://barista.cloudapp.net:8080'
httpPathBase = '/domain/endpoints'
basePath = httpServer + httpPathBase
username = 'admin'
password = 'secret'
auth = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
ep_names = []
def discoverEndpoints(basePath):
uriObject = urlparse(basePath)
httpConnection = httplib.HTTPConnection(uriObject.netloc)
httpConnection.request('GET', uriObject.path, headers= \
{"Accept" : "application/json", "Authorization": ("Basic %s" % auth) })
response = httpConnection.getresponse()
print response.status, response.reason
if response.status == 200:
endpoints = json.loads(response.read())
httpConnection.close()
return endpoints
def discoverLEDresources(endpoints):
for endpoint in endpoints:
if endpoint['type'] == 'mbed_device':
ep_names.append(endpoint['name'])
print ep_names
return ep_names
def actuateLEDs(ep_names,color):
jsonObject = json.dumps(color)
for ep in ep_names:
path = basePath + ep + '/11100/0/5900?sync=true'
uriObject = urlparse(path)
httpConnection = httplib.HTTPConnection(uriObject.netloc)
httpConnection.request('PUT', uriObject.path, jsonObject, \
{"Content-Type" : "application/json", "Authorization": ("Basic %s" % auth)})
response = httpConnection.getresponse()
print response.status, response.reason
httpConnection.close()
def capsule2color(capsuleType):
colorTable = {
'kazaar':'00005000',
'dharkan':'00404000',
'ristretto':'18181000',
'arpeggio':'20003000',
'roma':'30302000',
'livanto':'40100000',
'capriccio':'00300000',
'volluto':'50300000',
'decaffeinato_intenso':'30001800',
'vivalto_lungo':'20204000'
}
return colorTable[capsuleType]
def processPayload(payload):
payload = json.loads(payload)
if payload.has_key('currentCapsule'):
print payload['currentCapsule']
actuateLEDs(ep_names, capsule2color(payload['currentCapsule']))
#system._objectFromPath('/11101/0/5001', system._baseObject).set(payload['currentCapsule'])
#system._objectFromPath('/11100/0/5900', system._baseObject).set(capsule2color(payload['currentCapsule']))
"""
Start
"""
print "Started"
#system = SystemInstance(exampleConstructor)
ep_names = discoverLEDresources(discoverEndpoints(basePath))
ws = websocket.WebSocket()
ws.connect('ws://localhost:4001/ws')
#ws.connect('ws://barista.cloudapp.net:4001/ws')
print 'ws connected'
try:
while 1:
processPayload(ws.recv())
except KeyboardInterrupt: pass
print 'got KeyboardInterrupt'
ws.close()
print 'closed'