simple-ocpp-cs/app/ocpp_proto/variable_manager.py

64 lines
4.1 KiB
Python
Raw Normal View History

2024-04-20 02:36:46 +02:00
from decimal import Decimal
from uuid import UUID
from app.database import SessionLocal
from app.models.chargepoint_variable import ChargepointVariable as DbChargepointVariable
from app.schemas.chargepoint_variable import AttributeType, DataType, MutabilityType
async def create_or_update_variable(chargepoint_id: UUID, report_data):
with SessionLocal() as db:
for variable_attribute in report_data['variable_attribute']:
query = db.query(DbChargepointVariable).filter(
DbChargepointVariable.chargepoint_id == chargepoint_id,
DbChargepointVariable.component_name == report_data['component']['name'],
DbChargepointVariable.name == report_data['variable']['name']
)
if "instance" in report_data['component'].keys():
query = query.filter(DbChargepointVariable.component_instance == report_data['component']['instance'])
if "evse" in report_data['component'].keys():
query = query.filter(DbChargepointVariable.evse == report_data['component']['evse']['id'])
if "connectorId" in report_data['component']['evse'].keys():
query = query.filter(DbChargepointVariable.connector_id == report_data['component']['evse']['connectorId'])
if "type" in variable_attribute.keys():
query = query.filter(DbChargepointVariable.type == AttributeType(variable_attribute['type']))
else:
query = query.filter(DbChargepointVariable.type == AttributeType.ACTUAL)
db_variable = query.first()
if db_variable == None:
db_variable = DbChargepointVariable()
db_variable.chargepoint_id = chargepoint_id
db_variable.component_name = report_data['component']['name']
db_variable.name = report_data['variable']['name']
if "value" in variable_attribute.keys():
db_variable.value = variable_attribute['value']
if "instance" in report_data['component'].keys():
db_variable.component_instance = report_data['component']['instance']
if "evse" in report_data['component'].keys():
db_variable.evse = report_data['component']['evse']['id']
if "connector_id" in report_data['component']['evse'].keys():
db_variable.connector_id = report_data['component']['evse']['connector_id']
if "constant" in variable_attribute.keys():
db_variable.constant = variable_attribute['constant']
if "persistent" in variable_attribute.keys():
db_variable.constant = variable_attribute['persistent']
if "mutability" in variable_attribute.keys():
db_variable.mutability = MutabilityType(variable_attribute['mutability'])
if "type" in variable_attribute.keys():
db_variable.type = AttributeType(variable_attribute['type'])
if "variable_characteristics" in report_data.keys():
db_variable.data_type = DataType(report_data['variable_characteristics']['data_type'])
if "min_limit" in report_data['variable_characteristics'].keys():
db_variable.min_limit = Decimal(report_data['variable_characteristics']['min_limit'])
if "max_limit" in report_data['variable_characteristics'].keys():
db_variable.max_limit = Decimal(report_data['variable_characteristics']['max_limit'])
if "unit" in report_data['variable_characteristics'].keys():
db_variable.unit = report_data['variable_characteristics']['unit']
if "values_list" in report_data['variable_characteristics'].keys():
db_variable.values_list = report_data['variable_characteristics']['values_list']
db.add(db_variable)
else:
if "value" in variable_attribute.keys():
db_variable.value = variable_attribute['value']
db.commit()