mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-01-08 05:24:39 +01:00
02287b8909
Script still needs a ton of work to be useful
67 lines
1.9 KiB
Python
Executable file
67 lines
1.9 KiB
Python
Executable file
#!/usr/bin/python3
|
|
|
|
import argparse, re
|
|
import paho.mqtt.client as mqtt
|
|
|
|
g_topics = [
|
|
'$SYS/broker/clients/disconnected',
|
|
# '$SYS/broker/+/+',
|
|
'xw4/device/#',
|
|
]
|
|
|
|
sDevIDPat = re.compile('xw4/device/([\dA-F]+)')
|
|
|
|
# Define event callbacks
|
|
def on_connect(client, userdata, flags, rc):
|
|
print('on_connect(): rc: {}'.format(rc))
|
|
|
|
def on_message(client, obj, msg):
|
|
match = sDevIDPat.match(msg.topic)
|
|
if match:
|
|
print('on_message(): for: {}, len: {}'.format(match.group(1), len(msg.payload)))
|
|
|
|
def on_publish(client, obj, mid):
|
|
print("mid: " + str(mid))
|
|
|
|
def on_subscribe(client, obj, mid, granted_qos):
|
|
print("Subscribed: " + str(mid) + " " + str(granted_qos))
|
|
|
|
def on_log(client, obj, level, string):
|
|
print(string)
|
|
|
|
def makeClient():
|
|
mqttc = mqtt.Client()
|
|
# Assign event callbacks
|
|
mqttc.on_message = on_message
|
|
mqttc.on_connect = on_connect
|
|
mqttc.on_publish = on_publish
|
|
mqttc.on_subscribe = on_subscribe
|
|
return mqttc
|
|
|
|
def mkParser():
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument('--host', dest = 'HOST', default = 'liquidsugar.net',
|
|
help = 'the host mosquitto is on')
|
|
parser.add_argument('--port', dest = 'PORT', default = 1883,
|
|
help = 'the port mosquitto is on')
|
|
return parser
|
|
|
|
def main():
|
|
args = mkParser().parse_args()
|
|
|
|
mqttc = makeClient()
|
|
mqttc.username_pw_set('xwuser', password='xw4r0cks')
|
|
mqttc.connect(args.HOST, args.PORT)
|
|
print('connected to {}:{}'.format(args.HOST, args.PORT))
|
|
# Start subscribe, with QoS level 2
|
|
for topic in g_topics:
|
|
mqttc.subscribe(topic, 2)
|
|
while True:
|
|
err = mqttc.loop()
|
|
if 0 != err:
|
|
print('got {} from loop()'.format(err))
|
|
break
|
|
|
|
##############################################################################
|
|
if __name__ == '__main__':
|
|
main()
|