Add ocpp message logging
All checks were successful
ci/woodpecker/push/docker Pipeline was successful

This commit is contained in:
Oliver Traber 2024-04-20 17:14:45 +02:00
parent 0c502630c7
commit ebc5e1306a

View file

@ -1,6 +1,9 @@
import logging
from fastapi import WebSocket, WebSocketDisconnect from fastapi import WebSocket, WebSocketDisconnect
from websockets import ConnectionClosed from websockets import ConnectionClosed
logger = logging.getLogger("gunicorn.access")
# Wrapper to transform a FastAPI websocket to a standard websocket # Wrapper to transform a FastAPI websocket to a standard websocket
class WebSocketWrapper(): class WebSocketWrapper():
def __init__(self, websocket: WebSocket): def __init__(self, websocket: WebSocket):
@ -8,11 +11,14 @@ class WebSocketWrapper():
async def recv(self) -> str: async def recv(self) -> str:
try: try:
text = await self._websocket.receive_text()
logger.info("Message received: {}", text)
return await self._websocket.receive_text() return await self._websocket.receive_text()
except WebSocketDisconnect as e: except WebSocketDisconnect as e:
raise ConnectionClosed(e.code, 'WebSocketWrapper') raise ConnectionClosed(e.code, 'WebSocketWrapper')
async def send(self, msg: str) -> None: async def send(self, msg: str) -> None:
logger.info("Message sent: {}", msg)
await self._websocket.send_text(msg) await self._websocket.send_text(msg)
async def close(self, code: int, reason: str) -> None: async def close(self, code: int, reason: str) -> None: