NodeMapPolling#

NodeMapPolling.py#
from itala import itala
import time 

TEMPERATURE_READS = 10
POLL_FREQ_S = 750


def on_device_temperature_changed(node: itala.INode):
    print("\ton_device_temperature_changed function called.")
    print(f"\tDevice temperature node changed. New value is {node.value} C\n")


def node_map_polling():
    system = itala.create_system()
    devices_info = system.enumerate_devices(500)
    if(len(devices_info) == 0):
        print("No devices found. Example canceled.")
        exit(1)
    if(devices_info[0].access_status != itala.DeviceAccessStatus_AvailableReadWrite):
        print("Target device is unaccessible in RW mode. Example cancelled.")
        exit(1)
    device = system.create_device(devices_info[0])
    print("First device initialized.")
    nodemap = device.node_map

    device_temperature_node = nodemap.DeviceTemperature
    if(not itala.is_readable(device_temperature_node)):
        print("DeviceTemperature feature not available. Aborting.")
        exit(1)

    # Register the on_device_temperature_changed callback to the DeviceTemperature node
    callback_handle = itala.register(device_temperature_node.node, on_device_temperature_changed)
    print("on_device_temperature_changed register to Deviceemperature node\n")

    for i in range(TEMPERATURE_READS):
        # Sleep for a bit
        time.sleep(POLL_FREQ_S*0.001)
        print("Polling temperature...")

        # And then poll nodes of the map. At this point, all nodes with a polling time
        # shorter than the elapsed time are updated and their callback executed, if any.
        nodemap.poll(POLL_FREQ_S)

    itala.deregister(callback_handle)

    device.dispose()
    print("Device instance disposed.")

    system.dispose()
    print("System instance disposed.")


if __name__ == "__main__":
    try:
        node_map_polling()
    except Exception as e:
        print(str(e))
        exit(1)
    exit(0)