import os import django import sys BASE_DIR = os.path.dirname(os.path.abspath(__file__)) # 定位到你的django根目录 sys.path.append(os.path.abspath(os.path.join(BASE_DIR, os.pardir))) os.environ.setdefault("DJANGO_SETTINGS_MODULE", "yfwlw_pro.settings") # project_name 项目名称 django.setup() import json import time import requests import datetime import sys from apps.ReportManage.all_dict import trap_list from apps.AppInfoManage.models import Equip, Equip_type, Trapstatus, Trapdata print("--------trap_transpond start!-----------") url = 'http://telemo.org/pest/data/grid/userdataview.do' # dat= {'username': 'jkTest','password': '123456','sign':'cf6345abb2ed948a534467d278a79190'} #将携带的参数传给params # r = requests.get(url, params=dat) # rec = r.text # data = json.loads(rec) # print(data['resultData'][-1]) while True: nowtime = datetime.datetime.now().strftime('%Y%m%d') origin = sys.stdout f = open('../logs/'+nowtime+'trap.txt','a+') sys.stdout = f for i in trap_list: print("============================") r = requests.get(url, params=i) rec = r.text data = json.loads(rec) # print("data:",data) try: trapstatus = data['resultData'][-1] except: print("-------------continue-------------") continue # print("设备位置为:",trapstatus) imei = trapstatus['modelSerail'] detester = datetime.datetime.strptime(trapstatus['createDate'],"%Y-%m-%d %H:%M:%S") trap_exist = Equip.objects.filter(equip_id=imei) print("-----------imei:%s---------------------"%imei) print(datetime.datetime.now()) # 设备存在: if trap_exist.exists(): imei = Equip.objects.get(equip_id=imei) if Trapstatus.objects.filter(equip_id=imei).exists(): print("<-----this equip's status is existed!----->") sta1 = Trapdata.objects.filter(equip_id=imei).order_by('-upl_time')[:1] if Trapdata.objects.filter(equip_id=imei).exists(): if sta1[0].upl_time == detester: print("<-----data time exists!----->") else: # 设备数据表直接储存数据 Trapdata.objects.create(equip_id=imei, addr=trapstatus['locationName'], tem=trapstatus['temperature'], hum=trapstatus['humidity'], pest=trapstatus['cnName'], pestnum = trapstatus['pestNum'],upl_time=detester) print("<-----data update success!----->") else: # 设备数据表直接储存数据 Trapdata.objects.create(equip_id=imei, addr=trapstatus['locationName'], tem=trapstatus['temperature'], hum=trapstatus['humidity'], pest=trapstatus['cnName'], pestnum = trapstatus['pestNum'],upl_time=detester) print("<-----data update success!----->") try: sta = Trapstatus.objects.get(equip_id=imei) sta.addr = trapstatus['locationName'] sta.tem = trapstatus['temperature'] sta.hum = trapstatus['humidity'] sta.pest = trapstatus['cnName'] sta.pestnum = trapstatus['pestNum'] sta.upl_time = detester sta.save() print("<-----status update success!----->") except: print("<-----status update failed!----->") else: # 设备状态表不存在、创建状态表: print("<-----this equip's status is not existed!----->") try: # 设备数据表直接储存数据 Trapdata.objects.create(equip_id=e_id, addr=trapstatus['locationName'], tem=trapstatus['temperature'], hum=trapstatus['humidity'], pest=trapstatus['cnName'], pestnum = trapstatus['pestNum'],upl_time=detester) print("<-----data update success!----->") except: print("<-----data update failed!----->") try: e_id = Equip.objects.get(equip_id=imei) try: Trapstatus.objects.create(equip_id=e_id, addr=trapstatus['locationName'], tem=trapstatus['temperature'], hum=trapstatus['humidity'], pest=trapstatus['cnName'], pestnum = trapstatus['pestNum'],upl_time=detester) print("<-----this equip's status table re-create successed!----->") except: print("<-----this equip's status table re-create failed!----->") except: print("<-----this equip didn't exist!----->") else: # 设备不存在,在设备列表中创建: try: # 得到设备类型实例: equip_t = Equip_type.objects.get(type_id=8) try: e_id = Equip.objects.create(equip_id=imei, equip_type=equip_t) print("<-----this imei add successed!----->") try: # 设备数据表直接储存数据 Trapdata.objects.create(equip_id=e_id, addr=trapstatus['locationName'], tem=trapstatus['temperature'], hum=trapstatus['humidity'], pest=trapstatus['cnName'], pestnum = trapstatus['pestNum'],upl_time=detester) print("<-----data update success!----->") except: print("<-----data update failed!----->") try: Trapstatus.objects.create(equip_id=e_id, addr=trapstatus['locationName'], tem=trapstatus['temperature'], hum=trapstatus['humidity'], pest=trapstatus['cnName'], pestnum = trapstatus['pestNum'],upl_time=detester) print("<-----this imei register successed!----->") except: print("<-----this imei register failed!----->") except: print("<-----this imei add failed!----->") except: print("<-----this imei register failed because this equip type is not exist,!----->") sys.stdout = origin f.close() time.sleep(3600)