
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/client/5_callbacks.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_5_callbacks.py>`
        to download the full example code.

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

.. _sphx_glr_gallery_client_5_callbacks.py:


Callbacks
=========

Showcases the different callbacks that clients support.

.. GENERATED FROM PYTHON SOURCE LINES 7-13

.. code-block:: Python


    from pyaseba.client import Message, Event, Client
    import time

    client = Client()








.. GENERATED FROM PYTHON SOURCE LINES 14-19

Networks connection and disconnection
-------------------------------------

Callbacks for when new networks are connected
and/or disconnected.

.. GENERATED FROM PYTHON SOURCE LINES 19-28

.. code-block:: Python



    def connection_callback(connection: int, target: str) -> None:
        print(f"Connected network #{connection} at {target}")


    client.add_connection_callback(connection_callback)
    client.connect("tcp", port=33333)





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

 .. code-block:: none

    Connected network #1 at tcp:host=0.0.0.0;port=33333;sock=-1

    1



.. GENERATED FROM PYTHON SOURCE LINES 29-33

Nodes
-----

Callbacks for when new nodes are discovered and/or disconnected.

.. GENERATED FROM PYTHON SOURCE LINES 33-43

.. code-block:: Python



    def node_callback(node_id: int, connection: int) -> None:
        print(f"Discovered node #{node_id} on network #{connection}")


    client.add_node_callback(node_callback)
    node_id, _ = client.wait_node()






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

 .. code-block:: none

    Discovered node #0 on network #1




.. GENERATED FROM PYTHON SOURCE LINES 44-48

Messages
--------

Callbacks for when messages are received.

.. GENERATED FROM PYTHON SOURCE LINES 48-59

.. code-block:: Python



    def message_callback(message: Message, connection: int) -> None:
        print(f"Received {message} on network #{connection}")


    client.add_message_callback(message_callback)
    time.sleep(1)
    client.clear_message_callbacks()






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

 .. code-block:: none

    Received NodePresent(source=0, version=9) on network #1




.. GENERATED FROM PYTHON SOURCE LINES 60-64

Events
--------

Callbacks for when events are received.

.. GENERATED FROM PYTHON SOURCE LINES 64-84

.. code-block:: Python


    script = """
    onevent event
    emit count counter
    """

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


    def event_callback(event: Event) -> None:
        print(f"Received {event}")


    client.add_event_callback(event_callback)
    time.sleep(1)
    client.clear_message_callbacks()





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

 .. code-block:: none

    Received Event(source=0, name='count', data=[226])
    Received Event(source=0, name='count', data=[227])
    Received Event(source=0, name='count', data=[228])
    Received Event(source=0, name='count', data=[229])
    Received Event(source=0, name='count', data=[230])
    Received Event(source=0, name='count', data=[231])
    Received Event(source=0, name='count', data=[232])
    Received Event(source=0, name='count', data=[233])
    Received Event(source=0, name='count', data=[234])
    Received Event(source=0, name='count', data=[235])




.. GENERATED FROM PYTHON SOURCE LINES 85-86

.. code-block:: Python

    client.close()








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

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


.. _sphx_glr_download_gallery_client_5_callbacks.py:

.. only:: html

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

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

      :download:`Download Jupyter notebook: 5_callbacks.ipynb <5_callbacks.ipynb>`

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

      :download:`Download Python source code: 5_callbacks.py <5_callbacks.py>`

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

      :download:`Download zipped: 5_callbacks.zip <5_callbacks.zip>`


.. only:: html

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

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