def __process(logger, client, iface, nodes, node2keys): last_timestamp = time.time() topic = Utils.gettopic("rfemon/{0}/{1}/{2}") while is_running: if iface is None: time.sleep(0.1) iface = __try_open(logger) continue try: # Execute run method iface.run() # Read socket current_values = iface.read() if current_values is None and time.time( ) - last_timestamp > MAX_TIME_WITHOUT_READ: current_values = __build_null_values(node2keys) logger.alert( "No values read for a long time, probably something is wrong with RF device" ) iface.close() time.sleep(0.1) iface = __try_open(logger) elif current_values is not None: current_values = [current_values] # If complete and valid data values were received if current_values is not None: for values in current_values: logger.debug(str(values)) t = values[0] nodeId = values[1] for conf in node2keys[str(nodeId)]: last_data = conf["data"] key = conf["key"] name = conf["name"] mul = conf.get("mul", 1.0) add = conf.get("add", 0.0) v = values[key] if v is not None: v = (v + add) * mul alert_below_th = conf.get("alert_below_threshold", None) if alert_below_th is not None and v is not None and v < alert_below_th: logger.alert( "Value %f for nodeId %d key %d registered with name %s is below threshold %f", float(v), nodeId, key, name, float(alert_below_th)) if Utils.compute_relative_difference( last_data, v ) > conf.get( "tolerance", DEFAULT_POWER_TOLERANCE ) or t - conf["when"] > MAX_TIME_BETWEEN_READINGS: message = {'timestamp': t, 'data': v} client.publish(topic.format(name, nodeId, key), json.dumps(message)) conf["data"] = v conf["when"] = t last_timestamp = t except: print "Unexpected error:", traceback.format_exc() logger.error("Unexpected error: %s", traceback.format_exc()) time.sleep(0.05)
def publish(): """Publishes circle messages via MQTT.""" try: # All circles messages are generated together and before sending data # via MQTT. This way sending data overhead is ignored and we expect # similar timestamps between all circles. messages = [] for i, c in enumerate(circles): config = circles_config[i] t = time.time() mac = config["mac"] name = config["name"] last_powers = [config["power" + x["suffix"]] for x in OUTPUT_LIST] last_state = config["state"] try: reading = c.get_power_usage() powers = [reading[x["key"]] for x in OUTPUT_LIST] state = c.get_info()['relay_state'] alert_below_th = config.get("alert_below_threshold", None) for i, p in enumerate(powers): p = max(0, p) key = OUTPUT_LIST[i]["key"] suffix = OUTPUT_LIST[i]["suffix"] if alert_below_th is not None and p < alert_below_th: logger.alert( "Value %f %s for circle %s registered with name %s is below threshold %f", float(p), suffix, mac, name, float(alert_below_th)) if Utils.compute_relative_difference( last_powers[i], p) > config.get( "tolerance", DEFAULT_POWER_TOLERANCE ) or t - config[ "when" + suffix] > MAX_TIME_BETWEEN_READINGS: usage_message = {'timestamp': t, 'data': p} messages.append((topic.format(name, "power" + suffix, mac), usage_message)) config["power" + suffix] = p config["when" + suffix] = t # check state transition before message is appended if state != last_state: state_message = {'timestamp': t, 'data': state} messages.append((topic.format(name, "state", mac), state_message)) config["state"] = state # track current state value except: print "Unexpected error:", traceback.format_exc() logger.info("Error happened while processing circles data: %s", traceback.format_exc()) for top, message in messages: client.publish(top, json.dumps(message)) except: print "Unexpected error:", traceback.format_exc() logger.error("Error happened while processing circles data") raise
def __process(logger, client, iface, nodes, node2keys): last_timestamp = time.time() topic = Utils.gettopic("rfemon/{0}/{1}/{2}") while is_running: if iface is None: time.sleep(0.1) iface = __try_open(logger) continue try: # Execute run method iface.run() # Read socket current_values = iface.read() if current_values is None and time.time() - last_timestamp > MAX_TIME_WITHOUT_READ: current_values = __build_null_values(node2keys) logger.alert("No values read for a long time, probably something is wrong with RF device") iface.close() time.sleep(0.1) iface = __try_open(logger) elif current_values is not None: current_values = [ current_values ] # If complete and valid data values were received if current_values is not None: for values in current_values: logger.debug(str(values)) t = values[0] nodeId = values[1] for conf in node2keys[str(nodeId)]: last_data = conf["data"] key = conf["key"] name = conf["name"] mul = conf.get("mul", 1.0) add = conf.get("add", 0.0) v = values[key] if v is not None: v = (v + add) * mul alert_below_th = conf.get("alert_below_threshold", None) if alert_below_th is not None and v is not None and v < alert_below_th: logger.alert("Value %f for nodeId %d key %d registered with name %s is below threshold %f", float(v), nodeId, key, name, float(alert_below_th)) if Utils.compute_relative_difference(last_data, v) > conf.get("tolerance",DEFAULT_POWER_TOLERANCE) or t - conf["when"] > MAX_TIME_BETWEEN_READINGS: message = { 'timestamp' : t, 'data' : v } client.publish(topic.format(name, nodeId, key), json.dumps(message)) conf["data"] = v conf["when"] = t last_timestamp = t except: print "Unexpected error:",traceback.format_exc() logger.error("Unexpected error: %s", traceback.format_exc()) time.sleep(0.05)