2024-04-21 14:17:20 +02:00
|
|
|
from fastapi import APIRouter, Depends, HTTPException
|
2024-04-19 00:08:29 +02:00
|
|
|
from fastapi.params import Security
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
|
2024-04-21 14:17:20 +02:00
|
|
|
from ocpp.v201.call import RequestStopTransactionPayload
|
|
|
|
|
|
|
|
from app.ocpp_proto import chargepoint_manager
|
2024-04-19 00:08:29 +02:00
|
|
|
from app.security import get_api_key
|
|
|
|
from app.database import get_db
|
2024-04-21 14:17:20 +02:00
|
|
|
from app.schemas.transaction import Transaction, RemoteTransactionStartStopResponse, TransactionStatus, RemoteTransactionStartStopStatus
|
2024-04-19 00:08:29 +02:00
|
|
|
from app.models.transaction import Transaction as DbTransaction
|
|
|
|
|
|
|
|
router = APIRouter(
|
|
|
|
prefix="/transactions",
|
|
|
|
tags=["Transaction (v1)"]
|
|
|
|
)
|
|
|
|
|
|
|
|
@router.get(path="", response_model=list[Transaction])
|
|
|
|
async def get_transactions(
|
|
|
|
skip: int = 0,
|
|
|
|
limit: int = 20,
|
|
|
|
api_key: str = Security(get_api_key),
|
|
|
|
db: Session = Depends(get_db)
|
|
|
|
):
|
2024-04-21 14:17:20 +02:00
|
|
|
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.")
|