| 1 |
import asyncio |
| 2 |
import logging |
| 3 |
|
| 4 |
try: |
| 5 |
import websockets |
| 6 |
except ModuleNotFoundError: |
| 7 |
print("This example relies on the 'websockets' package.") |
| 8 |
print("Please install it by running: ") |
| 9 |
print() |
| 10 |
print(" $ pip install websockets") |
| 11 |
import sys |
| 12 |
|
| 13 |
sys.exit(1) |
| 14 |
|
| 15 |
|
| 16 |
from ocpp.v21 import ChargePoint as cp |
| 17 |
from ocpp.v21 import call |
| 18 |
from ocpp.v21.datatypes import ChargingStationType |
| 19 |
|
| 20 |
logging.basicConfig(level=logging.INFO) |
| 21 |
|
| 22 |
|
| 23 |
class ChargePoint(cp): |
| 24 |
async def send_heartbeat(self, interval): |
| 25 |
request = call.Heartbeat() |
| 26 |
while True: |
| 27 |
await self.call(request) |
| 28 |
await asyncio.sleep(interval) |
| 29 |
|
| 30 |
async def send_boot_notification(self): |
| 31 |
print("YESSSS") |
| 32 |
request = call.BootNotification( |
| 33 |
charging_station=ChargingStationType( |
| 34 |
model="Wallbox XYZ", vendor_name="anewone" |
| 35 |
), |
| 36 |
reason="PowerUp", |
| 37 |
) |
| 38 |
response = await self.call(request) |
| 39 |
|
| 40 |
if response.status == "Accepted": |
| 41 |
print("Connected to central system.") |
| 42 |
await self.send_heartbeat(response.interval) |
| 43 |
|
| 44 |
|
| 45 |
async def main(): |
| 46 |
async with websockets.connect( |
| 47 |
"ws://localhost:9000/CP_1", subprotocols=["ocpp2.1"] |
| 48 |
) as ws: |
| 49 |
|
| 50 |
charge_point = ChargePoint("CP_1", ws) |
| 51 |
await asyncio.gather( |
| 52 |
charge_point.start(), charge_point.send_boot_notification() |
| 53 |
) |
| 54 |
|
| 55 |
|
| 56 |
if __name__ == "__main__": |
| 57 |
# asyncio.run() is used when running this example with Python >= 3.7v |
| 58 |
asyncio.run(main()) |