send mean values of last 120s via mqtt
This commit is contained in:
parent
e1f97c4a4e
commit
3c34127656
@ -1,6 +1,6 @@
|
||||
import config
|
||||
import logging
|
||||
import mqtt
|
||||
import paho.mqtt.client as paho
|
||||
import socket
|
||||
import report
|
||||
import time
|
||||
@ -15,27 +15,74 @@ except ImportError:
|
||||
logger = logging.getLogger(ROOT_LOGGER_NAME).getChild('main')
|
||||
|
||||
|
||||
mc = mqtt.mqtt_client(config.APP_NAME, config.MQTT_SERVER, 1883, config.MQTT_USER, config.MQTT_PASS)
|
||||
class meaner(dict):
|
||||
def __init__(self):
|
||||
super().__init__(self)
|
||||
self.stop_adding = False
|
||||
self.start_time = time.time()
|
||||
|
||||
def add_data(self, **kwargs):
|
||||
while self.stop_adding:
|
||||
time.sleep(0.1)
|
||||
for key in kwargs:
|
||||
if key not in self:
|
||||
self[key] = kwargs[key]
|
||||
self['__' + key + '_n__'] = 1
|
||||
else:
|
||||
self[key] += kwargs[key]
|
||||
self['__' + key + '_n__'] += 1
|
||||
|
||||
def pop(self):
|
||||
self.stop_adding = True
|
||||
rv = {}
|
||||
for key in self:
|
||||
if not key.startswith('__'):
|
||||
rv[key] = self[key] / self['__' + key + '_n__']
|
||||
self.clear()
|
||||
self.start_time = time.time()
|
||||
self.stop_adding = False
|
||||
return rv
|
||||
|
||||
def meaning_time(self):
|
||||
return time.time() - self.start_time
|
||||
|
||||
|
||||
dht_meaner = meaner()
|
||||
bmp_meaner = meaner()
|
||||
|
||||
def send_data_to_mqtt(data):
|
||||
for key in data:
|
||||
topic = config.MQTT_TOPIC + "/" + key
|
||||
logger.info("Sending Ambient Information to mqtt %s=%s", topic, str(data[key]))
|
||||
mc.send(topic, data[key])
|
||||
client = paho.Client("temp_sens")
|
||||
client.username_pw_set(config.MQTT_USER, config.MQTT_PASS)
|
||||
try:
|
||||
client.connect(config.MQTT_SERVER, 1883)
|
||||
for key in data:
|
||||
topic = config.MQTT_TOPIC + "/" + key
|
||||
logger.info("Sending Ambient Information to mqtt %s=%s", topic, str(data[key]))
|
||||
client.publish(topic, data[key])
|
||||
except (socket.timeout, OSError) as e:
|
||||
logger.warning("Erro while sending ambient information")
|
||||
|
||||
|
||||
def dht_callback(**data):
|
||||
del(data["time"])
|
||||
send_data_to_mqtt(data)
|
||||
dht_meaner.add_data(**data)
|
||||
if dht_meaner.meaning_time() > 2 * 60:
|
||||
sd = dht_meaner.pop()
|
||||
del (sd["time"])
|
||||
send_data_to_mqtt(sd)
|
||||
|
||||
|
||||
def bmp_callback(**data):
|
||||
del(data["time"])
|
||||
del(data["temperature"])
|
||||
send_data_to_mqtt(data)
|
||||
|
||||
bmp_meaner.add_data(**data)
|
||||
if bmp_meaner.meaning_time() > 2 * 30:
|
||||
sd = bmp_meaner.pop()
|
||||
del (sd["time"])
|
||||
del (sd["temperature"])
|
||||
send_data_to_mqtt(sd)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
report.appLoggingConfigure(config.__BASEPATH__, config.LOGTARGET, ((config.APP_NAME, config.LOGLVL), ), fmt=config.formatter, host=config.LOGHOST, port=config.LOGPORT)
|
||||
if __name__ == '__main__':
|
||||
report.appLoggingConfigure(config.__BASEPATH__, config.LOGTARGET, ((config.APP_NAME, config.LOGLVL), ),
|
||||
fmt=config.formatter, host=config.LOGHOST, port=config.LOGPORT)
|
||||
|
||||
#
|
||||
# Initialise data collectors
|
||||
|
Loading…
x
Reference in New Issue
Block a user