From 332bdc00d7182ba6d4d9e1243f1f841a26f5876d Mon Sep 17 00:00:00 2001 From: BluemediaGER Date: Sat, 20 Apr 2024 19:06:39 +0200 Subject: [PATCH] Fix transaction handling --- app/ocpp_proto/chargepoint.py | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/app/ocpp_proto/chargepoint.py b/app/ocpp_proto/chargepoint.py index 4c34ed2..c529f42 100644 --- a/app/ocpp_proto/chargepoint.py +++ b/app/ocpp_proto/chargepoint.py @@ -147,12 +147,10 @@ class ChargePoint(cp): timestamp, trigger_reason, transaction_info, - id_token, - meter_value, **kwargs ): - if id_token != None: - id_token_info, token_owner_id = await self.__get_id_token_info(id_token) + if "id_token" in kwargs.keys(): + id_token_info, token_owner_id = await self.__get_id_token_info(kwargs['id_token']) with SessionLocal() as db: chargepoint = db.query(DbChargePoint).filter(DbChargePoint.identity == self.id).first() @@ -160,8 +158,8 @@ class ChargePoint(cp): if event_type == str(TransactionEventType.started): meter_start=0 - if meter_value != None: - for meter_value_entry in meter_value: + if "meter_value" in kwargs.keys(): + for meter_value_entry in kwargs['meter_value']: for sampled_value in meter_value_entry['sampled_value']: if "measurand" in sampled_value.keys(): if sampled_value['measurand'] == str(Measurand.ENERGY_ACTIVE_IMPORT_REGISTER): @@ -176,14 +174,14 @@ class ChargePoint(cp): price=chargepoint.price, chargepoint_id=chargepoint.id ) - if id_token != None: + if "id_token" in kwargs.keys(): if id_token_info.status == AuthorizationStatusType.accepted: transaction.user_id = UUID(token_owner_id) db.add(transaction) elif event_type == str(TransactionEventType.updated): transaction = db.get(DbTransaction, transaction_info["transaction_id"]) - if meter_value != None: - for meter_value_entry in meter_value: + if "meter_value" in kwargs.keys(): + for meter_value_entry in kwargs['meter_value']: timestamp = datetime.fromisoformat(meter_value_entry['timestamp']) for sampled_value in meter_value_entry['sampled_value']: db_meter_value = DbMeterValue() @@ -202,7 +200,7 @@ class ChargePoint(cp): db_meter_value.unit = "Wh" db_meter_value.value = sampled_value['value'] db.add(db_meter_value) - if id_token != None: + if "id_token" in kwargs.keys(): if id_token_info.status == AuthorizationStatusType.accepted: transaction.user_id = UUID(token_owner_id) elif event_type == str(TransactionEventType.ended): @@ -211,8 +209,8 @@ class ChargePoint(cp): transaction.ended_at = datetime.fromisoformat(timestamp) transaction.end_reason = TransactionEventTriggerReason(trigger_reason) meter_end=0 - if meter_value != None: - for meter_value_entry in meter_value: + if "meter_value" in kwargs.keys(): + for meter_value_entry in kwargs['meter_value']: for sampled_value in meter_value_entry['sampled_value']: if "measurand" in sampled_value.keys(): if sampled_value['measurand'] == str(Measurand.ENERGY_ACTIVE_IMPORT_REGISTER): @@ -220,12 +218,12 @@ class ChargePoint(cp): else: meter_end = sampled_value['value'] transaction.meter_end = meter_end - if id_token != None: + if "id_token" in kwargs.keys(): if id_token_info.status == AuthorizationStatusType.accepted: transaction.user_id = UUID(token_owner_id) db.commit() - if id_token != None: + if "id_token" in kwargs.keys(): return call_result.TransactionEventPayload(id_token_info=id_token_info) return call_result.TransactionEventPayload()