def post(self, controller_id, token): data = request.get_json() data_cleaned = controller_activate_schema.load(data) data_cleaned = data_cleaned.data stub = objects_pb2_grpc.ObjectServiceStub(self.object) # { # "id": # "name": # "meta": # "object_id": # } object_id = utils_pb2.ObjectId(object_id=data_cleaned["object_id"]) controllerid = utils_pb2.ControllerId(controller_id=controller_id) uc = objects_pb2.ControllerActivate( id=controllerid, name=data_cleaned["name"], meta=data_cleaned["meta"], object_id=object_id, ) try: rsp = stub.ActivateController(uc) except Exception as e: log.error("Error handling {}".format(str(e))) return NotFound("Not found error").get_message() rspr = Relations.collect_controller_info(rsp) sensors = Relations.collect_controller_relations( rsp, self.data_chan, self.stats_chan) sensors = Listed(sensors) kwargs = dict(controllers=Listed([ rspr, ]), sensors=sensors) final = ObjList(**kwargs) return final.get_message()
def get(self, object_id, token): stub = objects_pb2_grpc.ObjectServiceStub(self.object) log.info("some shitty log") uu = utils_pb2.ObjectId(object_id=object_id) try: rsp = stub.GetObjectInfo(uu) except Exception as e: log.error("Error handling {}".format(str(e))) return NotFound("Not found error").get_message() def controller(cntrlr): def sensor(ssr): rs = SensorInfo(ssr.id.sensor_id, ssr.name, None, None, ssr.sensor_type, ssr.company) if ssr.HasField("deactivation_date_val"): rs.deactivation_date = ssr.deactivation_date_val if ssr.HasField("activation_date_val"): rs.activation_date = ssr.activation_date_val return rs ctr = ControllerInfo(cntrlr.id.controller_id, cntrlr.name, cntrlr.meta, None, cntrlr.status, cntrlr.mac, cntrlr.controller_type, None, [sensor(i) for i in cntrlr.sensors]) if cntrlr.HasField("deactivation_date_val"): ctr.deactivation_date = cntrlr.deactivation_date_val if cntrlr.HasField("activation_date_val"): ctr.activation_date = cntrlr.activation_date_val return ctr uo = Listed([controller(i) for i in rsp.controllers]) return uo.get_message()
def get(self, token): stub = objects_pb2_grpc.ObjectServiceStub(self.object) include = request.args.getlist("include") log.info("some shitty log {}".format(include)) uu = utils_pb2.UserId(user_id=1) try: rsp = stub.GetUsersInfo(uu) except Exception as e: log.error("Error handling {}".format(str(e))) return NotFound("Not found error").get_message() objects, controllers, sensors = Relations.collect_user_relations( rsp, self.data_chan, self.stats_chan) objects = Listed(objects) controllers = Listed(controllers) sensors = Listed(sensors) kwargs = {} if include: if 'objects' in include: kwargs['objects'] = objects if 'controllers' in include: kwargs['controllers'] = controllers if 'sensors' in include: kwargs['sensors'] = sensors else: kwargs = dict(objects=objects, controllers=controllers, sensors=sensors) return ObjList(**kwargs).get_message()
def delete(self, _id, token): stub = objects_pb2_grpc.ObjectServiceStub(self.object) uc = utils_pb2.ObjectId(object_id=_id, ) try: stub.DeleteObject(uc) except Exception as e: log.error("Error handling {}".format(str(e))) return NotFound("Not found error").get_message() return DeleteOk("Controller deleted").get_message()
def delete(self, controller_id, token): stub = objects_pb2_grpc.ObjectServiceStub(self.object) uc = utils_pb2.ControllerId(controller_id=controller_id, ) try: rsp = stub.DeactivateController(uc) except Exception as e: log.error("Error handling {}".format(str(e))) return NotFound("Not found error").get_message() rsp = Relations.collect_controller_info(rsp) return rsp.get_message()
def post(self, token): data = request.get_json() data_cleaned = controller_create_schema.load(data) data_cleaned = data_cleaned.data stub = objects_pb2_grpc.ObjectServiceStub(self.object) uc = objects_pb2.ControllerCreate( mac=data_cleaned["mac"], controller_type=data_cleaned["controller_type"], ) try: rsp = stub.CreateController(uc) except Exception as e: log.error("Error handling {}".format(str(e))) return NotFound("Not found error").get_message() rsp = Relations.collect_controller_info(rsp) return rsp.get_message()
def post(self, token): data = request.get_json() data_cleaned = object_create_schema.load(data) data_cleaned = data_cleaned.data stub = objects_pb2_grpc.ObjectServiceStub(self.object) uc = objects_pb2.ObjectCreate( name=data_cleaned["name"], address=data_cleaned["address"], ) try: rsp = stub.CreateObject(uc) except Exception as e: log.error("Error handling {}".format(str(e))) return NotFound("Not found error").get_message() rsp = Relations.collect_object_info(rsp) return rsp.get_message()
def post(self, token): data = request.get_json() data_cleaned = sensor_create_schema.load(data) data_cleaned = data_cleaned.data stub = objects_pb2_grpc.ObjectServiceStub(self.object) uc = objects_pb2.SensorCreate( date=datetime.datetime.now().strftime("%Y-%m-%d"), sensor_type=data_cleaned["sensor_type"], name=data_cleaned["name"], company=data_cleaned["company"], controller_id=data_cleaned["controller_id"] ) try: rsp = stub.CreateSensor(uc) except Exception as e: log.error("Error handling {}".format(str(e))) return NotFound("Not found error").get_message() rsp = Relations.collect_sensor_info(rsp, self.data_chan, self.stats_chan) return rsp.get_message()