"""Put virtual chargers online against your CSMS, then wait until they connect.
export OCPPLAB_TOKEN="<api-key>"
export CSMS_WS_URL="wss://csms.example.com/ocpp"
uv run python examples/quickstart.py
Creating a location returns 202 at once; the chargers deploy in the background,
so this polls their deployment stage until every one is online.
"""
from __future__ import annotations
import os
import time
from ocpplab import OcpplabSDK
from ocpplab.types import VirtualLocationChargerItem, VirtualLocationCoordinates
IDENTITIES = ["DEMO-CP-001", "DEMO-CP-002"]
def main() -> None:
# Defaults to https://api.ocpplab.io; pass base_url= for another host.
client = OcpplabSDK(token=os.environ["OCPPLAB_TOKEN"])
accepted = client.evcharger.location.create(
name="Demo hub",
address="1 Parvis de la Defense",
city="Puteaux",
country="FRA",
coordinates=VirtualLocationCoordinates(lat=48.892, lng=2.238),
chargers=[
VirtualLocationChargerItem(
identity=identity,
brand_slug="wallbox",
model="wallbox-pulsar-plus",
connector_type="Type2",
ws_url=os.environ["CSMS_WS_URL"],
)
for identity in IDENTITIES
],
)
print(f"location {accepted.location_id}: {accepted.detail}")
deadline = time.monotonic() + 600
while True:
chargers = client.evcharger.deployment_runtime_ops.status(
identity=IDENTITIES
).items
for charger in chargers:
print(charger.identity, charger.stage, charger.outcome, charger.hint or "")
if chargers and all(charger.stage == "online" for charger in chargers):
return
if any(charger.outcome in ("failed", "stuck") for charger in chargers):
raise SystemExit("a charger failed to deploy; see the hint above")
if time.monotonic() > deadline:
raise SystemExit("chargers were not online after 10 minutes")
time.sleep(5)
if __name__ == "__main__":
main()