diff --git a/app/routers/transaction_v1.py b/app/routers/transaction_v1.py index 66139e1..faa8fe7 100644 --- a/app/routers/transaction_v1.py +++ b/app/routers/transaction_v1.py @@ -1,10 +1,13 @@ -from fastapi import APIRouter, Depends +from fastapi import APIRouter, Depends, HTTPException from fastapi.params import Security from sqlalchemy.orm import Session +from ocpp.v201.call import RequestStopTransactionPayload + +from app.ocpp_proto import chargepoint_manager from app.security import get_api_key from app.database import get_db -from app.schemas.transaction import Transaction +from app.schemas.transaction import Transaction, RemoteTransactionStartStopResponse, TransactionStatus, RemoteTransactionStartStopStatus from app.models.transaction import Transaction as DbTransaction router = APIRouter( @@ -19,4 +22,45 @@ async def get_transactions( api_key: str = Security(get_api_key), db: Session = Depends(get_db) ): - return db.query(DbTransaction).offset(skip).limit(limit).all() \ No newline at end of file + return db.query(DbTransaction).offset(skip).limit(limit).all() + +@router.get(path="/{transaction_id}", response_model=Transaction) +async def get_transaction( + transaction_id: str, + api_key: str = Security(get_api_key), + db: Session = Depends(get_db) +): + transaction = db.get(DbTransaction, transaction_id) + if transaction == None: + raise HTTPException(404, "Transaction not found") + return transaction + +@router.post(path="/{transaction_id}/remote-stop", response_model=RemoteTransactionStartStopResponse) +async def remote_stop_transaction( + transaction_id: str, + api_key: str = Security(get_api_key), + db: Session = Depends(get_db) +): + transaction = db.get(DbTransaction, transaction_id) + if transaction == None: + raise HTTPException(404, "Transaction not found") + if transaction.status != TransactionStatus.ONGOING: + raise HTTPException(status_code=422, detail=[{ + "loc": ["path", "transaction_id"], + "msg": "Transaction is not ongoing", + "type": "invalid_transaction_state" + }]) + if chargepoint_manager.is_connected(transaction.chargepoint_id) == False: + raise HTTPException(status_code=503, detail="Chargepoint not connected.") + try: + result = await chargepoint_manager.call( + transaction.chargepoint_id, + payload=RequestStopTransactionPayload( + transaction_id=transaction.id + ) + ) + if RemoteTransactionStartStopStatus(result.status) != RemoteTransactionStartStopStatus.REJECTED: + raise HTTPException(status_code=500, detail=result.status) + return RemoteTransactionStartStopResponse(status=result.status) + except TimeoutError: + raise HTTPException(status_code=503, detail="Chargepoint didn't respond in time.") diff --git a/app/schemas/transaction.py b/app/schemas/transaction.py index 9c9009b..d50ccad 100644 --- a/app/schemas/transaction.py +++ b/app/schemas/transaction.py @@ -32,6 +32,10 @@ class TransactionEventTriggerReason(enum.Enum): SIGNED_DATA_RECEIVED = "SignedDataReceived" RESET_COMMAND = "ResetCommand" +class RemoteTransactionStartStopStatus(enum.Enum): + ACCEPTED = "Accepted" + REJECTED = "Rejected" + class Transaction(BaseModel): id: str status: TransactionStatus @@ -45,4 +49,7 @@ class Transaction(BaseModel): chargepoint_id: UUID class Config: - from_attributes = True \ No newline at end of file + from_attributes = True + +class RemoteTransactionStartStopResponse(BaseModel): + status: RemoteTransactionStartStopStatus