Debian Codegraph @

examples/v201/charge_point.py

raw
1 import asyncio
2 import logging
3
4 try:
5 import websockets
6 except ModuleNotFoundError:
7 print("This example relies on the 'websockets' package.")
8 print("Please install it by running: ")
9 print()
10 print(" $ pip install websockets")
11 import sys
12
13 sys.exit(1)
14
15
16 from ocpp.v201 import ChargePoint as cp
17 from ocpp.v201 import call
18
19 logging.basicConfig(level=logging.INFO)
20
21
22 class ChargePoint(cp):
23 async def send_heartbeat(self, interval):
24 request = call.Heartbeat()
25 while True:
26 await self.call(request)
27 await asyncio.sleep(interval)
28
29 async def send_boot_notification(self):
30 request = call.BootNotification(
31 charging_station={"model": "Wallbox XYZ", "vendor_name": "anewone"},
32 reason="PowerUp",
33 )
34 response = await self.call(request)
35
36 if response.status == "Accepted":
37 print("Connected to central system.")
38 await self.send_heartbeat(response.interval)
39
40
41 async def main():
42 async with websockets.connect(
43 "ws://localhost:9000/CP_1", subprotocols=["ocpp2.0.1"]
44 ) as ws:
45
46 charge_point = ChargePoint("CP_1", ws)
47 await asyncio.gather(
48 charge_point.start(), charge_point.send_boot_notification()
49 )
50
51
52 if __name__ == "__main__":
53 # asyncio.run() is used when running this example with Python >= 3.7v
54 asyncio.run(main())