123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- # -*- coding: utf-8 -*-
- # !/usr/bin/env python
- import os
- import sys
- from concurrent.futures import ThreadPoolExecutor, Future
- PROJECT_ROOT = os.path.join(os.path.abspath(os.path.split(os.path.realpath(__file__))[0] + "/.."), '..')
- sys.path.insert(0, PROJECT_ROOT)
- os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'configs.production')
- from script.base import init_env
- init_env(False)
- from django.conf import settings
- from apps.web.device.models import Device, DeviceDict
- from apps.web.core.networking import MessageSender
- from apps.web.device.define import DeviceChannelType
- def is_online(devNo):
- return bool(Device.get_dev(devNo).online)
- def done_callback(f): # type:(Future) -> None
- debug, devNo, old_port = f.result()
- if debug:
- print('{} debug.'.format(devNo))
- else:
- with open("e:\\temp\\change_ip\\done_{}.txt".format(old_port), "a") as f:
- f.write("{}\n".format(devNo))
- def change_ip_and_record(devNo, debug, new_port, old_port):
- if debug:
- return debug, devNo
- else:
- device = Device.get_dev(devNo) # type: DeviceDict
- if device.channelType == DeviceChannelType.Channel_BT:
- return debug, devNo
- if len(devNo) < 15:
- return debug, devNo
- serverIp, port = device.server.split(":")
- if serverIp != settings.MQTT_HOSTNAME:
- return debug, devNo
- if int(port) != old_port:
- print('{} is not in {}.'.format(device.devNo, old_port))
- return debug, devNo
- MessageSender.send_no_wait(
- device = device,
- cmd = 202,
- payload = {
- "IMEI": device.devNo,
- "addr_set": {"ip1": serverIp, "port1": new_port}
- },
- timeout = 5
- )
- return debug, device.devNo, old_port
- def run(debug, devices, new_port, old_port):
- with ThreadPoolExecutor(max_workers = 20) as executor:
- for _dev in devices: # type: Device
- if not is_online(_dev.devNo):
- continue
- if 'DUMMY' in _dev.devNo:
- continue
- if 'LuaRTOS' in _dev.coreVer:
- print('{} is lua os'.format(_dev.devNo))
- continue
- executor.submit(change_ip_and_record, _dev.devNo, debug, new_port, old_port).add_done_callback(
- done_callback)
- if __name__ == '__main__':
- __list = []
- __done_list = []
- old_port = int(sys.argv[1])
- new_port = int(sys.argv[2])
- try:
- with open("e:\\temp\\change_ip\\done_{}.txt".format(old_port), "r") as f:
- lines = f.readlines()
- for line in lines:
- line = line.strip()
- __done_list.append(line)
- except Exception as e:
- pass
- try:
- with open("e:\\temp\\change_ip\\do_{}.txt".format(old_port), "r") as f:
- lines = f.readlines()
- for line in lines:
- line = line.strip()
- if line not in __done_list:
- __list.append(line)
- except Exception as e:
- pass
- filter = {
- 'devNo': {'$in': __list}
- }
- devices = [dev for dev in Device.objects(__raw__ = filter)]
- run(debug = False, devices = devices, new_port = new_port, old_port = old_port)
|