hd_v3_update_bin.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. # coding=utf-8
  2. from base import init_env
  3. init_env(True)
  4. from apps.web.device.models import Device, DeviceDict
  5. from mongoengine import QuerySet
  6. from collections import defaultdict
  7. from apps.web.core.networking import MessageSender
  8. from apps.web.core.helpers import ActionDeviceBuilder
  9. TARGET_DRIVER_CODE = "100259"
  10. TARGET_DRIVER_VERSION = "v5.1.5"
  11. FILE_MAP = {
  12. "4gv1": {"fw_url": "http://121.43.232.118/uploaded/version/4g/SmartBox_6.1.157_Luat_V0036_ASR1802_720D_4gsck_100259.bin"},
  13. "4gv2": {"fw_url": "http://121.43.232.118/uploaded/version/4g/SmartBox_6.1.157_Luat_V0036_ASR1802_720D_4gv2_100259.bin"},
  14. "4gv3": {"fw_url": "http://121.43.232.118/uploaded/version/4g/SmartBox_6.1.157_Luat_V0036_ASR1802_720D_4gv3_100259.bin"},
  15. "4gcat1_0017": {"fw_url": "http://121.43.232.118/uploaded/version/cat1/100259__0017__0032.bin"},
  16. "4gcat1_0019": {"fw_url": "http://121.43.232.118/uploaded/version/cat1/100259__0019__0032.bin"},
  17. "4gcat1_0022": {"fw_url": "http://121.43.232.118/uploaded/version/cat1/100259__0022__0032.bin"},
  18. "4gcat1_0024": {"fw_url": "http://121.43.232.118/uploaded/version/cat1/100259__0024__0032.bin"},
  19. "4gcat1_0030": {"fw_url": "http://121.43.232.118/uploaded/version/cat1/100259__0030__0032.bin"},
  20. "4gcat1_0031": {"fw_url": "http://121.43.232.118/uploaded/version/cat1/100259__0031__0032.bin"},
  21. "4gcat1_0032": {"fw_url": "http://121.43.232.118/uploaded/version/cat1/100259__0032__0032.bin"},
  22. "4gcat1_3028": {"fw_url": "http://121.43.232.118/uploaded/version/cat1/100259__3028__3037.bin"},
  23. "4gcat1_3029": {"fw_url": "http://121.43.232.118/uploaded/version/cat1/100259__3029__3037.bin"},
  24. "4gcat1_3032": {"fw_url": "http://121.43.232.118/uploaded/version/cat1/100259__3032__3037.bin"},
  25. "4gcat1_3035": {"fw_url": "http://121.43.232.118/uploaded/version/cat1/100259__3035__3037.bin"},
  26. "4gcat1_3037": {"fw_url": "http://121.43.232.118/uploaded/version/cat1/100259__3037__3037.bin"},
  27. }
  28. def get_update_device_by_group(groupId):
  29. """
  30. 通过地址获取设备
  31. :param groupId:
  32. :return:
  33. """
  34. groupId = groupId.strip()
  35. return Device.objects.filter(
  36. groupId=groupId
  37. )
  38. def get_update_device_by_nos(nos):
  39. """
  40. 通过编号获取设备
  41. :param nos: 设备编号字符串
  42. :return:
  43. """
  44. nos = nos.strip()
  45. logicalCodes = nos.split()
  46. return Device.objects.filter(
  47. logicalCode__in=logicalCodes
  48. )
  49. def parse_devices(devs): # type:(QuerySet) -> (int, dict)
  50. """
  51. 分析设备的情况
  52. :param devs:
  53. :return:
  54. """
  55. # 符合条件的升级设备的总数量
  56. count = devs.count()
  57. devVerMap = defaultdict(list)
  58. for dev in devs: # type: Device
  59. # 4gv1 的设备
  60. if dev.softVer.startswith("v50.1") or dev.hwVer in ["4gv1", "4gsck"]:
  61. devVerMap["4gv1"].append(dev.devNo)
  62. # 4gv2 的设备
  63. elif dev.softVer.startswith("v50.12") or dev.hwVer in ["4gv2"]:
  64. devVerMap["4gv2"].append(dev.devNo)
  65. # 4gv3 的设备
  66. elif dev.softVer.startswith("v50.13") or dev.hwVer in ["4gv3"]:
  67. devVerMap["4gv3"].append(dev.devNo)
  68. # 4g_v2_8910
  69. elif dev.softVer.startswith("v60.12") or dev.hwVer == "4gcat1":
  70. coreVerStr = dev.coreVer[6:10]
  71. devVerMap["4gcat1_{}".format(coreVerStr)].append(dev.devNo)
  72. # 其余的2G的设备
  73. else:
  74. devVerMap["other"].append(dev.devNo)
  75. return count, devVerMap
  76. def main_show():
  77. """
  78. 首页功能展示
  79. :return:
  80. """
  81. print """
  82. 和动V3升级系统
  83. 1. 输入设备组(需要输入地址组ID)
  84. 2. 输入设备编号(需要输入设备编号)
  85. """
  86. def device_show(count, verMap):
  87. """
  88. 设备 版本分布 展示
  89. :param count:
  90. :param verMap:
  91. :return:
  92. """
  93. print """
  94. 共查询符合条件设备 {} 台
  95. 版本分布情况如下:
  96. """.format(count)
  97. for _ver, _devNos in verMap.items():
  98. print u"版本: {}, 共 {}台".format(_ver, _devNos)
  99. def update_device(dev, ota_set): # type:(DeviceDict, str) -> None
  100. print """
  101. 设备编号: {}
  102. 设备所属地址: {}
  103. 设备软件版本: {} - {}
  104. 设备硬件版本: {}
  105. 设备驱动版本: {}
  106. 设备驱动编码: {}
  107. 在线状态: {}
  108. 升级文件: {}
  109. """.format(
  110. dev.logicalCode,
  111. dev.group.groupName,
  112. dev.softVer,
  113. dev.coreVer,
  114. dev.hwVer,
  115. dev.driverVersion,
  116. dev.driverCode,
  117. dev.online,
  118. ota_set
  119. )
  120. isContinue = raw_input(u"是否继续升级(y/n)?")
  121. if isContinue != "y":
  122. print u"不继续升级 退出程序"
  123. return
  124. MessageSender.send(
  125. device=dev,
  126. cmd=202,
  127. payload={
  128. "IMEI": dev.devNo,
  129. "ota_set": ota_set
  130. }
  131. )
  132. def send_mqtt_to_update_devices(devs):
  133. """
  134. 升级设备
  135. :param devs:
  136. :return:
  137. """
  138. # 分析设备的情况
  139. # 再次过滤一次
  140. devs = devs.filter(driverVersion__ne=TARGET_DRIVER_VERSION)
  141. count, verMap = parse_devices(devs)
  142. device_show(count, verMap)
  143. isContinue = raw_input(u"是否继续升级(y/n)?")
  144. if isContinue != "y":
  145. print u"不继续升级 退出程序"
  146. return
  147. for _ver, _devNos in verMap.items():
  148. _ota_set = FILE_MAP.get(_ver, u"未知版本")
  149. isContinue = raw_input(u"当前处理升级版本是 {}, 共有设备{}, 升级地址 {}. 是否继续升级(y/n)?".format(_ver, len(_devNos), _ota_set))
  150. if isContinue != "y":
  151. print u"不继续升级 退出程序"
  152. continue
  153. for _devNo in _devNos:
  154. # 获取设备 以及相应的升级文件
  155. _dev = Device.get_dev(_devNo)
  156. # 进入升级设备
  157. update_device(_dev, _ota_set)
  158. def send_mqtt_to_update_settings(devs):
  159. """
  160. 更新设备配置
  161. :param devs:
  162. :return:
  163. """
  164. model_devNo = '863488053227925'
  165. model_device = Device.objects.get(devNo=model_devNo)
  166. model_box = ActionDeviceBuilder.create_action_device(Device.get_dev(model_devNo))
  167. model_settings = model_box.get_dev_setting()
  168. for _dev in devs:
  169. # 数据库数据保存
  170. device = Device.objects.get(devNo=_dev.devNo) # type: Device
  171. device.washConfig = model_device.washConfig
  172. device.devType = model_device.devType
  173. device.otherConf = model_device.otherConf
  174. # 退款的添加
  175. device.autoRefundLeftMoney = True
  176. device.save()
  177. Device.invalid_device_cache(_dev.devNo)
  178. # 主板参数下发
  179. box = ActionDeviceBuilder.create_action_device(Device.get_dev(_dev.devNo))
  180. box.set_dev_setting(model_settings)
  181. # 缓存清除
  182. Device.invalid_device_cache(_dev.devNo)
  183. def main():
  184. main_show()
  185. # 查找设备
  186. enterTypes = raw_input(u"输入选择:")
  187. if enterTypes == "1":
  188. groupId = raw_input(u"请输入地址组ID:")
  189. devs = get_update_device_by_group(groupId)
  190. elif enterTypes == "2":
  191. nos = raw_input(u"请输入设备编号 多个设备编号之间以空格分隔:")
  192. devs = get_update_device_by_nos(nos)
  193. else:
  194. print u"错误的选择! 退出程序"
  195. return
  196. handle = raw_input(u"""
  197. 请选择需要进行的操作
  198. 1. 升级设备
  199. 2. 更新设备配置
  200. """)
  201. if handle == "1":
  202. send_mqtt_to_update_devices(devs)
  203. elif handle == "2":
  204. send_mqtt_to_update_settings(devs)
  205. else:
  206. print u"错误的选择! 退出程序"
  207. return
  208. if __name__ == '__main__':
  209. main()