Debian Codegraph @

docs/source/usage/client_side.rst

raw
1 Client side usage
2 =================
3
4 This document explains how to create a charge point and connect it to a central system.
5
6 The following snippet creates a simple websocket client that connects to a central system running locally on port 9000.
7
8 .. code-block:: python
9
10 # client.py
11 import asyncio
12
13 import websockets
14
15 from ocpp.v16 import ChargePoint as cp
16 from ocpp.v16 import call, call_result
17 from ocpp.v16.enums import RegistrationStatus
18
19
20 class ChargePoint(cp):
21
22 async def send_boot_notification(self):
23 request = call.BootNotification(
24 charge_point_model="Wallbox XYZ",
25 charge_point_vendor="acme",
26 )
27 response: call_result.BootNotification = await self.call(request)
28
29 if response.status == RegistrationStatus.accepted:
30 print("Connected to central system.")
31
32
33 async def main():
34 async with websockets.connect(
35 "ws://localhost:9000/CP_1", subprotocols=["ocpp1.6"]
36 ) as ws:
37 charge_point = ChargePoint("CP_1", ws)
38
39 await asyncio.gather(
40 charge_point.start(),
41 charge_point.send_boot_notification(),
42 )
43
44
45 if __name__ == "__main__":
46 asyncio.run(main())
47
48
49
50 A few things to note about the above code:
51
52 - The `websockets.connect()`_ method is used to create a websocket connection to the server.
53 The first argument is the URL on which the connection is made. Note the ``CP_1`` suffix which is the charge point identifier.
54
55 - The subprotocol parameter is used to specify the OCPP version that the client supports.
56 To connect with OCPP 2.0.1, you can specify the subprotocol as follows:
57
58 .. code-block:: python
59
60 subprotocols=["ocpp2.0.1"]
61
62 - The ``ChargePoint`` class subclasses the same `ocpp.v16.ChargePoint`_ class that is used in the server example.
63 This class implements the OCPP protocol and handles the communication with the server.
64
65 - The ``send_boot_notification()`` method sends a ``BootNotification`` request to the server.
66 To create a valid OCPP 1.6 message, the ocpp package provides support via pre-defined dataclasses that can be found in the `call`_ module.
67
68 - The `ocpp.v16.ChargePoint`_ expose a public coroutine: `call()`_, that accepts a payload, serializes it to a OCPP compliant message, and sends it to the server.
69 It returns a serialized message in the form of the corresponding `call_result`_ dataclass. In the above case it's the `ocpp.v16.call_result.BootNotification`_ dataclass.
70 This allows you to focus on the business logic and let ocpp package handle all the heavy lifting which includes serialization, deserialization, and validation of payloads.
71
72 - The `asyncio.gather()`_ method is used to run multiple corouting concurrently.
73 In this case, the ``start()`` method starts listening messages from the server and the ``send_boot_notification()`` method sends the ``BootNotification`` request to the server.
74
75 After running the above script, you should see the following output:
76
77 .. code-block:: bash
78
79 $ python client.py
80 CP_1: send [2, "e4c131d6-6b2c-4f50-9a0e-9242a37acb7a", "BootNotification", {"chargePointVendor": "Wallbox XYZ", "chargePointModel": "acme"}]
81 CP_1: receive message [3, "e4c131d6-6b2c-4f50-9a0e-9242a37acb7a", {"currentTime": "2025-05-10T12:00:00+00:00", "interval": 10, "status": "Accepted"}]
82 Connected to central system.
83
84 .. _websockets.connect(): https://websockets.readthedocs.io/en/stable/reference/asyncio/client.html#websockets.asyncio.client.connect
85 .. _ocpp.v16.ChargePoint: https://github.com/search?q=repo%3Amobilityhouse%2Focpp+%22class+ChargePoint%28%22+path%3A%2F%5Eocpp%5C%2Fv16%5C%2F%2F&type=code
86 .. _call: https://github.com/mobilityhouse/ocpp/blob/master/ocpp/v16/call.py
87 .. _call_result: https://github.com/mobilityhouse/ocpp/blob/master/ocpp/v16/call_result.py
88 .. _call(): https://github.com/search?q=repo%3Amobilityhouse%2Focpp+%22def+call%22&type=code
89 .. _ocpp.v16.call_result.BootNotification: https://github.com/search?q=repo%3Amobilityhouse%2Focpp+%22class+BootNotification%22+path%3A%2F%5Eocpp%5C%2Fv16%5C%2Fcall_result%2F&type=code
90 .. _asyncio.gather(): https://docs.python.org/3/library/asyncio-task.html#running-tasks-concurrently