Ejemplo n.º 1
0
def dialogue(addr=""):
    logger.info("Talking with {}".format(addr))
    retcode = 0
    bdg = None
    try:
        bdg = Badge(addr)
        logger.info("Connected")
        while not bdg.dlg.gotStatus:
            bdg.RfdReadWrite.write("s")  # ask for status
            bdg.waitForNotifications(1.0)  # waiting for status report

        logger.info("Got status")
        if bdg.dlg.needDate:
            bdg.sendDateTime()
            logger.info("Date sent")
        else:
            logger.info("Already synced")

        if bdg.dlg.dataReady:
            logger.info("Requesting data...")
            bdg.RfdReadWrite.write("d")  # ask for data
            wait_count = 0
            while True:
                if bdg.waitForNotifications(1.0):
                    # if got data, don't inrease the wait counter
                    continue
                logger.info("Waiting for more data...")
                wait_count = wait_count + 1
                if wait_count >= PULL_WAIT: break
            logger.info("finished reading data")

    except BTLEException, e:
        retcode = -1
        logger.error("failed pulling data")
        logger.error(e.code)
        logger.error(e.message)
Ejemplo n.º 2
0
def dialogue(addr=""):
    logger.info("Connecting to {}".format(addr))
    retcode = 0
    bdg = None
    try:
        with timeout(seconds=5, error_message="Connect timeout"):
            bdg = Badge(addr)

        logger.info("Connected")

        with timeout(
                seconds=5,
                error_message="Dialogue timeout (wrong firmware version?)"):
            while not bdg.dlg.gotStatus:
                bdg.sendStatusRequest()  # ask for status
                bdg.waitForNotifications(1.0)  # waiting for status report

            logger.info("Got status")

            #while not bdg.dlg.gotDateTime:
            #	bdg.NrfReadWrite.write("t")  # ask for time
            #	bdg.waitForNotifications(1.0)

            logger.info(
                "Badge datetime: {},{}, Voltage: {}, Recording? {}".format(
                    bdg.dlg.timestamp_sec, bdg.dlg.timestamp_ms,
                    bdg.dlg.voltage, bdg.dlg.recording))

        with timeout(
                seconds=5,
                error_message="Dialogue timeout (wrong firmware version?)"):
            while not bdg.dlg.sentStartRec:
                bdg.sendStartRecRequest(
                    RECORDING_TIMEOUT)  # ask to start recoding
                #bdg.sendStatusRequest()  # ask for status instead
                bdg.waitForNotifications(1.0)  # waiting for status report

            logger.info("Got Ack for start recording")

        # data request using the "r" command - data since time X
        logger.info("Requesting data...")
        lastChunkDate = None
        with badgeDB() as db:
            lastChunkDate = db.getLastChunkDate(addr)

        if (lastChunkDate == None):
            lastChunkDate = datetime.datetime.now()
            logger.info(
                "Cannot find saved chunks. Setting last chunk date to: {}".
                format(lastChunkDate))
        else:
            logger.info("Last chunk date: {}".format(lastChunkDate))

        bdg.sendDataRequest(lastChunkDate)  # ask for data
        wait_count = 0
        while True:
            if bdg.dlg.gotEndOfData == True:
                logger.info("End of data")
                break
            if bdg.waitForNotifications(1.0):
                # if got data, don't inrease the wait counter
                continue
            logger.info("Waiting for more data...")
            wait_count = wait_count + 1
            if wait_count >= PULL_WAIT: break
        logger.info("finished reading data")

    except BTLEException, e:
        retcode = -1
        logger.error("failed pulling data")
        logger.error(e.code)
        logger.error(e.message)