change_device_version.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. import os
  4. import sys
  5. import time
  6. from concurrent.futures import ThreadPoolExecutor, Future
  7. from django.conf import settings
  8. os.environ.update({"DJANGO_SETTINGS_MODULE": "configs.production"})
  9. from base import init_env
  10. init_env(False)
  11. from apps.web.device.models import Device
  12. from apps.web.core.networking import MessageSender
  13. from apps.web.device.define import DeviceChannelType
  14. from apps.web.core.mqtt_client import MqttClient
  15. from apps.web.core.sysparas import SysParas
  16. def is_online(devNo):
  17. return bool(Device.get_dev(devNo).online)
  18. def done_callback(f): # type:(Future) -> None
  19. debug, logicalCode, result = f.result()
  20. if debug:
  21. print('{} debug.'.format(logicalCode))
  22. else:
  23. if result:
  24. with open("done_version_device.txt", "a") as f:
  25. f.write("{}\n".format(logicalCode))
  26. else:
  27. print('do {} upgrade failure.'.format(logicalCode))
  28. def change_version_and_record(device, debug, fw_url):
  29. if debug:
  30. return debug, device.logicalCode, 'success'
  31. else:
  32. if not is_online(device.devNo):
  33. print('{} is not online.'.format(device.devNo))
  34. return debug, device.logicalCode, 'offline'
  35. if 'DUMMY' in device.devNo:
  36. return debug, device.logicalCode, 'error {}'.format('DUMMY')
  37. if 'LuaRTOS' in device.coreVer:
  38. return debug, device.logicalCode, 'error {}'.format(device.coreVer)
  39. if device.channelType == DeviceChannelType.Channel_BT:
  40. return debug, device.logicalCode, 'error BT'
  41. if len(device.devNo) < 8:
  42. return debug, device.logicalCode, 'error devNo {}'.format(device.devNo)
  43. start_time = int(time.time())
  44. result = MessageSender.send(
  45. device = Device.get_dev(device.devNo),
  46. cmd = 202,
  47. payload = {
  48. "IMEI": device.devNo,
  49. "ota_set": {"fw_url": fw_url}
  50. },
  51. timeout = 15)
  52. if result['rst'] == 0:
  53. while int(time.time()) - start_time < 10:
  54. print('loop > 10 seconds.')
  55. time.sleep(1)
  56. return debug, device.logicalCode, 'success'
  57. else:
  58. return debug, device.logicalCode, 'rst=-1'
  59. def run(debug, devices):
  60. with ThreadPoolExecutor(max_workers = 10) as executor:
  61. for device in devices: # type: Device
  62. matchCore = False
  63. matchDriver = False
  64. if device.softVer.startswith('v1.7.') or device.softVer.startswith('v1.8.'):
  65. if device.softVer == 'v1.8.116':
  66. print('is new version v1.8.116')
  67. continue
  68. fw_url = "http://121.43.232.118/uploaded/version/2g/SmartBox_1.8.116_Luat_{core}_8955_SSL_100000.bin"
  69. matchCore = True
  70. matchDriver = False
  71. elif device.softVer.startswith('v4.0.') or device.softVer.startswith('v3.0.'):
  72. fw_url = "http://121.43.232.118/uploaded/version/2g/SmartBox_6.1.153_Luat_{core}_8955_SSL_v3_100000.bin"
  73. matchCore = True
  74. matchDriver = False
  75. elif device.softVer.startswith('v4.11.'):
  76. fw_url = "http://121.43.232.118/uploaded/version/2g/SmartBox_5.11.160_Luat_V0040_8955_SSL_100118.bin"
  77. matchCore = False
  78. matchDriver = False
  79. elif device.softVer.startswith('v4.9.'):
  80. if device.driverCode in ['100205', '100210', '100202', '100206']:
  81. fw_url = "http://121.43.232.118/uploaded/version/2g/SmartBox_6.1.153_Luat_V0033_8955_SSL_pureuart_{code}.bin"
  82. else:
  83. fw_url = "http://121.43.232.118/uploaded/version/2g/SmartBox_5.9.160_Luat_V0040_8955_SSL_{code}.bin"
  84. matchCore = True
  85. matchDriver = True
  86. elif device.softVer.startswith('v40.1.'):
  87. if device.driverCode in ['100205', '100210', '100202', '100206']:
  88. fw_url = "http://121.43.232.118/uploaded/version/2g/SmartBox_6.1.155_Luat_V0040_8955_SSL_v5_{code}.bin"
  89. else:
  90. fw_url = "http://121.43.232.118/uploaded/version/2g/SmartBox_5.1.160_Luat_V0040_8955_SSL_{code}.bin"
  91. matchCore = False
  92. matchDriver = True
  93. else:
  94. continue
  95. if matchCore:
  96. if 'V0020' in device.coreVer:
  97. _fw_url = fw_url.format(core = 'V0020')
  98. elif 'V0015' in device.coreVer:
  99. _fw_url = fw_url.format(core = 'V0015')
  100. elif 'V0016' in device.coreVer:
  101. _fw_url = fw_url.format(core = 'V0016')
  102. elif 'V0033' in device.coreVer:
  103. _fw_url = fw_url.format(core = 'V0033')
  104. elif 'V0036' in device.coreVer:
  105. _fw_url = fw_url.format(core = 'V0036')
  106. elif 'V0038' in device.coreVer:
  107. _fw_url = fw_url.format(core = 'V0038')
  108. else:
  109. print device.coreVer
  110. continue
  111. else:
  112. _fw_url = fw_url
  113. try:
  114. if matchDriver:
  115. if device.driverCode == '100205':
  116. _fw_url = _fw_url.format(code = '100210')
  117. elif device.driverCode == '100202':
  118. _fw_url = _fw_url.format(code = '100206')
  119. elif device.driverCode == '':
  120. _fw_url = _fw_url.format(code = '100000')
  121. else:
  122. _fw_url = _fw_url.format(code = device.driverCode)
  123. except Exception as e:
  124. print _fw_url
  125. print device.devNo
  126. raise e
  127. executor.submit(change_version_and_record, device, debug, _fw_url).add_done_callback(done_callback)
  128. def operate_kick_off(devNo, port):
  129. try:
  130. mqttc = MqttClient(client_id = devNo)
  131. mqttc.username_pw_set(settings.MQTT_USER, settings.MQTT_PSWD)
  132. mqttc.connect(SysParas.get_private_ip(settings.MQTT_HOSTNAME), port, 60)
  133. finally:
  134. mqttc.disconnect()
  135. mqttc.close()
  136. # _function = 'checkVersions'
  137. _check_version = ['v6.1.155', 'v5.1.160']
  138. # _function = 'changeIP'
  139. _changed_ip = '120.27.251.159'
  140. _changed_port = '1885'
  141. if __name__ == '__main__':
  142. if len(sys.argv) < 2:
  143. _function = 'upgrade'
  144. else:
  145. _function = sys.argv[1]
  146. __done_list = []
  147. if _function == 'upgrade':
  148. try:
  149. with open("done_version_device.txt", "r") as f:
  150. lines = f.readlines()
  151. for line in lines:
  152. line = line.strip()
  153. __done_list.append(line)
  154. except Exception as e:
  155. pass
  156. __list = []
  157. try:
  158. with open("change_version_device.txt", "r") as f:
  159. lines = f.readlines()
  160. for line in lines:
  161. line = line.strip()
  162. __list.append(line)
  163. except Exception as e:
  164. pass
  165. filter = {
  166. 'logicalCode': {'$in': __list}
  167. }
  168. devices = [dev for dev in Device.objects(__raw__ = filter) if dev.devNo not in __done_list]
  169. if _function == 'kickoff':
  170. for device in devices:
  171. if device.softVer not in _check_version:
  172. host, port = device.server.split(':')
  173. operate_kick_off(device.devNo, str(port))
  174. elif _function == 'checkVersions':
  175. for device in devices:
  176. if device.softVer not in _check_version:
  177. print device.logicalCode
  178. elif _function == 'changeIP':
  179. for device in devices: # type: Device
  180. if device.server == '{}:{}'.format(_changed_ip, _changed_port):
  181. print('correct ip for {}'.format(device.devNo))
  182. continue
  183. if ':1884' not in device.server:
  184. continue
  185. MessageSender.send(
  186. device = Device.get_dev(device.devNo),
  187. cmd = 202,
  188. payload = {
  189. "IMEI": device.devNo,
  190. "addr_set": {"ip1": _changed_ip, "port1": _changed_port}
  191. },
  192. timeout = 10
  193. )
  194. else:
  195. run(debug = False, devices = devices)