Mercurial > code > home > repos > mqtt_metrics
comparison mqtt_metrics.py @ 6:bc2a93b306e9
port over the powermeter measurements
author | drewp@bigasterisk.com |
---|---|
date | Sat, 10 Aug 2024 23:03:57 -0700 |
parents | 8390d5d0d512 |
children | a640efa9fb01 |
comparison
equal
deleted
inserted
replaced
5:8390d5d0d512 | 6:bc2a93b306e9 |
---|---|
57 message['payload'] = message['payload'].decode('utf-8') | 57 message['payload'] = message['payload'].decode('utf-8') |
58 | 58 |
59 return message | 59 return message |
60 | 60 |
61 | 61 |
62 async def requestStatuses(client: aiomqtt.Client, period=10): | |
63 '''shellys post status updates periodically, but I want them more often.''' | |
64 while True: | |
65 for topic in [ | |
66 "do-r-power/command/switch:0", | |
67 "ga-fridge-power/command/switch:0", | |
68 "ga-washer-power/command/switch:0", | |
69 "tt-fridge-power/command/switch:0", | |
70 "ws-bench-power/command/switch:0", | |
71 "ws-desk-power/command/switch:0", | |
72 "ws-solder-power/command/switch:0", | |
73 ]: | |
74 await client.publish(topic, 'status_update') | |
75 await asyncio.sleep(period) | |
76 | |
77 | |
62 async def mqttTask(metrics: MetricsWriter): | 78 async def mqttTask(metrics: MetricsWriter): |
63 try: | 79 try: |
64 client = aiomqtt.Client('mqtt2.bigasterisk.com', identifier="mqtt-exporter") | 80 client = aiomqtt.Client('mqtt2.bigasterisk.com', identifier="mqtt-exporter") |
65 async with client: | 81 async with client: |
82 asyncio.create_task(requestStatuses(client)) | |
66 await client.subscribe('#') | 83 await client.subscribe('#') |
67 async for mqttMessage in client.messages: | 84 async for mqttMessage in client.messages: |
68 try: | 85 try: |
69 onMqttMessage(metrics, mqttMessage) | 86 onMqttMessage(metrics, mqttMessage) |
70 except Exception as e: | 87 except Exception as e: |