Changeset - b2ba6517bd31
[Not reviewed]
default
0 1 0
Dennis Fink - 5 years ago 2020-02-24 19:08:25
dennis.fink@c3l.lu
Fix handle_sensor
1 file changed with 2 insertions and 2 deletions:
0 comments (0 inline, 0 general)
mqtt2prometheus.py
Show inline comments
 
@@ -50,25 +50,25 @@ energy_consumption = Gauge(
 
    labelnames=["location"],
 
)
 

	
 

	
 
def on_connect(client, userdata, flags, rc):
 
    client.publish("availability/mqtt2prometheus", "online", 2, True)
 

	
 
    topics_handler = {
 
        "availability/#": handle_availability,
 
        "space/status": handle_space_status,
 
        "space/member_count": handle_member_count,
 
        "sensors/people_now_present": handle_people_now_present,
 
        "sensors/#": handle_sensors,
 
        "sensors/#": handle_sensor,
 
    }
 

	
 
    for topic, handler in topics_handler.items():
 
        client.message_callback_add(topic, handler)
 
        client.subscribe(topic)
 

	
 

	
 
def handle_people_now_present(client, userdata, msg):
 
    people_now_present.set(json.loads(msg.payload))
 

	
 

	
 
def handle_space_status(client, userdata, msg):
 
@@ -84,25 +84,25 @@ def handle_member_count(client, userdata
 

	
 
def handle_availability(client, userdata, msg):
 
    t = msg.topic.split("/")
 
    if len(t) == 2:
 
        c = t[-1]
 
    else:
 
        c = "_".join(t[1:])
 
    v = 1 if msg.payload == b"online" else 0
 
    availability.labels(c).set(v)
 

	
 

	
 
def handle_sensor(client, userdata, msg):
 
    t = msg.topic_split("/")
 
    t = msg.topic.split("/")
 
    location = "-".join(t[2:])
 
    if t[1] == "people_now_present":
 
        return
 
    metric = globals().get(t[1], None)
 
    if metric is not None:
 
        try:
 
            metric.labels(location).set(float(msg.payload))
 
        except:
 
            pass
 

	
 

	
 
def main():
0 comments (0 inline, 0 general)