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