Skip to content

Commit

Permalink
Pass params that get passed into "on" handler into the "after" handler (
Browse files Browse the repository at this point in the history
#25)

* Pass params that get passed into on handler into the after handler

* updating documentation and examples

* updating test with an after route
  • Loading branch information
proelke authored Sep 23, 2019
1 parent 03b2572 commit 0e331a1
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
7 changes: 6 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,18 @@ calls. You can find a detailed explaination of the code in the `Central System d
class ChargePoint(cp):
@on(Action.BootNotification)
def on_boot_notitication(self, charge_point_vendor, charge_point_model, **kwargs):
def on_boot_notification(self, charge_point_vendor, charge_point_model, **kwargs):
return call_result.BootNotificationPayload(
current_time=datetime.utcnow().isoformat(),
interval=10,
status=RegistrationStatus.accepted
)
@after(Action.BootNotification)
def after_boot_notification(self, charge_point_vendor, charge_point_model, **kwargs):
print("ChargePoint Vendor is: %s", charge_point_vendor)
print("ChargePoint Model is: %s",charge_point_model)
async def on_connect(websocket, path):
""" For every new charge point that connects, create a ChargePoint instance
Expand Down
3 changes: 2 additions & 1 deletion ocpp/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ def inner(*args, **kwargs):
def after(action):
""" Function decorator to mark function as hook to post-request hook.
The hook doesn't receive any argument and its return value is ignored.
This hook's arguments are the data that is in the payload for the specific
action.
It can be used like so:
Expand Down
6 changes: 1 addition & 5 deletions ocpp/v16/charge_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,7 @@ def __init__(self, id, connection, response_timeout=30):
"""
Args:
hb_interval (int): Time between heartbeat messages.
charger_id (str): ID of the charger.
id_tag_list (list): List with allowed RFID tags.
phases: Unused
max_current: Unused
connection: Connection to CP.
response_timeout (int): When no response on a request is received
within this interval, a asyncio.TimeoutError is raised.
Expand Down Expand Up @@ -209,7 +205,7 @@ async def _handle_call(self, msg):

try:
handler = handlers['_after_action']
await asyncio.coroutine(handler)()
await asyncio.coroutine(handler)(**snake_case_payload)
except KeyError:
# '_on_after' hooks are not required. Therefore ignore exception
# when no '_on_after' hook is installed.
Expand Down
8 changes: 8 additions & 0 deletions tests/test_routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,15 @@ def on_boot_notification(charge_point_model, charge_point_vendor, **kwargs): #
status='Accepted',
)

@after(Action.BootNotification)
def after_boot_notification(charge_point_model, charge_point_vendor,
**kwargs): # noqa
assert charge_point_vendor == "Alfen BV"
assert charge_point_model == "ICU Eve Mini"

setattr(base_central_system, 'on_boot_notification', on_boot_notification)
setattr(base_central_system, 'after_boot_notification',
after_boot_notification)
base_central_system.route_map = create_route_map(base_central_system)

await base_central_system.route_message(boot_notification_call)
Expand Down

0 comments on commit 0e331a1

Please sign in to comment.