Add variable management
This commit is contained in:
parent
a65dee8962
commit
0ea0cb9d98
7 changed files with 287 additions and 13 deletions
|
@ -6,7 +6,7 @@ from fastapi import APIRouter, HTTPException, Security
|
|||
from fastapi.params import Depends
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ocpp.v201.call import ResetPayload
|
||||
from ocpp.v201.call import ResetPayload, SetVariablesPayload
|
||||
|
||||
from app.database import get_db
|
||||
from app.ocpp_proto import chargepoint_manager
|
||||
|
@ -20,8 +20,16 @@ from app.schemas.chargepoint import (
|
|||
ChargePointResetResponse
|
||||
)
|
||||
from app.schemas.id_token import IdTokenLearnRequest, IdTokenLearnResponse
|
||||
from app.schemas.chargepoint_variable import (
|
||||
ChargepointVariable,
|
||||
ChargepointVariableUpdate,
|
||||
ChargepointVariableResponse,
|
||||
MutabilityType,
|
||||
SetVariableStatusType
|
||||
)
|
||||
from app.models.chargepoint import ChargePoint as DbChargePoint
|
||||
from app.models.user import User as DbUser
|
||||
from app.models.chargepoint_variable import ChargepointVariable as DbChargepointVariable
|
||||
from app.security import get_api_key
|
||||
|
||||
router = APIRouter(
|
||||
|
@ -213,3 +221,74 @@ async def get_id_token_learn_request(
|
|||
db.commit()
|
||||
|
||||
return []
|
||||
|
||||
@router.get(path="/{chargepoint_id}/variables", response_model=list[ChargepointVariable])
|
||||
async def get_chargepoint_variables(
|
||||
chargepoint_id: UUID,
|
||||
api_key: str = Security(get_api_key),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
chargepoint = db.get(DbChargePoint, chargepoint_id)
|
||||
if chargepoint is None:
|
||||
raise HTTPException(status_code=404, detail="Chargepoint not found")
|
||||
|
||||
return db.query(DbChargepointVariable).filter(DbChargepointVariable.chargepoint_id == chargepoint_id).all()
|
||||
|
||||
@router.put(path="/{chargepoint_id}/variables/{variable_id}", response_model=ChargepointVariableResponse)
|
||||
async def update_chargepoint_variable(
|
||||
chargepoint_id: UUID,
|
||||
variable_id: UUID,
|
||||
variable_update: ChargepointVariableUpdate,
|
||||
api_key: str = Security(get_api_key),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
chargepoint = db.get(DbChargePoint, chargepoint_id)
|
||||
if chargepoint is None:
|
||||
raise HTTPException(status_code=404, detail="Chargepoint not found")
|
||||
|
||||
variable = db.query(DbChargepointVariable).filter(
|
||||
DbChargepointVariable.chargepoint_id == chargepoint_id,
|
||||
DbChargepointVariable.id == variable_id
|
||||
).first()
|
||||
if variable is None:
|
||||
raise HTTPException(status_code=404, detail="ChargepointVariable not found")
|
||||
if variable.mutability == MutabilityType.READ_ONLY:
|
||||
raise HTTPException(status_code=422, detail="ChargepointVariable is read-only")
|
||||
|
||||
variable.value = variable_update.value
|
||||
|
||||
if chargepoint_manager.is_connected(chargepoint_id) == False:
|
||||
raise HTTPException(status_code=503, detail="Chargepoint not connected.")
|
||||
try:
|
||||
evse = None
|
||||
if variable.evse != None:
|
||||
evse = {
|
||||
'id': variable.evse
|
||||
}
|
||||
if variable.connector_id != None:
|
||||
evse['connectorId'] = variable.connector_id
|
||||
result = await chargepoint_manager.call(
|
||||
chargepoint_id,
|
||||
payload=SetVariablesPayload(set_variable_data=[
|
||||
{
|
||||
'attributeType': variable.type.value,
|
||||
'attributeValue': variable_update.value,
|
||||
'component': {
|
||||
'name': variable.component_name,
|
||||
'instance': variable.component_instance,
|
||||
'evse': evse
|
||||
},
|
||||
'variable': {
|
||||
'name': variable.name
|
||||
}
|
||||
}
|
||||
])
|
||||
)
|
||||
status = result.set_variable_result[0]['attribute_status']
|
||||
if SetVariableStatusType(status) in [SetVariableStatusType.ACCEPTED, SetVariableStatusType.REBOOT_REQUIRED]:
|
||||
db.commit()
|
||||
else:
|
||||
raise HTTPException(status_code=500, detail=status)
|
||||
return ChargepointVariableResponse(status=status)
|
||||
except TimeoutError:
|
||||
raise HTTPException(status_code=503, detail="Chargepoint didn't respond in time.")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue