driver_update_for4.3.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import json
  2. import re
  3. import traceback
  4. import library.paho.mqtt.client as mqtt
  5. from library.paho.mqtt import publish, MQTTException
  6. from pymongo import MongoClient
  7. uri = "mongodb://washpayer:UY3Bw%lK8NM56eQG@120.26.227.50:27118/washpay?authSource=washpay&authMechanism=SCRAM-SHA-1"
  8. device_collection = MongoClient(uri).washpay.Device
  9. update_retry = {}
  10. def update(imei, url, host, port):
  11. message = {
  12. 'cmd': 202,
  13. 'IMEI': imei,
  14. 'driver_set': {
  15. 'driver_url': url
  16. }
  17. }
  18. try:
  19. publish.single("smart_box/%s/202" % imei, json.dumps(message), hostname=host, port=port)
  20. except MQTTException, e:
  21. print traceback.format_exc()
  22. print "imei: %s, capture a mqtt exception: %s" % (imei, e)
  23. except Exception, e:
  24. print "imei: %s, capture a exception: %s" % (imei, e)
  25. def update_version(device):
  26. if re.compile(r'^v4.3.*$').match(device['softVer']) is not None:
  27. if 'devType' in device and 'code' in device['devType']:
  28. if device['device']['code'] in ['100710']:
  29. url = 'http://121.41.38.172:18070/uploaded/massage_switch_2.3volt_high.driver'
  30. host, port = device['server'].split(':')
  31. update(device['devNo'], url, host, int(port))
  32. if device['device']['code'] in ['100711']:
  33. url = 'http://121.41.38.172:18070/uploaded/massage_switch_2.3volt_high.driver'
  34. host, port = device['server'].split(':')
  35. update(device['devNo'], url, host, int(port))
  36. if device['device']['code'] in ['100712']:
  37. url = 'http://121.41.38.172:18070/uploaded/massage_switch_2.3volt_high.driver'
  38. host, port = device['server'].split(':')
  39. update(device['devNo'], url, host, int(port))
  40. if device['device']['code'] in ['100713']:
  41. url = 'http://121.41.38.172:18070/uploaded/massage_switch_2.3volt_high.driver'
  42. host, port = device['server'].split(':')
  43. update(device['devNo'], url, host, int(port))
  44. if device['device']['code'] in ['100714']:
  45. url = 'http://121.41.38.172:18070/uploaded/massage_switch_2.3volt_high.driver'
  46. host, port = device['server'].split(':')
  47. update(device['devNo'], url, host, int(port))
  48. def update_test(imei, url):
  49. device = device_collection.find_one({'devNo':imei})
  50. if not device:
  51. print('device is not found')
  52. host, port = device['server'].split(':')
  53. message = {
  54. 'cmd': 202,
  55. 'IMEI': imei,
  56. 'driver_set': {
  57. 'driver_url': url
  58. }
  59. }
  60. print(message)
  61. try:
  62. publish.single("smart_box/%s/202" % imei, json.dumps(message), hostname=host, port=int(port),
  63. auth={'username': "20160528@vivestone", 'password': "j429QXqI5CTv"})
  64. print('done!')
  65. except MQTTException, e:
  66. print traceback.format_exc()
  67. print "imei: %s, capture a mqtt exception: %s" % (imei, e)
  68. except Exception, e:
  69. print "imei: %s, capture a exception: %s" % (imei, e)
  70. class Listener(mqtt.Client):
  71. def on_connect(self, mqttc, obj, flags, rc):
  72. print("rc: "+str(rc))
  73. def on_message(self, mqttc, obj, msg):
  74. results = re.compile(r'^server/(.*)/207$').match(msg.topic)
  75. imei = results.group(1)
  76. device = device_collection.find_one({'devNo': imei},
  77. {'_id': 0, 'devNo': 1, 'server': 1, 'softVer': 1, 'devType': 1})
  78. if device is not None and 'softVer' in device:
  79. if device['devNo'] not in update_retry:
  80. print imei
  81. update_retry[device['devNo']] = 1
  82. update_version(device)
  83. elif device['devNo'] in update_retry and update_retry[device['devNo']] < 3:
  84. print imei
  85. update_retry[device['devNo']] = update_retry[device['devNo']] + 1
  86. update_version(device)
  87. def on_publish(self, mqttc, obj, mid):
  88. pass
  89. def on_subscribe(self, mqttc, obj, mid, granted_qos):
  90. pass
  91. def run(self, port):
  92. self.username_pw_set("20160528@vivestone", "j429QXqI5CTv")
  93. self.connect("120.27.251.159", port, 60)
  94. self.subscribe("server/+/207", 0)
  95. rc = 0
  96. while rc == 0:
  97. rc = self.loop()
  98. return rc
  99. if __name__ == '__main__':
  100. listener = Listener()
  101. listener.run(1883)
  102. listener.run(1884)
  103. print('done!')