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
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