From 38d30646520a22f4f1eff6c4243e68f50d2e3233 Mon Sep 17 00:00:00 2001 From: BluemediaGER Date: Sun, 14 Apr 2024 22:56:51 +0200 Subject: [PATCH] Add ability to reset chargepoints --- app/ocpp_proto/chargepoint_manager.py | 2 +- app/routers/chargepoint_v1.py | 26 +++++++++++++++++++++++++- app/schemas/chargepoint.py | 9 +++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/app/ocpp_proto/chargepoint_manager.py b/app/ocpp_proto/chargepoint_manager.py index 60ebdc6..56b2885 100644 --- a/app/ocpp_proto/chargepoint_manager.py +++ b/app/ocpp_proto/chargepoint_manager.py @@ -24,7 +24,7 @@ async def call( ) -> Coroutine[Any, Any, Any | None]: try: cp = __active_connections[chargepoint_id] - return cp.call(payload, suppress, unique_id) + return await cp.call(payload, suppress, unique_id) except KeyError as e: raise e diff --git a/app/routers/chargepoint_v1.py b/app/routers/chargepoint_v1.py index 35d78f1..1dfdd00 100644 --- a/app/routers/chargepoint_v1.py +++ b/app/routers/chargepoint_v1.py @@ -5,9 +5,19 @@ from fastapi import APIRouter, HTTPException, Security from fastapi.params import Depends from sqlalchemy.orm import Session +from ocpp.v201.call import ResetPayload + 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.schemas.chargepoint import ( + ChargePoint, + ChargePointCreate, + ChargePointUpdate, + ChargePointPassword, + ChargePointConnectionInfo, + ChargePointResetRequest, + ChargePointResetResponse +) from app.models.chargepoint import ChargePoint as DbChargePoint from app.security import get_api_key @@ -112,3 +122,17 @@ async def get_chargepoint_status( return ChargePointConnectionInfo( connected=chargepoint_manager.is_connected(chargepoint_id) ) + +@router.post(path="/{chargepoint_id}/reset", response_model=ChargePointResetResponse) +async def reset_chargepoint( + chargepoint_id: UUID, + reset_request: ChargePointResetRequest, + api_key: str = Security(get_api_key) +): + if chargepoint_manager.is_connected(chargepoint_id) == False: + raise HTTPException(status_code=423, detail="Chargepoint not connected") + response = await chargepoint_manager.call( + chargepoint_id, + payload=ResetPayload(type=reset_request.type, evse_id=reset_request.evse_id) + ) + return ChargePointResetResponse(status=response.status) diff --git a/app/schemas/chargepoint.py b/app/schemas/chargepoint.py index a16db84..7d94118 100644 --- a/app/schemas/chargepoint.py +++ b/app/schemas/chargepoint.py @@ -5,6 +5,8 @@ from pydantic import BaseModel from app.schemas.connector import Connector +from ocpp.v201.enums import ResetType, ResetStatusType + class ChargePointBase(BaseModel): friendly_name: str is_active: bool @@ -33,3 +35,10 @@ class ChargePointPassword(BaseModel): class ChargePointConnectionInfo(BaseModel): connected: bool + +class ChargePointResetRequest(BaseModel): + type: ResetType + evse_id: Optional[int] = None + +class ChargePointResetResponse(BaseModel): + status: ResetStatusType