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