def __init__( self, baseObject=None, port=None ): # FIXME if no baseObject given, create a default base object if port == None: self._port = 5683 # IETF default port for coap:// else: self._port = port if baseObject == None: from iottoolkit.core.SmartObject import SmartObject self._baseObject = SmartObject() else: self._baseObject = baseObject self.resources = self._baseObject.resources self._host = gethostname() self._baseObject.Properties.update( {'coapService': 'coap://' + self._host + ':' + repr(self._port)}) self._coapHandler = CoapRequestHandler(self._baseObject) self._coapServer = COAPServer(self._host, self._port, self._coapHandler)
def __init__(self, baseObject=None, port=None): if port == None: self._port = 8000 else: self._port = port # default port 8000 if baseObject == None: from iottoolkit.core.SmartObject import SmartObject self._baseObject = SmartObject() else: self._baseObject = baseObject self.resources = self._baseObject.resources self.start(self._port)
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/", } 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]))
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/", } 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
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/' } 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]))
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/' } 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