|
| 1 | +#ifndef MqttMap_h |
| 2 | +#define MqttMap_h |
| 3 | + |
| 4 | +#include <Arduino.h> |
| 5 | +#include <PubSubClient.h> |
| 6 | + |
| 7 | +class MqttMap { |
| 8 | + private: |
| 9 | + void (*outFunction)(String payload) = NULL; |
| 10 | + String (*inFunction)() = NULL; |
| 11 | + |
| 12 | + const char* name = "N/A"; |
| 13 | + int interval = 0; |
| 14 | + unsigned long nextUpdate = 0; // Last read of data |
| 15 | + |
| 16 | + MqttMap* parent = NULL; |
| 17 | + MqttMap* next = NULL; |
| 18 | + MqttMap* children = NULL; |
| 19 | + |
| 20 | + protected: |
| 21 | + MqttMap(const char* _name, MqttMap& _parent) { |
| 22 | + name = _name; |
| 23 | + parent = &_parent; |
| 24 | + interval = 0; |
| 25 | + } |
| 26 | + |
| 27 | + virtual String getTopic() { |
| 28 | + if(parent) { |
| 29 | + return parent->getTopic() + "/" + name; |
| 30 | + } else { |
| 31 | + return String(name); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + public: |
| 36 | + MqttMap(const char* _name) { |
| 37 | + name = _name; |
| 38 | + interval = 5; |
| 39 | + } |
| 40 | + |
| 41 | + int getInterval() { |
| 42 | + if(interval == 0) { |
| 43 | + return parent->getInterval(); |
| 44 | + } |
| 45 | + return interval; |
| 46 | + } |
| 47 | + |
| 48 | + void setInterval(int _interval) { |
| 49 | + interval = _interval; |
| 50 | + } |
| 51 | + |
| 52 | + void callback(const char* _topic, uint8_t* payload, unsigned int length) { |
| 53 | + if(strcmp(getTopic().c_str(), _topic) == 0) { |
| 54 | + if(outFunction != NULL) { |
| 55 | + char _payload[length]; |
| 56 | + for (int i=0;i<length;i++) { |
| 57 | + _payload[i] = (char)payload[i]; |
| 58 | + } |
| 59 | + printf("Found callback, %s: %s\n", _topic, _payload); |
| 60 | + outFunction(String(_payload)); |
| 61 | + } |
| 62 | + } else { |
| 63 | + MqttMap* child = children; |
| 64 | + while(child != NULL) { |
| 65 | + child->callback(_topic, payload, length); |
| 66 | + child = child->next; |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + void subscribe(PubSubClient& client) { |
| 72 | + if(outFunction != NULL) { |
| 73 | + String topic = getTopic(); |
| 74 | + printf("Subscribe: %s\n", topic.c_str()); |
| 75 | + client.subscribe(topic.c_str()); |
| 76 | + } |
| 77 | + MqttMap* child = children; |
| 78 | + while(child != NULL) { |
| 79 | + child->subscribe(client); |
| 80 | + child = child->next; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + void loop(PubSubClient& client) { |
| 85 | + if(inFunction != NULL) { |
| 86 | + unsigned long time = millis(); |
| 87 | + if(time >= nextUpdate) { |
| 88 | + nextUpdate = time + (getInterval() * 1000); |
| 89 | + String topic = getTopic(); |
| 90 | + String value = inFunction(); |
| 91 | + printf("Value(%s): %s\n", topic.c_str(), value.c_str()); |
| 92 | + client.publish(topic.c_str(), value.c_str()); |
| 93 | + } |
| 94 | + } |
| 95 | + MqttMap* child = children; |
| 96 | + while(child != NULL) { |
| 97 | + child->loop(client);; |
| 98 | + child = child->next; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + MqttMap & operator[](const char* name) { |
| 103 | + MqttMap * child = children; |
| 104 | + while(child != NULL) { |
| 105 | + if(strcmp(child->name, name) == 0) { |
| 106 | + return *child; |
| 107 | + } |
| 108 | + child = child->next; |
| 109 | + } |
| 110 | + MqttMap * oldChild = children; |
| 111 | + children = new MqttMap(name, *this); |
| 112 | + children->next = oldChild; |
| 113 | + return *children; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Read data from function and send it to mqtt |
| 118 | + */ |
| 119 | + void operator<<(String (*inFunction)()) { |
| 120 | + MqttMap::inFunction = inFunction; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Handle data comming from mqtt |
| 125 | + */ |
| 126 | + void operator>>(void (*outFunction)(String payload)) { |
| 127 | + MqttMap::outFunction = outFunction; |
| 128 | + } |
| 129 | +}; |
| 130 | + |
| 131 | +#endif |
0 commit comments