diff --git a/examples/tests/MQTTClient/MQTTClient.ino b/examples/tests/MQTTClient/MQTTClient.ino new file mode 100644 index 0000000..71218a7 --- /dev/null +++ b/examples/tests/MQTTClient/MQTTClient.ino @@ -0,0 +1,23 @@ +#include +#include +#include +#include + +YunClient net; +MQTTClient client; +MQTTTest test; + +void setup() { + Bridge.begin(); + Serial.begin(9600); + + // run a very basic automated test + client.begin("broker.shiftr.io", net); + test.run(&client); +} + +void loop() {} + +void messageReceived(String topic, String payload, char * bytes, unsigned int length) { + test.message(topic, payload); +} diff --git a/examples/tests/YunMQTTClient/YunMQTTClient.ino b/examples/tests/YunMQTTClient/YunMQTTClient.ino new file mode 100644 index 0000000..1a63750 --- /dev/null +++ b/examples/tests/YunMQTTClient/YunMQTTClient.ino @@ -0,0 +1,21 @@ +#include +#include +#include + +YunMQTTClient client; +MQTTTest test; + +void setup() { + Bridge.begin(); + Serial.begin(9600); + + // run a very basic automated test + client.begin("broker.shiftr.io"); + test.run(&client); +} + +void loop() {} + +void messageReceived(String topic, String payload, char * bytes, unsigned int length) { + test.message(topic, payload); +} diff --git a/src/MQTTTest.h b/src/MQTTTest.h new file mode 100644 index 0000000..caaf26c --- /dev/null +++ b/src/MQTTTest.h @@ -0,0 +1,86 @@ +#include + +template +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 +void MQTTTest::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 +void MQTTTest::message(String topic, String payload) { + this->newMessage = true; + this->passedTest = topic.equals(this->testTopic) && payload.equals(this->testPayload); +} + +/* Helpers */ + +template +void MQTTTest::printResult(boolean res) { + res ? Serial.println("SUCCESS") : Serial.println("FAILED"); +} + +template +boolean MQTTTest::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 +boolean MQTTTest::testConnectivity(boolean test) { + while(this->client->connected() != test) { + this->client->loop(); + } + + return this->client->connected() == test; +} diff --git a/yun/bridge.py b/yun/bridge.py index 739a0c9..0bcb600 100644 --- a/yun/bridge.py +++ b/yun/bridge.py @@ -11,15 +11,16 @@ def __init__(self): self.client = None self.will_topic = "" self.will_payload = "" + self.stopped = False # Bridge Callbacks def on_connect(self, _, __, ___, rc): self.send_command("a;" if rc == 0 else "r;") def on_message(self, _, __, msg): self.send_command("m:" + msg.topic + ":" + str(len(msg.payload)) + ";" + str(msg.payload)) - def on_disconnect(self): - self.client.loop_stop() + def on_disconnect(self, _, __, ___): self.send_command("e;") + self.stopped = True # Command Helpers def parse_command(self, line): @@ -51,6 +52,7 @@ def do_connect(self, args): self.client = mqtt.Client(args[2]) self.client.on_connect = self.on_connect self.client.on_message = self.on_message + self.client.on_disconnect = self.on_disconnect if len(args) >= 5: self.client.username_pw_set(args[3], args[4]) if len(self.will_topic) > 0: @@ -68,12 +70,13 @@ def do_unsubscribe(self, args): def do_publish(self, args): self.client.publish(args[0], self.read_chunk(int(args[1]))) def do_disconnect(self): + self.client.loop_stop() self.client.disconnect() # Main def run(self): self.send_command("b;") - while True: + while not self.stopped: self.parse_command(self.read_until(self.PAYLOAD_SEPERATOR)) # Low Level Helpers