def connect(self, gateway_id): """ Connect to IoT web service """ if self._client is None: self._client = IoTClient(gateway_id, proxies=config.get_all_proxy())
class Sensor(object): _client = None _object_map = { '/a/pir': None, '/a/gas': None, '/a/fan': None, '/a/solar': None, '/a/illuminance': None, '/a/led': None, '/a/rgbled': None, '/a/buzzer': None, '/a/temperature': None, '/a/button': None, '/a/power': None } def __init__(self, uuid, path, username): self.path = path self.id = uuid self.resp = None if self.path not in self._object_map.keys(): raise Exception("Unsupported query path: {}. ". format(self.path)) self.connect(username) def connect(self, username): """ Connect to IoT web service """ if self._client is None: self._client = IoTClient(username, proxies=config.get_all_proxy()) def get_data(self, stream=False, callback=None, **kargs): # obj = self._object_map[self.path] data = {'obs': 1} if stream else {} uri = "{}?di={}".format(self.path, self.id) self.resp = self._client.get(uri, data=data, stream=stream) if stream: self.resp.get_data(callback, **kargs) else: return self.resp.content if self.resp.ok() else None def terminate(self): self.resp.close() def update_status(self, data): ret = False uri = "{}?di={}".format(self.path, self.id) if isinstance(data, dict): self.resp = self._client.put(uri, json.dumps(data)) if self.resp.ok(): # print "The response status is " + str(self.resp.status_code) ret = True else: print('Failed to update {} status: {}'.format(uri, str(self.resp.errors()))) return ret
class Sensor(object): _client = None def __init__(self, uuid, path, username): self.path = path self.id = uuid # self.resource_type = resource_type.strip() if resource_type else None self.resp = None # self._object_map = Sensor.get_sensor_types_map() # if self.resource_type not in self._object_map: # raise Exception("(Sensor): Unsupported resource type: {}. ". format(self.path)) self.connect(username) # @staticmethod # def get_sensor_types_map(): # types = sensor_type.get_all_types() # mapping = [t_dict['type'] for t_dict in types] # return mapping def connect(self, username): """ Connect to IoT web service """ if self._client is None: self._client = IoTClient(username, proxies=config.get_all_proxy()) def get_data(self, stream=False, callback=None, **kargs): data = {'obs': 1} if stream else {} uri = "{}?di={}".format(self.path, self.id) self.resp = self._client.get(uri, data=data, stream=stream) if stream: self.resp.get_data(callback, **kargs) else: return self.resp.content if self.resp.ok() else None def terminate(self): if self.resp: self.resp.close() def update_status(self, data): ret = False uri = "{}?di={}".format(self.path, self.id) if isinstance(data, dict): self.resp = self._client.post(uri, json.dumps(data)) # print "########POST data " + json.dumps(data) if self.resp.ok(): # print "The response status is " + str(self.resp.status_code) ret = True else: print('Failed to update {} status: {}'.format( uri, str(self.resp.errors()))) raise IoTRequestError(self.resp.status_code) return ret
class Sensor(object): _client = None def __init__(self, uuid, path, username): self.path = path self.id = uuid # self.resource_type = resource_type.strip() if resource_type else None self.resp = None # self._object_map = Sensor.get_sensor_types_map() # if self.resource_type not in self._object_map: # raise Exception("(Sensor): Unsupported resource type: {}. ". format(self.path)) self.connect(username) # @staticmethod # def get_sensor_types_map(): # types = sensor_type.get_all_types() # mapping = [t_dict['type'] for t_dict in types] # return mapping def connect(self, username): """ Connect to IoT web service """ if self._client is None: self._client = IoTClient(username, proxies=config.get_all_proxy()) def get_data(self, stream=False, callback=None, **kargs): data = {'obs': 1} if stream else {} uri = "{}?di={}".format(self.path, self.id) self.resp = self._client.get(uri, data=data, stream=stream) if stream: self.resp.get_data(callback, **kargs) else: return self.resp.content if self.resp.ok() else None def terminate(self): if self.resp: self.resp.close() def update_status(self, data): ret = False uri = "{}?di={}".format(self.path, self.id) if isinstance(data, dict): self.resp = self._client.post(uri, json.dumps(data)) # print "########POST data " + json.dumps(data) if self.resp.ok(): # print "The response status is " + str(self.resp.status_code) ret = True else: print('Failed to update {} status: {}'.format(uri, str(self.resp.errors()))) raise IoTRequestError(self.resp.status_code) return ret
def __init__(self, username, href, dev_id): threading.Thread.__init__(self) self.username = username self.gateway_id = IoTClient.get_gatewayid_by_user(username) self.href = href self.dev_id = dev_id self.name = dev_id self.client_conn = None self.start()
def __init__(self, username, resource_info): threading.Thread.__init__(self) self.username = username self.gateway_id = IoTClient.get_gatewayid_by_user(username) self.href = resource_info[0] self.dev_id = resource_info[1] self.name = resource_info[3] self.resource_type = resource_info[2] self.client_conn = None self.start()
class Resource(object): _client = None def __init__(self, username): self.connect(username) def connect(self, username): """ Connect to IoT web service """ if self._client is None: self._client = IoTClient(username, proxies=config.get_all_proxy()) def list_resource(self): """ Return all the resources discovered in the network """ result = self._client.get('/res') sensors = [] if result.ok(): for item in result: """ sample response in json [ { "di": "d38d75a3-f1be-4d2d-b084-e943005fbf8d", "links": [ { "href": "/a/gas", "rt": "o", "if": "o" } ] } ] """ try: href = str(item['links'][0]['href']) except: href = None if href and href.startswith("/a"): sensors.append((item['di'], href)) else: logging.error("Failed to get resources: {}".format( str(result.errors()))) return sensors
class Resource(object): _client = None def __init__(self, username): self.connect(username) def connect(self, username): """ Connect to IoT web service """ if self._client is None: self._client = IoTClient(username, proxies=config.get_all_proxy()) def list_resource(self): """ Return all the resources discovered in the network """ result = self._client.get('/res') sensors = [] if result.ok(): for item in result: """ sample response in json [ { "di": "d38d75a3-f1be-4d2d-b084-e943005fbf8d", "links": [ { "href": "/a/gas", "rt": "o", "if": "o" } ] } ] """ try: href = str(item['links'][0]['href']) except: href = None if href and href.startswith("/a"): sensors.append((item['di'], href)) else: logging.error("Failed to get resources: {}".format(str(result.errors()))) return sensors
def __init__(self, username): self._client = None self.username = username self.gateway_id = IoTClient.get_gatewayid_by_user(username)
def __init__(self, username): self._client = None self._sensor_type_map = DataManager.get_sensor_types_map() self.username = username self.gateway_id = IoTClient.get_gatewayid_by_user(username) self.devices = dict()
def connect(self, username): """ Connect to IoT web service """ if self._client is None: self._client = IoTClient(username, proxies=config.get_all_proxy())
class Resource(object): _client = None def __init__(self, username): self.connect(username) def connect(self, username): """ Connect to IoT web service """ if self._client is None: self._client = IoTClient(username, proxies=config.get_all_proxy()) def list_resource(self): """ Return all the resources discovered in the network """ result = self._client.get('/res') sensors = [] if result.ok(): for item in result: """ sample response in json [ { "di": "d38d75a3-f1be-4d2d-b084-e943005fbf8d", "links": [ { "href": "/a/fan", "rt": "oic.r.fan", "if": "o" } ] } ] """ try: href = str(item['links'][0]['href']) rt = str(item['links'][0]['rt']) if rt and not rt.startswith("oic.wk") and href \ and (href.startswith("/a/") or href.startswith("/brillo/")): sensors.append((item['di'], href, rt)) else: # ignore the wrong or unregistered json types pass except: logging.error("Failed to get resources: index out of range. ") else: logging.error("Failed to get resources: {}".format(str(result.errors()))) return sensors def list_device(self): """ Return all the devices discovered in the network """ result = self._client.get('/d') devices = dict() if result.ok(): for item in result: """ sample response in json [{ di: "de356706-13cf-49b5-a023-f1f45079ff72", n: "Smart Home Fan", icv: "core.1.1.0" },] """ try: device_id = str(item['di']) device_name = str(item['n']) devices[device_id] = device_name except: logging.error("Failed to get device: index out of range. ") else: logging.error("Failed to get devices: {}".format(str(result.errors()))) return devices
class Resource(object): _client = None def __init__(self, gateway_id): self.connect(gateway_id) def connect(self, gateway_id): """ Connect to IoT web service """ if self._client is None: self._client = IoTClient(gateway_id, proxies=config.get_all_proxy()) def list_resource(self): """ Return all the resources discovered in the network """ result = self._client.get('/res') sensors = [] if result.ok(): for item in result: """ sample response in json [ { "di": "d38d75a3-f1be-4d2d-b084-e943005fbf8d", "links": [ { "href": "/a/fan", "rt": "oic.r.fan", "if": "o", "p": { "bm": 3, "secure": false } } ] } ] """ try: href = str(item['links'][0]['href']) rt = str(item['links'][0]['rt']) # by default, obs is True observable = True # check whether policy param is supported if item['links'][0].get('p'): bm = item['links'][0]['p']['bm'] secure = item['links'][0]['p']['secure'] if bm == 1 and not secure: observable = False elif bm == 3 and not secure: observable = True if rt and not href.startswith("/oic/") and \ not href.startswith("/introspection"): sensors.append((item['di'], href, rt, observable)) else: # ignore the wrong or unregistered json types pass except: logging.error("Failed to get resources: index out of range. ") else: logging.error("Failed to get resources: {}".format(str(result.errors()))) return sensors def list_device(self): """ Return all the devices discovered in the network """ result = self._client.get('/d') devices = dict() if result.ok(): for item in result: """ sample response in json [{ di: "de356706-13cf-49b5-a023-f1f45079ff72", n: "Smart Home Fan", icv: "core.1.1.0" },] """ try: device_id = str(item['di']) device_name = str(item['n']) devices[device_id] = device_name except: logging.error("Failed to get device: index out of range. ") else: logging.error("Failed to get devices: {}".format(str(result.errors()))) return devices
class Resource(object): _client = None def __init__(self, username): self.connect(username) def connect(self, username): """ Connect to IoT web service """ if self._client is None: self._client = IoTClient(username, proxies=config.get_all_proxy()) def list_resource(self): """ Return all the resources discovered in the network """ result = self._client.get('/res') sensors = [] if result.ok(): for item in result: """ sample response in json [ { "di": "d38d75a3-f1be-4d2d-b084-e943005fbf8d", "links": [ { "href": "/a/fan", "rt": "oic.r.fan", "if": "o" } ] } ] """ try: href = str(item['links'][0]['href']) rt = str(item['links'][0]['rt']) if rt and not rt.startswith("oic.wk") and href \ and (href.startswith("/a/") or href.startswith("/brillo/")): sensors.append((item['di'], href, rt)) else: # ignore the wrong or unregistered json types pass except: logging.error( "Failed to get resources: index out of range. ") else: logging.error("Failed to get resources: {}".format( str(result.errors()))) return sensors def list_device(self): """ Return all the devices discovered in the network """ result = self._client.get('/d') devices = dict() if result.ok(): for item in result: """ sample response in json [{ di: "de356706-13cf-49b5-a023-f1f45079ff72", n: "Smart Home Fan", icv: "core.1.1.0" },] """ try: device_id = str(item['di']) device_name = str(item['n']) devices[device_id] = device_name except: logging.error("Failed to get device: index out of range. ") else: logging.error("Failed to get devices: {}".format( str(result.errors()))) return devices