change_ip.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. import os
  4. import sys
  5. from concurrent.futures import ThreadPoolExecutor, Future
  6. PROJECT_ROOT = os.path.join(os.path.abspath(os.path.split(os.path.realpath(__file__))[0] + "/.."), '..')
  7. sys.path.insert(0, PROJECT_ROOT)
  8. os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'configs.production')
  9. from script.base import init_env
  10. init_env(False)
  11. from django.conf import settings
  12. from apps.web.device.models import Device, DeviceDict
  13. from apps.web.core.networking import MessageSender
  14. from apps.web.device.define import DeviceChannelType
  15. def is_online(devNo):
  16. return bool(Device.get_dev(devNo).online)
  17. def done_callback(f): # type:(Future) -> None
  18. debug, devNo, old_port = f.result()
  19. if debug:
  20. print('{} debug.'.format(devNo))
  21. else:
  22. with open("e:\\temp\\change_ip\\done_{}.txt".format(old_port), "a") as f:
  23. f.write("{}\n".format(devNo))
  24. def change_ip_and_record(devNo, debug, new_port, old_port):
  25. if debug:
  26. return debug, devNo
  27. else:
  28. device = Device.get_dev(devNo) # type: DeviceDict
  29. if device.channelType == DeviceChannelType.Channel_BT:
  30. return debug, devNo
  31. if len(devNo) < 15:
  32. return debug, devNo
  33. serverIp, port = device.server.split(":")
  34. if serverIp != settings.MQTT_HOSTNAME:
  35. return debug, devNo
  36. if int(port) != old_port:
  37. print('{} is not in {}.'.format(device.devNo, old_port))
  38. return debug, devNo
  39. MessageSender.send_no_wait(
  40. device = device,
  41. cmd = 202,
  42. payload = {
  43. "IMEI": device.devNo,
  44. "addr_set": {"ip1": serverIp, "port1": new_port}
  45. },
  46. timeout = 5
  47. )
  48. return debug, device.devNo, old_port
  49. def run(debug, devices, new_port, old_port):
  50. with ThreadPoolExecutor(max_workers = 20) as executor:
  51. for _dev in devices: # type: Device
  52. if not is_online(_dev.devNo):
  53. continue
  54. if 'DUMMY' in _dev.devNo:
  55. continue
  56. if 'LuaRTOS' in _dev.coreVer:
  57. print('{} is lua os'.format(_dev.devNo))
  58. continue
  59. executor.submit(change_ip_and_record, _dev.devNo, debug, new_port, old_port).add_done_callback(
  60. done_callback)
  61. if __name__ == '__main__':
  62. __list = []
  63. __done_list = []
  64. old_port = int(sys.argv[1])
  65. new_port = int(sys.argv[2])
  66. try:
  67. with open("e:\\temp\\change_ip\\done_{}.txt".format(old_port), "r") as f:
  68. lines = f.readlines()
  69. for line in lines:
  70. line = line.strip()
  71. __done_list.append(line)
  72. except Exception as e:
  73. pass
  74. try:
  75. with open("e:\\temp\\change_ip\\do_{}.txt".format(old_port), "r") as f:
  76. lines = f.readlines()
  77. for line in lines:
  78. line = line.strip()
  79. if line not in __done_list:
  80. __list.append(line)
  81. except Exception as e:
  82. pass
  83. filter = {
  84. 'devNo': {'$in': __list}
  85. }
  86. devices = [dev for dev in Device.objects(__raw__ = filter)]
  87. run(debug = False, devices = devices, new_port = new_port, old_port = old_port)