2024-04-13 22:43:03 +02:00
|
|
|
from datetime import datetime, UTC
|
|
|
|
import os
|
|
|
|
|
2024-04-20 02:36:46 +02:00
|
|
|
from ocpp.routing import on, after
|
2024-04-13 22:43:03 +02:00
|
|
|
from ocpp.v201 import ChargePoint as cp
|
|
|
|
from ocpp.v201 import call_result
|
2024-07-25 20:59:40 +02:00
|
|
|
from ocpp.v201.enums import Action, RegistrationStatusType, TransactionEventType
|
2024-04-20 02:36:46 +02:00
|
|
|
from ocpp.v201.call import GetBaseReportPayload
|
2024-04-13 22:43:03 +02:00
|
|
|
|
2024-07-25 20:59:40 +02:00
|
|
|
from app.services import (
|
|
|
|
variable_service,
|
|
|
|
id_token_service,
|
|
|
|
chargepoint_service,
|
|
|
|
transaction_service
|
|
|
|
)
|
2024-04-13 22:43:03 +02:00
|
|
|
|
|
|
|
class ChargePoint(cp):
|
|
|
|
|
|
|
|
@on(Action.BootNotification)
|
2024-04-14 17:55:26 +02:00
|
|
|
async def on_boot_notification(self, charging_station, **kwargs):
|
2024-07-25 20:59:40 +02:00
|
|
|
await chargepoint_service.update_attributes(
|
|
|
|
chargepoint_identity=self.id,
|
|
|
|
charging_station=charging_station
|
|
|
|
)
|
2024-04-13 22:43:03 +02:00
|
|
|
return call_result.BootNotificationPayload(
|
|
|
|
current_time=datetime.now(UTC).isoformat(),
|
|
|
|
interval=int(os.getenv("CS_HEARTBEAT_INTERVAL", "1800")),
|
|
|
|
status=RegistrationStatusType.accepted
|
|
|
|
)
|
|
|
|
|
2024-04-20 02:36:46 +02:00
|
|
|
@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):
|
2024-07-25 20:59:40 +02:00
|
|
|
for entry in report_data:
|
|
|
|
await variable_service.create_or_update_variable(
|
|
|
|
chargepoint_identity=self.id,
|
|
|
|
report_entry=entry
|
|
|
|
)
|
2024-04-20 02:36:46 +02:00
|
|
|
return call_result.NotifyReportPayload()
|
|
|
|
|
2024-04-13 22:43:03 +02:00
|
|
|
@on(Action.Heartbeat)
|
|
|
|
async def on_heartbeat_request(self):
|
|
|
|
return call_result.HeartbeatPayload(
|
|
|
|
current_time=datetime.now(UTC).isoformat()
|
|
|
|
)
|
|
|
|
|
2024-07-25 20:59:40 +02:00
|
|
|
@after(Action.Heartbeat)
|
|
|
|
async def after_heartbeat_request(self):
|
|
|
|
await chargepoint_service.update_last_seen(chargepoint_identity=self.id)
|
|
|
|
|
2024-04-13 22:43:03 +02:00
|
|
|
@on(Action.StatusNotification)
|
|
|
|
async def on_status_notification(self, evse_id: int, connector_id: int, connector_status: str, **kwargs):
|
2024-07-25 20:59:40 +02:00
|
|
|
await chargepoint_service.create_or_update_connector(
|
|
|
|
chargepoint_identity=self.id,
|
|
|
|
evse_id=evse_id,
|
|
|
|
connector_id=connector_id,
|
|
|
|
connector_status=connector_status
|
|
|
|
)
|
2024-04-13 22:43:03 +02:00
|
|
|
return call_result.StatusNotificationPayload()
|
|
|
|
|
|
|
|
@on(Action.Authorize)
|
|
|
|
async def on_authorize(self, id_token, **kwargs):
|
2024-07-25 20:59:40 +02:00
|
|
|
id_token_info, _ = await id_token_service.get_id_token_info(chargepoint_id=self.id, id_token=id_token)
|
2024-04-19 00:08:29 +02:00
|
|
|
return call_result.AuthorizePayload(id_token_info)
|
|
|
|
|
|
|
|
@on(Action.TransactionEvent)
|
|
|
|
async def on_transaction_event(
|
|
|
|
self,
|
|
|
|
event_type,
|
|
|
|
timestamp,
|
|
|
|
trigger_reason,
|
|
|
|
transaction_info,
|
|
|
|
**kwargs
|
|
|
|
):
|
2024-07-25 20:59:40 +02:00
|
|
|
|
2024-04-20 19:06:39 +02:00
|
|
|
if "id_token" in kwargs.keys():
|
2024-07-25 20:59:40 +02:00
|
|
|
id_token_info, token_owner_id = await id_token_service.get_id_token_info(chargepoint_id=self.id, id_token=kwargs['id_token'])
|
|
|
|
else:
|
|
|
|
id_token_info = None
|
|
|
|
token_owner_id = None
|
2024-04-13 22:43:03 +02:00
|
|
|
|
2024-07-25 20:59:40 +02:00
|
|
|
if event_type == str(TransactionEventType.started):
|
|
|
|
await transaction_service.create_transaction(
|
|
|
|
chargepoint_identity=self.id,
|
|
|
|
user_id=token_owner_id,
|
|
|
|
timestamp=datetime.fromisoformat(timestamp),
|
|
|
|
transaction_info=transaction_info,
|
|
|
|
transaction_data=kwargs
|
|
|
|
)
|
|
|
|
elif event_type == str(TransactionEventType.updated):
|
|
|
|
await transaction_service.update_transaction(
|
|
|
|
transaction_id=transaction_info["transaction_id"],
|
|
|
|
transaction_data=kwargs
|
|
|
|
)
|
|
|
|
elif event_type == str(TransactionEventType.ended):
|
|
|
|
await transaction_service.end_transaction(
|
|
|
|
transaction_id=transaction_info["transaction_id"],
|
|
|
|
timestamp=datetime.fromisoformat(timestamp),
|
|
|
|
trigger_reason=trigger_reason,
|
|
|
|
transaction_data=kwargs,
|
|
|
|
user_id=token_owner_id
|
|
|
|
)
|
2024-04-19 00:08:29 +02:00
|
|
|
|
2024-07-25 20:59:40 +02:00
|
|
|
if id_token_info == None:
|
|
|
|
return call_result.TransactionEventPayload()
|
|
|
|
else:
|
2024-04-19 00:08:29 +02:00
|
|
|
return call_result.TransactionEventPayload(id_token_info=id_token_info)
|
2024-04-20 17:45:20 +02:00
|
|
|
|
|
|
|
@on(Action.MeterValues)
|
|
|
|
async def on_meter_values(self, **kwargs):
|
|
|
|
return call_result.MeterValuesPayload()
|
2024-04-20 17:46:46 +02:00
|
|
|
|
|
|
|
@on(Action.SecurityEventNotification)
|
|
|
|
async def on_security_event_notification(self, **kwargs):
|
|
|
|
return call_result.SecurityEventNotificationPayload()
|