WIP: MQTT combined mode

This commit is contained in:
Sebastian Lohff 2021-01-10 23:32:09 +01:00
parent 34f35cf13a
commit 3a086962c0
4 changed files with 42 additions and 11 deletions

View File

@ -28,6 +28,7 @@ def main():
debug = debug or config.get('debug') debug = debug or config.get('debug')
config['debug'] = debug config['debug'] = debug
esp_reset_time = config['host'].get('esp-reset-time')
sensorlib.Sensor.configure(config['host']['name'], sys.platform) sensorlib.Sensor.configure(config['host']['name'], sys.platform)
mcfg = config['mqtt'] mcfg = config['mqtt']
@ -36,6 +37,7 @@ def main():
else: else:
client_id = None client_id = None
mqtt_mode = config['mqtt'].get('mode', 'split')
mqtt = MQTTClient(mcfg['host'], mcfg['user'], mcfg['password'], client_id) mqtt = MQTTClient(mcfg['host'], mcfg['user'], mcfg['password'], client_id)
default_mqtt_topic = mcfg.get('sensor_topic') default_mqtt_topic = mcfg.get('sensor_topic')
print("Connected to", mqtt.host, "with default topic", default_mqtt_topic) print("Connected to", mqtt.host, "with default topic", default_mqtt_topic)
@ -54,11 +56,12 @@ def main():
print("Subscribe to", topic) print("Subscribe to", topic)
mqtt.subscribe(topic) mqtt.subscribe(topic)
last_measurement = time.time() last_measurement = start_time = time.time()
while True: while True:
# handle regular sensor data # handle regular sensor data
if debug: if debug:
print("Getting values from sensors...") print("Getting values from sensors...")
if mqtt_mode == 'split':
for sensor in sensors: for sensor in sensors:
if sensor.HAS_SENSOR_DATA: if sensor.HAS_SENSOR_DATA:
data = sensor.gen_datapoint() data = sensor.gen_datapoint()
@ -67,6 +70,21 @@ def main():
mqtt.send_data(default_mqtt_topic, data) mqtt.send_data(default_mqtt_topic, data)
if not mqtt.is_async(): if not mqtt.is_async():
mqtt.process_mqtt() mqtt.process_mqtt()
elif mqtt_mode == 'combined':
data = {
'hostname': config['host']['name'],
'platform': sys.platform,
}
for sensor in sensors:
if sensor.HAS_SENSOR_DATA:
data[sensor.id] = sensor.get_data()
if debug:
print(default_mqtt_topic, data)
mqtt.send_data(default_mqtt_topic, data)
if not mqtt.is_async():
mqtt.process_mqtt()
else:
raise RuntimeError("Unknown mqtt mode {}".format(mqtt_mode))
if mqtt.is_async(): if mqtt.is_async():
time.sleep(max(0.0, time.time() + config['host']['interval'] - last_measurement)) time.sleep(max(0.0, time.time() + config['host']['interval'] - last_measurement))
@ -76,6 +94,11 @@ def main():
time.sleep(0.1) time.sleep(0.1)
last_measurement = time.time() last_measurement = time.time()
if sys.implementation.name == 'micropython' and esp_reset_time and \
last_measurement - start_time > esp_reset_time:
import machine
machine.reset()
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@ -15,3 +15,6 @@ class MQTTClientBase:
def is_async(self): def is_async(self):
return False return False
def process_mqtt(self):
pass

View File

@ -9,8 +9,13 @@ class MQTTClient(MQTTClientBase):
def _connect(self): def _connect(self):
self._mqtt = mqtt.Client(self.client_id) self._mqtt = mqtt.Client(self.client_id)
self._mqtt.username_pw_set(self.user, self.password) self._mqtt.username_pw_set(self.user, self.password)
self._mqtt.on_connect = self.on_connect
self._mqtt.connect(self.host) self._mqtt.connect(self.host)
def on_connect(self, *args, **kwargs):
#print("Connected! client={} userdata={} msg={}".format(client, userdata, msg))
print("Connected!", *args, **kwargs)
def send_data(self, topic, data): def send_data(self, topic, data):
self._mqtt.publish(topic, json.dumps(data)) self._mqtt.publish(topic, json.dumps(data))

View File

@ -17,10 +17,10 @@ class Sensor(object):
_mqtt_callbacks = {} _mqtt_callbacks = {}
HAS_SENSOR_DATA = True HAS_SENSOR_DATA = True
def __init__(self, sensor_id, name, sensor_type, pin, description, **sensor_conf): def __init__(self, id, name, type, pin, description, **sensor_conf):
self.id = sensor_id self.id = id
self.name = name self.name = name
self.type = sensor_type self.type = type
self.pin = pin self.pin = pin
self.description = description self.description = description
@ -41,7 +41,7 @@ class Sensor(object):
def get_data(self): def get_data(self):
raise NotImplementedError("You missed a spot!") raise NotImplementedError("You missed a spot!")
def gen_datapoint(self): def gen_datapoint(self, legacy=True):
return { return {
'type': 'measurement', 'type': 'measurement',
'tags': { 'tags': {