70 lines
No EOL
2.2 KiB
Python
70 lines
No EOL
2.2 KiB
Python
import asyncio
|
|
|
|
import websockets
|
|
import json
|
|
import logging
|
|
|
|
from app.class_settings import Settings
|
|
from app.class_file_path import Path
|
|
from app.class_file_convert import Convert
|
|
|
|
var_convert_active = False
|
|
|
|
class Server:
|
|
def __init__(self):
|
|
self.websocket = None
|
|
obj_settings = Settings()
|
|
obj_settings.set_logging()
|
|
|
|
self.yaml = obj_settings.yaml
|
|
self.obj_path = Path(self.yaml)
|
|
self.obj_convert = Convert(self, self.yaml, self.obj_path)
|
|
|
|
async def send_websocket(self, message):
|
|
try:
|
|
await self.websocket.send(message)
|
|
except websockets.exceptions.ConnectionClosed:
|
|
logging.warning("No websocket client connected!")
|
|
|
|
async def handle_client(self, websocket):
|
|
self.websocket = websocket
|
|
global var_convert_active
|
|
|
|
print("Server sagt: Client verbunden")
|
|
try:
|
|
async for message in websocket:
|
|
print(f"Server hat Empfangen: {message}")
|
|
|
|
data = json.loads(message)
|
|
|
|
if data.get("data_path"):
|
|
self.obj_path.receive_paths(data.get("data_path"))
|
|
|
|
if var_convert_active == False and self.yaml['autostart']:
|
|
await self.obj_convert.snake_waiting()
|
|
var_convert_active = True
|
|
else:
|
|
self.obj_convert.snake_update()
|
|
|
|
elif data.get("data_command"):
|
|
pass
|
|
|
|
#response = f"Server antwortet: {message.upper()}"
|
|
#await websocket.send(response)
|
|
except websockets.exceptions.ConnectionClosedError:
|
|
pass
|
|
except websockets.exceptions.InvalidUpgrade:
|
|
pass
|
|
except websockets.exceptions.ConnectionClosed:
|
|
print("Server sagt: Client getrennt")
|
|
|
|
@staticmethod
|
|
def set_var_convert_active(value: bool):
|
|
global var_convert_active
|
|
|
|
var_convert_active = value
|
|
|
|
async def start(self):
|
|
server = await websockets.serve(self.handle_client, self.yaml['server_ip'], self.yaml['server_port'])
|
|
print(f"Websocket Server läuft auf IP: {self.yaml['server_ip']} Port: {self.yaml['server_port']}")
|
|
await server.wait_closed() |