Note
Go to the end to download the full example code.
Events#
Shows how a client loads an Aseba script to a remote node, and interacts with it using user-defined events.
from pyaseba.client import Client
client = Client()
client.connect("tcp", port=33333)
node_id, connection = client.wait_node()
The remote node defines variables
desc = client.get_description(node_id=node_id)
assert desc
list(desc.variables)
['_productId', 'args', 'counter', 'id', 'source', 'value']
and events
list(desc.local_events)
['event']
which are accessible from Aseba scripts like this one
script = """
onevent reset
counter = 0
onevent event
emit count counter
"""
where
event
resetresets the counter variable when received on the remote node,event
countis broadcasted each time the local event (which is not accessible outside the remote node) is emitted by the remote node.
client.load_script(node_id=node_id,
script=script,
events={"count": 1, "reset": 0})
The description should now contain the new events
desc = client.get_description(node_id=node_id)
assert desc
desc.user_events
{'count': 1, 'reset': 0}
Let us ask the remote node to start running the script
client.cmd_run(node_id)
Let us reset the counter
client.emit_event(node_id=node_id, name="reset")
and start waiting for count events.
for _ in range(5):
event = client.get_event(node_id=node_id, name="count", wait_ms=1000)
print(f"Received {event}")
Received Event(source=0, name='count', data=[1])
Received Event(source=0, name='count', data=[2])
Received Event(source=0, name='count', data=[3])
Received Event(source=0, name='count', data=[4])
Received Event(source=0, name='count', data=[5])
Total running time of the script: (0 minutes 1.013 seconds)