def update_sensor(): """ update sensor status Http PUT: { 'resource_id': '5', 'data': { 'value': false} } """ content = request.get_json(silent=True) resource_id = content.get('resource_id') data = content.get('data') if not resource_id or not isinstance(resource_id, int): abort(400) try: res = resource.get_resource(id=resource_id) except: abort(404) print "content: " + str(content) sensor = Sensor(uuid=res.get('uuid'), path=res.get('path'), gateway_id=session.get('gateway_id')) try: sts = sensor.update_status(data) return jsonify({'status': sts}), 201 except IoTRequestError: abort(500)
def update_resource(self, active_resource=[]): """ Pull resource and update their status in db :param: active_resource: tuple, (uuid, device_ type) """ active_resource_ids = [] for uuid, typ in active_resource: if typ not in self._sensor_type_map.keys(): self.log.error("Unsupported query path: {}.".format(typ)) continue try: ret = resource.get_resource(exception_when_missing=False, path=typ, uuid=uuid) except IoTConnectionError: continue if not ret: rs = resource.add_resource({ 'uuid': uuid, 'sensor_type_id': self._sensor_type_map[typ], 'gateway_id': self.gateway_id, 'status': True, 'path': typ, }) self.log.info('Resource {} is added.'.format(str(rs))) active_resource_ids.append(rs.get('id')) elif not ret.get('status'): # update resource status resource.update_resource(ret.get('id'), status=True) active_resource_ids.append(ret.get('id')) else: active_resource_ids.append(ret.get('id')) self._update_resource(active_resource_ids)
def run(self): try: # grabs urls of hosts and print data print("{}?di={}".format(self.href, self.dev_id)) # ret = self.client.get("{}?di={}".format(typ, dev_id), {'obs': 1}, stream=True) self.connect() res = resource.get_resource(exception_when_missing=False, path=self.href, uuid=self.dev_id, status=True) self.name = res.get('id') kargs = { 'sensor': res.get('sensor_type').get('mapping_class'), 'resource_id': res.get('id'), 'uuid': self.dev_id, 'gateway_id': self.gateway_id, } if self.obs: # observable self.client_conn.get_data(stream=True, callback=self.parse_data, **kargs) else: # not observable while True and not self.event.is_set(): data = self.client_conn.get_data() self.parse_data(data, **kargs) time.sleep(3) except IoTConnectionError as e: raise Exception("There was an error connecting to %s: %s : %r" % (self.dev_id, self.href, e))
def update_resource(self, active_resource=[]): """ Pull resource and update their status in db :param: active_resource: tuple, (uuid, href, resource_type, obs) """ active_resource_ids = [] for i, v in enumerate(active_resource): uuid, href, typ, obs = v[0], v[1], v[2], v[3] if typ not in self._sensor_type_map.keys(): typ = "generic" try: ret = resource.get_resource(exception_when_missing=False, path=href, uuid=uuid) except IoTConnectionError: continue if not ret: if typ == "generic" and uuid not in self.devices.keys(): self.devices = self._get_devices() self.log.info('Update device list: {}'.format( str(self.devices))) ret = resource.add_resource({ 'uuid': uuid, 'sensor_type_id': self._sensor_type_map[typ], 'gateway_id': self.gateway_id, 'status': True, 'path': href, 'tag': self.devices.get(uuid) if typ == "generic" and self.devices.get(uuid) else None, 'observable': obs }) self.log.info('Resource {} is added.'.format(str(ret))) active_resource_ids.append(ret.get('id')) elif not ret.get('status'): # update resource status resource.update_resource(ret.get('id'), status=True) active_resource_ids.append(ret.get('id')) else: active_resource_ids.append(ret.get('id')) active_resource[i] = v + (ret.get('id'), ) self._update_resource(active_resource_ids)
def _compose_sensor_tag(data): if "tag" in data["value"]: res = resource.get_resource(id=data["resource_id"]) sensor_type = res.get("sensor_type").get("mapping_class") if sensor_type == "environment": new_tag = data["value"]['tag'] tag = res.get("tag") sensor_type = data["type"] if sensor_type: sensor_type = sensor_type.replace(" ", "_") try: tag_dict = json.loads(tag) except: tag_dict = {} tag_dict.update({sensor_type: new_tag}) data["value"].update({"tag": json.dumps(tag_dict)})
def run(self): try: # grabs urls of hosts and print data typ = self.href dev_id = self.dev_id print "{}?di={}".format(typ, dev_id) # ret = self.client.get("{}?di={}".format(typ, dev_id), {'obs': 1}, stream=True) self.connect() res = resource.get_resource(exception_when_missing=False, path=typ, uuid=dev_id, status=True) kargs = { 'sensor': res.get('sensor_type').get('type'), 'uuid': self.dev_id, 'gateway_id': self.gateway_id } self.client_conn.get_data(stream=True, callback=self.parse_data, **kargs) except IoTConnectionError as e: raise Exception("There was an error connecting to %s: %r" % (dev_id, e))
def update_sensor(): """ update sensor status Http PUT: { 'href': '/a/fan', 'data': { 'value': false} } """ content = request.get_json(silent=True) path = content.get('href') data = content.get('data') path_list = ['/a/' + typ for typ in UPDATE_GRP] # validate put parameters if not content or not path or not data or path not in path_list: abort(400) print "content: " + str(content) res = resource.get_resource(path=path, gateway_id=session.get('gateway_id'), status=True) uuid = res.get('uuid') sensor = Sensor(uuid=uuid, path=path, username=session.get('username')) sts = sensor.update_status(data) return jsonify({'status': sts}), 201
def _compose_sensor_tag(data): if "tag" in data["value"]: res = resource.get_resource(id=data["resource_id"]) sensor_type = res.get("sensor_type").get("mapping_class") if sensor_type == "environment": new_tag = data["value"]['tag'] tag = res.get("tag") sensor_type = data["type"] if sensor_type: sensor_type = sensor_type.replace(" ", "_") try: tag_dict = json.loads(tag) except: tag_dict = {} tag_dict.update({ sensor_type: new_tag }) data["value"].update({ "tag": json.dumps(tag_dict) })
def update_resource(self, active_resource=[]): """ Pull resource and update their status in db :param: active_resource: tuple, (uuid, href, resource_type) """ active_resource_ids = [] for i, v in enumerate(active_resource): uuid, href, typ = v[0], v[1], v[2] if typ not in self._sensor_type_map.keys(): typ = "generic" try: ret = resource.get_resource(exception_when_missing=False, path=href, uuid=uuid) except IoTConnectionError: continue if not ret: if typ == "generic" and uuid not in self.devices.keys(): self.devices = self._get_devices() self.log.info('Update device list: {}'.format(str(self.devices))) ret = resource.add_resource({ 'uuid': uuid, 'sensor_type_id': self._sensor_type_map[typ], 'gateway_id': self.gateway_id, 'status': True, 'path': href, 'tag': self.devices.get(uuid) if self.devices.get(uuid) else None }) self.log.info('Resource {} is added.'.format(str(ret))) active_resource_ids.append(ret.get('id')) elif not ret.get('status'): # update resource status resource.update_resource(ret.get('id'), status=True) active_resource_ids.append(ret.get('id')) else: active_resource_ids.append(ret.get('id')) active_resource[i] = v + (ret.get('id'), ) self._update_resource(active_resource_ids)
def update_sensor(): """ update sensor status Http PUT: { 'resource_id': '5', 'data': { 'value': false} } """ content = request.get_json(silent=True) resource_id = content.get('resource_id') data = content.get('data') if not resource_id or not isinstance(resource_id, int): abort(400) try: res = resource.get_resource(id=resource_id) except: abort(404) print "content: " + str(content) sensor = Sensor(uuid=res.get('uuid'), path=res.get('path'), username=session.get('username')) try: sts = sensor.update_status(data) return jsonify({'status': sts}), 201 except IoTRequestError: abort(500)