Add variable management

This commit is contained in:
Oliver Traber 2024-04-20 02:36:46 +02:00
parent a65dee8962
commit 0ea0cb9d98
Signed by: Bluemedia
GPG key ID: C0674B105057136C
7 changed files with 287 additions and 13 deletions

View file

@ -2,11 +2,12 @@ from datetime import datetime, UTC
import os
from uuid import UUID
from ocpp.routing import on
from ocpp.routing import on, after
from ocpp.v201 import ChargePoint as cp
from ocpp.v201 import call_result
from ocpp.v201.datatypes import IdTokenInfoType, IdTokenType
from ocpp.v201.enums import Action, RegistrationStatusType, AuthorizationStatusType, IdTokenType as IdTokenEnumType, TransactionEventType
from ocpp.v201.call import GetBaseReportPayload
from app.database import SessionLocal
from app.models.chargepoint import ChargePoint as DbChargePoint
@ -17,6 +18,7 @@ from app.models.meter_value import MeterValue as DbMeterValue
from app.schemas.connector import ConnectorStatus
from app.schemas.transaction import TransactionStatus, TransactionEventTriggerReason
from app.schemas.meter_value import Measurand, PhaseType
from app.ocpp_proto.variable_manager import create_or_update_variable
class ChargePoint(cp):
@ -35,21 +37,43 @@ class ChargePoint(cp):
with SessionLocal() as db:
db_id_token = db.query(DbIdToken).filter(DbIdToken.token == id_token["id_token"]).first()
if db_id_token == None:
return IdTokenInfoType(
id_token_info = IdTokenInfoType(
status=AuthorizationStatusType.unknown
)
if db_id_token.is_active == False:
id_token_info=IdTokenInfoType(
status=AuthorizationStatusType.blocked
)
db_chargepoint = db.query(DbChargePoint).filter(DbChargePoint.identity == self.id).first()
# Learn token if requested
if db_chargepoint.learn_user_id != None:
if db_chargepoint.learn_until.timestamp() < datetime.now(UTC).timestamp():
db_id_token = DbIdToken()
db_id_token.friendly_name = "New token learned by {}".format(self.id)
db_id_token.is_active = True
db_id_token.owner_id = db_chargepoint.learn_user_id
db_id_token.token = id_token["id_token"]
db.add(db_id_token)
id_token_info=IdTokenInfoType(
status=AuthorizationStatusType.accepted,
group_id_token=IdTokenType(
type=IdTokenEnumType.central,
id_token=str(db_id_token.owner_id)
)
)
db_chargepoint.learn_user_id = None
db_chargepoint.learn_until = None
db.commit()
else:
id_token_info=IdTokenInfoType(
status=AuthorizationStatusType.accepted,
group_id_token=IdTokenType(
type=IdTokenEnumType.central,
id_token=str(db_id_token.owner_id)
if db_id_token.is_active == False:
id_token_info=IdTokenInfoType(
status=AuthorizationStatusType.blocked
)
else:
id_token_info=IdTokenInfoType(
status=AuthorizationStatusType.accepted,
group_id_token=IdTokenType(
type=IdTokenEnumType.central,
id_token=str(db_id_token.owner_id)
)
)
)
return id_token_info
@on(Action.BootNotification)
@ -67,6 +91,21 @@ class ChargePoint(cp):
status=RegistrationStatusType.accepted
)
@after(Action.BootNotification)
async def after_boot_notification(self, **kwargs):
await self.call(payload=GetBaseReportPayload(request_id=0, report_base="FullInventory"))
@on(Action.NotifyReport)
async def on_notify_report(self, report_data, **kwargs):
with SessionLocal() as db:
db_chargepoint = db.query(DbChargePoint).filter(DbChargePoint.identity == self.id).first()
for entry in report_data:
await create_or_update_variable(
chargepoint_id=db_chargepoint.id,
report_data=entry
)
return call_result.NotifyReportPayload()
@on(Action.Heartbeat)
async def on_heartbeat_request(self):
await self.__update_last_seen()

View file

@ -0,0 +1,64 @@
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()