Ejemplo n.º 1
0
def enable_only( serial_numbers, recycle = False, timeout = 5 ):
    """
    Enable only the devices corresponding to the given serial-numbers. This can work either
    with or without Acroname: without, the devices will simply be HW-reset, but other devices
    will still be present.

    NOTE: will raise an exception if any SN is unknown!

    :param serial_numbers: A collection of serial-numbers to enable - all others' ports are
                           disabled and will no longer be usable!
    :param recycle: If False, the devices will not be reset if they were already enabled. If
                    True, the devices will be recycled by disabling the port, waiting, then
                    re-enabling
    :param timeout: The maximum seconds to wait to make sure the devices are indeed online
    """
    if acroname:
        #
        ports = [ get_port( sn ) for sn in serial_numbers ]
        #
        if recycle:
            #
            log.d( 'recycling ports via acroname:', ports )
            #
            acroname.disable_ports( acroname.ports() )
            _wait_until_removed( serial_numbers, timeout = timeout )
            #
            acroname.enable_ports( ports )
            #
        else:
            #
            acroname.enable_ports( ports, disable_other_ports = True )
        #
        _wait_for( serial_numbers, timeout = timeout )
        #
    elif recycle:
        #
        hw_reset( serial_numbers )
        #
    else:
        log.d( 'no acroname; ports left as-is' )
Ejemplo n.º 2
0
def map_unknown_ports():
    """
    Fill in unknown ports in devices by enabling one port at a time, finding out which device
    is there.
    """
    if not acroname:
        return
    global _device_by_sn
    devices_with_unknown_ports = [
        device for device in _device_by_sn.values() if device.port is None
    ]
    if not devices_with_unknown_ports:
        return
    #
    ports = acroname.ports()
    known_ports = [
        device.port for device in _device_by_sn.values()
        if device.port is not None
    ]
    unknown_ports = [port for port in ports if port not in known_ports]
    try:
        log.d('mapping unknown ports', unknown_ports, '...')
        log.debug_indent()
        #log.d( "active ports:", ports )
        #log.d( "- known ports:", known_ports )
        #log.d( "= unknown ports:", unknown_ports )
        #
        for known_port in known_ports:
            if known_port not in ports:
                log.e("A device was found on port", known_port,
                      "but the port is not reported as used by Acroname!")
        #
        if len(unknown_ports) == 1:
            device = devices_with_unknown_ports[0]
            log.d('... port', unknown_ports[0], 'has', device.handle)
            device._port = unknown_ports[0]
            return
        #
        acroname.disable_ports(ports)
        wait_until_all_ports_disabled()
        #
        # Enable one port at a time to try and find what device is connected to it
        n_identified_ports = 0
        for port in unknown_ports:
            #
            log.d('enabling port', port)
            acroname.enable_ports([port], disable_other_ports=True)
            sn = None
            for retry in range(5):
                if len(enabled()) == 1:
                    sn = list(enabled())[0]
                    break
                time.sleep(1)
            if not sn:
                log.d('could not recognize device in port', port)
            else:
                device = _device_by_sn.get(sn)
                if device:
                    log.d('... port', port, 'has', device.handle)
                    device._port = port
                    n_identified_ports += 1
                    if len(devices_with_unknown_ports) == n_identified_ports:
                        #log.d( 'no more devices; stopping' )
                        break
                else:
                    log.w("Device with serial number", sn, "was found in port",
                          port, "but was not in context")
            acroname.disable_ports([port])
            wait_until_all_ports_disabled()
    finally:
        log.debug_unindent()