mobilityhouse / ocpp

Python implementation of the Open Charge Point Protocol (OCPP).
MIT License
788 stars 310 forks source link

ocpp library as an event based message handler #168

Open villekr opened 3 years ago

villekr commented 3 years ago

Enhancement issue based on original discussion topic: https://github.com/mobilityhouse/ocpp/discussions/165.

Main motivation for this is to:

villekr commented 3 years ago

Current branch in PR https://github.com/mobilityhouse/ocpp/pull/171 supports now the following usages:

  1. Current way of bundling both websocket connection and routes within one class, which derives from version specific ChargePoint classes (see examples/v16/central_system.py)
  2. (New) stateful event based way where Central System holds Charging Station instances and based on id routes messages to correct instance (see examples/v16/central_system_asgi_stateful.py).
  3. (New) stateless event based way where messages are routed directly to correct action handler and it's up to action handler to figure out based on context-argument charging_station related information (see examples/v16/central_system_asgi_stateless.py).

Both new ways implement ASGI interface and therefore must be run behind ASGI server (e.g. uvicorn).

Current implementation should now give clear idea of change but will still require quite a much polishing to make usage clear.

The final goal is to implement also ASGI http.request handling but this will be out of scope of ocpp library in my opinion as the implementation will be heavily environment specific (e.g. AWS).

villekr commented 3 years ago

I've been lately too busy on other work so haven't been able to proceed any further with this. However, my plan is to next try to make a "ASGI first" version and then try to think how current ocpp implementation would fit to that model.

villekr commented 3 years ago

Quite a much time has passed since I had time to proceed with this change. But now I have a next iteration draft ready. Let me first review what has been done so far:

The code is by no means polished but the intention should be now clear. Please review the code a let me know what you think about the road ahead. I have no specific interest to start maintaining this library specifically so if this ocpp library can be extented to this direction then I'm happy to collaborate. However, at the same time I feel that ocpp-asgi -library tries to solve a different problem as ocpp -library. So in a way, it could also be it's own library.

villekr commented 3 years ago

I've worked further to enable even based message handling (and ultimately running OCPP as serverless e.g with AWS Lambda). The library has evolved to 0.2.0 version, which could be stated as an alpha version. It has ASGI compliant interface and supports WebSocket as well as HTTP events (HTTP is what makes serverless possible).

ASGI approach is relevant only when implementing Central System so it's even more clear that this must be a separate library.

Please see and if anyone is interested or has business need for something like this then please let me know. https://github.com/villekr/ocpp-asgi

I've also described the motivation and cloud architecture iterations that have inspired to continue this work. https://ville-karkkainen.medium.com/towards-event-based-serverless-ocpp-backend-system-part-i-motivation-and-architecture-options-5d51ba09dfd6