-
Notifications
You must be signed in to change notification settings - Fork 3
/
PubSubClientTools.cpp
83 lines (66 loc) · 2.51 KB
/
PubSubClientTools.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "PubSubClientTools.h"
// Public
PubSubClientTools::PubSubClientTools(PubSubClient& _pubSub) : pubSub(_pubSub) {
pubSub.setCallback(mqtt_callback);
};
bool PubSubClientTools::connected() {
return pubSub.connected();
}
bool PubSubClientTools::connect(String clientId) {
char client_char[CLIENTID_BUFFER_SIZE];
clientId.toCharArray(client_char, CLIENTID_BUFFER_SIZE);
return pubSub.connect(client_char);
}
bool PubSubClientTools::connect(String clientId, String willTopic, int willQoS, bool willRetain, String willMessage) {
char client_char[CLIENTID_BUFFER_SIZE];
char topic_char[TOPIC_BUFFER_SIZE];
char msg_char[MESSAGE_BUFFER_SIZE];
clientId.toCharArray(client_char, CLIENTID_BUFFER_SIZE);
willTopic.toCharArray(topic_char, TOPIC_BUFFER_SIZE);
willMessage.toCharArray(msg_char, MESSAGE_BUFFER_SIZE);
return pubSub.connect(client_char, topic_char, willQoS, willRetain, msg_char);
}
bool PubSubClientTools::publish(String topic, String message) {
return this->publish(topic, message, false);
}
bool PubSubClientTools::publish(String topic, String message, bool retained) {
char topic_char[TOPIC_BUFFER_SIZE];
char msg_char[MESSAGE_BUFFER_SIZE];
topic.toCharArray(topic_char, TOPIC_BUFFER_SIZE);
message.toCharArray(msg_char, MESSAGE_BUFFER_SIZE);
return pubSub.publish(topic_char, msg_char, retained);
}
bool PubSubClientTools::subscribe(String topic, CALLBACK_SIGNATURE) {
if (callbackCount >= CALLBACK_LIST_SIZE) return false;
char topic_char[TOPIC_BUFFER_SIZE];
topic.toCharArray(topic_char, TOPIC_BUFFER_SIZE);
callbackList[callbackCount].topic = topic;
callbackList[callbackCount].callback = callback;
callbackCount++;
return pubSub.subscribe(topic_char);
}
int PubSubClientTools::resubscribe() {
int count = 0;
pubSub.setCallback(mqtt_callback);
for (int i = 0; i < callbackCount; i++) {
char topic_char[TOPIC_BUFFER_SIZE];
callbackList[i].topic.toCharArray(topic_char, TOPIC_BUFFER_SIZE);
if ( pubSub.subscribe(topic_char) ) {
count++;
}
}
return count;
}
// Private
void PubSubClientTools::callback(PUBSUBCLIENT_CALLBACK_PARAMETERS) {
String topic = String(topicChar);
String message = "";
for (int i = 0; i < length; i++) {
message += (char)payload[i];
}
for (int i = 0; i < callbackCount; i++) {
if (MqttWildcard::wildcardMatch(topic, callbackList[i].topic)) {
(*callbackList[i].callback)(topic,message);
}
}
}