Mercurial > code > home > repos > mqtt_metrics
diff convert.py @ 2:579df3a4e62d
rewrite converters as register'able functions
author | drewp@bigasterisk.com |
---|---|
date | Fri, 09 Aug 2024 17:37:00 -0700 |
parents | |
children | cd1b8d7bda78 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/convert.py Fri Aug 09 17:37:00 2024 -0700 @@ -0,0 +1,38 @@ +converters = [] + + +def topic(topic_pattern: str): + + def decorator(func): + func.topic_pattern = topic_pattern + converters.append(func) + return func + + return decorator + + +@topic(r'([^-]+)-air-quality/sensor/particulate_matter__10_0__m_concentration/state') +def pm(message, topicGroups: tuple[str]): + return { + 'name': 'air_quality_pm', + 'labels': [{ + 'labelName': 'location', + 'labelValue': topicGroups[0], + }, { + 'labelName': 'size', + 'labelValue': '10', + }], + 'value': message['payload'], + } + + +@topic(r'([^-]+)-air-quality/sensor/air_temperature_c/state') +def air_temp(message, topicGroups: tuple[str]): + return { + 'name': 'air_temperature_f', + 'labels': [{ + 'labelName': 'location', + 'labelValue': topicGroups[0], + }], + 'value': (float(message['payload']) * 9 / 5) + 32, + }