def __init__(self, read_interval=5, log_interval=600): """Constructs the PeriodicReader object: 'read_interval': the time interval between calls to the 'reader' read() method in seconds. 'log_interval': the time interval between points when the readings are summarized and logged to the logging handlers in seconds. """ self.read_interval = read_interval self.log_interval = log_interval # create a list of reader objects that read sensors. self.readers = [] # create a list of handlers to be called with summarized readings # at each logging event. self.logging_handlers = [] # create a dictionary to hold all the readings collected during a # logging interval. The keys of the dictionary will be the reading ID, # and the values will be a two-tuple, the first value being the # reading_type from the readers.base_reader module, and the second value # will be a list of readings, each reading being a tuple of the form: # (timestamp, value) self.read_data = {} # Create a poster object to post readings to the local MQTT broker. # It runs in a separate thread and must be started self.poster = mqtt_poster.MQTTposter() self.poster.start() # track whether a call has been make to log readings before. self.first_log_call = True
def get_last(meter_id): """Returns a (ts, val) tuple for Meter ID 'meter_id'. If that Meter ID is not present (None, None) is returned. """ return last_reads.get(meter_id, (None, None)) def set_last(meter_id, ts, val): """Sets the last meter reading for Meter ID 'meter_id'. The attributes stored are the timestamp 'ts' and the value 'val'. """ last_reads[meter_id] = (ts, val) # Start up the object that will post the final readings to the MQTT # broker. mqtt = mqtt_poster.MQTTposter() mqtt.start() # start the rtlamr program. rtlamr = subprocess.Popen(['/home/pi/gocode/bin/rtlamr', '-gainbyindex=24', # index 24 was found to be the most sensitive '-format=csv'], stdout=subprocess.PIPE, text=True) # Get the Meter multipliers from the Settings file and put # them into a dictionary. meter_mult = { 'Gas': getattr(settings, 'METER_MULT_GAS', 1000.0), 'Elec': getattr(settings, 'METER_MULT_ELEC', 1.0), 'Water': getattr(settings, 'METER_MULT_WATER', 1.0), }