2024-08-15 00:57:30 +03:00
|
|
|
import asyncio
|
2024-08-14 01:09:41 +03:00
|
|
|
|
2024-08-26 17:20:13 +03:00
|
|
|
from fastapi import FastAPI, Body
|
2024-08-15 00:57:30 +03:00
|
|
|
from fastapi.websockets import WebSocket, WebSocketDisconnect
|
2024-08-13 23:57:26 +03:00
|
|
|
|
|
|
|
import config
|
|
|
|
|
|
|
|
|
2024-08-15 00:57:30 +03:00
|
|
|
class ConnectionManager:
|
|
|
|
connections: list[WebSocket]
|
|
|
|
|
|
|
|
class StateError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
):
|
|
|
|
self.connections = []
|
|
|
|
|
|
|
|
async def connect(
|
|
|
|
self,
|
|
|
|
websocket: WebSocket,
|
|
|
|
):
|
|
|
|
await websocket.accept()
|
|
|
|
self.connections.append(websocket)
|
|
|
|
|
|
|
|
def disconnect(
|
|
|
|
self,
|
|
|
|
websocket: WebSocket,
|
|
|
|
):
|
|
|
|
self.connections.remove(websocket)
|
|
|
|
|
|
|
|
async def broadcast(
|
|
|
|
self,
|
2024-08-26 17:20:13 +03:00
|
|
|
data: any,
|
2024-08-15 00:57:30 +03:00
|
|
|
):
|
|
|
|
for connection in self.connections:
|
|
|
|
asyncio.ensure_future(connection.send_json(data))
|
|
|
|
|
|
|
|
|
2024-08-17 15:29:45 +03:00
|
|
|
api = FastAPI(
|
2024-08-14 01:09:41 +03:00
|
|
|
title=config.Main.app_name,
|
|
|
|
)
|
|
|
|
|
2024-08-26 17:20:13 +03:00
|
|
|
scripts = [
|
2024-08-17 00:52:52 +03:00
|
|
|
{
|
|
|
|
'id': 1,
|
|
|
|
'name': 'Текущее состояние сотрудников',
|
2024-08-26 17:20:13 +03:00
|
|
|
'time': '0 11 * * 1-5',
|
|
|
|
'messageNumber': '1 вопрос',
|
2024-08-17 15:29:45 +03:00
|
|
|
'isEnabled': True,
|
2024-08-17 00:52:52 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
'id': 2,
|
|
|
|
'name': 'Планы на обед',
|
2024-08-26 17:20:13 +03:00
|
|
|
'time': '45 11 * * 1-5',
|
|
|
|
'messageNumber': '2 вопроса',
|
2024-08-17 15:29:45 +03:00
|
|
|
'isEnabled': False,
|
2024-08-17 00:52:52 +03:00
|
|
|
},
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2024-08-26 17:20:13 +03:00
|
|
|
scripts_cm = ConnectionManager()
|
|
|
|
|
|
|
|
|
|
|
|
@api.post(
|
|
|
|
path='/api/scripts',
|
2024-08-17 00:52:52 +03:00
|
|
|
)
|
2024-08-26 17:20:13 +03:00
|
|
|
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
|
2024-08-17 00:52:52 +03:00
|
|
|
|
|
|
|
|
2024-08-26 17:20:13 +03:00
|
|
|
@api.websocket(
|
|
|
|
path='/ws/scripts',
|
2024-08-15 00:57:30 +03:00
|
|
|
)
|
|
|
|
async def _(
|
2024-08-26 17:20:13 +03:00
|
|
|
websocket: WebSocket,
|
2024-08-15 00:57:30 +03:00
|
|
|
):
|
2024-08-26 17:20:13 +03:00
|
|
|
await scripts_cm.connect(websocket)
|
|
|
|
try:
|
|
|
|
await scripts_cm.broadcast(scripts)
|
|
|
|
while True:
|
|
|
|
await websocket.receive_json()
|
|
|
|
except WebSocketDisconnect:
|
|
|
|
scripts_cm.disconnect(websocket)
|