|
| 1 | +#include <Arduino.h> |
| 2 | + |
| 3 | +template <class T> |
| 4 | +class MQTTTest { |
| 5 | +public: |
| 6 | + void run(T *client); |
| 7 | + void message(String topic, String payload); |
| 8 | +private: |
| 9 | + T *client; |
| 10 | + void printResult(boolean res); |
| 11 | + boolean testMessage(const char * topic, const char * payload); |
| 12 | + boolean newMessage = false; |
| 13 | + boolean passedTest = false; |
| 14 | + String testTopic; |
| 15 | + String testPayload; |
| 16 | + boolean testConnectivity(boolean test); |
| 17 | +}; |
| 18 | + |
| 19 | +/* Methods */ |
| 20 | + |
| 21 | +template <class T> |
| 22 | +void MQTTTest<T>::run(T *client) { |
| 23 | + this->client = client; |
| 24 | + |
| 25 | + Serial.println("Starting tests..."); |
| 26 | + |
| 27 | + Serial.print("[Test 1] Connect: "); |
| 28 | + this->printResult(this->client->connect("arduino-mqtt-test", "try", "try") && this->testConnectivity(true)); |
| 29 | + |
| 30 | + Serial.print("[Test 2] Subscribe & Publish: "); |
| 31 | + this->client->subscribe("arduino-mqtt-test/topic1"); |
| 32 | + this->client->publish("arduino-mqtt-test/topic1", "test"); |
| 33 | + this->printResult(this->testMessage("arduino-mqtt-test/topic1", "test")); |
| 34 | + |
| 35 | + Serial.print("[Test 3] Unsubscribe: "); |
| 36 | + this->client->subscribe("arduino-mqtt-test/topic2"); |
| 37 | + this->client->subscribe("arduino-mqtt-test/topic3"); |
| 38 | + this->client->unsubscribe("arduino-mqtt-test/topic2"); |
| 39 | + this->client->publish("arduino-mqtt-test/topic2", "test"); |
| 40 | + this->client->publish("arduino-mqtt-test/topic3", "test"); |
| 41 | + this->printResult(this->testMessage("arduino-mqtt-test/topic3", "test")); |
| 42 | + |
| 43 | + Serial.print("[Test 4] Disconnect: "); |
| 44 | + this->client->disconnect(); |
| 45 | + this->printResult(this->testConnectivity(false)); |
| 46 | + |
| 47 | + Serial.println("Tests finished!"); |
| 48 | +} |
| 49 | + |
| 50 | +template <class T> |
| 51 | +void MQTTTest<T>::message(String topic, String payload) { |
| 52 | + this->newMessage = true; |
| 53 | + this->passedTest = topic.equals(this->testTopic) && payload.equals(this->testPayload); |
| 54 | +} |
| 55 | + |
| 56 | +/* Helpers */ |
| 57 | + |
| 58 | +template <class T> |
| 59 | +void MQTTTest<T>::printResult(boolean res) { |
| 60 | + res ? Serial.println("SUCCESS") : Serial.println("FAILED"); |
| 61 | +} |
| 62 | + |
| 63 | +template <class T> |
| 64 | +boolean MQTTTest<T>::testMessage(const char *topic, const char *payload) { |
| 65 | + this->testTopic = String(topic); |
| 66 | + this->testPayload = String(payload); |
| 67 | + |
| 68 | + while(!this->newMessage) { |
| 69 | + this->client->loop(); |
| 70 | + } |
| 71 | + |
| 72 | + boolean ret = this->passedTest; |
| 73 | + this->newMessage = false; |
| 74 | + this->passedTest = false; |
| 75 | + |
| 76 | + return ret; |
| 77 | +} |
| 78 | + |
| 79 | +template <class T> |
| 80 | +boolean MQTTTest<T>::testConnectivity(boolean test) { |
| 81 | + while(this->client->connected() != test) { |
| 82 | + this->client->loop(); |
| 83 | + } |
| 84 | + |
| 85 | + return this->client->connected() == test; |
| 86 | +} |
0 commit comments