Beispiel #1
0
    def insert_data(self, data, mesh=True, prediction=True):
        """ dict of data:
          'sensor': object of sensor
          'value': value - float
          'plant': current selected plant
    """
        start = datetime.datetime.now()
        current_entries = SensorData.select()\
                                    .where(SensorData.sensor == data['sensor'])\
                                    .count()

        persistant = False
        data['value'] = round(data['value'], 2)

        sensor_db = SensorData()
        sensor_db.value = data['value']
        sensor_db.plant = data['plant']
        sensor_db.sensor = data['sensor']
        sensor_db.persistant = False
        sensor_db.save()

        last_entry = self.get_second_last_entry(sensor_db, data['plant'])
        last_value = last_entry.value if last_entry is not None else data[
            'value']

        offset = abs(data['value'] - last_value)
        if offset >= data['sensor'].persistant_offset:
            persistant = True
        elif current_entries > 6:
            persistant = self.persistant_evaluation(data['plant'],
                                                    data['sensor'])

        sensor_db.persistant = persistant
        sensor_db.save()

        self.delete_non_persistant_overflow(data['sensor'], data['plant'])
        logger.debug('{} - {} persistant: {}'.format(data['plant'].name,
                                                     data['sensor'].name,
                                                     persistant))

        if persistant:
            data['satisfaction'] = self.modify_sensor_status(data, mesh)
            self.mail_evaluation(data)

            if prediction:
                SensorDataForecast().run(data)

            if mesh:
                from mesh_network.dedicated import MeshDedicatedDispatch
                MeshDedicatedDispatch().new_data(data['sensor'],
                                                 plant=data['plant'])

                if data['plant'].localhost:
                    print('slave data')
                    slaves = Plant.select().where(
                        Plant.role == str(data['plant'].uuid))
                    slaves = list(slaves)

                    for slave in slaves:
                        print('slaved')
                        MeshDedicatedDispatch().slave_data(
                            slave, data['sensor'])

        logger.debug('time elapsed: {}'.format(datetime.datetime.now() -
                                               start))
        return persistant
Beispiel #2
0
i = 0
for asset in db.SensorData.find({
        "_id": {
            '$gt': ObjectId("57e954100000000000000000")
        }
}).sort("_id", pymongo.ASCENDING):
    if asset['p'] == 'm':
        if asset['s'] == 't':
            sensor = temperature
        elif asset['s'] == 'h':
            sensor = humidity
        elif asset['s'] == 'l':
            sensor = light
        else:
            sensor = moisture

        imported = SensorData()
        imported.value = asset['v']
        imported.sensor = sensor
        imported.plant = plant
        imported.persistant = True
        imported.created_at = datetime.datetime.fromtimestamp(
            asset['_id'].generation_time.timestamp())
        imported.save()

        i += 1
        if i % 100 == 0:
            print(i)

print(i)