|
1 | 1 | import sys
|
2 | 2 | import paho.mqtt.client as mqtt
|
3 | 3 |
|
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]) |
59 | 64 |
|
60 | 65 | # Main Loop
|
61 | 66 |
|
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() |
70 | 69 |
|
71 | 70 | # TODO: better error handling
|
0 commit comments