Only persist MeterValues for ongoing transactions
All checks were successful
ci/woodpecker/push/docker Pipeline was successful

This commit is contained in:
Oliver Traber 2024-04-21 20:17:37 +02:00
parent f48a9d71ef
commit b6207a18c7
Signed by: Bluemedia
GPG key ID: C0674B105057136C

View file

@ -180,47 +180,50 @@ class ChargePoint(cp):
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" in kwargs.keys(): if transaction != None:
for meter_value_entry in kwargs['meter_value']: if transaction.status == TransactionStatus.ONGOING:
timestamp = datetime.fromisoformat(meter_value_entry['timestamp']) if "meter_value" in kwargs.keys():
for sampled_value in meter_value_entry['sampled_value']: for meter_value_entry in kwargs['meter_value']:
db_meter_value = DbMeterValue() timestamp = datetime.fromisoformat(meter_value_entry['timestamp'])
db_meter_value.transaction_id = transaction.id for sampled_value in meter_value_entry['sampled_value']:
db_meter_value.timestamp = timestamp db_meter_value = DbMeterValue()
if "measurand" in sampled_value.keys(): db_meter_value.transaction_id = transaction.id
db_meter_value.measurand = Measurand(sampled_value['measurand']) db_meter_value.timestamp = timestamp
else: if "measurand" in sampled_value.keys():
db_meter_value.measurand = Measurand.ENERGY_ACTIVE_IMPORT_REGISTER db_meter_value.measurand = Measurand(sampled_value['measurand'])
if "phase" in sampled_value.keys(): else:
db_meter_value.phase_type = PhaseType(sampled_value['phase']) db_meter_value.measurand = Measurand.ENERGY_ACTIVE_IMPORT_REGISTER
if "unit_of_measure" in sampled_value.keys(): if "phase" in sampled_value.keys():
if "unit" in sampled_value['unit_of_measure']: db_meter_value.phase_type = PhaseType(sampled_value['phase'])
db_meter_value.unit = sampled_value['unit_of_measure']['unit'] if "unit_of_measure" in sampled_value.keys():
else: if "unit" in sampled_value['unit_of_measure']:
db_meter_value.unit = "Wh" db_meter_value.unit = sampled_value['unit_of_measure']['unit']
db_meter_value.value = sampled_value['value'] else:
db.add(db_meter_value) db_meter_value.unit = "Wh"
if "id_token" in kwargs.keys(): db_meter_value.value = sampled_value['value']
if id_token_info.status == AuthorizationStatusType.accepted: db.add(db_meter_value)
transaction.user_id = token_owner_id if "id_token" in kwargs.keys():
if id_token_info.status == AuthorizationStatusType.accepted:
transaction.user_id = token_owner_id
elif event_type == str(TransactionEventType.ended): elif event_type == str(TransactionEventType.ended):
transaction = db.get(DbTransaction, transaction_info["transaction_id"]) transaction = db.get(DbTransaction, transaction_info["transaction_id"])
transaction.status = TransactionStatus.ENDED if transaction != None:
transaction.ended_at = datetime.fromisoformat(timestamp) transaction.status = TransactionStatus.ENDED
transaction.end_reason = TransactionEventTriggerReason(trigger_reason) transaction.ended_at = datetime.fromisoformat(timestamp)
meter_end=0 transaction.end_reason = TransactionEventTriggerReason(trigger_reason)
if "meter_value" in kwargs.keys(): meter_end=0
for meter_value_entry in kwargs['meter_value']: if "meter_value" in kwargs.keys():
for sampled_value in meter_value_entry['sampled_value']: for meter_value_entry in kwargs['meter_value']:
if "measurand" in sampled_value.keys(): for sampled_value in meter_value_entry['sampled_value']:
if sampled_value['measurand'] == str(Measurand.ENERGY_ACTIVE_IMPORT_REGISTER): if "measurand" in sampled_value.keys():
if sampled_value['measurand'] == str(Measurand.ENERGY_ACTIVE_IMPORT_REGISTER):
meter_end = sampled_value['value']
else:
meter_end = sampled_value['value'] meter_end = sampled_value['value']
else: transaction.meter_end = meter_end
meter_end = sampled_value['value'] if "id_token" in kwargs.keys():
transaction.meter_end = meter_end if id_token_info.status == AuthorizationStatusType.accepted:
if "id_token" in kwargs.keys(): transaction.user_id = token_owner_id
if id_token_info.status == AuthorizationStatusType.accepted:
transaction.user_id = token_owner_id
db.commit() db.commit()
if "id_token" in kwargs.keys(): if "id_token" in kwargs.keys():