simple-ocpp-cs/app/services/meter_value_service.py

31 lines
1.3 KiB
Python
Raw Permalink Normal View History

2024-07-25 20:59:40 +02:00
from datetime import datetime
from uuid import UUID
from app.database import SessionLocal
from app.models.meter_value import MeterValue
from app.schemas.meter_value import Measurand, PhaseType
async def create_meter_value(transaction_id: UUID, meter_value_data):
with SessionLocal() as db:
timestamp = datetime.fromisoformat(meter_value_data['timestamp'])
for sampled_value in meter_value_data['sampled_value']:
db_meter_value = MeterValue()
db_meter_value.transaction_id = transaction_id
db_meter_value.timestamp = timestamp
if "measurand" in sampled_value.keys():
db_meter_value.measurand = Measurand(sampled_value['measurand'])
else:
db_meter_value.measurand = Measurand.ENERGY_ACTIVE_IMPORT_REGISTER
if "phase" in sampled_value.keys():
db_meter_value.phase_type = PhaseType(sampled_value['phase'])
if "unit_of_measure" in sampled_value.keys():
if "unit" in sampled_value['unit_of_measure']:
db_meter_value.unit = sampled_value['unit_of_measure']['unit']
else:
db_meter_value.unit = "Wh"
db_meter_value.value = sampled_value['value']
db.add(db_meter_value)
db.commit()