Exemplo n.º 1
0
    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()
Exemplo n.º 2
0
 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()
Exemplo n.º 3
0
    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()
Exemplo n.º 4
0
 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()
Exemplo n.º 5
0
 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()
Exemplo n.º 6
0
 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()
Exemplo n.º 7
0
 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()
Exemplo n.º 8
0
 def __init__(self, database, host, user, password, objs, port=5672):
     logging.getLogger("pika").setLevel(logging.WARNING)
     logging.debug('starting to listen host {} user {} pass {}'.format(
         host, user, password))
     credentials = pika.PlainCredentials(user, password)
     self._connection = pika.BlockingConnection(
         pika.ConnectionParameters(host=host,
                                   port=port,
                                   credentials=credentials))
     logging.getLogger("pika").setLevel(logging.WARNING)
     self._channel = self._connection.channel()
     self._datamodel = SensorDataModel(database)
     self._channel.queue_declare(queue='sensor_data')
     self._channel.basic_consume(self.on_sensor_data_receive,
                                 queue='sensor_data',
                                 no_ack=True)
     self._objects_channel = objs
     self._objects_stub = objects_pb2_grpc.ObjectServiceStub(
         self._objects_channel)
Exemplo n.º 9
0
 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()
Exemplo n.º 10
0
    def GetControllerStat(self, request, context):
        stub = objects_pb2_grpc.ObjectServiceStub(self.objects)
        logging.info("some shitty log")
        controller_id = request.controller_id
        uu = utils_pb2.ControllerId(controller_id=controller_id)
        try:
            rsp = stub.GetControllerInfo(uu)
        except Exception as e: 
            logging.error("Error handling {}".format(str(e)))
            return NotFound("Not found error").get_message()

        uo = [i.id for i in rsp.sensors]
        curmnth_sm, prev_curmnth_sm, y_c_month_sm = 0, 0, 0
        for i in uo:
            curmnth, prev_curmnth, y_c_month = self.get_sensor_stats(i)
            curmnth_sm += curmnth
            prev_curmnth_sm += prev_curmnth
            y_c_month_sm += y_c_month
        return stats_pb2.ControllerStat(current_month = curmnth_sm,
                                    prev_year_month=prev_curmnth_sm,
                                    prev_year_average=y_c_month_sm)
Exemplo n.º 11
0
 def __init__(self, model, objs):
     self.__model = model
     self._objects_channel = objs
     self._objects_stub = objects_pb2_grpc.ObjectServiceStub(self._objects_channel)