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
|
2025-03-12 23:42:02 +00:00
|
|
|
from ocpp.v201.enums import Action, RegistrationStatusEnumType, TransactionEventEnumType
|
|
|
|
from ocpp.v201.call import GetBaseReport
|
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):
|
|
|
|
|
2025-03-12 23:42:02 +00:00
|
|
|
@on(Action.boot_notification)
|
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
|
|
|
|
)
|
2025-03-12 23:42:02 +00:00
|
|
|
return call_result.BootNotification(
|
2024-04-13 22:43:03 +02:00
|
|
|
current_time=datetime.now(UTC).isoformat(),
|
|
|
|
interval=int(os.getenv("CS_HEARTBEAT_INTERVAL", "1800")),
|
2025-03-12 23:42:02 +00:00
|
|
|
status=RegistrationStatusEnumType.accepted
|
2024-04-13 22:43:03 +02:00
|
|
|
)
|
|
|
|
|
2025-03-12 23:42:02 +00:00
|
|
|
@after(Action.boot_notification)
|
2024-04-20 02:36:46 +02:00
|
|
|
async def after_boot_notification(self, **kwargs):
|
2025-03-12 23:42:02 +00:00
|
|
|
await self.call(payload=GetBaseReport(request_id=0, report_base="FullInventory"))
|
2024-04-20 02:36:46 +02:00
|
|
|
|
2025-03-12 23:42:02 +00:00
|
|
|
@on(Action.notify_report)
|
2024-04-20 02:36:46 +02:00
|
|
|
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
|
|
|
|
)
|
2025-03-12 23:42:02 +00:00
|
|
|
return call_result.NotifyReport()
|
2024-04-20 02:36:46 +02:00
|
|
|
|
2025-03-12 23:42:02 +00:00
|
|
|
@on(Action.heartbeat)
|
2024-04-13 22:43:03 +02:00
|
|
|
async def on_heartbeat_request(self):
|
2025-03-12 23:42:02 +00:00
|
|
|
return call_result.Heartbeat(
|
2024-04-13 22:43:03 +02:00
|
|
|
current_time=datetime.now(UTC).isoformat()
|
|
|
|
)
|
|
|
|
|
2025-03-12 23:42:02 +00:00
|
|
|
@after(Action.heartbeat)
|
2024-07-25 20:59:40 +02:00
|
|
|
async def after_heartbeat_request(self):
|
|
|
|
await chargepoint_service.update_last_seen(chargepoint_identity=self.id)
|
|
|
|
|
2025-03-12 23:42:02 +00:00
|
|
|
@on(Action.status_notification)
|
2024-04-13 22:43:03 +02:00
|
|
|
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
|
|
|
|
)
|
2025-03-12 23:42:02 +00:00
|
|
|
return call_result.StatusNotification()
|
2024-04-13 22:43:03 +02:00
|
|
|
|
2025-03-12 23:42:02 +00:00
|
|
|
@on(Action.authorize)
|
2024-04-13 22:43:03 +02:00
|
|
|
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)
|
2025-03-12 23:42:02 +00:00
|
|
|
return call_result.Authorize(id_token_info)
|
2024-04-19 00:08:29 +02:00
|
|
|
|
2025-03-12 23:42:02 +00:00
|
|
|
@on(Action.transaction_event)
|
2024-04-19 00:08:29 +02:00
|
|
|
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
|
|
|
|
2025-03-12 23:42:02 +00:00
|
|
|
if event_type == str(TransactionEventEnumType.started):
|
2024-07-25 20:59:40 +02:00
|
|
|
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
|
|
|
|
)
|
2025-03-12 23:42:02 +00:00
|
|
|
elif event_type == str(TransactionEventEnumType.updated):
|
2024-07-25 20:59:40 +02:00
|
|
|
await transaction_service.update_transaction(
|
|
|
|
transaction_id=transaction_info["transaction_id"],
|
|
|
|
transaction_data=kwargs
|
|
|
|
)
|
2025-03-12 23:42:02 +00:00
|
|
|
elif event_type == str(TransactionEventEnumType.ended):
|
2024-07-25 20:59:40 +02:00
|
|
|
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:
|
2025-03-12 23:42:02 +00:00
|
|
|
return call_result.TransactionEvent()
|
2024-07-25 20:59:40 +02:00
|
|
|
else:
|
2025-03-12 23:42:02 +00:00
|
|
|
return call_result.TransactionEvent(id_token_info=id_token_info)
|
2024-04-20 17:45:20 +02:00
|
|
|
|
2025-03-12 23:42:02 +00:00
|
|
|
@on(Action.meter_values)
|
2024-04-20 17:45:20 +02:00
|
|
|
async def on_meter_values(self, **kwargs):
|
2025-03-12 23:42:02 +00:00
|
|
|
return call_result.MeterValues()
|
2024-04-20 17:46:46 +02:00
|
|
|
|
2025-03-12 23:42:02 +00:00
|
|
|
@on(Action.security_event_notification)
|
2024-04-20 17:46:46 +02:00
|
|
|
async def on_security_event_notification(self, **kwargs):
|
2025-03-12 23:42:02 +00:00
|
|
|
return call_result.SecurityEventNotification()
|