Commit | Line | Data |
---|---|---|
fa16d389 S |
1 | import asyncio |
2 | import logging | |
fa16d389 | 3 | from datetime import datetime, timezone |
aea49501 | 4 | from threading import Timer |
fa16d389 | 5 | |
a89844d4 | 6 | import ocpp.v201 |
1a0d2c47 | 7 | import websockets |
fa16d389 | 8 | from ocpp.routing import on |
339f65ad | 9 | from ocpp.v201.enums import RegistrationStatusType, ClearCacheStatusType, Action |
fa16d389 S |
10 | |
11 | # Setting up the logging configuration to display debug level messages. | |
12 | logging.basicConfig(level=logging.DEBUG) | |
13 | ||
1a0d2c47 | 14 | |
aea49501 JB |
15 | class RepeatTimer(Timer): |
16 | """ Class that inherits from the Timer class. It will run a | |
17 | function at regular intervals.""" | |
18 | ||
19 | def run(self): | |
20 | while not self.finished.wait(self.interval): | |
21 | self.function(*self.args, **self.kwargs) | |
22 | ||
23 | ||
fa16d389 | 24 | # Define a ChargePoint class inheriting from the OCPP 2.0.1 ChargePoint class. |
a89844d4 | 25 | class ChargePoint(ocpp.v201.ChargePoint): |
aea49501 | 26 | # Message handlers to receive OCPP messages. |
339f65ad | 27 | @on(Action.BootNotification) |
d6488e8d JB |
28 | async def on_boot_notification(self, charging_station, reason, **kwargs): |
29 | logging.info("Received BootNotification") | |
30 | # Create and return a BootNotification response with the current time, | |
31 | # an interval of 10 seconds, and an accepted status. | |
339f65ad | 32 | return ocpp.v201.call_result.BootNotification( |
d6488e8d | 33 | current_time=datetime.now(timezone.utc).isoformat(), |
115f3b17 | 34 | interval=60, |
d6488e8d JB |
35 | status=RegistrationStatusType.accepted |
36 | ) | |
1a0d2c47 | 37 | |
115f3b17 JB |
38 | @on(Action.Heartbeat) |
39 | async def on_heartbeat(self, charging_station, **kwargs): | |
40 | logging.info("Received Heartbeat") | |
41 | return ocpp.v201.call_result.Heartbeat(current_time=datetime.now(timezone.utc).isoformat()) | |
42 | ||
65c0600c JB |
43 | @on(Action.StatusNotification) |
44 | async def on_status_notification(self, charging_station, connector_id, status, **kwargs): | |
45 | logging.info("Received StatusNotification") | |
46 | return ocpp.v201.call_result.StatusNotification() | |
47 | ||
d6488e8d | 48 | # Request handlers to emit OCPP messages. |
a89844d4 JB |
49 | async def send_clear_cache(self): |
50 | request = ocpp.v201.call.ClearCache() | |
51 | response = await self.call(request) | |
52 | ||
53 | if response.status == ClearCacheStatusType.accepted: | |
e1c2dac9 | 54 | logging.info("Cache clearing successful") |
a89844d4 JB |
55 | else: |
56 | logging.info("Cache clearing failed") | |
1a0d2c47 | 57 | |
fa16d389 S |
58 | |
59 | # Function to handle new WebSocket connections. | |
60 | async def on_connect(websocket, path): | |
d6488e8d JB |
61 | """ For every new charge point that connects, create a ChargePoint instance and start |
62 | listening for messages.""" | |
63 | try: | |
64 | requested_protocols = websocket.request_headers['Sec-WebSocket-Protocol'] | |
65 | except KeyError: | |
66 | logging.info("Client hasn't requested any Subprotocol. Closing Connection") | |
67 | return await websocket.close() | |
1a0d2c47 | 68 | |
d6488e8d JB |
69 | if websocket.subprotocol: |
70 | logging.info("Protocols Matched: %s", websocket.subprotocol) | |
71 | else: | |
72 | logging.warning('Protocols Mismatched | Expected Subprotocols: %s,' | |
73 | ' but client supports %s | Closing connection', | |
74 | websocket.available_subprotocols, | |
75 | requested_protocols) | |
76 | return await websocket.close() | |
1a0d2c47 | 77 | |
d6488e8d JB |
78 | charge_point_id = path.strip('/') |
79 | cp = ChargePoint(charge_point_id, websocket) | |
1a0d2c47 | 80 | |
d6488e8d JB |
81 | # Start the ChargePoint instance to listen for incoming messages. |
82 | await cp.start() | |
1a0d2c47 | 83 | |
fa16d389 S |
84 | |
85 | # Main function to start the WebSocket server. | |
86 | async def main(): | |
d6488e8d JB |
87 | # Create the WebSocket server and specify the handler for new connections. |
88 | server = await websockets.serve( | |
89 | on_connect, | |
90 | '127.0.0.1', # Listen on loopback. | |
91 | 9000, # Port number. | |
339f65ad | 92 | subprotocols=['ocpp2.0', 'ocpp2.0.1'] # Specify OCPP 2.0.1 subprotocols. |
d6488e8d JB |
93 | ) |
94 | logging.info("WebSocket Server Started") | |
95 | # Wait for the server to close (runs indefinitely). | |
96 | await server.wait_closed() | |
1a0d2c47 | 97 | |
fa16d389 S |
98 | |
99 | # Entry point of the script. | |
100 | if __name__ == '__main__': | |
d6488e8d JB |
101 | # Run the main function to start the server. |
102 | asyncio.run(main()) |