change_ip.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # coding=utf-8
  2. import os
  3. os.environ.update({"DJANGO_SETTINGS_MODULE": "configs.production"})
  4. from base import init_env
  5. init_env(False)
  6. from django.conf import settings
  7. from apps.web.device.models import Device, DeviceDict
  8. from apps.web.core.networking import MessageSender
  9. from apps.web.device.define import DeviceChannelType
  10. def is_online(devNo):
  11. return bool(Device.get_dev(devNo).online)
  12. def change_ip_and_record(devNo, debug, new_port, old_port):
  13. if debug:
  14. print devNo
  15. else:
  16. device = Device.get_dev(devNo) # type: DeviceDict
  17. if device.channelType == DeviceChannelType.Channel_BT:
  18. return
  19. # if len(devNo) < 15:
  20. # return
  21. serverIp, port = device.server.split(":")
  22. if serverIp != settings.MQTT_HOSTNAME:
  23. return
  24. if int(port) != old_port:
  25. print('{} is not in {}.'.format(device.devNo, old_port))
  26. return
  27. MessageSender.send(
  28. device = device,
  29. cmd = 202,
  30. payload = {
  31. "IMEI": device.devNo,
  32. "addr_set": {"ip1": serverIp, "port1": new_port}
  33. },
  34. timeout = 5
  35. )
  36. with open("done_change_ip.txt", "a") as f:
  37. f.write("{}\n".format(devNo))
  38. def run(debug, __list, new_port, old_port):
  39. filter = {
  40. 'devNo': {'$in': __list}
  41. }
  42. devices = Device.objects(__raw__ = filter)
  43. for _dev in devices: # type: Device
  44. if not is_online(_dev.devNo):
  45. continue
  46. if 'DUMMY' in _dev.devNo:
  47. continue
  48. if 'LuaRTOS' in _dev.coreVer:
  49. print('{} is lua os'.format(_dev.devNo))
  50. continue
  51. try:
  52. change_ip_and_record(_dev.devNo, debug, new_port, old_port)
  53. except Exception as e:
  54. print "{}-{}".format(_dev.devNo, e)
  55. if __name__ == '__main__':
  56. __list = []
  57. __done_list = []
  58. old_port = 1884
  59. new_port = 1883
  60. try:
  61. with open("done_change_ip.txt", "r") as f:
  62. lines = f.readlines()
  63. for line in lines:
  64. line = line.strip()
  65. __done_list.append(line)
  66. except Exception as e:
  67. pass
  68. try:
  69. with open("e:\\temp\\{}\\do_change_ip.txt".format(old_port), "r") as f:
  70. lines = f.readlines()
  71. for line in lines:
  72. line = line.strip()
  73. if line not in __done_list:
  74. __list.append(line)
  75. except Exception as e:
  76. pass
  77. run(debug = False, __list = __list, new_port = new_port, old_port = old_port)