Пример #1
0
def getResponse(url, params=None, reqtype='GET', **kwargs):
    """
    For the given URL, return the response raising exceptions if the result is
    empty, malformed, or the improper status (i.e. status != 200).
    HTTP action is GET by default, POST is also allowed.
    
    @param url: The full URL
    @type url: C{str}
    
    @param params: Dictionary of parameters to use in the GET or POST request
    @type params: C{dict}
    
    @param reqtype: HTTP protocol method. Allowed are 'GET' (default) and 'POST'
    @type reqtype: C{str}
    
    @param kwargs: Additional keyword arguments to pass to the requests library
    @type kwargs: C{dict}
    
    @return: Response content from the web service
    @rtype: Varies
    
    @raises ServiceException: If the web service call does not respond with a
    status code of 200 (OK).
    @raises Exception: If the response received is empty.

    """
    logger = getFunctionLogger()
    
    if reqtype == 'POST':
        resp = requests.post(url, data=params, **kwargs)
    elif reqtype == 'GET':
        resp = requests.get(url, params=params, **kwargs)
    else:
        raise Exception('Unknown reqtype: %s. Only GET and POST are allowed' % reqtype)
    
    if resp.ok:
        logger.debug("Response from %s : "% url + pprint.pformat(resp.content))
    
        if resp.content is None:
            raise Exception('The url (%s) sent back an empty response.' % url)
        else:
            logger.debug('The url (%s) sent back a valid response' % url)        
    else:
        raise ServiceException(url, resp, message='The call to %s was not successful.' % url)
    
    return resp.content
Пример #2
0
def initializeHardware(testContext, poc, pg, interrogate=False, logging=True):
    """
    Initialize a POC communicator and a PG. The KomodoRS POC and PG entities are 
    used as input to get the PGPro handle/object and to instantiate the Perseus 
    communicator object. 

    The communicator is reset to factory settings, rebooted, and the serial 
    number is set. Optionally, the communicator can be directed to perform an
    interrogation by setting interrogate=True.

    @param testContext: KomodoRS testContext
    @type testContext: C{Dict}
    
    @param poc: C{L{POCCommunicatorEntity<komodors.entities.POCCommunicatorEntity>}}
    @type poc: POCCommunicatorEntity
    
    @param pg: C{L{PGEntity<komodors.entities.PGEntity>}}
    @type pg: PGEntity    
    
    @param interrogate: True/False expression to perform a interrogation
    @type interrogate: C{bool}

    @param logging: True/False expression to start serial port logging on the communicator
    @type logging: C{bool}

    @return: Tuple containing Perseus communicator object and PG handle
    @rtype: C{tuple}
    """

    log = getFunctionLogger()
    assert "pgsim" in testContext, "The provided input for testContext does not have the pgsim key"
    assert type(poc) == POCCommunicatorEntity, "The provided input for POC is not of the correct type"
    assert type(pg) == PGEntity, "The provided input for pg is not of the correct type"

    # Power up the communicator
    relaybox = perseus.relaybox.LatitudeRelayBox()
    log.info("Checking power state of the communicator")
    if not relaybox.getCommunicatorPower():
        relaybox.setCommunicatorPower(1)
        log.info("Powering up communicator and waiting for one minute")
        time.sleep(60)

    # Setup the communicator
    try:
        communicator = perseus.communicator.getInstance()

    # If getInstance fails unable to connect to diag or otherwise, just try
    # again after rebooting first
    except:
        log.info("A problem occurred when initializing the communicator.")
        log.info("Power-cycling the communicator and waiting for DIAG")
        relaybox.setCommunicatorPower(0)
        time.sleep(3)
        relaybox.setCommunicatorPower(1)
        time.sleep(45)
        communicator = perseus.communicator.getInstance()

    assert communicator is not None, "Instantiation of communicator hardware returned a None result"
    assert (
        poc.modelNumber == communicator.getModelNumber()
    ), "Instantiation of communicator hardware returned a different hardware object"
    if logging:
        communicator.startSerialPortLogging(communicator.LOG_FILE)
        log.info("Communicator serial port logging has begun")

    testContext["comm"] = communicator

    communicator.waitForDiagUp()
    communicator.setUTCDateAndTime(perseus.dateandtime.utcNow())
    log.info("Setting communicator serial number to %s" % poc.serialNumber)
    communicator.setSerialNumber(poc.serialNumber)
    communicator.resetToFactorySettingsAndWait()

    # Configure the Wanded G2-based Communicators for PGSim
    if testContext["pgsim"]:
        communicator.configureForPGSim(pg.modelNumber, pg.serialNumber)
    else:
        communicator.configureForPG()

    # Get the PG Handle
    log.info("Initializing the Pulse Generator")
    pghandle = getPGInstance(testContext, pg)
    assert pghandle is not None, "Instantiation of pg hardware returned a None result"
    testContext["pg"] = pghandle

    # Do a PII if requested
    if interrogate:
        log.info("Attempting Patient-initiated interrogation")
        communicator.performPII(returnToHome=True)
    else:
        log.info("Skipping interrogation of pulse generator by request")

    return communicator, pghandle