示例#1
0
    def create_sensor(self, sensor):
        sdata: MQTTMessage = sensor.current_data
        # self.logg.log(sdata.__dict__)
        if not sdata:
            return None

        self.logg.log("create sensor for topic: " + sensor.topic_name +
                      " (code " + str(sensor.topic_code) + ")")
        self.cursor.execute(
            'select * from topic where name=%s', (sensor.topic_name,))
        topic = self.cursor.fetchone()
        self.logg.log(
            "topic[" + str(sensor.topic_name) + "]: " + str(topic))
        # self.logg.log(topic["id"])
        sensor.id = Utils.get_sensor_id_encoding(
            sensor.raw_id, topic["code"])
        sql = "INSERT INTO sensor (sensor_id, log_rate, topic_code, timestamp) VALUES (%s, %s, %s, %s)"
        sensor.log_rate = topic["log_rate"]
        sensor.topic_code = topic["code"]

        ts = datetime.now()

        self.logg.log("sensor: " + str(sensor.__dict__))
        params = (sensor.id, sensor.log_rate, topic["code"], ts)
        self.logg.log(sql + str(params))
        self.cursor.execute(sql, params)
        # commit the changes to the database
        self.connection.commit()
        # close communication with the database
        # self.cursor.close()
        return sensor
示例#2
0
    def update_sensor_data(self, raw_id, d1: MQTTMessage):
        ts = time.time()
        found = False
        s1: Sensor = Sensor()

        if d1.data is None:
            return

        # remove heading
        data = d1.data
        if d1.data[0] == "data":
            data = d1.data[1:]

        d1.data = data

        try:
            for s in self.sensors:
                s1 = s
                if s1.id == Utils.get_sensor_id_encoding(
                        raw_id, self.get_topic_code(d1.topic)):
                    found = True
                    # self.logg.log("found / " + d1.topic + ": " + str(s1.id) + " raw id: " + str(raw_id) + " topic code: " + str(s1.topic_code))
                    break

            if found:
                # print(d1)
                # for real time monitor
                # self.logg.log("update sensor/" + d1.topic)

                s1.current_data = data

                # handle sample and db log
                # only add to data buffer if enough time has passed since last recorded message
                # ts = 0 => log all incoming messages
                if (s1.ts == 0) or (ts - s1.ts >= s1.log_rate):
                    # self.logg.log("sample")
                    s1.ts = ts
                    s1.data_buffer.append(d1)

                # handle dump to db
                if ts - s1.log_ts >= self.default_log_rate:
                    self.logg.log("log db")
                    s1.log_ts = ts
                    # check if data in buffer
                    if len(s1.data_buffer) > 0:
                        self.log_sensor_data(s1)
                        s1.data_buffer = []
            else:
                # sensor is not defined in the db, save and use defaults
                # assign to the topic (should be defined)
                self.logg.log("create sensor")
                s1.current_data = d1
                s1.raw_id = raw_id
                s1.type = d1.type
                s1.log_rate = self.default_log_rate
                s1.ts = ts
                s1.log_ts = ts
                s1.topic_name = d1.topic

                # write to db
                s1 = self.create_sensor(s1)
                # topic code is now assigned
                # if s1 is not None:
                # add to list anyways (the sensor may already be registered and db returns error)
                self.logg.log("new sensor: " + str(s1.__dict__))
                self.sensors.append(s1)

        except:
            self.logg.log(
                Utils.format_exception(self.__class__.__name__) +
                " at message: " + str(d1.__dict__))