def get_measurement(self): lid = ConfigManager.Instance().get_location() data = super().get_measurement() if lid is None or data is None: return None location = Locations().get(lid) if location is None or location.get_height() is None: return None value = location.get_height() - data.get_value() value = self.round(value) return SensorMeasurement(value, data.get_quality())
def home(self): sensor_data = Sensors().get_all() sensor_id = None if sensor_data is not None and len(sensor_data) > 0: sensor_id = sensor_data[0].get_id() location = ConfigManager.Instance().get_location() locationObj = Locations().get(location) data = { 'setup': False, 'location': locationObj, 'default_sensor': sensor_id, 'sensors': {}, 'now': datetime.datetime.utcnow().strftime("%a %b %d %Y %H:%M:%S") } if locationObj is None or sensor_id is None: data['setup'] = True for sensor in sensor_data: sensor_id = sensor.get_id() last = self.__getlastvalue(location, sensor_id) data['sensors'][sensor_id] = { 'sensor': sensor, 'hourly': self.__getminmaxavgvalue(time.strftime('%Y-%m-%dT%H:00:00Z'), location, sensor), 'daily': self.__getminmaxavgvalue(time.strftime('%Y-%m-%dT00:00:00Z'), location, sensor), 'monthly': self.__getminmaxavgvalue(time.strftime('%Y-%m-01T00:00:00Z'), location, sensor), 'yearly': self.__getminmaxavgvalue(time.strftime('%Y-01-01T00:00:00Z'), location, sensor), 'accum': self.__getminmaxavgvalue('2015-01-01T00:00:00Z', location, sensor), 'last': last['last'], 'datetime': last['datetime'], 'trend': Measurements().calc_trend(sensor_id, location)['description'] } return self.get_view('index.html').data(data)
def config_locations(self): data = { "locations": Locations().get_all(), "default_location": ConfigManager.Instance().get_location() } return self.get_view('config_location.html').data(data)
def data(self): data = { 'locations': Locations().get_all(), 'sensors': Sensors().get_all(), 'timerange': Measurements().get_time_range(), 'location': ConfigManager.Instance().get_location() } return self.get_view('data.html').data(data)
def _get_location(self, id): location_model = Locations().get(id) location = {} location['id'] = location_model.get_id() location['name'] = location_model.get_name() location['lat'] = location_model.get_latitude() location['lon'] = location_model.get_longitude() location['height'] = location_model.get_height() return location
def config_locations_change(self, mode, id): data = { "edit": (mode == 'edit'), "mode": mode, "location": None } if mode == 'edit' and id is not None: location = Locations().get(id) data['location'] = location return self.get_view('config_location_change.html').data(data)
def create_location(department: str, city: str, address: str, pin: str) -> Locations: location = Locations() location.department = department location.city = city location.address = address location.pin = pin return location
def __trigger(self, sensors, pending=False): data = [] interval = None if pending is True: interval = ConfigManager.Instance().get_interval() if interval < 1: return [] # No valid interval specified location = Locations().get(ConfigManager.Instance().get_location()) if location is None: return data # No location found for this id measurements = Measurements() for sensor in sensors: # Sensor is disabled, ignore it if not sensor.is_active(): continue # Ignore the sensor if no implementation can be found impl = sensor.get_sensor_impl() if impl is None: continue # If we want to trigger only pending sensors, check that and ignore sensors that are not pending to their rules if pending is True and impl.is_due(sensor.get_extra('pending'), interval) is False: continue measurementObj = None try: measurementObj = impl.get_measurement() except: print("Could not take a measurement for sensor " + str(sensor.get_id())) if measurementObj is not None: measurement = measurements.create() measurement.set_value(measurementObj.get_value()) measurement.set_quality(measurementObj.get_quality()) measurement.set_sensor(sensor) measurement.set_location(location) measurement.create() data.append(measurement) Notifiers().notify(measurement) return data
def get_measurement(self): if self.owm is None: api_key = self.get_setting("apikey") if api_key is None or len(api_key) < 1: return None self.owm = pyowm.OWM(api_key) lid = ConfigManager.Instance().get_location() if lid is None: return None location = Locations().get(lid) if location is None or location.get_latitude() is None or location.get_longitude() is None: return None obs = self.owm.weather_at_coords(location.get_latitude(), location.get_longitude()) w = obs.get_weather() vol_rain = w.get_rain() vol_snow = w.get_snow() value = None quality = 1.0 if '3h' in vol_rain and '3h' in vol_snow: value = vol_rain['3h'] + vol_snow['3h'] elif '3h' in vol_rain: value = vol_rain['3h'] quality = 0.5 elif '3h' in vol_snow: value = vol_snow['3h'] quality = 0.5 else: # Not sure whether this is true (API returns nothing when there is no rain/snow?) value = 0 quality = 0.0 if value is not None: value = self.round(value) return SensorMeasurement(value, quality) else: return None
def location(self, id): if (request.method == 'DELETE'): location = Locations().get(id) if location is None: return self.get_view().bad_request('Location does not exist') if location.delete(): return self.get_view().success() else: return self.get_view().error() elif (request.method == 'PUT'): input = request.get_json() if(input is None): return self.get_view().bad_request('expected json') if('id' in input): location = Locations().get(int(input['id'])) if location is None: return self.get_view().bad_request('The sensor you are trying to update does not exist try to create it instead') try: location.set_id(int(input['id'])) if('name' in input): location.set_name(str(input['name'])) if('lat' in input and 'lon' in input): location.set_position(float(input['lat']), float(input['lon'])) if('height' in input): location.set_height(float(input['height'])) updated = location.update() if not updated: return self.get_view().bad_request('The location you are trying to update does not exist try to create it instead') except ValueError: return self.get_view().bad_request('input not in the right format') else: return self.get_view().bad_request('not all necessary field set') return self.get_view().success() elif (request.method == 'POST'): input = request.get_json() if(input is None): return self.get_view().bad_request('expected json') if('name' in input and 'lat' in input and 'lon' in input and 'height' in input): location = Locations().create() try: location.set_name(str(input['name'])) location.set_position(float(input['lat']), float(input['lon'])) location.set_height(float(input['height'])) created = location.create() if not created: return self.get_view().bad_request('The location could not be created') except ValueError: return self.get_view().bad_request('input not in the right format') else: return self.get_view().bad_request('not all necessary field available') return self.get_view().success()
def list(self): data = Locations().get_all() return self.get_view().data(data)
def config(self): data = { "config": ConfigManager.Instance(), "locations": Locations().get_all() } return self.get_view('config.html').data(data)
def location(self, id): if (request.method == 'DELETE'): location = Locations().get(id) if location is None: return self.get_view().bad_request('Location does not exist') if location.delete(): return self.get_view().success() else: return self.get_view().error() elif (request.method == 'PUT'): input = request.get_json() if (input is None): return self.get_view().bad_request('expected json') if ('id' in input): location = Locations().get(int(input['id'])) if location is None: return self.get_view().bad_request( 'The sensor you are trying to update does not exist try to create it instead' ) try: location.set_id(int(input['id'])) if ('name' in input): location.set_name(str(input['name'])) if ('lat' in input and 'lon' in input): location.set_position(float(input['lat']), float(input['lon'])) if ('height' in input): location.set_height(float(input['height'])) updated = location.update() if not updated: return self.get_view().bad_request( 'The location you are trying to update does not exist try to create it instead' ) except ValueError: return self.get_view().bad_request( 'input not in the right format') else: return self.get_view().bad_request( 'not all necessary field set') return self.get_view().success() elif (request.method == 'POST'): input = request.get_json() if (input is None): return self.get_view().bad_request('expected json') if ('name' in input and 'lat' in input and 'lon' in input and 'height' in input): location = Locations().create() try: location.set_name(str(input['name'])) location.set_position(float(input['lat']), float(input['lon'])) location.set_height(float(input['height'])) created = location.create() if not created: return self.get_view().bad_request( 'The location could not be created') except ValueError: return self.get_view().bad_request( 'input not in the right format') else: return self.get_view().bad_request( 'not all necessary field available') return self.get_view().success()
def get_location_object(self): from models.locations import Locations return Locations().get(self.location)