123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- # coding=utf-8
- from base import init_env
- init_env(True)
- from apps.web.device.models import Device, DeviceDict
- from mongoengine import QuerySet
- from collections import defaultdict
- from apps.web.core.networking import MessageSender
- from apps.web.core.helpers import ActionDeviceBuilder
- TARGET_DRIVER_CODE = "100259"
- TARGET_DRIVER_VERSION = "v5.1.5"
- FILE_MAP = {
- "4gv1": {"fw_url": "http://121.43.232.118/uploaded/version/4g/SmartBox_6.1.157_Luat_V0036_ASR1802_720D_4gsck_100259.bin"},
- "4gv2": {"fw_url": "http://121.43.232.118/uploaded/version/4g/SmartBox_6.1.157_Luat_V0036_ASR1802_720D_4gv2_100259.bin"},
- "4gv3": {"fw_url": "http://121.43.232.118/uploaded/version/4g/SmartBox_6.1.157_Luat_V0036_ASR1802_720D_4gv3_100259.bin"},
- "4gcat1_0017": {"fw_url": "http://121.43.232.118/uploaded/version/cat1/100259__0017__0032.bin"},
- "4gcat1_0019": {"fw_url": "http://121.43.232.118/uploaded/version/cat1/100259__0019__0032.bin"},
- "4gcat1_0022": {"fw_url": "http://121.43.232.118/uploaded/version/cat1/100259__0022__0032.bin"},
- "4gcat1_0024": {"fw_url": "http://121.43.232.118/uploaded/version/cat1/100259__0024__0032.bin"},
- "4gcat1_0030": {"fw_url": "http://121.43.232.118/uploaded/version/cat1/100259__0030__0032.bin"},
- "4gcat1_0031": {"fw_url": "http://121.43.232.118/uploaded/version/cat1/100259__0031__0032.bin"},
- "4gcat1_0032": {"fw_url": "http://121.43.232.118/uploaded/version/cat1/100259__0032__0032.bin"},
- "4gcat1_3028": {"fw_url": "http://121.43.232.118/uploaded/version/cat1/100259__3028__3037.bin"},
- "4gcat1_3029": {"fw_url": "http://121.43.232.118/uploaded/version/cat1/100259__3029__3037.bin"},
- "4gcat1_3032": {"fw_url": "http://121.43.232.118/uploaded/version/cat1/100259__3032__3037.bin"},
- "4gcat1_3035": {"fw_url": "http://121.43.232.118/uploaded/version/cat1/100259__3035__3037.bin"},
- "4gcat1_3037": {"fw_url": "http://121.43.232.118/uploaded/version/cat1/100259__3037__3037.bin"},
- }
- def get_update_device_by_group(groupId):
- """
- 通过地址获取设备
- :param groupId:
- :return:
- """
- groupId = groupId.strip()
- return Device.objects.filter(
- groupId=groupId
- )
- def get_update_device_by_nos(nos):
- """
- 通过编号获取设备
- :param nos: 设备编号字符串
- :return:
- """
- nos = nos.strip()
- logicalCodes = nos.split()
- return Device.objects.filter(
- logicalCode__in=logicalCodes
- )
- def parse_devices(devs): # type:(QuerySet) -> (int, dict)
- """
- 分析设备的情况
- :param devs:
- :return:
- """
- # 符合条件的升级设备的总数量
- count = devs.count()
- devVerMap = defaultdict(list)
- for dev in devs: # type: Device
- # 4gv1 的设备
- if dev.softVer.startswith("v50.1") or dev.hwVer in ["4gv1", "4gsck"]:
- devVerMap["4gv1"].append(dev.devNo)
- # 4gv2 的设备
- elif dev.softVer.startswith("v50.12") or dev.hwVer in ["4gv2"]:
- devVerMap["4gv2"].append(dev.devNo)
- # 4gv3 的设备
- elif dev.softVer.startswith("v50.13") or dev.hwVer in ["4gv3"]:
- devVerMap["4gv3"].append(dev.devNo)
- # 4g_v2_8910
- elif dev.softVer.startswith("v60.12") or dev.hwVer == "4gcat1":
- coreVerStr = dev.coreVer[6:10]
- devVerMap["4gcat1_{}".format(coreVerStr)].append(dev.devNo)
- # 其余的2G的设备
- else:
- devVerMap["other"].append(dev.devNo)
- return count, devVerMap
- def main_show():
- """
- 首页功能展示
- :return:
- """
- print """
- 和动V3升级系统
- 1. 输入设备组(需要输入地址组ID)
- 2. 输入设备编号(需要输入设备编号)
- """
- def device_show(count, verMap):
- """
- 设备 版本分布 展示
- :param count:
- :param verMap:
- :return:
- """
- print """
- 共查询符合条件设备 {} 台
- 版本分布情况如下:
- """.format(count)
- for _ver, _devNos in verMap.items():
- print u"版本: {}, 共 {}台".format(_ver, _devNos)
- def update_device(dev, ota_set): # type:(DeviceDict, str) -> None
- print """
- 设备编号: {}
- 设备所属地址: {}
- 设备软件版本: {} - {}
- 设备硬件版本: {}
- 设备驱动版本: {}
- 设备驱动编码: {}
- 在线状态: {}
- 升级文件: {}
- """.format(
- dev.logicalCode,
- dev.group.groupName,
- dev.softVer,
- dev.coreVer,
- dev.hwVer,
- dev.driverVersion,
- dev.driverCode,
- dev.online,
- ota_set
- )
- isContinue = raw_input(u"是否继续升级(y/n)?")
- if isContinue != "y":
- print u"不继续升级 退出程序"
- return
- MessageSender.send(
- device=dev,
- cmd=202,
- payload={
- "IMEI": dev.devNo,
- "ota_set": ota_set
- }
- )
- def send_mqtt_to_update_devices(devs):
- """
- 升级设备
- :param devs:
- :return:
- """
- # 分析设备的情况
- # 再次过滤一次
- devs = devs.filter(driverVersion__ne=TARGET_DRIVER_VERSION)
- count, verMap = parse_devices(devs)
- device_show(count, verMap)
- isContinue = raw_input(u"是否继续升级(y/n)?")
- if isContinue != "y":
- print u"不继续升级 退出程序"
- return
- for _ver, _devNos in verMap.items():
- _ota_set = FILE_MAP.get(_ver, u"未知版本")
- isContinue = raw_input(u"当前处理升级版本是 {}, 共有设备{}, 升级地址 {}. 是否继续升级(y/n)?".format(_ver, len(_devNos), _ota_set))
- if isContinue != "y":
- print u"不继续升级 退出程序"
- continue
- for _devNo in _devNos:
- # 获取设备 以及相应的升级文件
- _dev = Device.get_dev(_devNo)
- # 进入升级设备
- update_device(_dev, _ota_set)
- def send_mqtt_to_update_settings(devs):
- """
- 更新设备配置
- :param devs:
- :return:
- """
- model_devNo = '863488053227925'
- model_device = Device.objects.get(devNo=model_devNo)
- model_box = ActionDeviceBuilder.create_action_device(Device.get_dev(model_devNo))
- model_settings = model_box.get_dev_setting()
- for _dev in devs:
- # 数据库数据保存
- device = Device.objects.get(devNo=_dev.devNo) # type: Device
- device.washConfig = model_device.washConfig
- device.devType = model_device.devType
- device.otherConf = model_device.otherConf
- # 退款的添加
- device.autoRefundLeftMoney = True
- device.save()
- Device.invalid_device_cache(_dev.devNo)
- # 主板参数下发
- box = ActionDeviceBuilder.create_action_device(Device.get_dev(_dev.devNo))
- box.set_dev_setting(model_settings)
- # 缓存清除
- Device.invalid_device_cache(_dev.devNo)
- def main():
- main_show()
- # 查找设备
- enterTypes = raw_input(u"输入选择:")
- if enterTypes == "1":
- groupId = raw_input(u"请输入地址组ID:")
- devs = get_update_device_by_group(groupId)
- elif enterTypes == "2":
- nos = raw_input(u"请输入设备编号 多个设备编号之间以空格分隔:")
- devs = get_update_device_by_nos(nos)
- else:
- print u"错误的选择! 退出程序"
- return
- handle = raw_input(u"""
- 请选择需要进行的操作
- 1. 升级设备
- 2. 更新设备配置
- """)
- if handle == "1":
- send_mqtt_to_update_devices(devs)
- elif handle == "2":
- send_mqtt_to_update_settings(devs)
- else:
- print u"错误的选择! 退出程序"
- return
- if __name__ == '__main__':
- main()
|