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, timestamp,
trigger_reason, trigger_reason,
transaction_info, transaction_info,
id_token,
meter_value,
**kwargs **kwargs
): ):
if id_token != None: if "id_token" in kwargs.keys():
id_token_info, token_owner_id = await self.__get_id_token_info(id_token) id_token_info, token_owner_id = await self.__get_id_token_info(kwargs['id_token'])
with SessionLocal() as db: with SessionLocal() as db:
chargepoint = db.query(DbChargePoint).filter(DbChargePoint.identity == self.id).first() chargepoint = db.query(DbChargePoint).filter(DbChargePoint.identity == self.id).first()
@ -160,8 +158,8 @@ class ChargePoint(cp):
if event_type == str(TransactionEventType.started): if event_type == str(TransactionEventType.started):
meter_start=0 meter_start=0
if meter_value != None: if "meter_value" in kwargs.keys():
for meter_value_entry in meter_value: for meter_value_entry in kwargs['meter_value']:
for sampled_value in meter_value_entry['sampled_value']: for sampled_value in meter_value_entry['sampled_value']:
if "measurand" in sampled_value.keys(): if "measurand" in sampled_value.keys():
if sampled_value['measurand'] == str(Measurand.ENERGY_ACTIVE_IMPORT_REGISTER): if sampled_value['measurand'] == str(Measurand.ENERGY_ACTIVE_IMPORT_REGISTER):
@ -176,14 +174,14 @@ class ChargePoint(cp):
price=chargepoint.price, price=chargepoint.price,
chargepoint_id=chargepoint.id chargepoint_id=chargepoint.id
) )
if id_token != None: if "id_token" in kwargs.keys():
if id_token_info.status == AuthorizationStatusType.accepted: if id_token_info.status == AuthorizationStatusType.accepted:
transaction.user_id = UUID(token_owner_id) transaction.user_id = UUID(token_owner_id)
db.add(transaction) db.add(transaction)
elif event_type == str(TransactionEventType.updated): elif event_type == str(TransactionEventType.updated):
transaction = db.get(DbTransaction, transaction_info["transaction_id"]) transaction = db.get(DbTransaction, transaction_info["transaction_id"])
if meter_value != None: if "meter_value" in kwargs.keys():
for meter_value_entry in meter_value: for meter_value_entry in kwargs['meter_value']:
timestamp = datetime.fromisoformat(meter_value_entry['timestamp']) timestamp = datetime.fromisoformat(meter_value_entry['timestamp'])
for sampled_value in meter_value_entry['sampled_value']: for sampled_value in meter_value_entry['sampled_value']:
db_meter_value = DbMeterValue() db_meter_value = DbMeterValue()
@ -202,7 +200,7 @@ class ChargePoint(cp):
db_meter_value.unit = "Wh" db_meter_value.unit = "Wh"
db_meter_value.value = sampled_value['value'] db_meter_value.value = sampled_value['value']
db.add(db_meter_value) db.add(db_meter_value)
if id_token != None: if "id_token" in kwargs.keys():
if id_token_info.status == AuthorizationStatusType.accepted: if id_token_info.status == AuthorizationStatusType.accepted:
transaction.user_id = UUID(token_owner_id) transaction.user_id = UUID(token_owner_id)
elif event_type == str(TransactionEventType.ended): elif event_type == str(TransactionEventType.ended):
@ -211,8 +209,8 @@ class ChargePoint(cp):
transaction.ended_at = datetime.fromisoformat(timestamp) transaction.ended_at = datetime.fromisoformat(timestamp)
transaction.end_reason = TransactionEventTriggerReason(trigger_reason) transaction.end_reason = TransactionEventTriggerReason(trigger_reason)
meter_end=0 meter_end=0
if meter_value != None: if "meter_value" in kwargs.keys():
for meter_value_entry in meter_value: for meter_value_entry in kwargs['meter_value']:
for sampled_value in meter_value_entry['sampled_value']: for sampled_value in meter_value_entry['sampled_value']:
if "measurand" in sampled_value.keys(): if "measurand" in sampled_value.keys():
if sampled_value['measurand'] == str(Measurand.ENERGY_ACTIVE_IMPORT_REGISTER): if sampled_value['measurand'] == str(Measurand.ENERGY_ACTIVE_IMPORT_REGISTER):
@ -220,12 +218,12 @@ class ChargePoint(cp):
else: else:
meter_end = sampled_value['value'] meter_end = sampled_value['value']
transaction.meter_end = meter_end transaction.meter_end = meter_end
if id_token != None: if "id_token" in kwargs.keys():
if id_token_info.status == AuthorizationStatusType.accepted: if id_token_info.status == AuthorizationStatusType.accepted:
transaction.user_id = UUID(token_owner_id) transaction.user_id = UUID(token_owner_id)
db.commit() 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(id_token_info=id_token_info)
return call_result.TransactionEventPayload() return call_result.TransactionEventPayload()