Ejemplo n.º 1
0
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()
Ejemplo n.º 2
0
        def test_events(self):
            self.assertFalse(self._attached)
            self.assertFalse(self._detached)

            # this is ugly, but it works.
            with patch.object(USBDevice, 'find_all', return_value=[(0, 0, 'AD2-1', 1, 'AD2'), (0, 0, 'AD2-2', 1, 'AD2')]):
                USBDevice.start_detection(on_attached=self.attached_event, on_detached=self.detached_event)

                with patch.object(USBDevice, 'find_all', return_value=[(0, 0, 'AD2-2', 1, 'AD2')]):
                    USBDevice.find_all()
                    time.sleep(1)
                    USBDevice.stop_detection()

            self.assertTrue(self._attached)
            self.assertTrue(self._detached)
Ejemplo n.º 3
0
        # 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, ex:
        print 'Exception:', ex

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

        USBDevice.stop_detection()

def create_device(device_args):
    """
    Creates an AlarmDecoder from the specified USB device arguments.

    :param device_args: Tuple containing information on the USB device to open.
    :type device_args: Tuple (vid, pid, serialnumber, interface_count, description)
    """
    device = AlarmDecoder(USBDevice.find(device_args))
    device.on_message += handle_message
    device.open()

    return device

def handle_message(sender, message):
Ejemplo n.º 4
0
        USBDevice.start_detection(on_attached=handle_attached,
                                  on_detached=handle_detached)

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

    except Exception, ex:
        print 'Exception:', ex

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

        USBDevice.stop_detection()


def create_device(device_args):
    """
    Creates an AlarmDecoder from the specified USB device arguments.

    :param device_args: Tuple containing information on the USB device to open.
    :type device_args: Tuple (vid, pid, serialnumber, interface_count, description)
    """
    device = AlarmDecoder(USBDevice.find(device_args))
    device.on_message += handle_message
    device.open()

    return device