Example application that shows how to handle attach/detach events generated
    by the USB devices.

    In this case we open the device and listen for messages when it is attached.
    And when it is detached we remove it from our list of monitored devices.

def main():
    """
    Example application that shows how to handle attach/detach events generated
    by the USB devices.

    In this case we open the device and listen for messages when it is attached.
    And when it is detached we remove it from our list of monitored devices.
    """
    try:
        # Start up the detection thread such that handle_attached and handle_detached will
        # be called when devices are attached and detached, respectively.
        USBDevice.start_detection(on_attached=handle_attached, on_detached=handle_detached)

        # Wait for events.
        while True:
            time.sleep(1)

    except Exception as ex:
        print('Exception:', ex)

    finally:
        # Close all devices and stop detection.
        for sn, device in __devices.items():
            device.close()

        USBDevice.stop_detection()