from qcloudsms_py import SmsSingleSender from qcloudsms_py.httpclient import HTTPError from qcloudsms_py import SmsMultiSender 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 from apps.ReportManage.all_dict import mobile_list, insect_dict_new from apps.AppInfoManage.models import Equip, Equip_type, CBDphoto, Msg_Send, Msg_Conf # 短信应用 SDK AppID appid = 1400227496 # SDK AppID 以1400开头 # 短信应用 SDK AppKey appkey = "c9aba961bd30072f1e1b9272077abaea" while True: end_date = datetime.datetime.now() one_day = datetime.timedelta(days=1) one_day_later = datetime.datetime.now() - one_day print(one_day_later) new_time = "%s-%s-%s %s:%s:%s" %(one_day_later.year,one_day_later.month,one_day_later.day,"19","00","00") start_date = datetime.datetime.strptime(new_time,"%Y-%m-%d %H:%M:%S") if end_date.hour == 8: msg_conf_list = Msg_Conf.objects.all() for i in msg_conf_list: cbd_obj = Equip.objects.get(equip_id=i.equip_id) equip_name = "" if cbd_obj.equip_name == "" or cbd_obj.equip_name == None: equip_name = cbd_obj.equip_id else: equip_name = cbd_obj.equip_name msgconf = eval(i.conf) # 害虫种类超限预警 if msgconf["pestCategory"] == "on": photo_list = CBDphoto.objects.filter(equip_id=i.equip_id,upl_time__range=(start_date, end_date)) pest_name = [] pest_list = [] for x in photo_list: if x.indentify_result == None or x.indentify_result == "": pass else: for a in x.indentify_result.split("#"): pest_name.append(a.split(",")[0]) for a in pest_name: if a not in pest_list: pest_list.append(a) if int(msgconf["pestCategoryNum"]) <= len(pest_list): # 需要发送短信的手机号码 phone_numbers = msgconf["phone"] # 短信模板ID,需要在短信控制台中申请 template_id = 456159 # NOTE: 这里的模板 ID`7839`只是示例,真实的模板 ID 需要在短信控制台中申请 # 签名 sms_sign = "云飞科技" # NOTE: 签名参数使用的是`签名内容`,而不是`签名ID`。这里的签名"腾讯云"只是示例,真实的签名需要在短信控制台中申请 ssender = SmsSingleSender(appid, appkey) params = [equip_name,one_day_later.strftime("%Y-%m-%d"),len(pest_list)] # 当模板没有参数时,`params = []` try: result = ssender.send_with_param(86, phone_numbers, template_id, params, sign=sms_sign, extend="", ext="") # 签名参数未提供或者为空时,会使用默认签名发送短信 except HTTPError as e: print(e) except Exception as e: print(e) print(result) Msg_Send.objects.create(mobile=msgconf["phone"],equip_id=i.equip_id,send_dec=params,result_desc=result) pest_name = [] print("----发送成功") # 指定害虫监测 if msgconf["appointPest"] == "on": pest_num = 0 pest_name = [] pest_nums = [] pest_dict = {} pest_dict_new = {} cbd_list = CBDphoto.objects.filter(equip_id=i.equip_id,upl_time__range=(start_date, end_date)) for x in cbd_list: if x.indentify_result == None or x.indentify_result == "": pass else: for a in x.indentify_result.split("#"): pest_num = pest_num + int(a.split(",")[1]) pest_name.append(int(a.split(",")[0])) pest_nums.append(int(a.split(",")[1])) for xx in range(len(pest_name)): c = {pest_name[xx]:pest_nums[xx]} for d in c: if d in pest_dict: pest_dict[d] = pest_dict[d] + c[d] else: pest_dict.update(c) print(pest_dict) send_dict = {} print(msgconf["appointPestName"].split("#")) print(msgconf["appointPestNum"].split("#")) msg_name = msgconf["appointPestName"].split("#") msg_num = msgconf["appointPestNum"].split("#") for i in range(len(msg_name)): if int(msg_name[i]) != 0: if pest_dict[int(msg_name[i])] > int(msg_num[i]): aaa = {insect_dict_new[msg_name[i]]:"%s头"%int(msg_num[i])} send_dict.update(aaa) print(send_dict) # 需要发送短信的手机号码 phone_numbers = msgconf["phone"] # 短信模板ID,需要在短信控制台中申请 template_id = 467892 # NOTE: 这里的模板 ID`7839`只是示例,真实的模板 ID 需要在短信控制台中申请 # 签名 sms_sign = "云飞科技" # NOTE: 签名参数使用的是`签名内容`,而不是`签名ID`。这里的签名"腾讯云"只是示例,真实的签名需要在短信控制台中申请 ssender = SmsSingleSender(appid, appkey) params = [equip_name,one_day_later.strftime("%Y-%m-%d"),send_dict] # 当模板没有参数时,`params = []` try: result = ssender.send_with_param(86, phone_numbers, template_id, params, sign=sms_sign, extend="", ext="") # 签名参数未提供或者为空时,会使用默认签名发送短信 except HTTPError as e: print(e) except Exception as e: print(e) print(result) Msg_Send.objects.create(mobile=msgconf["phone"],equip_id=i.equip_id,send_dec=params,result_desc=result) pest_name = [] print("----发送成功") send_dict = {} pest_num = 0 pest_name = [] pest_nums = [] pest_dict = {} pest_dict_new = {} # 害虫总数超限预警 if msgconf["pestTotal"] == "on": pest_num = 0 pest_name = [] pest_nums = [] pest_dict = {} pest_dict_new = {} cbd_list = CBDphoto.objects.filter(equip_id=i.equip_id,upl_time__range=(start_date, end_date)) for x in cbd_list: if x.indentify_result == None or x.indentify_result == "": pass else: for a in x.indentify_result.split("#"): pest_num = pest_num + int(a.split(",")[1]) pest_name.append(int(a.split(",")[0])) pest_nums.append(int(a.split(",")[1])) for xx in range(len(pest_name)): c = {pest_name[xx]:pest_nums[xx]} for d in c: if d in pest_dict: pest_dict[d] = pest_dict[d] + c[d] else: pest_dict.update(c) print(pest_dict) TotalNum = 0 for a in pest_dict: TotalNum = TotalNum + pest_dict[a] if TotalNum >= msgconf["pestTotalNum"]: # 需要发送短信的手机号码 phone_numbers = msgconf["phone"] # 短信模板ID,需要在短信控制台中申请 template_id = 456173 # NOTE: 这里的模板 ID`7839`只是示例,真实的模板 ID 需要在短信控制台中申请 # 签名 sms_sign = "云飞科技" # NOTE: 签名参数使用的是`签名内容`,而不是`签名ID`。这里的签名"腾讯云"只是示例,真实的签名需要在短信控制台中申请 ssender = SmsSingleSender(appid, appkey) params = [equip_name,one_day_later.strftime("%Y-%m-%d"),TotalNum] # 当模板没有参数时,`params = []` try: result = ssender.send_with_param(86, phone_numbers, template_id, params, sign=sms_sign, extend="", ext="") # 签名参数未提供或者为空时,会使用默认签名发送短信 except HTTPError as e: print(e) except Exception as e: print(e) print(result) Msg_Send.objects.create(mobile=msgconf["phone"],equip_id=i.equip_id,send_dec=params,result_desc=result) pest_name = [] print("----发送成功") TotalNum = 0 pest_num = 0 pest_name = [] pest_nums = [] pest_dict = {} pest_dict_new = {} # 害虫总体预警 if msgconf["pestWarn"] == "on": pest_num = 0 pest_name = [] pest_nums = [] pest_dict = {} pest_dict_new = {} cbd_list = CBDphoto.objects.filter(equip_id=i.equip_id,upl_time__range=(start_date, end_date)) # cbd_list = CBDphoto.objects.filter(equip_id=i.equip_id) for x in cbd_list: if x.indentify_result == None or x.indentify_result == "": pass else: for a in x.indentify_result.split("#"): pest_num = pest_num + int(a.split(",")[1]) pest_name.append(int(a.split(",")[0])) pest_nums.append(int(a.split(",")[1])) for xx in range(len(pest_name)): c = {pest_name[xx]:pest_nums[xx]} for d in c: if d in pest_dict: pest_dict[d] = pest_dict[d] + c[d] else: pest_dict.update(c) print(pest_dict) for k in pest_dict: dict_b = {insect_dict_new[str(k)]:"%s头"%pest_dict[k]} pest_dict_new.update(dict_b) cbd_obj = Equip.objects.get(equip_id=i.equip_id) # 需要发送短信的手机号码 phone_numbers = msgconf["phone"] # 短信模板ID,需要在短信控制台中申请 template_id = 435309 # NOTE: 这里的模板 ID`7839`只是示例,真实的模板 ID 需要在短信控制台中申请 # 签名 sms_sign = "云飞科技" # NOTE: 签名参数使用的是`签名内容`,而不是`签名ID`。这里的签名"腾讯云"只是示例,真实的签名需要在短信控制台中申请 ssender = SmsSingleSender(appid, appkey) params = [equip_name,one_day_later.strftime("%Y-%m-%d"),str(pest_dict_new)] # 当模板没有参数时,`params = []` try: result = ssender.send_with_param(86, phone_numbers, template_id, params, sign=sms_sign, extend="", ext="") # 签名参数未提供或者为空时,会使用默认签名发送短信 except HTTPError as e: print(e) except Exception as e: print(e) print(result) print("害虫总数为",pest_num) Msg_Send.objects.create(mobile=phone_numbers,equip_id=i.equip_id,send_dec=params,result_desc=result) pest_num = 0 pest_name = [] pest_nums = [] pest_dict = {} pest_dict_new = {} print("----发送成功") time.sleep(3600)