update_task_driver.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. import sys
  4. import uuid
  5. import os
  6. PROJECT_ROOT = os.path.join(os.path.abspath(os.path.split(os.path.realpath(__file__))[0] + "/.."), '..')
  7. sys.path.insert(0, PROJECT_ROOT)
  8. import os
  9. os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'configs.{env}'.format(env = 'production'))
  10. from django.conf import settings
  11. from script.base import init_env
  12. import simplejson as json
  13. init_env(interactive = False)
  14. from apps.web.core.networking import MessageSender
  15. from apps.web.device.models import Device, DeviceDict
  16. from apps.web.constant import Const, DeviceOnlineStatus
  17. from apps.web.core.mqtt_client import MqttClient
  18. driver_code = '100230'
  19. driver_ver = '1.0.5'
  20. driver_path = 'http://121.43.232.118/uploaded/version/drivers/task/100230_uart_huandian_anqi/default.driver'
  21. max_count = -1
  22. mqttc1883 = None
  23. mqttc1884 = None
  24. try:
  25. def on_connect(client, userdata, flags, rc):
  26. pass
  27. def on_message(mqttc, obj, msg):
  28. # type: (MqttClient, obj, json)->None
  29. msgDict = json.loads(bytes.decode(msg.payload))
  30. imei = msgDict['IMEI']
  31. print 'msg dict is: {}'.format(msgDict)
  32. def on_disconnect(client, userdata, self):
  33. print('exit. client = %s' % str(client))
  34. mqttc1883 = MqttClient(client_id = 'webapp_' + str(uuid.uuid1()))
  35. try:
  36. mqttc1883.on_message = on_message
  37. mqttc1883.on_connect = on_connect
  38. mqttc1883.on_disconnect = on_disconnect
  39. mqttc1883.username_pw_set(settings.MQTT_USER, settings.MQTT_PSWD)
  40. mqttc1883.connect(settings.MQTT_HOSTNAME, 1883, 60)
  41. mqttc1883.loop_start()
  42. except Exception as e:
  43. print(e)
  44. mqttc1884 = MqttClient(client_id = 'webapp_' + str(uuid.uuid1()))
  45. try:
  46. mqttc1884.on_message = on_message
  47. mqttc1884.on_connect = on_connect
  48. mqttc1884.on_disconnect = on_disconnect
  49. mqttc1884.username_pw_set(settings.MQTT_USER, settings.MQTT_PSWD)
  50. mqttc1884.connect(settings.MQTT_HOSTNAME, 1884, 60)
  51. mqttc1884.loop_start()
  52. except Exception as e:
  53. print(e)
  54. count = 0
  55. objs = Device.get_collection().find({'driverCode':driver_code})
  56. for obj in objs:
  57. try:
  58. if max_count != -1 and count >= 1:
  59. print 'max count reach.'
  60. break
  61. devNo = obj['devNo']
  62. dev = Device.get_dev(devNo) # type: DeviceDict
  63. print(dev)
  64. if dev.online != DeviceOnlineStatus.DEV_STATUS_ONLINE:
  65. print '{} is offline.'.format(devNo)
  66. continue
  67. if dev['driverVersion'] == 'v{}'.format(driver_ver):
  68. continue
  69. cmdPara = {'IMEI': devNo, "driver_set": { "driver_url": driver_path}}
  70. print cmdPara
  71. result = MessageSender.send(device = dev, cmd = Const.CMD_CODE_SET_DEVINFO, payload = cmdPara, timeout = 15)
  72. if result['rst'] != 0:
  73. print 'IMEI({}) failure.'.format(devNo)
  74. continue
  75. else:
  76. count = count + 1
  77. except Exception, e:
  78. print 'some exception =%s' % e
  79. finally:
  80. if mqttc1883:
  81. mqttc1883.loop_stop()
  82. mqttc1883.disconnect()
  83. mqttc1883.close()
  84. if mqttc1884:
  85. mqttc1884.loop_stop()
  86. mqttc1884.disconnect()
  87. mqttc1884.close()
  88. print('finished')