-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
164 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import asyncio | ||
from datetime import datetime | ||
|
||
try: | ||
import websockets | ||
except ModuleNotFoundError: | ||
print("This example relies on the 'websockets' package.") | ||
print("Please install it by running: ") | ||
print() | ||
print(" $ pip install websockets") | ||
import sys | ||
sys.exit(1) | ||
|
||
from ocpp.routing import on | ||
from ocpp.v20 import ChargePoint as cp | ||
from ocpp.v20 import call_result | ||
|
||
|
||
class ChargePoint(cp): | ||
@on('BootNotification') | ||
def on_boot_notitication(self, charging_station, reason, **kwargs): | ||
return call_result.BootNotificationPayload( | ||
current_time=datetime.utcnow().isoformat(), | ||
interval=10, | ||
status='Accepted' | ||
) | ||
|
||
@on('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" | ||
) | ||
|
||
|
||
async def on_connect(websocket, path): | ||
""" For every new charge point that connects, create a ChargePoint instance | ||
and start listening for messages. | ||
""" | ||
charge_point_id = path.strip('/') | ||
cp = ChargePoint(charge_point_id, websocket) | ||
|
||
await cp.start() | ||
|
||
|
||
async def main(): | ||
server = await websockets.serve( | ||
on_connect, | ||
'0.0.0.0', | ||
9000, | ||
subprotocols=['ocpp1.6'] | ||
) | ||
|
||
await server.wait_closed() | ||
|
||
|
||
if __name__ == '__main__': | ||
try: | ||
# asyncio.run() is used when running this example with Python 3.7 and | ||
# higher. | ||
asyncio.run(main()) | ||
except AttributeError: | ||
# For Python 3.6 a bit more code is required to run the main() task on | ||
# an event loop. | ||
loop = asyncio.get_event_loop() | ||
loop.run_until_complete(main()) | ||
loop.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import asyncio | ||
|
||
try: | ||
import websockets | ||
except ModuleNotFoundError: | ||
print("This example relies on the 'websockets' package.") | ||
print("Please install it by running: ") | ||
print() | ||
print(" $ pip install websockets") | ||
import sys | ||
sys.exit(1) | ||
|
||
|
||
from ocpp.v20 import call | ||
from ocpp.v20 import ChargePoint as cp | ||
|
||
|
||
class ChargePoint(cp): | ||
|
||
async def send_heartbeat(self, interval): | ||
request = call.HeartbeatPayload() | ||
while True: | ||
await self.call(request) | ||
await asyncio.sleep(interval) | ||
|
||
async def send_boot_notification(self): | ||
request = call.BootNotificationPayload( | ||
charging_station={ | ||
'model': 'Wallbox XYZ', | ||
'vendor_name': 'anewone' | ||
}, | ||
reason="PowerUp" | ||
) | ||
response = await self.call(request) | ||
|
||
if response.status == 'Accepted': | ||
print("Connected to central system.") | ||
await self.send_heartbeat(response.interval) | ||
|
||
|
||
async def main(): | ||
async with websockets.connect( | ||
'ws://localhost:9000/CP_1', | ||
subprotocols=['ocpp2.0'] | ||
) as ws: | ||
|
||
cp = ChargePoint('CP_1', ws) | ||
|
||
await asyncio.gather(cp.start(), cp.send_boot_notification()) | ||
|
||
|
||
if __name__ == '__main__': | ||
try: | ||
# asyncio.run() is used when running this example with Python 3.7 and | ||
# higher. | ||
asyncio.run(main()) | ||
except AttributeError: | ||
# For Python 3.6 a bit more code is required to run the main() task on | ||
# an event loop. | ||
loop = asyncio.get_event_loop() | ||
loop.run_until_complete(main()) | ||
loop.close() |