diff --git a/app/ocpp_proto/chargepoint.py b/app/ocpp_proto/chargepoint.py index d2accb2..f2fb317 100644 --- a/app/ocpp_proto/chargepoint.py +++ b/app/ocpp_proto/chargepoint.py @@ -4,10 +4,11 @@ import os from ocpp.routing import on from ocpp.v201 import ChargePoint as cp 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.models.chargepoint import ChargePoint +from app.models.chargepoint import ChargePoint as DbChargePoint from app.models.connector import Connector from app.models.id_token import IdToken from app.schemas.connector import ConnectorStatus @@ -17,7 +18,7 @@ class ChargePoint(cp): @on(Action.BootNotification) async def on_boot_notification(self, charging_station, reason, **kwargs): 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.commit() return call_result.BootNotificationPayload( @@ -29,7 +30,7 @@ class ChargePoint(cp): @on(Action.Heartbeat) async def on_heartbeat_request(self): 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.commit() return call_result.HeartbeatPayload( @@ -39,7 +40,7 @@ class ChargePoint(cp): @on(Action.StatusNotification) async def on_status_notification(self, evse_id: int, connector_id: int, connector_status: str, **kwargs): 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_connector = db.query(Connector).filter( @@ -64,24 +65,38 @@ class ChargePoint(cp): @on(Action.Authorize) async def on_authorize(self, id_token, **kwargs): - if id_token == None: - return call_result.AuthorizePayload(id_token_info={'status': AuthorizationStatusType.invalid}) - if id_token.type != "ISO14443" | "ISO15693": - return call_result.AuthorizePayload(id_token_info={'status': AuthorizationStatusType.invalid}) + if id_token["type"] not in ["ISO14443", "ISO15693"]: + return call_result.AuthorizePayload( + id_token_info=IdTokenInfoType( + status=AuthorizationStatusType.invalid + ) + ) 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_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() - if db_id_token == None: - return call_result.AuthorizePayload(id_token_info={'status': AuthorizationStatusType.unknown}) - if db_id_token.is_active == False: - return call_result.AuthorizePayload(id_token_info={'status': AuthorizationStatusType.blocked}) - - return call_result.AuthorizePayload(id_token_info={'status': AuthorizationStatusType.accepted, 'groupIdToken': str(db_id_token.owner_id)}) + if db_id_token == None: + id_token_info=IdTokenInfoType( + status=AuthorizationStatusType.unknown + ) + else: + 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) async def on_transaction_event(self):