simple-ocpp-cs/app/services/chargepoint_service.py

46 lines
1.7 KiB
Python
Raw Permalink Normal View History

2024-07-25 20:59:40 +02:00
from datetime import datetime, UTC
from app.database import SessionLocal
from app.models.chargepoint import ChargePoint
from app.models.connector import Connector
from app.schemas.connector import ConnectorStatus
async def update_last_seen(chargepoint_identity: str):
with SessionLocal() as db:
db_chargepoint = db.query(ChargePoint).filter(ChargePoint.identity == chargepoint_identity).first()
db_chargepoint.last_seen = datetime.now(UTC)
db.commit()
async def update_attributes(chargepoint_identity: str, charging_station):
with SessionLocal() as db:
db_chargepoint = db.query(ChargePoint).filter(ChargePoint.identity == chargepoint_identity).first()
for key in charging_station.keys():
if key in db_chargepoint.__dict__:
setattr(db_chargepoint, key, charging_station[key])
db.commit()
async def create_or_update_connector(
chargepoint_identity: str,
evse_id: int,
connector_id: int,
connector_status: str
):
with SessionLocal() as db:
db_chargepoint = db.query(ChargePoint).filter(ChargePoint.identity == chargepoint_identity).first()
db_connector = db.query(Connector).filter(
Connector.chargepoint_id == db_chargepoint.id,
Connector.evse == evse_id,
Connector.index == connector_id
).first()
if db_connector == None:
db_connector = Connector(
chargepoint_id = db_chargepoint.id,
evse = evse_id,
index = connector_id,
status = ConnectorStatus(connector_status)
)
db.add(db_connector)
else:
db_connector.status = ConnectorStatus(connector_status)
db.commit()