simple-ocpp-cs/app/routers/chargepoint_v1.py

115 lines
4 KiB
Python
Raw Normal View History

2024-04-13 22:43:03 +02:00
import random
import string
from uuid import UUID
from fastapi import APIRouter, HTTPException, Security
from fastapi.params import Depends
from sqlalchemy.orm import Session
from app.database import get_db
from app.ocpp_proto import chargepoint_manager
from app.schemas.chargepoint import ChargePoint, ChargePointCreate, ChargePointUpdate, ChargePointPassword, ChargePointConnectionInfo
2024-04-14 01:42:51 +02:00
from app.models.chargepoint import ChargePoint as DbChargePoint
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
)
@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)
@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-13 22:43:03 +02:00
friendly_name=chargepoint.friendly_name,
is_active=chargepoint.is_active,
password=''.join(random.choice(string.ascii_letters + string.digits) for i in range(24))
)
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
@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)
)