summaryrefslogtreecommitdiff
path: root/api
diff options
context:
space:
mode:
Diffstat (limited to 'api')
-rw-r--r--api/main.py6
-rw-r--r--api/routes/locations.py7
-rw-r--r--api/routes/measures.py25
-rw-r--r--api/routes/readings.py28
-rw-r--r--api/routes/sensors.py27
-rw-r--r--api/routes/stations.py33
6 files changed, 122 insertions, 4 deletions
diff --git a/api/main.py b/api/main.py
index b4c85c5..ffdf071 100644
--- a/api/main.py
+++ b/api/main.py
@@ -1,8 +1,12 @@
from fastapi import FastAPI
-from .routes import locations
+from .routes import locations, measures, stations, sensors, readings
app = FastAPI()
app.include_router(locations.router)
+app.include_router(measures.router)
+app.include_router(stations.router)
+app.include_router(sensors.router)
+app.include_router(readings.router)
@app.get('/')
diff --git a/api/routes/locations.py b/api/routes/locations.py
index c3b30aa..5050cef 100644
--- a/api/routes/locations.py
+++ b/api/routes/locations.py
@@ -12,10 +12,11 @@ router = APIRouter(
async def get_locations():
dc = {'locations': []}
session = SessionLocal()
- stmt = select(Location.street, Location.avenue, Location.zip_code)
- locs = session.execute(stmt).all() # TODO: Page results
- for loc in locs:
+ stmt = select(Location)
+ locs = session.execute(stmt) # TODO: Page results
+ for loc in locs.scalars():
l = {
+ 'id': loc.id,
'street': loc.street,
'avenue': loc.avenue,
'zip_code': loc.zip_code
diff --git a/api/routes/measures.py b/api/routes/measures.py
index e69de29..f98c203 100644
--- a/api/routes/measures.py
+++ b/api/routes/measures.py
@@ -0,0 +1,25 @@
+from fastapi import APIRouter
+from sqlalchemy import select
+from ..database import SessionLocal
+from ..models import Measure
+
+router = APIRouter(
+ prefix='/measures',
+ tags=['measures']
+)
+
+@router.get('/')
+async def get_measures():
+ dc = {'measures': []}
+ session = SessionLocal()
+ stmt = select(Measure)
+ measures = session.execute(stmt)
+ for measure in measures.scalars():
+ mem = {
+ 'id': measure.id,
+ 'name': measure.name,
+ 'unit_code': measure.unit_code
+ }
+ dc['measures'].append(mem)
+ dc['size'] = len(dc['measures'])
+ return dc
diff --git a/api/routes/readings.py b/api/routes/readings.py
index e69de29..957106c 100644
--- a/api/routes/readings.py
+++ b/api/routes/readings.py
@@ -0,0 +1,28 @@
+from fastapi import APIRouter
+from sqlalchemy import select
+from ..database import SessionLocal
+from ..models import Reading
+
+router = APIRouter(
+ prefix='/readings',
+ tags=['readings']
+)
+
+@router.get('/')
+async def get_readings():
+ dc = {'readings': []}
+ session = SessionLocal()
+ stmt = select(Reading)
+ readings = session.execute(stmt)
+ for reading in readings.scalars():
+ re = {
+ 'id': reading.id,
+ 'sensor_device_id': reading.sensor_device_id,
+ 'measure_id': reading.measure_id,
+ 'value': reading.value,
+ 'time': reading.time
+ }
+ dc['readings'].append(re)
+ dc['size'] = len(dc['readings'])
+ return dc
+
diff --git a/api/routes/sensors.py b/api/routes/sensors.py
index e69de29..18e0f84 100644
--- a/api/routes/sensors.py
+++ b/api/routes/sensors.py
@@ -0,0 +1,27 @@
+from fastapi import APIRouter
+from sqlalchemy import select
+from ..database import SessionLocal
+from ..models import SensorDevice
+
+router = APIRouter(
+ prefix='/sensors',
+ tags=['sensors']
+)
+
+@router.get('/')
+async def get_sensors():
+ dc = {'sensors': []}
+ session = SessionLocal()
+ stmt = select(SensorDevice)
+ devices = session.execute(stmt)
+ for device in devices.scalars():
+ dev = {
+ 'id': device.id,
+ 'name': device.name,
+ 'installation_date': device.installation_date,
+ 'station_id': device.station_id,
+ 'is_active': device.is_active
+ }
+ dc['sensors'].append(dev)
+ dc['size'] = len(dc['sensors'])
+ return dc
diff --git a/api/routes/stations.py b/api/routes/stations.py
index e69de29..62e2643 100644
--- a/api/routes/stations.py
+++ b/api/routes/stations.py
@@ -0,0 +1,33 @@
+from fastapi import APIRouter
+from sqlalchemy import select
+from ..database import SessionLocal
+from ..models import Station
+
+router = APIRouter(
+ prefix='/stations',
+ tags=['stations']
+)
+
+@router.get('/')
+async def get_stations():
+ dc = {'stations': []}
+ session = SessionLocal()
+ stmt = select(Station)
+ stations = session.execute(stmt)
+ for station in stations.scalars():
+ stmt_devices = select(SensorDevice.id).where(SensorDevice.station_id == station.id)
+ ids_res = session.execute(stmt_devices).all()
+ ids = []
+ for id in ids_res:
+ ids.append(id.id)
+ stat = {
+ 'id': station.id,
+ 'name': station.name,
+ 'installation_date': station.installation_date,
+ 'location_id': station.location_id,
+ 'is_active': station.is_active,
+ 'sensor_device_ids': ids
+ }
+ dc['stations'].append(stat)
+ dc['size'] = len(dc['stations'])
+ return dc