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)
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))
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']))
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
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))
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))
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
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
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))
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
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))
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)
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)
def log(self): logger.error(self.args[0])