From b4f5a63d311f6639c57e9c68d2e9183f54920a38 Mon Sep 17 00:00:00 2001 From: csasq Date: Mon, 26 Aug 2024 17:20:13 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B0=D0=B7=D1=80=D0=B0=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=D0=B0=D0=BD=D0=B0=20=D0=BA=D0=BE=D0=BD=D0=B5=D1=87=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F=20=D1=82=D0=BE=D1=87=D0=BA=D0=B0=20=D0=B4=D0=BB?= =?UTF-8?q?=D1=8F=20=D0=BF=D1=80=D0=BE=D1=81=D0=BB=D1=83=D1=88=D0=B8=D0=B2?= =?UTF-8?q?=D0=B0=D0=BD=D0=B8=D1=8F=20=D1=81=D1=86=D0=B5=D0=BD=D0=B0=D1=80?= =?UTF-8?q?=D0=B8=D0=B5=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/main.py | 76 +++++++++++++++++++++++++++-------------------------- 1 file changed, 39 insertions(+), 37 deletions(-) diff --git a/api/main.py b/api/main.py index c7e8e9c..7eb59e8 100644 --- a/api/main.py +++ b/api/main.py @@ -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)