123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- # -*- coding: utf-8 -*-
- # !/usr/bin/env python
- import os
- import sys
- import time
- from concurrent.futures import ThreadPoolExecutor, Future
- from django.conf import settings
- os.environ.update({"DJANGO_SETTINGS_MODULE": "configs.production"})
- from base import init_env
- init_env(False)
- from apps.web.device.models import Device
- from apps.web.core.networking import MessageSender
- from apps.web.device.define import DeviceChannelType
- from apps.web.core.mqtt_client import MqttClient
- from apps.web.core.sysparas import SysParas
- def is_online(devNo):
- return bool(Device.get_dev(devNo).online)
- def done_callback(f): # type:(Future) -> None
- debug, logicalCode, result = f.result()
- if debug:
- print('{} debug.'.format(logicalCode))
- else:
- if result:
- with open("done_version_device.txt", "a") as f:
- f.write("{}\n".format(logicalCode))
- else:
- print('do {} upgrade failure.'.format(logicalCode))
- def change_version_and_record(device, debug, fw_url):
- if debug:
- return debug, device.logicalCode, 'success'
- else:
- if not is_online(device.devNo):
- print('{} is not online.'.format(device.devNo))
- return debug, device.logicalCode, 'offline'
- if 'DUMMY' in device.devNo:
- return debug, device.logicalCode, 'error {}'.format('DUMMY')
- if 'LuaRTOS' in device.coreVer:
- return debug, device.logicalCode, 'error {}'.format(device.coreVer)
- if device.channelType == DeviceChannelType.Channel_BT:
- return debug, device.logicalCode, 'error BT'
- if len(device.devNo) < 8:
- return debug, device.logicalCode, 'error devNo {}'.format(device.devNo)
- start_time = int(time.time())
- result = MessageSender.send(
- device = Device.get_dev(device.devNo),
- cmd = 202,
- payload = {
- "IMEI": device.devNo,
- "ota_set": {"fw_url": fw_url}
- },
- timeout = 15)
- if result['rst'] == 0:
- while int(time.time()) - start_time < 10:
- print('loop > 10 seconds.')
- time.sleep(1)
- return debug, device.logicalCode, 'success'
- else:
- return debug, device.logicalCode, 'rst=-1'
- def run(debug, devices):
- with ThreadPoolExecutor(max_workers = 10) as executor:
- for device in devices: # type: Device
- matchCore = False
- matchDriver = False
- if device.softVer.startswith('v1.7.') or device.softVer.startswith('v1.8.'):
- if device.softVer == 'v1.8.116':
- print('is new version v1.8.116')
- continue
- fw_url = "http://121.43.232.118/uploaded/version/2g/SmartBox_1.8.116_Luat_{core}_8955_SSL_100000.bin"
- matchCore = True
- matchDriver = False
- elif device.softVer.startswith('v4.0.') or device.softVer.startswith('v3.0.'):
- fw_url = "http://121.43.232.118/uploaded/version/2g/SmartBox_6.1.153_Luat_{core}_8955_SSL_v3_100000.bin"
- matchCore = True
- matchDriver = False
- elif device.softVer.startswith('v4.11.'):
- fw_url = "http://121.43.232.118/uploaded/version/2g/SmartBox_5.11.160_Luat_V0040_8955_SSL_100118.bin"
- matchCore = False
- matchDriver = False
- elif device.softVer.startswith('v4.9.'):
- if device.driverCode in ['100205', '100210', '100202', '100206']:
- fw_url = "http://121.43.232.118/uploaded/version/2g/SmartBox_6.1.153_Luat_V0033_8955_SSL_pureuart_{code}.bin"
- else:
- fw_url = "http://121.43.232.118/uploaded/version/2g/SmartBox_5.9.160_Luat_V0040_8955_SSL_{code}.bin"
- matchCore = True
- matchDriver = True
- elif device.softVer.startswith('v40.1.'):
- if device.driverCode in ['100205', '100210', '100202', '100206']:
- fw_url = "http://121.43.232.118/uploaded/version/2g/SmartBox_6.1.155_Luat_V0040_8955_SSL_v5_{code}.bin"
- else:
- fw_url = "http://121.43.232.118/uploaded/version/2g/SmartBox_5.1.160_Luat_V0040_8955_SSL_{code}.bin"
- matchCore = False
- matchDriver = True
- else:
- continue
- if matchCore:
- if 'V0020' in device.coreVer:
- _fw_url = fw_url.format(core = 'V0020')
- elif 'V0015' in device.coreVer:
- _fw_url = fw_url.format(core = 'V0015')
- elif 'V0016' in device.coreVer:
- _fw_url = fw_url.format(core = 'V0016')
- elif 'V0033' in device.coreVer:
- _fw_url = fw_url.format(core = 'V0033')
- elif 'V0036' in device.coreVer:
- _fw_url = fw_url.format(core = 'V0036')
- elif 'V0038' in device.coreVer:
- _fw_url = fw_url.format(core = 'V0038')
- else:
- print device.coreVer
- continue
- else:
- _fw_url = fw_url
- try:
- if matchDriver:
- if device.driverCode == '100205':
- _fw_url = _fw_url.format(code = '100210')
- elif device.driverCode == '100202':
- _fw_url = _fw_url.format(code = '100206')
- elif device.driverCode == '':
- _fw_url = _fw_url.format(code = '100000')
- else:
- _fw_url = _fw_url.format(code = device.driverCode)
- except Exception as e:
- print _fw_url
- print device.devNo
- raise e
- executor.submit(change_version_and_record, device, debug, _fw_url).add_done_callback(done_callback)
- def operate_kick_off(devNo, port):
- try:
- mqttc = MqttClient(client_id = devNo)
- mqttc.username_pw_set(settings.MQTT_USER, settings.MQTT_PSWD)
- mqttc.connect(SysParas.get_private_ip(settings.MQTT_HOSTNAME), port, 60)
- finally:
- mqttc.disconnect()
- mqttc.close()
- # _function = 'checkVersions'
- _check_version = ['v6.1.155', 'v5.1.160']
- # _function = 'changeIP'
- _changed_ip = '120.27.251.159'
- _changed_port = '1885'
- if __name__ == '__main__':
- if len(sys.argv) < 2:
- _function = 'upgrade'
- else:
- _function = sys.argv[1]
- __done_list = []
- if _function == 'upgrade':
- try:
- with open("done_version_device.txt", "r") as f:
- lines = f.readlines()
- for line in lines:
- line = line.strip()
- __done_list.append(line)
- except Exception as e:
- pass
- __list = []
- try:
- with open("change_version_device.txt", "r") as f:
- lines = f.readlines()
- for line in lines:
- line = line.strip()
- __list.append(line)
- except Exception as e:
- pass
- filter = {
- 'logicalCode': {'$in': __list}
- }
- devices = [dev for dev in Device.objects(__raw__ = filter) if dev.devNo not in __done_list]
- if _function == 'kickoff':
- for device in devices:
- if device.softVer not in _check_version:
- host, port = device.server.split(':')
- operate_kick_off(device.devNo, str(port))
- elif _function == 'checkVersions':
- for device in devices:
- if device.softVer not in _check_version:
- print device.logicalCode
- elif _function == 'changeIP':
- for device in devices: # type: Device
- if device.server == '{}:{}'.format(_changed_ip, _changed_port):
- print('correct ip for {}'.format(device.devNo))
- continue
- if ':1884' not in device.server:
- continue
- MessageSender.send(
- device = Device.get_dev(device.devNo),
- cmd = 202,
- payload = {
- "IMEI": device.devNo,
- "addr_set": {"ip1": _changed_ip, "port1": _changed_port}
- },
- timeout = 10
- )
- else:
- run(debug = False, devices = devices)
|