forked from 256dpi/arduino-mqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMQTTTest.h
86 lines (68 loc) · 2.34 KB
/
MQTTTest.h
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
84
85
86
#include <Arduino.h>
template <class T>
class MQTTTest {
public:
void run(T *client);
void message(String topic, String payload);
private:
T *client;
void printResult(boolean res);
boolean testMessage(const char * topic, const char * payload);
boolean newMessage = false;
boolean passedTest = false;
String testTopic;
String testPayload;
boolean testConnectivity(boolean test);
};
/* Methods */
template <class T>
void MQTTTest<T>::run(T *client) {
this->client = client;
Serial.println("Starting tests...");
Serial.print("[Test 1] Connect: ");
this->printResult(this->client->connect("arduino-mqtt-test", "try", "try") && this->testConnectivity(true));
Serial.print("[Test 2] Subscribe & Publish: ");
this->client->subscribe("arduino-mqtt-test/topic1");
this->client->publish("arduino-mqtt-test/topic1", "test");
this->printResult(this->testMessage("arduino-mqtt-test/topic1", "test"));
Serial.print("[Test 3] Unsubscribe: ");
this->client->subscribe("arduino-mqtt-test/topic2");
this->client->subscribe("arduino-mqtt-test/topic3");
this->client->unsubscribe("arduino-mqtt-test/topic2");
this->client->publish("arduino-mqtt-test/topic2", "test");
this->client->publish("arduino-mqtt-test/topic3", "test");
this->printResult(this->testMessage("arduino-mqtt-test/topic3", "test"));
Serial.print("[Test 4] Disconnect: ");
this->client->disconnect();
this->printResult(this->testConnectivity(false));
Serial.println("Tests finished!");
}
template <class T>
void MQTTTest<T>::message(String topic, String payload) {
this->newMessage = true;
this->passedTest = topic.equals(this->testTopic) && payload.equals(this->testPayload);
}
/* Helpers */
template <class T>
void MQTTTest<T>::printResult(boolean res) {
res ? Serial.println("SUCCESS") : Serial.println("FAILED");
}
template <class T>
boolean MQTTTest<T>::testMessage(const char *topic, const char *payload) {
this->testTopic = String(topic);
this->testPayload = String(payload);
while(!this->newMessage) {
this->client->loop();
}
boolean ret = this->passedTest;
this->newMessage = false;
this->passedTest = false;
return ret;
}
template <class T>
boolean MQTTTest<T>::testConnectivity(boolean test) {
while(this->client->connected() != test) {
this->client->loop();
}
return this->client->connected() == test;
}