cbc7cd46177f3cd8dabd772bf96af5e4d303f46f
[remote-debug.git] / mqtt-client.py
1 #!/usr/bin/env python2
2
3 from subprocess import call, Popen, PIPE, STDOUT
4 import paho.mqtt.client as mqtt
5 import argparse
6
7 MQTT_SERVER = 'mqtt.suanzi.ai'
8 MQTT_PORT = 1883
9
10 if __name__ == '__main__':
11     parser = argparse.ArgumentParser(description='Choose an avaiable port and run autossh.')
12     parser.add_argument('-i', '--id', dest='id', help='the unique id of remote machine, such as rpdzkj-080027a6f8dc, where the rpdzkj is the user, and 080027a6f8dc is the mac addres', required=True)
13     args = parser.parse_args()
14     id = args.id
15
16     def on_connect(client, userdata, flags, rc):
17         client.subscribe(id + "-response")
18         print("Connected with result code "+str(rc))
19         client.publish(id + '-request', 0)
20
21     def on_message(client, userdata, msg):
22         print(msg.topic+" "+str(msg.payload))
23         if msg.topic == id + '-response':
24             print 'port is', str(msg.payload)
25
26     client = mqtt.Client()
27     client.on_connect = on_connect
28     client.on_message = on_message
29     client.connect(MQTT_SERVER, MQTT_PORT, 60)
30     client.loop_forever()