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 __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)
import re import requests import time import traceback import unicodedata import urllib2 from xml.etree.ElementTree import parse import raspi_mon_sys.aemet as aemet import raspi_mon_sys.LoggerClient as LoggerClient import raspi_mon_sys.Utils as Utils NUM_DAYS = 4 weather_topic = Utils.gettopic("aemet/{0}/{1}") forecast_topic = Utils.gettopic("aemet/{0}/{1}/{2}", "forecast") # This variables are loaded from MongoDB server at start() function. tz = None logger = None current_weather_url = None location_id = None config = None daily_forecast_info = ( # more than one value ( "get_precipitacion", "period", "rain_prob" ), ( "get_cota_nieve", "period", "snow_level" ), ( "get_estado_cielo", "period", "sky" ), ( "get_viento", "period", "wind_direction", "wind_speed" ),
import datetime import json import pytz import sys import traceback import time import urllib2 sys.path.append("../") import raspi_mon_sys.LoggerClient as LoggerClient import raspi_mon_sys.Scheduler as Scheduler import raspi_mon_sys.Utils as Utils logger = None topic = Utils.gettopic("electricity_prices/{0}") url = 'http://www.esios.ree.es/Solicitar?fileName=PVPC_CURV_DD_{0}&fileType=txt&idioma=es' def __on_connect(client, userdata, rc): logger.info("Connected to MQTT broker") def __configure(client): client.on_connect = __on_connect def __publish_data_of_day(day_str, ref_time): try: client = Utils.getpahoclient(logger, __configure) except: logger.error("Unable to connecto to MQTT broker") raise tomorrow_url = url.format(day_str)
import datetime import json import pytz import sys import traceback import time import urllib2 sys.path.append("../") import raspi_mon_sys.LoggerClient as LoggerClient import raspi_mon_sys.Scheduler as Scheduler import raspi_mon_sys.Utils as Utils logger = None topic = Utils.gettopic("electricity_prices/{0}") url = 'http://www.esios.ree.es/Solicitar?fileName=PVPC_CURV_DD_{0}&fileType=txt&idioma=es' def __on_connect(client, userdata, rc): logger.info("Connected to MQTT broker") def __configure(client): client.on_connect = __on_connect def __publish_data_of_day(day_str, ref_time): try: client = Utils.getpahoclient(logger, __configure) except:
import json import time import traceback import raspi_mon_sys.LoggerClient as LoggerClient import raspi_mon_sys.plugwise.api as plugwise_api import raspi_mon_sys.Scheduler as Scheduler import raspi_mon_sys.Utils as Utils # Plugwise connection configuration. MAX_TIME_BETWEEN_READINGS = 1800 DEFAULT_POWER_TOLERANCE = 0.0 DEFAULT_SERIAL_PORT = "/dev/ttyUSB0" # USB port used by Plugwise receiver OUTPUT_LIST = [{"key": 0, "suffix": "1s"}, {"key": 1, "suffix": "8s"}] topic = Utils.gettopic("plugwise/{0}/{1}/{2}") logger = None client = None config = None device = None circles_config = None circles = None mac2circle = None verbose = False def __on_connect(client, userdata, rc): # We will use this topic to send on/off commands to our circles. client.subscribe("plugwise/#")
import re import requests import time import traceback import unicodedata import urllib2 from xml.etree.ElementTree import parse import raspi_mon_sys.aemet as aemet import raspi_mon_sys.LoggerClient as LoggerClient import raspi_mon_sys.Utils as Utils NUM_DAYS = 4 weather_topic = Utils.gettopic("aemet/{0}/{1}") forecast_topic = Utils.gettopic("aemet/{0}/{1}/{2}", "forecast") # This variables are loaded from MongoDB server at start() function. tz = None logger = None current_weather_url = None location_id = None config = None daily_forecast_info = ( # more than one value ("get_precipitacion", "period", "rain_prob"), ("get_cota_nieve", "period", "snow_level"), ("get_estado_cielo", "period", "sky"), ("get_viento", "period", "wind_direction", "wind_speed"),