Reafctor app
This commit is contained in:
parent
b8216f6ade
commit
7740be8bb5
36 changed files with 389 additions and 208 deletions
0
app/routers/__init__.py
Normal file
0
app/routers/__init__.py
Normal file
114
app/routers/chargepoint_v1.py
Normal file
114
app/routers/chargepoint_v1.py
Normal file
|
@ -0,0 +1,114 @@
|
|||
import random
|
||||
import string
|
||||
from uuid import UUID
|
||||
from fastapi import APIRouter, HTTPException, Security
|
||||
from fastapi.params import Depends
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.database import get_db
|
||||
from app.ocpp_proto import chargepoint_manager
|
||||
from app.schemas.chargepoint import ChargePoint, ChargePointCreate, ChargePointUpdate, ChargePointPassword, ChargePointConnectionInfo
|
||||
from app.models.chargepoint import ChargePoint as DBChargePoint
|
||||
from app.security import get_api_key
|
||||
|
||||
router = APIRouter(
|
||||
prefix="/chargepoint",
|
||||
tags=["chargepoint"],
|
||||
)
|
||||
|
||||
@router.get(path="/", response_model=list[ChargePoint])
|
||||
async def get_chargepoints(
|
||||
skip: int = 0,
|
||||
limit: int = 20,
|
||||
api_key: str = Security(get_api_key),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
return db.query(DBChargePoint).offset(skip).limit(limit).all()
|
||||
|
||||
@router.get(path="/{chargepoint_id}", response_model=ChargePoint)
|
||||
async def get_chargepoint(
|
||||
chargepoint_id: UUID,
|
||||
api_key: str = Security(get_api_key),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
chargepoint = db.query(DBChargePoint).filter(DBChargePoint.id == chargepoint_id).first()
|
||||
if chargepoint is None:
|
||||
raise HTTPException(status_code=404, detail="Chargepoint not found")
|
||||
return chargepoint
|
||||
|
||||
@router.get(path="/{chargepoint_id}/password", response_model=ChargePointPassword)
|
||||
async def get_chargepoint_password(
|
||||
chargepoint_id: UUID,
|
||||
api_key: str = Security(get_api_key),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
chargepoint = db.query(DBChargePoint).filter(DBChargePoint.id == chargepoint_id).first()
|
||||
if chargepoint is None:
|
||||
raise HTTPException(status_code=404, detail="Chargepoint not found")
|
||||
return ChargePointPassword(password=chargepoint.password)
|
||||
|
||||
@router.delete(path="/{chargepoint_id}/password", response_model=ChargePointPassword)
|
||||
async def reset_chargepoint_password(
|
||||
chargepoint_id: UUID,
|
||||
api_key: str = Security(get_api_key),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
chargepoint = db.query(DBChargePoint).filter(DBChargePoint.id == chargepoint_id).first()
|
||||
if chargepoint is None:
|
||||
raise HTTPException(status_code=404, detail="Chargepoint not found")
|
||||
chargepoint.password = ''.join(random.choice(string.ascii_letters + string.digits) for i in range(24))
|
||||
db.commit()
|
||||
return ChargePointPassword(password=chargepoint.password)
|
||||
|
||||
@router.post(path="/", response_model=ChargePoint)
|
||||
async def create_chargepoint(
|
||||
chargepoint: ChargePointCreate,
|
||||
api_key: str = Security(get_api_key),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
chargepoint_db = DBChargePoint(
|
||||
friendly_name=chargepoint.friendly_name,
|
||||
is_active=chargepoint.is_active,
|
||||
password=''.join(random.choice(string.ascii_letters + string.digits) for i in range(24))
|
||||
)
|
||||
db.add(chargepoint_db)
|
||||
db.commit()
|
||||
db.refresh(chargepoint_db)
|
||||
return chargepoint_db
|
||||
|
||||
@router.patch(path="/{chargepoint_id}", response_model=ChargePoint)
|
||||
async def update_chargepoint(
|
||||
chargepoint_id: UUID,
|
||||
chargepoint_update: ChargePointUpdate,
|
||||
api_key: str = Security(get_api_key),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
chargepoint = db.query(DBChargePoint).filter(DBChargePoint.id == chargepoint_id).first()
|
||||
if chargepoint is None:
|
||||
raise HTTPException(status_code=404, detail="Chargepoint not found")
|
||||
for key, value in chargepoint_update.model_dump(exclude_unset=True).items():
|
||||
setattr(chargepoint, key, value)
|
||||
db.commit()
|
||||
return chargepoint
|
||||
|
||||
@router.delete(path="/{chargepoint_id}", response_model=[])
|
||||
async def delete_chargepoint(
|
||||
chargepoint_id: UUID,
|
||||
api_key: str = Security(get_api_key),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
chargepoint = db.query(DBChargePoint).filter(DBChargePoint.id == chargepoint_id).first()
|
||||
if chargepoint is None:
|
||||
raise HTTPException(status_code=404, detail="Chargepoint not found")
|
||||
db.delete(chargepoint)
|
||||
db.commit()
|
||||
return []
|
||||
|
||||
@router.get(path="/{chargepoint_id}/status", response_model=ChargePointConnectionInfo)
|
||||
async def get_chargepoint_status(
|
||||
chargepoint_id: UUID,
|
||||
api_key: str = Security(get_api_key)
|
||||
):
|
||||
return ChargePointConnectionInfo(
|
||||
connected=chargepoint_manager.is_connected(chargepoint_id)
|
||||
)
|
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