Reafctor app
This commit is contained in:
parent
b8216f6ade
commit
7740be8bb5
36 changed files with 389 additions and 208 deletions
47
app/routers/ocpp_v1.py
Normal file
47
app/routers/ocpp_v1.py
Normal file
|
@ -0,0 +1,47 @@
|
|||
import logging
|
||||
from fastapi import APIRouter, WebSocket, WebSocketException
|
||||
|
||||
from app.ocpp_proto import chargepoint_manager
|
||||
from app.ocpp_proto.chargepoint import ChargePoint
|
||||
from app.util.websocket_wrapper import WebSocketWrapper
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.websocket("/{charging_station_friendly_name}")
|
||||
async def websocket_endpoint(
|
||||
*,
|
||||
websocket: WebSocket,
|
||||
charging_station_friendly_name: str,
|
||||
):
|
||||
""" For every new charging station that connects, create a ChargePoint
|
||||
instance and start listening for messages.
|
||||
"""
|
||||
if (websocket.user.friendly_name != charging_station_friendly_name):
|
||||
raise WebSocketException(code=1008, reason="Username doesn't match chargepoint identifier")
|
||||
|
||||
logging.info("Charging station '%s' (%s) connected", charging_station_friendly_name, websocket.user.id)
|
||||
|
||||
# Check protocols
|
||||
try:
|
||||
requested_protocols = websocket.headers['sec-websocket-protocol']
|
||||
logging.info("Protocols advertised by charging station: %s", requested_protocols)
|
||||
except KeyError:
|
||||
logging.warning("Charging station hasn't advertised any subprotocol. "
|
||||
"Closing Connection")
|
||||
return await websocket.close()
|
||||
|
||||
if "ocpp2.0.1" in requested_protocols:
|
||||
logging.info("Matched supported protocol: ocpp2.0.1")
|
||||
else:
|
||||
logging.warning('Protocols mismatched | Expected subprotocols: %s,'
|
||||
' but client supports %s | Closing connection',
|
||||
"ocpp2.0.1",
|
||||
requested_protocols)
|
||||
await websocket.accept()
|
||||
await websocket.close()
|
||||
return
|
||||
|
||||
# Accept connection and begin communication
|
||||
await websocket.accept(subprotocol="ocpp2.0.1")
|
||||
cp = ChargePoint(charging_station_friendly_name, WebSocketWrapper(websocket))
|
||||
await chargepoint_manager.start(websocket.user.id, cp)
|
Loading…
Add table
Add a link
Reference in a new issue