Разработана конечная точка для прослушивания сценариев

Этот коммит содержится в:
Глеб Иваницкий 2024-08-26 17:20:13 +03:00 коммит произвёл Gleb O. Ivaniczkij
родитель fb131822cc
Коммит b4f5a63d31

Просмотреть файл

@ -1,8 +1,6 @@
import asyncio
import os
from fastapi import FastAPI
from fastapi.responses import HTMLResponse
from fastapi import FastAPI, Body
from fastapi.websockets import WebSocket, WebSocketDisconnect
import config
@ -34,66 +32,70 @@ class ConnectionManager:
async def broadcast(
self,
data: dict,
data: any,
):
for connection in self.connections:
asyncio.ensure_future(connection.send_json(data))
connection_manager = ConnectionManager()
api = FastAPI(
title=config.Main.app_name,
)
polls = [
scripts = [
{
'id': 1,
'name': 'Текущее состояние сотрудников',
'daysOfWeek': 'ПН-ПТ',
'time': '11:00',
'questionNumber': '1 вопрос',
'time': '0 11 * * 1-5',
'messageNumber': '1 вопрос',
'isEnabled': True,
},
{
'id': 2,
'name': 'Планы на обед',
'daysOfWeek': 'ПН-ПТ',
'time': '11:45-12:00',
'questionNumber': '2 вопроса',
'time': '45 11 * * 1-5',
'messageNumber': '2 вопроса',
'isEnabled': False,
},
]
@api.get(
path='/api/polls',
)
async def _():
return polls
scripts_cm = ConnectionManager()
@api.put(
path='/api/polls',
@api.post(
path='/api/scripts',
)
async def _(
poll: dict,
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',
),
):
for i, p in enumerate(polls):
if p['id'] == poll['id']:
polls[i] = poll
i = script_id - 1
scripts[i]['name'] = name
scripts[i]['time'] = time
scripts[i]['messageNumber'] = message_number
scripts[i]['isEnabled'] = is_enabled
# @app.websocket(
# path='/ws/sync',
# )
# async def _(
# websocket: WebSocket,
# ):
# await connection_manager.connect(websocket)
# try:
# while True:
# await connection_manager.broadcast(await websocket.receive_json())
# except WebSocketDisconnect:
# connection_manager.disconnect(websocket)
@api.websocket(
path='/ws/scripts',
)
async def _(
websocket: WebSocket,
):
await scripts_cm.connect(websocket)
try:
await scripts_cm.broadcast(scripts)
while True:
await websocket.receive_json()
except WebSocketDisconnect:
scripts_cm.disconnect(websocket)