Send OCPP message through RouterContext #5
Replies: 2 comments 1 reply
-
@villekr To date, I have started a thread inside my on_connect. Inside the thread I have created an async loop that triggers remote_start_transaction. on_connect
some_function
async_function
|
Beta Was this translation helpful? Give feedback.
-
I think the simplest is to use async def on_connect(self, context: RouterContext) -> bool:
print(
f"(CentralSystem) Charging Station id: {context.charging_station_id} subprotocol: {context.subprotocol} connected." # noqa: E501
)
# You can inspect context.scope["headers"] and perform e.g. basic authentication
allow_connection = True
if allow_connection:
# Create task for running any logic that happens during connection setup
asyncio.create_task(self.after_on_connect(context))
return allow_connection async def after_on_connect(self, context: RouterContext):
# Example on how to send message to Charging Station e.g. after connection setup
await asyncio.sleep(1) # Give Charging Station some time once connected
if context.subprotocol == Subprotocol.ocpp16.value:
message = call16.RemoteStartTransactionPayload(id_tag="abc")
router = self.routers[context.subprotocol]
elif context.subprotocol == Subprotocol.ocpp20.value:
id_token = {"idToken": "abc", "type": "Central"}
message = call20.RequestStartTransactionPayload(
id_token=id_token, remote_start_id=123
)
router = self.routers[context.subprotocol]
elif context.subprotocol == Subprotocol.ocpp201.value:
id_token = {"idToken": "abc", "type": "Central"}
message = call201.RequestStartTransactionPayload(
id_token=id_token, remote_start_id=123
)
router = self.routers[context.subprotocol]
else:
raise ValueError(f"Unknown sub-protocol value: {context.subprotocol=}")
response = await router.call(message=message, context=context)
print(f"(Central System) Charging Station {id=} {response=}") This does look a little strange, why CentralSystem should care about different ocpp protocol versions. So alternatively you could implement some generic functions in routers for all Central System initiated ocpp messages. |
Beta Was this translation helpful? Give feedback.
-
I want to send RemoteStartTransaction from a function called in on_connect.
But as my context: RouterContext and send is implemented in HandlerContext.
What can I do?
Beta Was this translation helpful? Give feedback.
All reactions