Skip to content

Commit 6689187

Browse files
committed
added disconnect and other stuff
1 parent 2294b31 commit 6689187

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

src/YunMQTTClient.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,16 @@ void YunMQTTClient::loop() {
8989
if(av > 0) {
9090
String ret = process.readStringUntil('\n');
9191

92-
if(ret.charAt(0) == 'm') {
92+
if(ret.startsWith("m")) {
9393
int startTopic = 2;
9494
int endTopic = ret.indexOf(':', startTopic + 1);
9595
int startPayload = endTopic + 1;
9696
int endPayload = ret.indexOf(':', startPayload + 1);
9797
String topic = ret.substring(startTopic, endTopic);
9898
String payload = ret.substring(startPayload, endPayload);
9999
messageReceived(topic, payload, (char*)payload.c_str(), payload.length());
100+
} else if(ret.startsWith("e")) {
101+
this->alive = false;
100102
}
101103
}
102104
}
@@ -106,5 +108,6 @@ boolean YunMQTTClient::connected() {
106108
}
107109

108110
void YunMQTTClient::disconnect() {
109-
//TODO: implement!
111+
// send disconnect request
112+
this->process.print("d\n");
110113
}

yun/abi.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Command | Description | Format
1212
→ | publish | `p:(topic):(data)`
1313
← | message | `m:(topic):(data)`
1414
→ | disconnect | `d`
15+
← | closed | `e`
1516

1617
Todo:
1718

yun/client.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ def on_connect(self, _, __, ___, rc):
1414
self.send_command("cd")
1515
def on_message(self, _, __, msg):
1616
self.send_command("m:" + msg.topic + ":" + str(msg.payload))
17+
def on_disconnect(self):
18+
self.client.loop_stop()
19+
self.send_command("e")
1720

1821
# Command Helpers
1922
def parse_command(self, line):
@@ -44,17 +47,17 @@ def do_connect(self, args):
4447
self.client.connect(args[0], int(args[1]))
4548
self.client.loop_start()
4649
def do_subscribe(self, args):
47-
if len(args) >= 1:
50+
if self.client and len(args) >= 1:
4851
self.client.subscribe(args[0])
4952
def do_unsubscribe(self, args):
50-
if len(args) >= 1:
53+
if self.client and len(args) >= 1:
5154
self.client.unsubscribe(args[0])
5255
def do_publish(self, args):
53-
if len(args) >= 2:
56+
if self.client and len(args) >= 2:
5457
self.client.publish(args[0], args[1])
5558
def do_disconnect(self):
56-
self.client.disconnect()
57-
self.client.loop_stop()
59+
if self.client:
60+
self.client.disconnect()
5861

5962
# Main
6063
def run(self):

0 commit comments

Comments
 (0)