OCPP-ASGI
ocpp-asgi provides ASGI compliant interface for implementing event-driven server-side support for OCPP protocol with Python. It depends on and extends mobilityhouse/ocpp.
The key features are:
- ASGI compliant interface supporting both WebSocket and HTTP protocols.
- Event-driven and "stateless" approach for implementing action handlers for OCPP messages.
- Highly-scalable and supports serverless (e.g. AWS Lambda, Azure Functions) with compatible ASGI server.
- Requires small and straightforward changes from ocpp to action handlers (but is not backwards compatible).
Read the blog post about the motivation behind creating this library.
Disclaimer!
This library is still in alpha state. At the moment I don't have time nor immediate business need to invest in further development. However, I'm happy to discuss and help if you are considering the benefits of using this library in you project.
Please send email if you have any questions about this library or have some business inquiries in mind.
Getting started
Installation
pip install ocpp-asgi
Also ASGI server is required e.g. uvicorn or mangum when deployed to AWS Lambda with API Gateway.
pip install uvicorn
Action handlers
Decorating OCPP message action handlers follows the similar approach as in ocpp-library. However, you don't need to define classes per OCPP protocol version.
# provisioning_router.py
from datetime import datetime
from ocpp.v16 import call, call_result
from ocpp.v16.enums import Action, RegistrationStatus
from ocpp_asgi.router import HandlerContext, Router, Subprotocol
router = Router(subprotocol=Subprotocol.ocpp16)
@router.on(Action.BootNotification)
async def on_boot_notification(
*, payload: call.BootNotificationPayload, context: HandlerContext
) -> call_result.BootNotificationPayload:
id = context.charging_station_id
print(f"(Central System) on_boot_notification Charging Station {id=}")
# Do something with the payload...
return call_result.BootNotificationPayload(
current_time=datetime.utcnow().isoformat(),
interval=10,
status=RegistrationStatus.accepted,
)
Central System
Central System is a collection of routes. You implement it by subclassing from ocpp_asti.ASGIApplication and overriding necessary methods to accommodate your needs. Here's a minimal example using uvicorn.
# central_system.py
import uvicorn
from provisioning_router import router
from ocpp_asgi.app import ASGIApplication, RouterContext, Subprotocol
class CentralSystem(ASGIApplication):
"""Central System is collection of routers."""
if __name__ == "__main__":
central_system = CentralSystem()
central_system.include_router(router)
subprotocols = f"{Subprotocol.ocpp16}"
headers = [("Sec-WebSocket-Protocol", subprotocols)]
uvicorn.run(central_system, host="0.0.0.0", port=9000, headers=headers)
Run the examples
In order to run the examples clone the ocpp-asgi repository and install dependencies. Poetry and pyenv are recommended.
There are two kind of examples how to implement central system with ocpp-asgi: standalone and serverless. Both examples use same ocpp action handlers (routers).
Standalone example
Run the following commands e.g. in different terminal windows (or run the files within IDE).
Start Central System:
python ./examples/central_system/standalone/central_system.py
Start Charging Station:
python ./examples/charging_station.py
Serverless example
Run the following commands in different terminal windows (or run the files within IDE). Of course when you run the example like this it's not really serverless. But deploying something central_system_http.py to e.g. AWS Lambda and running it with Mangum ASGI server is totally possible.
Start Central System HTTP backend:
python ./examples/central_system/serverless/central_system_http.py
Start Central System WebSocket proxy:
python ./examples/central_system/serverless/central_system_proxy.py
Start Charging Station:
python ./examples/charging_station.py
Metadata
Release files for ocpp-asgi 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| ocpp_asgi-0.4.0.tar.gz | 12.2 kB | Without provenance | Details for ocpp_asgi-0.4.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| ocpp_asgi-0.4.0-py3-none-any.whl | Python 3 | none | any | Details for ocpp_asgi-0.4.0-py3-none-any.whl |
Total release size: 24.2 kB
Release files / ocpp_asgi-0.4.0.tar.gz
| Download URL | ocpp_asgi-0.4.0.tar.gz |
|---|---|
| Size | 12.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
108401967808a3257b5b33a27e6224181aa9f2402aceaa79ea0c11305cb2ee2b
|
|
BLAKE2b-256 checksum How to use checksums |
d796e7155c7bea93a3aa0eea188e8e6940d870c7feb0200696136d260406130e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.12 CPython/3.8.10 Darwin/22.1.0
|
Release files / ocpp_asgi-0.4.0-py3-none-any.whl
| Download URL | ocpp_asgi-0.4.0-py3-none-any.whl |
|---|---|
| Size | 12.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f7d14f067ea44ed4196d42ba77e287634f013f5e10b1f8b42ef37b4203a47405
|
|
BLAKE2b-256 checksum How to use checksums |
f7d6000f266d494f8c305c72b435d17eff0f1292c91454cc53e2d7b6f391790c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.12 CPython/3.8.10 Darwin/22.1.0
|
Security for ocpp-asgi 0.4.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.