update_device.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # -*- coding: utf-8 -*-
  2. #!/usr/bin/env python
  3. import os, sys,datetime
  4. from os.path import abspath, join
  5. import ConfigParser
  6. import uuid
  7. from django.conf import settings
  8. import simplejson as json
  9. from apps.web.constant import Const, DeviceCmdCode
  10. from apps.web.core.mqtt_client import MqttClient
  11. from apps.web.core.networking import MessageSender
  12. try:
  13. configParser = ConfigParser.ConfigParser()
  14. configParser.read('./settings.ini')
  15. settingModuleName = configParser.get('CORE', 'setting')
  16. except Exception:
  17. settingModuleName = 'staging'
  18. settingModuleIdentifier = "configs.production"
  19. PROJECT_ROOT = abspath(os.path.split(os.path.realpath(__file__))[0] + "/..")
  20. sys.path.insert(0, PROJECT_ROOT)
  21. sys.path.insert(0, join(PROJECT_ROOT, "apps"))
  22. os.environ.update({"DJANGO_SETTINGS_MODULE": settingModuleIdentifier})
  23. from apps.web.device.models import Device
  24. versionUrlDict = {
  25. '16':"http://www.washpayer.com/uploaded/SMARTBOX_1.0.0_Luat_V0016_8955_SSL.bin",
  26. '15':"http://www.washpayer.com/uploaded/SMARTBOX_1.0.0_Luat_V0015_8955_SSL.bin",
  27. '14':"http://www.washpayer.com/uploaded/SMARTBOX_1.0.0_Luat_V0014_8955_SSL.bin"
  28. }
  29. def on_disconnect(client, userdata, rc):
  30. print('dis conect ........')
  31. def on_connect(client, userdata, flags, rc):
  32. print('connect ok ........')
  33. #订阅设备的200响应消息,用于检查
  34. device_topic = Const.SERVER_TOPIC_PREFIX + '/+' + "/200"
  35. client.subscribe(device_topic, qos=Const.MQTT_QOS)
  36. def on_message(mqttc, obj, msg):
  37. global versionUrlDict
  38. try:
  39. devInfo = json.loads(bytes.decode(msg.payload))
  40. imei = devInfo['IMEI']
  41. dev = Device.get_dev(imei)
  42. if dev is None:
  43. return
  44. #串口设备的各家协议不一样,不要进行升级,否则升级完毕,机子就无法联通串口了
  45. if dev.has_key('devType') and dev['devType'].has_key('code')and dev['devType']['code'] >= '100901':
  46. return
  47. if dev.has_key('devType') and (not dev['devType'].has_key('code')):
  48. return
  49. if dev['devType']['code'] >= Const.DEVICE_TYPE_CODE_PEDICURE:
  50. return
  51. if devInfo['soft_ver'] >= 'v1.7.6':
  52. return
  53. devs = Device.get_collection().find({'devNo':imei})
  54. if devs.count() <= 0:
  55. return
  56. dbDev = devs[0]
  57. upDict = dbDev.get('upDict',{'16':0,'15':0,'14':0,'count':0,'lastTime':None})
  58. nowTime = datetime.datetime.now()
  59. if upDict.get('lastTime') != None and (nowTime-upDict['lastTime']).seconds < 300:
  60. return
  61. tVersion = '16'
  62. if upDict['16'] < 3:
  63. upDict['16'] += 1
  64. tVersion = '16'
  65. elif upDict['15'] < 3:
  66. upDict['15'] += 1
  67. tVersion = '15'
  68. elif upDict['14'] < 3:
  69. upDict['14'] += 1
  70. tVersion = '14'
  71. if upDict['14'] == 3:
  72. upDict['16'] = 0
  73. upDict['15'] = 0
  74. upDict['14'] = 0
  75. else:
  76. upDict['result'] = 'failed'
  77. result = MessageSender.send(device = dev, cmd = DeviceCmdCode.SET_DEVINFO,
  78. payload = {"IMEI": imei, "ota_set": versionUrlDict[tVersion]})
  79. if result['rst'] != 0:
  80. print('send updating cmd error,imei=%s' % imei)
  81. else:
  82. print('success for imei=%s,send the message' % imei)
  83. upDict['lastTime'] = datetime.datetime.now()
  84. upDict['count'] += 1
  85. Device.get_collection().update({'devNo':imei},{'$set':{'upDict':upDict}})
  86. except Exception,e:
  87. return
  88. mqttc = MqttClient(client_id='webapp_'+str(uuid.uuid1()))
  89. try:
  90. mqttc.on_message = on_message
  91. mqttc.on_disconnect = on_disconnect
  92. mqttc.on_connect = on_connect
  93. #连接到现网
  94. mqttc.connect(settings.MQTT_HOSTNAME, settings.MQTT_PORT,60)
  95. print('updating process is listening......')
  96. mqttc.loop_forever()
  97. finally:
  98. print('finish ok')
  99. mqttc.disconnect()
  100. mqttc.close()