Skip to content

Commit 2294b31

Browse files
committed
support clientId
1 parent 91e9b9b commit 2294b31

File tree

3 files changed

+65
-65
lines changed

3 files changed

+65
-65
lines changed

src/YunMQTTClient.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ boolean YunMQTTClient::connect(const char * clientId, const char * username, con
2424
this->process.print(this->hostname);
2525
this->process.print(':');
2626
this->process.print(this->port);
27+
this->process.print(':');
28+
this->process.print(clientId);
2729
if(strlen(username) > 0 && strlen(password) > 0) {
2830
this->process.print(':');
2931
this->process.print(username);

yun/abi.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
Command | Description | Format
66
---|---|---
7-
→ | connect | `c:(host):(port):(user):(pass)`
7+
→ | connect | `c:(host):(port):(id):(user):(pass)`
88
← | connection approved | `ca`
99
← | connection denied | `cd`
1010
→ | subscribe | `s:(topic)`
@@ -16,4 +16,3 @@ Command | Description | Format
1616
Todo:
1717

1818
- length prefix data (may contain `:`)
19-
- add client id

yun/client.py

Lines changed: 62 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,70 @@
11
import sys
22
import paho.mqtt.client as mqtt
33

4-
# Client Callbacks
5-
6-
def on_connect(_, __, ___, rc):
7-
if rc == 0:
8-
send_command("ca")
9-
else:
10-
send_command("cd")
11-
12-
def on_message(_, __, msg):
13-
send_command("m:" + msg.topic + ":" + str(msg.payload))
14-
15-
# Command Helpers
16-
17-
def parse_command(client, line):
18-
segments = line.split(":")
19-
cmd = segments[0]
20-
remaining = segments[1:]
21-
if cmd == 'c':
22-
do_connect(client, remaining)
23-
elif cmd == 's':
24-
do_subscribe(client, remaining)
25-
elif cmd == 'u':
26-
do_unsubscribe(client, remaining)
27-
elif cmd == 'p':
28-
do_publish(client, remaining)
29-
elif cmd == 'd':
30-
do_disconnect(client)
31-
32-
def send_command(line):
33-
sys.stdout.write(line + "\n")
34-
35-
# Command Handlers
36-
37-
def do_connect(client, args):
38-
if len(args) >= 4:
39-
client.username_pw_set(args[2], args[3])
40-
if len(args) >= 2:
41-
client.connect(args[0], int(args[1]))
42-
client.loop_start()
43-
44-
def do_subscribe(client, args):
45-
if len(args) >= 1:
46-
client.subscribe(args[0])
47-
48-
def do_unsubscribe(client, args):
49-
if len(args) >= 1:
50-
client.unsubscribe(args[0])
51-
52-
def do_publish(client, args):
53-
if len(args) >= 2:
54-
client.publish(args[0], args[1])
55-
56-
def do_disconnect(client):
57-
client.disconnect()
58-
client.loop_stop()
4+
class Client:
5+
# Constructor
6+
def __init__(self):
7+
self.client = None
8+
9+
# Client Callbacks
10+
def on_connect(self, _, __, ___, rc):
11+
if rc == 0:
12+
self.send_command("ca")
13+
else:
14+
self.send_command("cd")
15+
def on_message(self, _, __, msg):
16+
self.send_command("m:" + msg.topic + ":" + str(msg.payload))
17+
18+
# Command Helpers
19+
def parse_command(self, line):
20+
segments = line.split(":")
21+
cmd = segments[0]
22+
remaining = segments[1:]
23+
if cmd == 'c':
24+
self.do_connect(remaining)
25+
elif cmd == 's':
26+
self.do_subscribe(remaining)
27+
elif cmd == 'u':
28+
self.do_unsubscribe(remaining)
29+
elif cmd == 'p':
30+
self.do_publish(remaining)
31+
elif cmd == 'd':
32+
self.do_disconnect()
33+
def send_command(self, line):
34+
sys.stdout.write(line + "\n")
35+
36+
# Command Handlers
37+
def do_connect(self, args):
38+
if len(args) >= 3:
39+
self.client = mqtt.Client(args[2])
40+
self.client.on_connect = self.on_connect
41+
self.client.on_message = self.on_message
42+
if len(args) >= 5:
43+
self.client.username_pw_set(args[3], args[4])
44+
self.client.connect(args[0], int(args[1]))
45+
self.client.loop_start()
46+
def do_subscribe(self, args):
47+
if len(args) >= 1:
48+
self.client.subscribe(args[0])
49+
def do_unsubscribe(self, args):
50+
if len(args) >= 1:
51+
self.client.unsubscribe(args[0])
52+
def do_publish(self, args):
53+
if len(args) >= 2:
54+
self.client.publish(args[0], args[1])
55+
def do_disconnect(self):
56+
self.client.disconnect()
57+
self.client.loop_stop()
58+
59+
# Main
60+
def run(self):
61+
c.send_command("ok")
62+
while True:
63+
self.parse_command(sys.stdin.readline()[0:-1])
5964

6065
# Main Loop
6166

62-
c = mqtt.Client()
63-
c.on_connect = on_connect
64-
c.on_message = on_message
65-
66-
send_command("ok")
67-
68-
while True:
69-
parse_command(c, sys.stdin.readline()[0:-1])
67+
c = Client()
68+
c.run()
7069

7170
# TODO: better error handling

0 commit comments

Comments
 (0)