Add Transaction remote stop capability
All checks were successful
ci/woodpecker/push/docker Pipeline was successful
All checks were successful
ci/woodpecker/push/docker Pipeline was successful
This commit is contained in:
parent
9041c922f7
commit
f48a9d71ef
|
@ -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(
|
||||
|
@ -20,3 +23,44 @@ async def get_transactions(
|
|||
db: Session = Depends(get_db)
|
||||
):
|
||||
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.")
|
||||
|
|
|
@ -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
|
||||
|
@ -46,3 +50,6 @@ class Transaction(BaseModel):
|
|||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
class RemoteTransactionStartStopResponse(BaseModel):
|
||||
status: RemoteTransactionStartStopStatus
|
||||
|
|
Loading…
Reference in a new issue