123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- # coding=utf-8
- import os
- os.environ.update({"DJANGO_SETTINGS_MODULE": "configs.production"})
- from 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 change_ip_and_record(devNo, debug, new_port, old_port):
- if debug:
- print devNo
- else:
- device = Device.get_dev(devNo) # type: DeviceDict
- if device.channelType == DeviceChannelType.Channel_BT:
- return
- # if len(devNo) < 15:
- # return
- serverIp, port = device.server.split(":")
- if serverIp != settings.MQTT_HOSTNAME:
- return
- if int(port) != old_port:
- print('{} is not in {}.'.format(device.devNo, old_port))
- return
- MessageSender.send(
- device = device,
- cmd = 202,
- payload = {
- "IMEI": device.devNo,
- "addr_set": {"ip1": serverIp, "port1": new_port}
- },
- timeout = 5
- )
- with open("done_change_ip.txt", "a") as f:
- f.write("{}\n".format(devNo))
- def run(debug, __list, new_port, old_port):
- filter = {
- 'devNo': {'$in': __list}
- }
- devices = Device.objects(__raw__ = filter)
- 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
- try:
- change_ip_and_record(_dev.devNo, debug, new_port, old_port)
- except Exception as e:
- print "{}-{}".format(_dev.devNo, e)
- if __name__ == '__main__':
- __list = []
- __done_list = []
- old_port = 1884
- new_port = 1883
- try:
- with open("done_change_ip.txt", "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\\{}\\do_change_ip.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
- run(debug = False, __list = __list, new_port = new_port, old_port = old_port)
|