示例#1
0
def bytes_to_hashed_chunk(s):
    if len(s) > settings.CHUNK_SIZE:
        logger.error("Too big a byteseq to fit in one chunk")
        return

    b = bytearray()

    # Marker for the beginning of the chunk
    b += constant_to_bytes(Constant.BEG_CHUNK)

    # "Size block"
    b += constant_to_bytes(Constant.BEG_SIZE)
    b += uint_to_bytes(len(s))
    b += constant_to_bytes(Constant.END_SIZE)

    # "Data block"
    b += constant_to_bytes(Constant.BEG_DATA)
    b += s
    b += constant_to_bytes(Constant.END_DATA)

    # "Hash block"
    b += constant_to_bytes(Constant.BEG_HASH)
    b += utils.get_hash(s)
    b += constant_to_bytes(Constant.END_HASH)

    # Marker for the end of the chunk
    b += constant_to_bytes(Constant.END_CHUNK)

    # logger.debug("Constructed chunk {} for byteseq {}"
    #  .format(pretty_print(b), pretty_print(s)))
    return bytes(b)
示例#2
0
def on_measurement_received(measurement_json):
    logger.debug('[opentele] Measurement received: %s' % (measurement_json))

    try:
        endpoints.process_measurement(measurement_json)
    except Exception, e:
        logger.error('[opentele] Error processing measurement: %s' % (e))
示例#3
0
def process_measurement(measurement_json):
    logger.debug("[opentele] Processing %s measurement: %s" % (measurement_json['type'], measurement_json))

    if measurement_json['type'] == 'weight':
        send_weight_req = SendWeight(get_credentials(), measurement_json['value'], measurement_json['timestamp'])
        res = send_weight_req.post()

        if (res.status_code < 400 ):
            logger.debug("[opentele] The result of posting %s to OpenTele: %s -- with HTTP STATUS: %s" % (measurement_json, str(res.text), str(res.status_code)))
        else:
            logger.error("[opentele] The result of posting %s to OpenTele: %s -- with HTTP STATUS: %s" % (measurement_json, str(res.text), str(res.status_code)))

        return

    elif measurement_json['type'] == 'heartrate':
        send_bp_req = SendBP(get_credentials(), systolic=None, diastolic=None, pulse=measurement_json['value'],
                                timestamp = measurement_json['timestamp'])
        res = send_bp_req.post()

        if (res.status_code < 400 ):
            logger.debug("[opentele] The result of posting %s to OpenTele: %s -- with HTTP STATUS: %s" % (measurement_json, str(res.text), str(res.status_code)))
        else:
            logger.error("[opentele] The result of posting %s to OpenTele: %s -- with HTTP STATUS: %s" % (measurement_json, str(res.text), str(res.status_code)))

        return

    raise Exception("Unsupported measurement type: %s" % (measurement_json['type']))
示例#4
0
文件: transfer.py 项目: mrkgnao/refl
def bytes_to_hashed_chunk(s):
    if len(s) > settings.CHUNK_SIZE:
        logger.error("Too big a byteseq to fit in one chunk")
        return

    b = bytearray()

    # Marker for the beginning of the chunk
    b += constant_to_bytes(Constant.BEG_CHUNK)

    # "Size block"
    b += constant_to_bytes(Constant.BEG_SIZE)
    b += uint_to_bytes(len(s))
    b += constant_to_bytes(Constant.END_SIZE)

    # "Data block"
    b += constant_to_bytes(Constant.BEG_DATA)
    b += s
    b += constant_to_bytes(Constant.END_DATA)

    # "Hash block"
    b += constant_to_bytes(Constant.BEG_HASH)
    b += utils.get_hash(s)
    b += constant_to_bytes(Constant.END_HASH)

    # Marker for the end of the chunk
    b += constant_to_bytes(Constant.END_CHUNK)

    # logger.debug("Constructed chunk {} for byteseq {}"
    #  .format(pretty_print(b), pretty_print(s)))
    return bytes(b)
示例#5
0
def perform_login():
    logger.debug("[linkwatch] Getting auth token...")

    login_endpoint = LoginEndpoint(LINKWATCH_USER, LINKWATCH_PASSWORD)
    login_res = login_endpoint.post()
    if login_res.is_error():
        logger.error("[linkwatch] Could not log demo user in. Login result: %s" % (login_res.get_error_reason()))

    return login_res
示例#6
0
def match_next(opcode, conn):
    received_bytes = conn.recv(2)
    expected_bytes = constant_to_bytes(opcode)

    if received_bytes != expected_bytes:
        logger.error("Unexpected opcode: "
                     "expected {}, received {}".format(
                         pretty_print(expected_bytes),
                         pretty_print(received_bytes)))
    else:
        pass  # logger.info("Received expected opcode {}".format(opcode))
示例#7
0
def send_observation(api_token, observation):
    obs_endpoint = SendObservationsEndpoint(api_token, [observation])
    obs_res = obs_endpoint.post()

    if not obs_res.is_error():
        logger.info("[linkwatch] Observation POST result for %s: %s -- with HTTP STATUS: %s" % (observation, obs_res.get_text(), obs_res.get_status()))
    else:
        try:
            obs_res.response.raise_for_status()
        except Exception, e:
            logger.error("[linkwatch] Failed to POST new weight observation %s: %s" % (observation, e))
示例#8
0
def perform_login():
    logger.debug("[linkwatch] Getting auth token...")

    login_endpoint = LoginEndpoint(LINKWATCH_USER, LINKWATCH_PASSWORD)
    login_res = login_endpoint.post()
    if login_res.is_error():
        logger.error(
            "[linkwatch] Could not log demo user in. Login result: %s" %
            (login_res.get_error_reason()))

    return login_res
示例#9
0
def send_chunk(chunk, conn, num_retries=settings.MAX_RETRIES, ab=(0, 0)):
    tries_left = num_retries
    while tries_left > 0:
        conn.sendall(chunk)
        if recv_inforeq(conn) != Constant.INFO_RECV_OK:
            tries_left -= 1
            logger.error("Chunk {} of {} apparently received incorrectly "
                         "({} out of {} retries left)".format(
                             *ab, tries_left, num_retries))
        else:
            logger.debug("Chunk {} of {} sent successfully".format(*ab))
            break
示例#10
0
文件: transfer.py 项目: mrkgnao/refl
def match_next(opcode, conn):
    received_bytes = conn.recv(2)
    expected_bytes = constant_to_bytes(opcode)

    if received_bytes != expected_bytes:
        logger.error("Unexpected opcode: "
                     "expected {}, received {}"
                     .format(
                      pretty_print(expected_bytes),
                      pretty_print(received_bytes)))
    else:
        pass  # logger.info("Received expected opcode {}".format(opcode))
示例#11
0
文件: transfer.py 项目: mrkgnao/refl
def send_chunk(chunk, conn, num_retries=settings.MAX_RETRIES, ab=(0, 0)):
    tries_left = num_retries
    while tries_left > 0:
        conn.sendall(chunk)
        if recv_inforeq(conn) != Constant.INFO_RECV_OK:
            tries_left -= 1
            logger.error(
                "Chunk {} of {} apparently received incorrectly "
                "({} out of {} retries left)"
                .format(*ab, tries_left, num_retries))
        else:
            logger.debug("Chunk {} of {} sent successfully".format(*ab))
            break
示例#12
0
def send_observation(api_token, observation):
    obs_endpoint = SendObservationsEndpoint(api_token, [observation])
    obs_res = obs_endpoint.post()

    if not obs_res.is_error():
        logger.info(
            "[linkwatch] Observation POST result for %s: %s -- with HTTP STATUS: %s"
            % (observation, obs_res.get_text(), obs_res.get_status()))
    else:
        try:
            obs_res.response.raise_for_status()
        except Exception, e:
            logger.error(
                "[linkwatch] Failed to POST new weight observation %s: %s" %
                (observation, e))
示例#13
0
def process_measurement(measurement_json):
    login_res = perform_login()
    token = login_res.get_token()

    if not token:
        logger.error("[linkwatch] Auth token unavailable due to error in login_endpoint call. Reason: %s" % (login_res.get_error_reason()))
    else:
        logger.info("[linkwatch] Auth token value is: %s" % (token))

    obs = None
    try:
        obs = get_observation_from_measurement_json(measurement_json)
    except Exception as e:
        logger.error("[linkwatch] Could not convert measurement -- %s -- to linkwatch observation: %s" % (measurement_json, e))
        return

    send_observation(token, obs)
示例#14
0
def process_measurement(measurement_json):
    login_res = perform_login()
    token = login_res.get_token()

    if not token:
        logger.error(
            "[linkwatch] Auth token unavailable due to error in login_endpoint call. Reason: %s"
            % (login_res.get_error_reason()))
    else:
        logger.info("[linkwatch] Auth token value is: %s" % (token))

    obs = None
    try:
        obs = get_observation_from_measurement_json(measurement_json)
    except Exception as e:
        logger.error(
            "[linkwatch] Could not convert measurement -- %s -- to linkwatch observation: %s"
            % (measurement_json, e))
        return

    send_observation(token, obs)
示例#15
0
 def log(self):
     logger.error(self.args[0])