Skip to content

Commit 2607f8c

Browse files
authored
Updating examples (#710)
### Changes included in this PR Updates to the example usage of the library (doc update). ### Current behavior #708 and #709 describe how the current examples in the documentation fail due to 1) they still use the "Payload" suffix on payload data classes, this is not part of the latest version of this library 2) breaking changes from the WebSockets library, primarily regarding the path parameter passed to the connection handler and where to find the request headers. ### New behavior This PR updates the example code both for v16 and v201, so that the payload dataclass is correct and is compatible with the latest websockets library. ### Impact Only changes to documentation. However the documentation currently wouldn't support older versions of websockets library. ### Checklist 1. [x] Does your submission pass the existing tests? 2. [ ] Are there new tests that cover these additions/changes? 3. [x] Have you linted your code locally before submission?
1 parent f719379 commit 2607f8c

File tree

4 files changed

+17
-17
lines changed

4 files changed

+17
-17
lines changed

examples/v16/central_system.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import asyncio
22
import logging
3-
from datetime import datetime
3+
from datetime import datetime, timezone
44

55
try:
66
import websockets
@@ -26,19 +26,19 @@ class ChargePoint(cp):
2626
def on_boot_notification(
2727
self, charge_point_vendor: str, charge_point_model: str, **kwargs
2828
):
29-
return call_result.BootNotificationPayload(
30-
current_time=datetime.utcnow().isoformat(),
29+
return call_result.BootNotification(
30+
current_time=datetime.now(timezone.utc).isoformat(),
3131
interval=10,
3232
status=RegistrationStatus.accepted,
3333
)
3434

3535

36-
async def on_connect(websocket, path):
36+
async def on_connect(websocket):
3737
"""For every new charge point that connects, create a ChargePoint
3838
instance and start listening for messages.
3939
"""
4040
try:
41-
requested_protocols = websocket.request_headers["Sec-WebSocket-Protocol"]
41+
requested_protocols = websocket.request.headers["Sec-WebSocket-Protocol"]
4242
except KeyError:
4343
logging.error("Client hasn't requested any Subprotocol. Closing Connection")
4444
return await websocket.close()
@@ -56,7 +56,7 @@ async def on_connect(websocket, path):
5656
)
5757
return await websocket.close()
5858

59-
charge_point_id = path.strip("/")
59+
charge_point_id = websocket.request.path.strip("/")
6060
cp = ChargePoint(charge_point_id, websocket)
6161

6262
await cp.start()

examples/v16/charge_point.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
class ChargePoint(cp):
2424
async def send_boot_notification(self):
25-
request = call.BootNotificationPayload(
25+
request = call.BootNotification(
2626
charge_point_model="Optimus", charge_point_vendor="The Mobility House"
2727
)
2828

examples/v201/central_system.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import asyncio
22
import logging
3-
from datetime import datetime
3+
from datetime import datetime, timezone
44

55
try:
66
import websockets
@@ -24,24 +24,24 @@
2424
class ChargePoint(cp):
2525
@on(Action.boot_notification)
2626
def on_boot_notification(self, charging_station, reason, **kwargs):
27-
return call_result.BootNotificationPayload(
28-
current_time=datetime.utcnow().isoformat(), interval=10, status="Accepted"
27+
return call_result.BootNotification(
28+
current_time=datetime.now(timezone.utc).isoformat(), interval=10, status="Accepted"
2929
)
3030

3131
@on(Action.heartbeat)
3232
def on_heartbeat(self):
3333
print("Got a Heartbeat!")
34-
return call_result.HeartbeatPayload(
35-
current_time=datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S") + "Z"
34+
return call_result.Heartbeat(
35+
current_time=datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%S") + "Z"
3636
)
3737

3838

39-
async def on_connect(websocket, path):
39+
async def on_connect(websocket):
4040
"""For every new charge point that connects, create a ChargePoint
4141
instance and start listening for messages.
4242
"""
4343
try:
44-
requested_protocols = websocket.request_headers["Sec-WebSocket-Protocol"]
44+
requested_protocols = websocket.request.headers["Sec-WebSocket-Protocol"]
4545
except KeyError:
4646
logging.error("Client hasn't requested any Subprotocol. Closing Connection")
4747
return await websocket.close()
@@ -59,7 +59,7 @@ async def on_connect(websocket, path):
5959
)
6060
return await websocket.close()
6161

62-
charge_point_id = path.strip("/")
62+
charge_point_id = websocket.request.path.strip("/")
6363
charge_point = ChargePoint(charge_point_id, websocket)
6464

6565
await charge_point.start()

examples/v201/charge_point.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121

2222
class ChargePoint(cp):
2323
async def send_heartbeat(self, interval):
24-
request = call.HeartbeatPayload()
24+
request = call.Heartbeat()
2525
while True:
2626
await self.call(request)
2727
await asyncio.sleep(interval)
2828

2929
async def send_boot_notification(self):
30-
request = call.BootNotificationPayload(
30+
request = call.BootNotification(
3131
charging_station={"model": "Wallbox XYZ", "vendor_name": "anewone"},
3232
reason="PowerUp",
3333
)

0 commit comments

Comments
 (0)