import json import re import traceback import library.paho.mqtt.client as mqtt from library.paho.mqtt import publish, MQTTException from pymongo import MongoClient uri = "mongodb://washpayer:UY3Bw%lK8NM56eQG@120.26.227.50:27118/washpay?authSource=washpay&authMechanism=SCRAM-SHA-1" device_collection = MongoClient(uri).washpay.Device update_retry = {} def update(imei, url, host, port): message = { 'cmd': 202, 'IMEI': imei, 'driver_set': { 'driver_url': url } } try: publish.single("smart_box/%s/202" % imei, json.dumps(message), hostname=host, port=port) except MQTTException, e: print traceback.format_exc() print "imei: %s, capture a mqtt exception: %s" % (imei, e) except Exception, e: print "imei: %s, capture a exception: %s" % (imei, e) def update_version(device): if re.compile(r'^v4.3.*$').match(device['softVer']) is not None: if 'devType' in device and 'code' in device['devType']: if device['device']['code'] in ['100710']: url = 'http://121.41.38.172:18070/uploaded/massage_switch_2.3volt_high.driver' host, port = device['server'].split(':') update(device['devNo'], url, host, int(port)) if device['device']['code'] in ['100711']: url = 'http://121.41.38.172:18070/uploaded/massage_switch_2.3volt_high.driver' host, port = device['server'].split(':') update(device['devNo'], url, host, int(port)) if device['device']['code'] in ['100712']: url = 'http://121.41.38.172:18070/uploaded/massage_switch_2.3volt_high.driver' host, port = device['server'].split(':') update(device['devNo'], url, host, int(port)) if device['device']['code'] in ['100713']: url = 'http://121.41.38.172:18070/uploaded/massage_switch_2.3volt_high.driver' host, port = device['server'].split(':') update(device['devNo'], url, host, int(port)) if device['device']['code'] in ['100714']: url = 'http://121.41.38.172:18070/uploaded/massage_switch_2.3volt_high.driver' host, port = device['server'].split(':') update(device['devNo'], url, host, int(port)) def update_test(imei, url): device = device_collection.find_one({'devNo':imei}) if not device: print('device is not found') host, port = device['server'].split(':') message = { 'cmd': 202, 'IMEI': imei, 'driver_set': { 'driver_url': url } } print(message) try: publish.single("smart_box/%s/202" % imei, json.dumps(message), hostname=host, port=int(port), auth={'username': "20160528@vivestone", 'password': "j429QXqI5CTv"}) print('done!') except MQTTException, e: print traceback.format_exc() print "imei: %s, capture a mqtt exception: %s" % (imei, e) except Exception, e: print "imei: %s, capture a exception: %s" % (imei, e) class Listener(mqtt.Client): def on_connect(self, mqttc, obj, flags, rc): print("rc: "+str(rc)) def on_message(self, mqttc, obj, msg): results = re.compile(r'^server/(.*)/207$').match(msg.topic) imei = results.group(1) device = device_collection.find_one({'devNo': imei}, {'_id': 0, 'devNo': 1, 'server': 1, 'softVer': 1, 'devType': 1}) if device is not None and 'softVer' in device: if device['devNo'] not in update_retry: print imei update_retry[device['devNo']] = 1 update_version(device) elif device['devNo'] in update_retry and update_retry[device['devNo']] < 3: print imei update_retry[device['devNo']] = update_retry[device['devNo']] + 1 update_version(device) def on_publish(self, mqttc, obj, mid): pass def on_subscribe(self, mqttc, obj, mid, granted_qos): pass def run(self, port): self.username_pw_set("20160528@vivestone", "j429QXqI5CTv") self.connect("120.27.251.159", port, 60) self.subscribe("server/+/207", 0) rc = 0 while rc == 0: rc = self.loop() return rc if __name__ == '__main__': listener = Listener() listener.run(1883) listener.run(1884) print('done!')