Skip to main content

Python implementation of Open Charge Point Interface (OCPI) protocol based on fastapi.

Project description

Extrawest OCPI

pre-commit Code style: black Checked with mypy commit-check


Python implementation of Open Charge Point Interface (OCPI) protocol based on fastapi.

Supported OCPI versions: 2.2.1, 2.1.1

OCPI Documentation: 2.2.1, 2.1.1


Requirements


Python >= 3.10

Installation


  pip install extrawest-ocpi

Make sure to install any ASGI-server supported by fastapi:

  pip install uvicorn

Environment Variables


To use this project, you will need to add the following environment variables to your .env file, overwise default values would be taken

PROJECT_NAME BACKEND_CORS_ORIGINS OCPI_HOST OCPI_PREFIX PUSH_PREFIX COUNTRY_CODE PARTY_ID COMMAND_AWAIT_TIME

Usage/Examples


  1. Implement and connect your business logic and db methods inside this Crud class.

[NOTE]: Check example directory for a mongo implementation example. Click here

crud.py

from typing import Any, Tuple

from py_ocpi.core.enums import ModuleID, RoleEnum, Action


class Crud:

    @classmethod
    async def get(cls, module: ModuleID, role: RoleEnum, id, *args, **kwargs) -> Any:
        ...

    @classmethod
    async def list(cls, module: ModuleID, role: RoleEnum, filters: dict, *args, **kwargs) -> Tuple[list, int, bool]:
        ...

    @classmethod
    async def create(cls, module: ModuleID, role: RoleEnum, data: dict, *args, **kwargs) -> Any:
        ...

    @classmethod
    async def update(cls, module: ModuleID, role: RoleEnum, data: dict, id, *args, **kwargs) -> Any:
        ...

    @classmethod
    async def delete(cls, module: ModuleID, role: RoleEnum, id, *args, **kwargs):
        ...

    @classmethod
    async def do(cls, module: ModuleID, role: RoleEnum, action: Action, *args, data: dict = None, **kwargs) -> Any:
        ...
  1. Implement get_valid_token_c and get_valid_token_a method of Authenticator class which would return list of valid tokens. Given authorization token will be compared with this list.

[Reminder]: OCPI versions 2.2 and higher sends encoded authorization tokens, so it will be decoded before compared.

auth.py

from typing import List

from py_ocpi.core.authentication.authenticator import Authenticator


class ClientAuthenticator(Authenticator):
    @classmethod
    async def get_valid_token_c(cls) -> List[str]:
        """Return a list of valid tokens c."""
        ...
        return ["..."]

    @classmethod
    async def get_valid_token_a(cls) -> List[str]:
        """Return a list of valid tokens a."""
        ...
        return ["..."]
  1. Initialize fastapi application

If you need to have support for pushing the updates you could set http_push=True to use push endpoint or websocket_push=True to use websocket connection.

main.py

from py_ocpi import get_application
from py_ocpi.core.enums import RoleEnum, ModuleID
from py_ocpi.modules.versions.enums import VersionNumber

from auth import ClientAuthenticator
from crud import Crud


app = get_application(
    version_numbers=[VersionNumber.v_2_1_1, VersionNumber.v_2_2_1],
    roles=[RoleEnum.cpo],
    modules=[
        ModuleID.credentials_and_registration,
        ModuleID.locations,
        ModuleID.cdrs,
        ModuleID.tokens,
        ModuleID.tariffs,
        ModuleID.sessions,
        ModuleID.commands,
    ],
    authenticator=ClientAuthenticator,
    crud=Crud,
    http_push=False,
    websocket_push=False,
)
  1. Run
  uvicorn main:app --reload

API Reference


As this project is based on fastapi, use /docs or redoc/ to check .the documentation after the project is running.

[API's will appear depending on given version_numbers, roles and modules given to initializer.]

Example: http://127.0.0.1:8000/ocpi/docs/

Roadmap


  • [in progress] Add v2.2.1 modules: Charging Profiles, Hub Client Info

Related


The project was created through inspiration and adaptation of this project PY_OCPI.

License


This project is licensed under the terms of the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

extrawest_ocpi-2023.11.7.tar.gz (68.3 kB view details)

Uploaded Source

Built Distribution

extrawest_ocpi-2023.11.7-py3-none-any.whl (96.0 kB view details)

Uploaded Python 3

File details

Details for the file extrawest_ocpi-2023.11.7.tar.gz.

File metadata

  • Download URL: extrawest_ocpi-2023.11.7.tar.gz
  • Upload date:
  • Size: 68.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for extrawest_ocpi-2023.11.7.tar.gz
Algorithm Hash digest
SHA256 b1bc939b12f7e0d867b7f8a79b72341e5d5d7d7d87c188c1200dc2ce28bc4bf5
MD5 2a0d1bde1cdaf28858b1b9a1774b84d5
BLAKE2b-256 0fa89451687e75909ade90b3b3d97218b26f290e2f03db1f8d21e196904c6c8d

See more details on using hashes here.

File details

Details for the file extrawest_ocpi-2023.11.7-py3-none-any.whl.

File metadata

File hashes

Hashes for extrawest_ocpi-2023.11.7-py3-none-any.whl
Algorithm Hash digest
SHA256 f346a987a6efb2a97a56e5fa181748bff78928aa27f4c03a8b2c04d8c38d1a43
MD5 00877bb38867d0075937c5b0f25b7650
BLAKE2b-256 2011037f9599be278affac3fee28ccecc9f300b2386a0e4e0ada40ac42396d30

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page