Skip to content

Commit

Permalink
Updating examples (#710)
Browse files Browse the repository at this point in the history
### 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?
  • Loading branch information
a-alak authored Feb 13, 2025
1 parent f719379 commit 2607f8c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
12 changes: 6 additions & 6 deletions examples/v16/central_system.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import asyncio
import logging
from datetime import datetime
from datetime import datetime, timezone

try:
import websockets
Expand All @@ -26,19 +26,19 @@ class ChargePoint(cp):
def on_boot_notification(
self, charge_point_vendor: str, charge_point_model: str, **kwargs
):
return call_result.BootNotificationPayload(
current_time=datetime.utcnow().isoformat(),
return call_result.BootNotification(
current_time=datetime.now(timezone.utc).isoformat(),
interval=10,
status=RegistrationStatus.accepted,
)


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

charge_point_id = path.strip("/")
charge_point_id = websocket.request.path.strip("/")
cp = ChargePoint(charge_point_id, websocket)

await cp.start()
Expand Down
2 changes: 1 addition & 1 deletion examples/v16/charge_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

class ChargePoint(cp):
async def send_boot_notification(self):
request = call.BootNotificationPayload(
request = call.BootNotification(
charge_point_model="Optimus", charge_point_vendor="The Mobility House"
)

Expand Down
16 changes: 8 additions & 8 deletions examples/v201/central_system.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import asyncio
import logging
from datetime import datetime
from datetime import datetime, timezone

try:
import websockets
Expand All @@ -24,24 +24,24 @@
class ChargePoint(cp):
@on(Action.boot_notification)
def on_boot_notification(self, charging_station, reason, **kwargs):
return call_result.BootNotificationPayload(
current_time=datetime.utcnow().isoformat(), interval=10, status="Accepted"
return call_result.BootNotification(
current_time=datetime.now(timezone.utc).isoformat(), interval=10, status="Accepted"
)

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


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

charge_point_id = path.strip("/")
charge_point_id = websocket.request.path.strip("/")
charge_point = ChargePoint(charge_point_id, websocket)

await charge_point.start()
Expand Down
4 changes: 2 additions & 2 deletions examples/v201/charge_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@

class ChargePoint(cp):
async def send_heartbeat(self, interval):
request = call.HeartbeatPayload()
request = call.Heartbeat()
while True:
await self.call(request)
await asyncio.sleep(interval)

async def send_boot_notification(self):
request = call.BootNotificationPayload(
request = call.BootNotification(
charging_station={"model": "Wallbox XYZ", "vendor_name": "anewone"},
reason="PowerUp",
)
Expand Down

0 comments on commit 2607f8c

Please sign in to comment.