Add variable management
This commit is contained in:
parent
a65dee8962
commit
0ea0cb9d98
7 changed files with 287 additions and 13 deletions
|
@ -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()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue