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 _update_resource(self, active_resource_ids=[]): for res in resource.list_resource(status=True, gateway_id=self.gateway_id): res_id = res.get('id') # res_uuid = res.get('uuid') if res_id and res_id not in active_resource_ids: resource.update_resource(res_id, status=False)
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 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_attr(): """ Update sensor properties :return: uuid and status code """ data = request.json print data if "resource_id" not in data.keys() \ or "value" not in data.keys() \ or not data.get("resource_id") \ or not data.get("value") \ or not isinstance(data.get("value"), dict): abort(400) _compose_sensor_tag(data) updated_res = resource.update_resource(id=data['resource_id'], **data["value"]) return jsonify(resource_id=updated_res.get("id")), 200