Fix transaction handling
All checks were successful
ci/woodpecker/push/docker Pipeline was successful

This commit is contained in:
Oliver Traber 2024-04-20 19:06:39 +02:00
parent d2243c4f0b
commit 332bdc00d7

View file

@ -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()