summaryrefslogtreecommitdiff
path: root/api/routes/sensors.py
diff options
context:
space:
mode:
authorGustavoeklund01 <eklundgu@gmail.com>2025-11-04 18:52:57 -0300
committerGustavoeklund01 <eklundgu@gmail.com>2025-11-04 18:52:57 -0300
commite3b41c5c24488a48b552fb7aeffe250574b9c8a8 (patch)
treea9529250bba5965e8b9f5981295ad75b25247771 /api/routes/sensors.py
parent6e845c8fc10905e51f3c0bb081bc4a09e7062883 (diff)
parentcc1b5e2f48ef62670be05ccfc6346b16b61844c8 (diff)
Merge branch 'master' of https://github.com/Simplesmente-O-Grupo/iot-monitoring
Diffstat (limited to 'api/routes/sensors.py')
-rw-r--r--api/routes/sensors.py27
1 files changed, 25 insertions, 2 deletions
diff --git a/api/routes/sensors.py b/api/routes/sensors.py
index 89744e3..9c86309 100644
--- a/api/routes/sensors.py
+++ b/api/routes/sensors.py
@@ -1,7 +1,9 @@
-from fastapi import APIRouter
+from fastapi import APIRouter, HTTPException
from sqlalchemy import select
from ..database import SessionLocal
-from ..models import SensorDevice
+from ..models import SensorDevice, Station
+from ..schemas.sensor import PostSensor
+from datetime import datetime
router = APIRouter(
prefix='/sensors',
@@ -26,3 +28,24 @@ async def get_sensors():
dc['size'] = len(dc['sensors'])
session.close()
return dc
+
+@router.post('/')
+async def post_sensor(sensor: PostSensor):
+ session = SessionLocal()
+ station = session.get(Station, sensor.station_id)
+ if not station:
+ session.close()
+ raise HTTPException(
+ status_code=404,
+ detail=f"Não existe estação com id {sensor.station_id}"
+ )
+ if not sensor.installation_date:
+ installation_date = station.installation_date
+ else:
+ installation_date = datetime.fromtimestamp(sensor.installation_date)
+
+ session.add(SensorDevice(name=sensor.name, installation_date=installation_date, station_id=sensor.station_id, is_active=True))
+ session.commit()
+ session.close()
+ return {'msg': 'Sensor criado com sucesso.'}
+