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

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

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

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