Skip to content

Commit

Permalink
add asset-id to register a weather sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmad-Wahid committed Jul 27, 2023
1 parent 3022ffd commit 96a7c53
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 9 deletions.
14 changes: 12 additions & 2 deletions flexmeasures_openweathermap/cli/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from .schemas.weather_sensor import WeatherSensorSchema
from ..utils.modeling import (
get_or_create_weather_station,
get_asset_id_weather_station,
)
from ..utils.locating import get_locations, get_location_by_asset_id
from ..utils.filing import make_file_path
Expand Down Expand Up @@ -42,6 +43,7 @@
@click.option(
"--asset-id",
required=False,
type=int,
help="The asset id of the weather station (you can also give its location).",
)
@click.option(
Expand Down Expand Up @@ -73,8 +75,16 @@ def add_weather_sensor(**args):
f"[FLEXMEASURES-OWM] Please correct the following errors:\n{errors}.\n Use the --help flag to learn more."
)
raise click.Abort

weather_station = get_or_create_weather_station(args["latitude"], args["longitude"])
if args["asset_id"] is not None:
weather_station = get_asset_id_weather_station(args["asset_id"])
elif args["latitude"] is not None and args["longitude"] is not None:
weather_station = get_or_create_weather_station(
args["latitude"], args["longitude"]
)
else:
raise Exception(
"Arguments are missing to register a weather sensor. Provide either '--asset-id' or ('--latitude' and '--longitude')."
)

fm_sensor_specs = get_supported_sensor_spec(args["name"])
fm_sensor_specs["generic_asset"] = weather_station
Expand Down
28 changes: 21 additions & 7 deletions flexmeasures_openweathermap/cli/schemas/weather_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
import pytz
from flexmeasures import Sensor

from ...utils.modeling import get_or_create_weather_station
from ...utils.modeling import (
get_or_create_weather_station,
get_asset_id_weather_station,
)
from ...utils.owm import get_supported_sensor_spec, get_supported_sensors_str


Expand All @@ -22,8 +25,13 @@ class WeatherSensorSchema(Schema):

name = fields.Str(required=True)
timezone = fields.Str()
latitude = fields.Float(required=True, validate=validate.Range(min=-90, max=90))
longitude = fields.Float(required=True, validate=validate.Range(min=-180, max=180))
asset_id = fields.Int(required=False, allow_none=True)
latitude = fields.Float(
required=False, validate=validate.Range(min=-90, max=90), allow_none=True
)
longitude = fields.Float(
required=False, validate=validate.Range(min=-180, max=180), allow_none=True
)

@validates("name")
def validate_name_is_supported(self, name: str):
Expand All @@ -35,11 +43,17 @@ def validate_name_is_supported(self, name: str):

@validates_schema(skip_on_field_errors=False)
def validate_name_is_unique_in_weather_station(self, data, **kwargs):
if "name" not in data or "latitude" not in data or "longitude" not in data:
if "name" not in data:
return # That's a different validation problem
weather_station = get_or_create_weather_station(
data["latitude"], data["longitude"]
)
if data["latitude"] is not None and data["longitude"] is not None:
weather_station = get_or_create_weather_station(
data["latitude"], data["longitude"]
)
elif data["asset_id"] is not None:
weather_station = get_asset_id_weather_station(data["asset_id"])
else:
return

sensor = Sensor.query.filter(
Sensor.name == data["name"].lower(),
Sensor.generic_asset == weather_station,
Expand Down
17 changes: 17 additions & 0 deletions flexmeasures_openweathermap/utils/modeling.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,20 @@ def get_or_create_weather_station(latitude: float, longitude: float) -> GenericA
)
db.session.add(weather_station)
return weather_station


def get_asset_id_weather_station(asset_id: int) -> GenericAsset:
weather_station = GenericAsset.query.filter(
GenericAsset.generic_asset_type_id == asset_id
).one_or_none()
if weather_station is None:
raise Exception(
f"[FLEXMEASURES-OWM] Weather station is not present for the given asset id '{asset_id}'."
)

if weather_station.latitude is None or weather_station.longitude is None:
raise Exception(
f"[FLEXMEASURES-OWM] Weather station {weather_station} is missing location information [Latitude, Longitude]."
)

return weather_station

0 comments on commit 96a7c53

Please sign in to comment.