Mercurial > code > home > repos > mqtt_metrics
comparison convert.py @ 5:8390d5d0d512
one mqtt message can convert to multiple measurements
author | drewp@bigasterisk.com |
---|---|
date | Sat, 10 Aug 2024 23:02:51 -0700 |
parents | cd1b8d7bda78 |
children | bc2a93b306e9 |
comparison
equal
deleted
inserted
replaced
4:cd1b8d7bda78 | 5:8390d5d0d512 |
---|---|
1 ''' | 1 ''' |
2 Note that these functions need to parse the message['payload'] into a float | 2 Note that these functions need to parse the message['payload'] into a float |
3 ''' | 3 ''' |
4 converters = [] | 4 converters = [] |
5 | |
6 | |
7 def f_from_c(c): | |
8 return (c * 9 / 5) + 32 | |
5 | 9 |
6 | 10 |
7 def topic(topic_pattern: str): | 11 def topic(topic_pattern: str): |
8 | 12 |
9 def decorator(func): | 13 def decorator(func): |
13 | 17 |
14 return decorator | 18 return decorator |
15 | 19 |
16 | 20 |
17 @topic(r'([^-]+)-air-quality/sensor/particulate_matter__10_0__m_concentration/state') | 21 @topic(r'([^-]+)-air-quality/sensor/particulate_matter__10_0__m_concentration/state') |
18 def pm(message, topicGroups: tuple[str]): | 22 def pm(message, topicGroups: tuple[str]) -> list[dict]: |
19 return { | 23 return [{ |
20 'name': 'air_quality_pm', | 24 'name': 'air_quality_pm', |
21 'labels': [{ | 25 'labels': [{ |
22 'labelName': 'location', | 26 'labelName': 'location', |
23 'labelValue': topicGroups[0], | 27 'labelValue': topicGroups[0], |
24 }, { | 28 }, { |
25 'labelName': 'size', | 29 'labelName': 'size', |
26 'labelValue': '10', | 30 'labelValue': '10', |
27 }], | 31 }], |
28 'value': float(message['payload']), | 32 'value': float(message['payload']), |
29 } | 33 }] |
30 | 34 |
31 | 35 |
32 @topic(r'([^-]+)-air-quality/sensor/air_temperature_c/state') | 36 @topic(r'([^-]+)-air-quality/sensor/air_temperature_c/state') |
33 def air_temp(message, topicGroups: tuple[str]): | 37 def air_temp(message, topicGroups: tuple[str]) -> list[dict]: |
34 return { | 38 return [{ |
35 'name': 'air_temperature_f', | 39 'name': 'air_temperature_f', |
36 'labels': [{ | 40 'labels': [{ |
37 'labelName': 'location', | 41 'labelName': 'location', |
38 'labelValue': topicGroups[0], | 42 'labelValue': topicGroups[0], |
39 }], | 43 }], |
40 'value': (float(message['payload']) * 9 / 5) + 32, | 44 'value': f_from_c(float(message['payload'])), |
41 } | 45 }] |