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