Commit | Line | Data |
---|---|---|
fa16d389 S |
1 | import asyncio |
2 | import logging | |
fa16d389 | 3 | from datetime import datetime, timezone |
d6488e8d | 4 | from typing import Sequence |
fa16d389 | 5 | |
1a0d2c47 | 6 | import websockets |
fa16d389 S |
7 | from ocpp.routing import on |
8 | from ocpp.v201 import ChargePoint as cp | |
9 | from ocpp.v201 import call_result | |
d6488e8d | 10 | from ocpp.v201.enums import RegistrationStatusType |
fa16d389 S |
11 | |
12 | # Setting up the logging configuration to display debug level messages. | |
13 | logging.basicConfig(level=logging.DEBUG) | |
14 | ||
1a0d2c47 | 15 | |
fa16d389 S |
16 | # Define a ChargePoint class inheriting from the OCPP 2.0.1 ChargePoint class. |
17 | class ChargePoint(cp): | |
d6488e8d JB |
18 | # Message handlers to receive OCPP message. |
19 | @on('BootNotification') | |
20 | async def on_boot_notification(self, charging_station, reason, **kwargs): | |
21 | logging.info("Received BootNotification") | |
22 | # Create and return a BootNotification response with the current time, | |
23 | # an interval of 10 seconds, and an accepted status. | |
24 | return call_result.BootNotification( | |
25 | current_time=datetime.now(timezone.utc).isoformat(), | |
26 | interval=10, | |
27 | status=RegistrationStatusType.accepted | |
28 | ) | |
1a0d2c47 | 29 | |
d6488e8d | 30 | # Request handlers to emit OCPP messages. |
1a0d2c47 | 31 | |
fa16d389 S |
32 | |
33 | # Function to handle new WebSocket connections. | |
34 | async def on_connect(websocket, path): | |
d6488e8d JB |
35 | """ For every new charge point that connects, create a ChargePoint instance and start |
36 | listening for messages.""" | |
37 | try: | |
38 | requested_protocols = websocket.request_headers['Sec-WebSocket-Protocol'] | |
39 | except KeyError: | |
40 | logging.info("Client hasn't requested any Subprotocol. Closing Connection") | |
41 | return await websocket.close() | |
1a0d2c47 | 42 | |
d6488e8d JB |
43 | if websocket.subprotocol: |
44 | logging.info("Protocols Matched: %s", websocket.subprotocol) | |
45 | else: | |
46 | logging.warning('Protocols Mismatched | Expected Subprotocols: %s,' | |
47 | ' but client supports %s | Closing connection', | |
48 | websocket.available_subprotocols, | |
49 | requested_protocols) | |
50 | return await websocket.close() | |
1a0d2c47 | 51 | |
d6488e8d JB |
52 | charge_point_id = path.strip('/') |
53 | cp = ChargePoint(charge_point_id, websocket) | |
1a0d2c47 | 54 | |
d6488e8d JB |
55 | # Start the ChargePoint instance to listen for incoming messages. |
56 | await cp.start() | |
1a0d2c47 | 57 | |
fa16d389 S |
58 | |
59 | # Main function to start the WebSocket server. | |
60 | async def main(): | |
d6488e8d JB |
61 | # Create the WebSocket server and specify the handler for new connections. |
62 | server = await websockets.serve( | |
63 | on_connect, | |
64 | '127.0.0.1', # Listen on loopback. | |
65 | 9000, # Port number. | |
66 | subprotocols=Sequence['ocpp2.0', 'ocpp2.0.1'] # Specify OCPP 2.0.1 subprotocols. | |
67 | ) | |
68 | logging.info("WebSocket Server Started") | |
69 | # Wait for the server to close (runs indefinitely). | |
70 | await server.wait_closed() | |
1a0d2c47 | 71 | |
fa16d389 S |
72 | |
73 | # Entry point of the script. | |
74 | if __name__ == '__main__': | |
d6488e8d JB |
75 | # Run the main function to start the server. |
76 | asyncio.run(main()) |