2024-04-13 22:43:03 +02:00
|
|
|
import random
|
|
|
|
import string
|
2024-04-19 00:08:29 +02:00
|
|
|
from datetime import datetime, timedelta, UTC
|
2024-04-13 22:43:03 +02:00
|
|
|
from uuid import UUID
|
|
|
|
from fastapi import APIRouter, HTTPException, Security
|
|
|
|
from fastapi.params import Depends
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
|
2024-04-20 02:36:46 +02:00
|
|
|
from ocpp.v201.call import ResetPayload, SetVariablesPayload
|
2024-04-14 22:56:51 +02:00
|
|
|
|
2024-04-13 22:43:03 +02:00
|
|
|
from app.database import get_db
|
|
|
|
from app.ocpp_proto import chargepoint_manager
|
2024-04-14 22:56:51 +02:00
|
|
|
from app.schemas.chargepoint import (
|
|
|
|
ChargePoint,
|
|
|
|
ChargePointCreate,
|
|
|
|
ChargePointUpdate,
|
|
|
|
ChargePointPassword,
|
|
|
|
ChargePointConnectionInfo,
|
|
|
|
ChargePointResetRequest,
|
|
|
|
ChargePointResetResponse
|
|
|
|
)
|
2024-04-19 00:08:29 +02:00
|
|
|
from app.schemas.id_token import IdTokenLearnRequest, IdTokenLearnResponse
|
2024-04-20 02:36:46 +02:00
|
|
|
from app.schemas.chargepoint_variable import (
|
|
|
|
ChargepointVariable,
|
|
|
|
ChargepointVariableUpdate,
|
|
|
|
ChargepointVariableResponse,
|
|
|
|
MutabilityType,
|
|
|
|
SetVariableStatusType
|
|
|
|
)
|
2024-04-14 01:42:51 +02:00
|
|
|
from app.models.chargepoint import ChargePoint as DbChargePoint
|
2024-04-19 00:08:29 +02:00
|
|
|
from app.models.user import User as DbUser
|
2024-04-20 02:36:46 +02:00
|
|
|
from app.models.chargepoint_variable import ChargepointVariable as DbChargepointVariable
|
2024-04-13 22:43:03 +02:00
|
|
|
from app.security import get_api_key
|
|
|
|
|
|
|
|
router = APIRouter(
|
2024-04-14 17:34:46 +02:00
|
|
|
prefix="/chargepoints",
|
|
|
|
tags=["Chargepoint (v1)"],
|
2024-04-13 22:43:03 +02:00
|
|
|
)
|
|
|
|
|
2024-04-13 22:55:43 +02:00
|
|
|
@router.get(path="", response_model=list[ChargePoint])
|
2024-04-13 22:43:03 +02:00
|
|
|
async def get_chargepoints(
|
|
|
|
skip: int = 0,
|
|
|
|
limit: int = 20,
|
|
|
|
api_key: str = Security(get_api_key),
|
|
|
|
db: Session = Depends(get_db)
|
|
|
|
):
|
2024-04-14 01:42:51 +02:00
|
|
|
return db.query(DbChargePoint).offset(skip).limit(limit).all()
|
2024-04-13 22:43:03 +02:00
|
|
|
|
|
|
|
@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)
|
|
|
|
):
|
2024-04-14 01:42:51 +02:00
|
|
|
chargepoint = db.get(DbChargePoint, chargepoint_id)
|
2024-04-13 22:43:03 +02:00
|
|
|
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)
|
|
|
|
):
|
2024-04-14 01:42:51 +02:00
|
|
|
chargepoint = db.get(DbChargePoint, chargepoint_id)
|
2024-04-13 22:43:03 +02:00
|
|
|
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)
|
|
|
|
):
|
2024-04-14 01:42:51 +02:00
|
|
|
chargepoint = db.get(DbChargePoint, chargepoint_id)
|
2024-04-13 22:43:03 +02:00
|
|
|
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)
|
|
|
|
|
2024-04-13 22:55:43 +02:00
|
|
|
@router.post(path="", status_code=201, response_model=ChargePoint)
|
2024-04-13 22:43:03 +02:00
|
|
|
async def create_chargepoint(
|
|
|
|
chargepoint: ChargePointCreate,
|
|
|
|
api_key: str = Security(get_api_key),
|
|
|
|
db: Session = Depends(get_db)
|
|
|
|
):
|
2024-04-14 01:42:51 +02:00
|
|
|
chargepoint_db = DbChargePoint(
|
2024-04-19 00:08:29 +02:00
|
|
|
identity=chargepoint.identity,
|
2024-04-13 22:43:03 +02:00
|
|
|
is_active=chargepoint.is_active,
|
2024-04-19 00:08:29 +02:00
|
|
|
password=''.join(random.choice(string.ascii_letters + string.digits) for i in range(24)),
|
|
|
|
price=chargepoint.price
|
2024-04-13 22:43:03 +02:00
|
|
|
)
|
|
|
|
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)
|
|
|
|
):
|
2024-04-14 01:42:51 +02:00
|
|
|
chargepoint = db.get(DbChargePoint, chargepoint_id)
|
2024-04-13 22:43:03 +02:00
|
|
|
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
|
|
|
|
|
2024-04-13 22:55:43 +02:00
|
|
|
@router.delete(path="/{chargepoint_id}", response_model=None)
|
2024-04-13 22:43:03 +02:00
|
|
|
async def delete_chargepoint(
|
|
|
|
chargepoint_id: UUID,
|
|
|
|
api_key: str = Security(get_api_key),
|
|
|
|
db: Session = Depends(get_db)
|
|
|
|
):
|
2024-04-14 01:42:51 +02:00
|
|
|
chargepoint = db.get(DbChargePoint, chargepoint_id)
|
2024-04-13 22:43:03 +02:00
|
|
|
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)
|
|
|
|
)
|
2024-04-14 22:56:51 +02:00
|
|
|
|
|
|
|
@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:
|
2024-04-15 00:03:32 +02:00
|
|
|
raise HTTPException(status_code=503, detail="Chargepoint not connected.")
|
|
|
|
try:
|
|
|
|
response = await chargepoint_manager.call(
|
|
|
|
chargepoint_id,
|
|
|
|
payload=ResetPayload(type=reset_request.type, evse_id=reset_request.evse_id)
|
|
|
|
)
|
|
|
|
return ChargePointResetResponse(status=response.status)
|
|
|
|
except TimeoutError:
|
|
|
|
raise HTTPException(status_code=503, detail="Chargepoint didn't respond in time.")
|
2024-04-19 00:08:29 +02:00
|
|
|
|
|
|
|
@router.post(path="/{chargepoint_id}/token-learning", status_code=201, response_model=IdTokenLearnResponse)
|
|
|
|
async def create_id_token_learn_request(
|
|
|
|
chargepoint_id: UUID,
|
|
|
|
learn_request: IdTokenLearnRequest,
|
|
|
|
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")
|
|
|
|
|
|
|
|
owner = db.get(DbUser, learn_request.user_id)
|
|
|
|
if owner == None:
|
|
|
|
raise HTTPException(status_code=422, detail=[{
|
|
|
|
"loc": ["body", "user_id"],
|
|
|
|
"msg": "Target user not found",
|
|
|
|
"type": "invalid_relation"
|
|
|
|
}])
|
|
|
|
|
|
|
|
chargepoint.learn_user_id = learn_request.user_id
|
|
|
|
|
|
|
|
if learn_request.until == None:
|
|
|
|
chargepoint.learn_until = datetime.now(UTC) + timedelta(minutes=5)
|
|
|
|
else:
|
|
|
|
chargepoint.learn_until = learn_request.until
|
|
|
|
|
|
|
|
db.commit()
|
|
|
|
|
|
|
|
return IdTokenLearnResponse(
|
|
|
|
user_id=chargepoint.learn_user_id,
|
|
|
|
until=chargepoint.learn_until
|
|
|
|
)
|
|
|
|
|
|
|
|
@router.get(path="/{chargepoint_id}/token-learning", response_model=IdTokenLearnResponse)
|
|
|
|
async def get_id_token_learn_request(
|
|
|
|
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")
|
|
|
|
|
|
|
|
if chargepoint.learn_user_id == None:
|
|
|
|
raise HTTPException(status_code=404, detail="No active learning request")
|
|
|
|
|
|
|
|
return IdTokenLearnResponse(
|
|
|
|
user_id=chargepoint.learn_user_id,
|
|
|
|
until=chargepoint.learn_until
|
|
|
|
)
|
|
|
|
|
|
|
|
@router.delete(path="/{chargepoint_id}/token-learning", response_model=[])
|
|
|
|
async def get_id_token_learn_request(
|
|
|
|
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")
|
|
|
|
|
|
|
|
if chargepoint.learn_user_id == None:
|
|
|
|
raise HTTPException(status_code=404, detail="No active learning request")
|
|
|
|
|
|
|
|
chargepoint.learn_user_id = None
|
|
|
|
chargepoint.learn_until = None
|
|
|
|
db.commit()
|
|
|
|
|
|
|
|
return []
|
2024-04-20 02:36:46 +02:00
|
|
|
|
|
|
|
@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.")
|