Сравнить коммиты
2 Коммитов
b9f96398e3
...
b6661d3ebd
Автор | SHA1 | Дата | |
---|---|---|---|
b6661d3ebd | |||
b4f5a63d31 |
41
api/main.py
41
api/main.py
@ -1,8 +1,6 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import os
|
|
||||||
|
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI, Body
|
||||||
from fastapi.responses import HTMLResponse
|
|
||||||
from fastapi.websockets import WebSocket, WebSocketDisconnect
|
from fastapi.websockets import WebSocket, WebSocketDisconnect
|
||||||
|
|
||||||
import config
|
import config
|
||||||
@ -40,9 +38,6 @@ class ConnectionManager:
|
|||||||
asyncio.ensure_future(connection.send_json(data))
|
asyncio.ensure_future(connection.send_json(data))
|
||||||
|
|
||||||
|
|
||||||
connection_manager = ConnectionManager()
|
|
||||||
|
|
||||||
|
|
||||||
api = FastAPI(
|
api = FastAPI(
|
||||||
title=config.Main.app_name,
|
title=config.Main.app_name,
|
||||||
)
|
)
|
||||||
@ -65,16 +60,42 @@ scripts = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
scripts_cm = ConnectionManager()
|
||||||
|
|
||||||
|
|
||||||
|
@api.post(
|
||||||
|
path='/api/scripts',
|
||||||
|
)
|
||||||
|
async def _(
|
||||||
|
script_id: int = Body(
|
||||||
|
validation_alias='id',
|
||||||
|
),
|
||||||
|
name: str = Body(),
|
||||||
|
time: str = Body(),
|
||||||
|
message_number: str = Body(
|
||||||
|
validation_alias='messageNumber',
|
||||||
|
),
|
||||||
|
is_enabled: bool = Body(
|
||||||
|
validation_alias='isEnabled',
|
||||||
|
),
|
||||||
|
):
|
||||||
|
i = script_id - 1
|
||||||
|
scripts[i]['name'] = name
|
||||||
|
scripts[i]['time'] = time
|
||||||
|
scripts[i]['messageNumber'] = message_number
|
||||||
|
scripts[i]['isEnabled'] = is_enabled
|
||||||
|
|
||||||
|
|
||||||
@api.websocket(
|
@api.websocket(
|
||||||
path='/ws/scripts',
|
path='/ws/scripts',
|
||||||
)
|
)
|
||||||
async def _(
|
async def _(
|
||||||
websocket: WebSocket,
|
websocket: WebSocket,
|
||||||
):
|
):
|
||||||
await connection_manager.connect(websocket)
|
await scripts_cm.connect(websocket)
|
||||||
try:
|
try:
|
||||||
await connection_manager.broadcast(scripts)
|
await scripts_cm.broadcast(scripts)
|
||||||
while True:
|
while True:
|
||||||
await connection_manager.broadcast(await websocket.receive_json())
|
await websocket.receive_json()
|
||||||
except WebSocketDisconnect:
|
except WebSocketDisconnect:
|
||||||
connection_manager.disconnect(websocket)
|
scripts_cm.disconnect(websocket)
|
||||||
|
Загрузка…
Ссылка в новой задаче
Block a user