Fix chargepoint implementation

This commit is contained in:
Oliver Traber 2024-04-14 17:35:36 +02:00
parent e5ae0bd58e
commit d25f7f9838
Signed by: Bluemedia
GPG key ID: C0674B105057136C

View file

@ -4,10 +4,11 @@ import os
from ocpp.routing import on from ocpp.routing import on
from ocpp.v201 import ChargePoint as cp from ocpp.v201 import ChargePoint as cp
from ocpp.v201 import call_result from ocpp.v201 import call_result
from ocpp.v201.enums import Action, RegistrationStatusType, AuthorizationStatusType from ocpp.v201.datatypes import IdTokenInfoType, IdTokenType
from ocpp.v201.enums import Action, RegistrationStatusType, AuthorizationStatusType, IdTokenType as IdTokenEnumType
from app.database import SessionLocal from app.database import SessionLocal
from app.models.chargepoint import ChargePoint from app.models.chargepoint import ChargePoint as DbChargePoint
from app.models.connector import Connector from app.models.connector import Connector
from app.models.id_token import IdToken from app.models.id_token import IdToken
from app.schemas.connector import ConnectorStatus from app.schemas.connector import ConnectorStatus
@ -17,7 +18,7 @@ class ChargePoint(cp):
@on(Action.BootNotification) @on(Action.BootNotification)
async def on_boot_notification(self, charging_station, reason, **kwargs): async def on_boot_notification(self, charging_station, reason, **kwargs):
with SessionLocal() as db: with SessionLocal() as db:
db_chargepoint = db.query(ChargePoint).filter(ChargePoint.friendly_name == self.id).first() db_chargepoint = db.query(DbChargePoint).filter(DbChargePoint.friendly_name == self.id).first()
db_chargepoint.last_seen = datetime.now(UTC) db_chargepoint.last_seen = datetime.now(UTC)
db.commit() db.commit()
return call_result.BootNotificationPayload( return call_result.BootNotificationPayload(
@ -29,7 +30,7 @@ class ChargePoint(cp):
@on(Action.Heartbeat) @on(Action.Heartbeat)
async def on_heartbeat_request(self): async def on_heartbeat_request(self):
with SessionLocal() as db: with SessionLocal() as db:
db_chargepoint = db.query(ChargePoint).filter(ChargePoint.friendly_name == self.id).first() db_chargepoint = db.query(DbChargePoint).filter(DbChargePoint.friendly_name == self.id).first()
db_chargepoint.last_seen = datetime.now(UTC) db_chargepoint.last_seen = datetime.now(UTC)
db.commit() db.commit()
return call_result.HeartbeatPayload( return call_result.HeartbeatPayload(
@ -39,7 +40,7 @@ class ChargePoint(cp):
@on(Action.StatusNotification) @on(Action.StatusNotification)
async def on_status_notification(self, evse_id: int, connector_id: int, connector_status: str, **kwargs): async def on_status_notification(self, evse_id: int, connector_id: int, connector_status: str, **kwargs):
with SessionLocal() as db: with SessionLocal() as db:
db_chargepoint = db.query(ChargePoint).filter(ChargePoint.friendly_name == self.id).first() db_chargepoint = db.query(DbChargePoint).filter(DbChargePoint.friendly_name == self.id).first()
db_chargepoint.last_seen = datetime.now(UTC) db_chargepoint.last_seen = datetime.now(UTC)
db_connector = db.query(Connector).filter( db_connector = db.query(Connector).filter(
@ -64,24 +65,38 @@ class ChargePoint(cp):
@on(Action.Authorize) @on(Action.Authorize)
async def on_authorize(self, id_token, **kwargs): async def on_authorize(self, id_token, **kwargs):
if id_token == None: if id_token["type"] not in ["ISO14443", "ISO15693"]:
return call_result.AuthorizePayload(id_token_info={'status': AuthorizationStatusType.invalid}) return call_result.AuthorizePayload(
if id_token.type != "ISO14443" | "ISO15693": id_token_info=IdTokenInfoType(
return call_result.AuthorizePayload(id_token_info={'status': AuthorizationStatusType.invalid}) status=AuthorizationStatusType.invalid
)
)
with SessionLocal() as db: with SessionLocal() as db:
db_chargepoint = db.query(ChargePoint).filter(ChargePoint.friendly_name == self.id).first() db_chargepoint = db.query(DbChargePoint).filter(DbChargePoint.friendly_name == self.id).first()
db_chargepoint.last_seen = datetime.now(UTC) db_chargepoint.last_seen = datetime.now(UTC)
db_id_token = db.query(IdToken).filter(IdToken.token == id_token.id).first() db_id_token = db.query(IdToken).filter(IdToken.token == id_token["id_token"]).first()
db.commit() db.commit()
if db_id_token == None: if db_id_token == None:
return call_result.AuthorizePayload(id_token_info={'status': AuthorizationStatusType.unknown}) id_token_info=IdTokenInfoType(
if db_id_token.is_active == False: status=AuthorizationStatusType.unknown
return call_result.AuthorizePayload(id_token_info={'status': AuthorizationStatusType.blocked}) )
else:
return call_result.AuthorizePayload(id_token_info={'status': AuthorizationStatusType.accepted, 'groupIdToken': str(db_id_token.owner_id)}) if db_id_token.is_active == False:
id_token_info=IdTokenInfoType(
status=AuthorizationStatusType.blocked
)
else:
id_token_info=IdTokenInfoType(
status=AuthorizationStatusType.accepted,
group_id_token=IdTokenType(
type=IdTokenEnumType.central,
id_token=str(db_id_token.owner_id)
)
)
return call_result.AuthorizePayload(id_token_info)
@on(Action.TransactionEvent) @on(Action.TransactionEvent)
async def on_transaction_event(self): async def on_transaction_event(self):