
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/client/7_events.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_gallery_client_7_events.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_gallery_client_7_events.py:


Events
======

Shows how a client loads an Aseba script to a remote node,
and interacts with it using user-defined events.

.. GENERATED FROM PYTHON SOURCE LINES 8-15

.. code-block:: Python


    from pyaseba.client import Client

    client = Client()
    client.connect("tcp", port=33333)
    node_id, connection = client.wait_node()








.. GENERATED FROM PYTHON SOURCE LINES 16-17

The remote node defines variables

.. GENERATED FROM PYTHON SOURCE LINES 17-22

.. code-block:: Python


    desc = client.get_description(node_id=node_id)
    assert desc
    list(desc.variables)





.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    ['_productId', 'args', 'counter', 'id', 'source', 'value']



.. GENERATED FROM PYTHON SOURCE LINES 23-24

and events

.. GENERATED FROM PYTHON SOURCE LINES 24-27

.. code-block:: Python


    list(desc.local_events)





.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    ['event']



.. GENERATED FROM PYTHON SOURCE LINES 28-29

which are accessible from Aseba scripts like this one

.. GENERATED FROM PYTHON SOURCE LINES 29-38

.. code-block:: Python


    script = """
    onevent reset
    counter = 0

    onevent event
    emit count counter
    """








.. GENERATED FROM PYTHON SOURCE LINES 39-46

where

- event ``reset`` resets the counter variable
  when received on the remote node,
- event ``count`` is broadcasted each time the local
  event (which is not accessible outside the remote node)
  is emitted by the remote node.

.. GENERATED FROM PYTHON SOURCE LINES 46-51

.. code-block:: Python


    client.load_script(node_id=node_id,
                       script=script,
                       events={"count": 1, "reset": 0})








.. GENERATED FROM PYTHON SOURCE LINES 52-53

The description should now contain the new events

.. GENERATED FROM PYTHON SOURCE LINES 53-58

.. code-block:: Python


    desc = client.get_description(node_id=node_id)
    assert desc
    desc.user_events





.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    {'count': 1, 'reset': 0}



.. GENERATED FROM PYTHON SOURCE LINES 59-60

Let us ask the remote node to start running the script

.. GENERATED FROM PYTHON SOURCE LINES 60-63

.. code-block:: Python


    client.cmd_run(node_id)








.. GENERATED FROM PYTHON SOURCE LINES 64-65

Let us reset the counter

.. GENERATED FROM PYTHON SOURCE LINES 65-68

.. code-block:: Python


    client.emit_event(node_id=node_id, name="reset")








.. GENERATED FROM PYTHON SOURCE LINES 69-70

and start waiting for ``count`` events.

.. GENERATED FROM PYTHON SOURCE LINES 70-75

.. code-block:: Python


    for _ in range(5):
        event = client.get_event(node_id=node_id, name="count", wait_ms=1000)
        print(f"Received {event}")





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    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])




.. GENERATED FROM PYTHON SOURCE LINES 76-77

.. code-block:: Python

    client.close()








.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 1.013 seconds)


.. _sphx_glr_download_gallery_client_7_events.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: 7_events.ipynb <7_events.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: 7_events.py <7_events.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: 7_events.zip <7_events.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
