1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- # -*- coding: utf-8 -*-
- # !/usr/bin/env python
- import os
- import sys
- 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.testing')
- from script.base import init_env
- init_env(False)
- from apps.web.device.models import Device
- from apps.web.core.networking import MessageSender
- def is_online(devNo):
- return bool(Device.get_dev(devNo).online)
- def change_ip_and_record(devNo, debug):
- if debug:
- print devNo
- else:
- device = Device.get_dev(devNo)
- serverIp, _ = device.server.split(":")
- # IP 是写死的 需要切换
- MessageSender.send(
- device = device,
- cmd = 202,
- payload = {
- "IMEI": device.devNo,
- "addr_set": {"ip1": serverIp, "port1": 1884}
- },
- timeout = 5
- )
- with open("do_change_ip.txt", "a") as f:
- f.write("{}\n".format(devNo))
- def run(debug):
- if os.path.exists("do_change_ip.txt"):
- with open("do_change_ip.txt") as f:
- changedDevs = [_d.strip() for _d in f.readlines()]
- else:
- changedDevs = list()
- index = len(changedDevs)
- curIndex = index
- # 总量到了直接退出
- if index >= 10000:
- return
- devices = Device.objects.filter(server__endswith = ":1883", ownerId__ne = "")
- for _dev in devices:
- # 不需要特别关注时间的 筛选放到内存中去处理
- if _dev.devNo in changedDevs:
- continue
- if not is_online(_dev.devNo):
- continue
- try:
- change_ip_and_record(_dev.devNo, debug)
- except Exception as e:
- print "{}-{}".format(_dev.devNo, e)
- else:
- index += 1
- # 一次最多发1000条
- if index - curIndex >= 1000:
- break
- if __name__ == '__main__':
- # 修改 设备的IP端口 当debug为 True的时候 只记录 不发生实际修改
- # 每次运行最大执行设备数量为1000个 执行完毕之后自动停止 从 上次停止的设备号开始继续执行
- # 当执行设备到达10000的时候 执行程序不做任何事情
- # 执行设备号 记录在当前文件夹的 do_change_ip.txt
- run(debug = True)
|