def save_sensors(self, sensors: list, latitude: list, longitude: list, api_id, sensor_prefix, **kwargs) -> dict: sensor_objects = {} sensor_exists = set() for i in range(len(sensors)): # if sensor already exists dont save if kwargs['check_sensor_exists_by_name']: s_name = sensor_prefix + str( sensors[i]) if sensor_prefix is not None else str( sensors[i]) _sensor = Sensor.get_by_name(s_name) if _sensor: sensor_objects[_sensor.name] = _sensor continue if kwargs['check_sensor_exists_by_name_loc']: s_name = sensor_prefix + str( sensors[i]) if sensor_prefix is not None else str( sensors[i]) _sensor = Sensor.get_by_name_loc( s_name, None) # This needs to be fixed if _sensor: sensor_objects[_sensor.name] = _sensor continue if kwargs['check_sensor_exists_by_name_api']: s_name = sensor_prefix + str( sensors[i]) if sensor_prefix is not None else str( sensors[i]) _sensor = Sensor.get_by_name_api(s_name, api_id) if _sensor: sensor_objects[_sensor.name] = _sensor continue loc = self.save_location(float(latitude[i]), float(longitude[i])) s_name = sensor_prefix + str( sensors[i]) if sensor_prefix is not None else str(sensors[i]) _hash = self._hash_it(str(api_id), str(loc.id), s_name) if _hash in sensor_exists: continue if 'is_dependent' in kwargs: if kwargs['is_dependent']: _sensor = Sensor._get_by_api_location_name(a_id=api_id, l_id=loc.id, name=s_name) if _sensor: sensor_objects[_sensor.name] = _sensor sensor_exists.add(_hash) print(s_name, 'sensor already exists with API ID:', str(api_id), 'and Location ID:', str(loc.id)) continue sensor = Sensor(str(uuid.uuid4()), api_id, loc.id, s_name) sensor_exists.add(_hash) sensor = sensor.save() sensor_objects[sensor.name] = sensor # db.session.flush() return sensor_objects
return model_str for sensor in ['temperature', 'light', 'humidity', 'moisture']: if Sensor.select().where(Sensor.name == sensor).count() == 0: print('\n\nCREATE {}: \n\n'.format(sensor.upper())) db_sensor = Sensor() db_sensor.name = sensor db_sensor.model = model(sensor) db_sensor.unit = input('unit (only for display) ') db_sensor.min_value = input('minimum value? ') db_sensor.max_value = input('maximum value? ') db_sensor.persistant_offset = input('to persistant offset? ') db_sensor.save() all_sensors = ['temperature', 'light', 'humidity', 'moisture'] if SensorHardware.select().count() == 0: print('doing some other stuff') hardware_collection = { 'led_traffic_light': all_sensors, 'led_bar': ['moisture'], 'display': ['temperature', 'humidity'], 'mailer': all_sensors, 'water_pump': ['moisture'] } for hardware, values in hardware_collection.items(): current = SensorHardware(label=hardware, function='execute_' + hardware)
def save_sensors(self, sensors: list, latitude: list, longitude: list, api_id: int, sensor_prefix: str, **kwargs: {str: Any}) -> [db.Model]: """ Save Sensor Values :param sensors: Sensors :param latitude: list of latitudes for sensors :param longitude: list of longitudes for sensors :param api_id: api id :param sensor_prefix: sensors prefix :param kwargs: keyword arguments :return: returns a list of sensors """ sensor_objects = {} sensor_exists = set() for i in range(len(sensors)): # if sensor already exists dont save if kwargs['check_sensor_exists_by_name']: s_name = sensor_prefix + str( sensors[i]) if sensor_prefix is not None else str( sensors[i]) _sensor = Sensor.get_by_name(s_name) if _sensor: sensor_objects[_sensor.name] = _sensor continue if kwargs['check_sensor_exists_by_name_loc']: s_name = sensor_prefix + str( sensors[i]) if sensor_prefix is not None else str( sensors[i]) _sensor = Sensor.get_by_name_loc( s_name, None) # This needs to be fixed if _sensor: sensor_objects[_sensor.name] = _sensor continue if kwargs['check_sensor_exists_by_name_api']: s_name = sensor_prefix + str( sensors[i]) if sensor_prefix is not None else str( sensors[i]) _sensor = Sensor.get_by_name_api(s_name, api_id) if _sensor: sensor_objects[_sensor.name] = _sensor continue loc = self.save_location(float(latitude[i]), float(longitude[i])) s_name = sensor_prefix + str( sensors[i]) if sensor_prefix is not None else str(sensors[i]) _hash = self._hash_it(str(api_id), str(loc.id), s_name) if _hash in sensor_exists: continue if 'is_dependent' in kwargs: if kwargs['is_dependent']: _sensor = Sensor._get_by_api_location_name(a_id=api_id, l_id=loc.id, name=s_name) if _sensor: sensor_objects[_sensor.name] = _sensor sensor_exists.add(_hash) logger.info( '{} sensor already exists with API ID: {} and Location ID:' .format(s_name, str(api_id), str(loc.id))) continue sensor = Sensor(str(uuid.uuid4()), api_id, loc.id, s_name) sensor_exists.add(_hash) sensor = sensor.save() sensor_objects[sensor.name] = sensor return sensor_objects