Spaces:
Running
Running
Update app/routers/alerts.py
Browse files- app/routers/alerts.py +74 -33
app/routers/alerts.py
CHANGED
|
@@ -1,33 +1,74 @@
|
|
| 1 |
-
from
|
| 2 |
-
|
| 3 |
-
from
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datetime import datetime, timezone
|
| 2 |
+
|
| 3 |
+
from fastapi import APIRouter, Depends, WebSocket, WebSocketDisconnect
|
| 4 |
+
|
| 5 |
+
from app.core.auth import AuthUser, get_current_web_user
|
| 6 |
+
from app.models.schemas import DeviceMessageRequest, DeviceMessageResponse
|
| 7 |
+
from app.services.alert_hub import alert_hub
|
| 8 |
+
|
| 9 |
+
router = APIRouter(prefix="/alerts", tags=["alerts"])
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
@router.websocket("/ws/admin")
|
| 13 |
+
async def ws_admin(websocket: WebSocket) -> None:
|
| 14 |
+
await alert_hub.connect_admin(websocket)
|
| 15 |
+
try:
|
| 16 |
+
while True:
|
| 17 |
+
message = await websocket.receive_text()
|
| 18 |
+
if message == "ping":
|
| 19 |
+
await websocket.send_text("pong")
|
| 20 |
+
except WebSocketDisconnect:
|
| 21 |
+
alert_hub.disconnect_admin(websocket)
|
| 22 |
+
except Exception:
|
| 23 |
+
alert_hub.disconnect_admin(websocket)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
@router.websocket("/ws/device/{device_id}")
|
| 27 |
+
async def ws_device(websocket: WebSocket, device_id: str) -> None:
|
| 28 |
+
await alert_hub.connect_device(device_id, websocket)
|
| 29 |
+
try:
|
| 30 |
+
while True:
|
| 31 |
+
message = await websocket.receive_text()
|
| 32 |
+
if message == "ping":
|
| 33 |
+
await websocket.send_text("pong")
|
| 34 |
+
except WebSocketDisconnect:
|
| 35 |
+
alert_hub.disconnect_device(device_id, websocket)
|
| 36 |
+
except Exception:
|
| 37 |
+
alert_hub.disconnect_device(device_id, websocket)
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
@router.post("/device-message", response_model=DeviceMessageResponse)
|
| 41 |
+
async def send_device_message(
|
| 42 |
+
payload: DeviceMessageRequest,
|
| 43 |
+
user: AuthUser = Depends(get_current_web_user),
|
| 44 |
+
) -> DeviceMessageResponse:
|
| 45 |
+
text = payload.message.strip()
|
| 46 |
+
if not text:
|
| 47 |
+
return DeviceMessageResponse(queued=False, delivered_clients=0)
|
| 48 |
+
|
| 49 |
+
delivered_clients = alert_hub.device_connection_count(payload.device_id)
|
| 50 |
+
|
| 51 |
+
await alert_hub.send_device(
|
| 52 |
+
payload.device_id,
|
| 53 |
+
{
|
| 54 |
+
"type": "admin_message",
|
| 55 |
+
"device_id": payload.device_id,
|
| 56 |
+
"message": text,
|
| 57 |
+
"sender": user.username,
|
| 58 |
+
"role": user.role,
|
| 59 |
+
"created_at": datetime.now(timezone.utc).isoformat(),
|
| 60 |
+
},
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
await alert_hub.broadcast_admin(
|
| 64 |
+
{
|
| 65 |
+
"type": "info",
|
| 66 |
+
"device_id": payload.device_id,
|
| 67 |
+
"message": f"{user.username} sent message to {payload.device_id}",
|
| 68 |
+
}
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
return DeviceMessageResponse(
|
| 72 |
+
queued=True,
|
| 73 |
+
delivered_clients=delivered_clients,
|
| 74 |
+
)
|