cbd_send_msg.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. from qcloudsms_py import SmsSingleSender
  2. from qcloudsms_py.httpclient import HTTPError
  3. from qcloudsms_py import SmsMultiSender
  4. import os
  5. import django
  6. import sys
  7. BASE_DIR = os.path.dirname(os.path.abspath(__file__)) # 定位到你的django根目录
  8. sys.path.append(os.path.abspath(os.path.join(BASE_DIR, os.pardir)))
  9. os.environ.setdefault("DJANGO_SETTINGS_MODULE",
  10. "yfwlw_pro.settings") # project_name 项目名称
  11. django.setup()
  12. import json
  13. import time
  14. import requests
  15. import datetime
  16. from apps.ReportManage.all_dict import mobile_list, insect_dict_new
  17. from apps.AppInfoManage.models import Equip, Equip_type, CBDphoto, Msg_Send, Msg_Conf
  18. # 短信应用 SDK AppID
  19. appid = 1400227496 # SDK AppID 以1400开头
  20. # 短信应用 SDK AppKey
  21. appkey = "c9aba961bd30072f1e1b9272077abaea"
  22. while True:
  23. end_date = datetime.datetime.now()
  24. one_day = datetime.timedelta(days=1)
  25. one_day_later = datetime.datetime.now() - one_day
  26. print(one_day_later)
  27. new_time = "%s-%s-%s %s:%s:%s" %(one_day_later.year,one_day_later.month,one_day_later.day,"19","00","00")
  28. start_date = datetime.datetime.strptime(new_time,"%Y-%m-%d %H:%M:%S")
  29. if end_date.hour == 8:
  30. msg_conf_list = Msg_Conf.objects.all()
  31. for i in msg_conf_list:
  32. cbd_obj = Equip.objects.get(equip_id=i.equip_id)
  33. equip_name = ""
  34. if cbd_obj.equip_name == "" or cbd_obj.equip_name == None:
  35. equip_name = cbd_obj.equip_id
  36. else:
  37. equip_name = cbd_obj.equip_name
  38. msgconf = eval(i.conf)
  39. # 害虫种类超限预警
  40. if msgconf["pestCategory"] == "on":
  41. photo_list = CBDphoto.objects.filter(equip_id=i.equip_id,upl_time__range=(start_date, end_date))
  42. pest_name = []
  43. pest_list = []
  44. for x in photo_list:
  45. if x.indentify_result == None or x.indentify_result == "":
  46. pass
  47. else:
  48. for a in x.indentify_result.split("#"):
  49. pest_name.append(a.split(",")[0])
  50. for a in pest_name:
  51. if a not in pest_list:
  52. pest_list.append(a)
  53. if int(msgconf["pestCategoryNum"]) <= len(pest_list):
  54. # 需要发送短信的手机号码
  55. phone_numbers = msgconf["phone"]
  56. # 短信模板ID,需要在短信控制台中申请
  57. template_id = 456159 # NOTE: 这里的模板 ID`7839`只是示例,真实的模板 ID 需要在短信控制台中申请
  58. # 签名
  59. sms_sign = "云飞科技" # NOTE: 签名参数使用的是`签名内容`,而不是`签名ID`。这里的签名"腾讯云"只是示例,真实的签名需要在短信控制台中申请
  60. ssender = SmsSingleSender(appid, appkey)
  61. params = [equip_name,one_day_later.strftime("%Y-%m-%d"),len(pest_list)] # 当模板没有参数时,`params = []`
  62. try:
  63. result = ssender.send_with_param(86, phone_numbers,
  64. template_id, params, sign=sms_sign, extend="", ext="") # 签名参数未提供或者为空时,会使用默认签名发送短信
  65. except HTTPError as e:
  66. print(e)
  67. except Exception as e:
  68. print(e)
  69. print(result)
  70. Msg_Send.objects.create(mobile=msgconf["phone"],equip_id=i.equip_id,send_dec=params,result_desc=result)
  71. pest_name = []
  72. print("----发送成功")
  73. # 指定害虫监测
  74. if msgconf["appointPest"] == "on":
  75. pest_num = 0
  76. pest_name = []
  77. pest_nums = []
  78. pest_dict = {}
  79. pest_dict_new = {}
  80. cbd_list = CBDphoto.objects.filter(equip_id=i.equip_id,upl_time__range=(start_date, end_date))
  81. for x in cbd_list:
  82. if x.indentify_result == None or x.indentify_result == "":
  83. pass
  84. else:
  85. for a in x.indentify_result.split("#"):
  86. pest_num = pest_num + int(a.split(",")[1])
  87. pest_name.append(int(a.split(",")[0]))
  88. pest_nums.append(int(a.split(",")[1]))
  89. for xx in range(len(pest_name)):
  90. c = {pest_name[xx]:pest_nums[xx]}
  91. for d in c:
  92. if d in pest_dict:
  93. pest_dict[d] = pest_dict[d] + c[d]
  94. else:
  95. pest_dict.update(c)
  96. print(pest_dict)
  97. send_dict = {}
  98. print(msgconf["appointPestName"].split("#"))
  99. print(msgconf["appointPestNum"].split("#"))
  100. msg_name = msgconf["appointPestName"].split("#")
  101. msg_num = msgconf["appointPestNum"].split("#")
  102. for i in range(len(msg_name)):
  103. if int(msg_name[i]) != 0:
  104. if pest_dict[int(msg_name[i])] > int(msg_num[i]):
  105. aaa = {insect_dict_new[msg_name[i]]:"%s头"%int(msg_num[i])}
  106. send_dict.update(aaa)
  107. print(send_dict)
  108. # 需要发送短信的手机号码
  109. phone_numbers = msgconf["phone"]
  110. # 短信模板ID,需要在短信控制台中申请
  111. template_id = 467892 # NOTE: 这里的模板 ID`7839`只是示例,真实的模板 ID 需要在短信控制台中申请
  112. # 签名
  113. sms_sign = "云飞科技" # NOTE: 签名参数使用的是`签名内容`,而不是`签名ID`。这里的签名"腾讯云"只是示例,真实的签名需要在短信控制台中申请
  114. ssender = SmsSingleSender(appid, appkey)
  115. params = [equip_name,one_day_later.strftime("%Y-%m-%d"),send_dict] # 当模板没有参数时,`params = []`
  116. try:
  117. result = ssender.send_with_param(86, phone_numbers,
  118. template_id, params, sign=sms_sign, extend="", ext="") # 签名参数未提供或者为空时,会使用默认签名发送短信
  119. except HTTPError as e:
  120. print(e)
  121. except Exception as e:
  122. print(e)
  123. print(result)
  124. Msg_Send.objects.create(mobile=msgconf["phone"],equip_id=i.equip_id,send_dec=params,result_desc=result)
  125. pest_name = []
  126. print("----发送成功")
  127. send_dict = {}
  128. pest_num = 0
  129. pest_name = []
  130. pest_nums = []
  131. pest_dict = {}
  132. pest_dict_new = {}
  133. # 害虫总数超限预警
  134. if msgconf["pestTotal"] == "on":
  135. pest_num = 0
  136. pest_name = []
  137. pest_nums = []
  138. pest_dict = {}
  139. pest_dict_new = {}
  140. cbd_list = CBDphoto.objects.filter(equip_id=i.equip_id,upl_time__range=(start_date, end_date))
  141. for x in cbd_list:
  142. if x.indentify_result == None or x.indentify_result == "":
  143. pass
  144. else:
  145. for a in x.indentify_result.split("#"):
  146. pest_num = pest_num + int(a.split(",")[1])
  147. pest_name.append(int(a.split(",")[0]))
  148. pest_nums.append(int(a.split(",")[1]))
  149. for xx in range(len(pest_name)):
  150. c = {pest_name[xx]:pest_nums[xx]}
  151. for d in c:
  152. if d in pest_dict:
  153. pest_dict[d] = pest_dict[d] + c[d]
  154. else:
  155. pest_dict.update(c)
  156. print(pest_dict)
  157. TotalNum = 0
  158. for a in pest_dict:
  159. TotalNum = TotalNum + pest_dict[a]
  160. if TotalNum >= msgconf["pestTotalNum"]:
  161. # 需要发送短信的手机号码
  162. phone_numbers = msgconf["phone"]
  163. # 短信模板ID,需要在短信控制台中申请
  164. template_id = 456173 # NOTE: 这里的模板 ID`7839`只是示例,真实的模板 ID 需要在短信控制台中申请
  165. # 签名
  166. sms_sign = "云飞科技" # NOTE: 签名参数使用的是`签名内容`,而不是`签名ID`。这里的签名"腾讯云"只是示例,真实的签名需要在短信控制台中申请
  167. ssender = SmsSingleSender(appid, appkey)
  168. params = [equip_name,one_day_later.strftime("%Y-%m-%d"),TotalNum] # 当模板没有参数时,`params = []`
  169. try:
  170. result = ssender.send_with_param(86, phone_numbers,
  171. template_id, params, sign=sms_sign, extend="", ext="") # 签名参数未提供或者为空时,会使用默认签名发送短信
  172. except HTTPError as e:
  173. print(e)
  174. except Exception as e:
  175. print(e)
  176. print(result)
  177. Msg_Send.objects.create(mobile=msgconf["phone"],equip_id=i.equip_id,send_dec=params,result_desc=result)
  178. pest_name = []
  179. print("----发送成功")
  180. TotalNum = 0
  181. pest_num = 0
  182. pest_name = []
  183. pest_nums = []
  184. pest_dict = {}
  185. pest_dict_new = {}
  186. # 害虫总体预警
  187. if msgconf["pestWarn"] == "on":
  188. pest_num = 0
  189. pest_name = []
  190. pest_nums = []
  191. pest_dict = {}
  192. pest_dict_new = {}
  193. cbd_list = CBDphoto.objects.filter(equip_id=i.equip_id,upl_time__range=(start_date, end_date))
  194. # cbd_list = CBDphoto.objects.filter(equip_id=i.equip_id)
  195. for x in cbd_list:
  196. if x.indentify_result == None or x.indentify_result == "":
  197. pass
  198. else:
  199. for a in x.indentify_result.split("#"):
  200. pest_num = pest_num + int(a.split(",")[1])
  201. pest_name.append(int(a.split(",")[0]))
  202. pest_nums.append(int(a.split(",")[1]))
  203. for xx in range(len(pest_name)):
  204. c = {pest_name[xx]:pest_nums[xx]}
  205. for d in c:
  206. if d in pest_dict:
  207. pest_dict[d] = pest_dict[d] + c[d]
  208. else:
  209. pest_dict.update(c)
  210. print(pest_dict)
  211. for k in pest_dict:
  212. dict_b = {insect_dict_new[str(k)]:"%s头"%pest_dict[k]}
  213. pest_dict_new.update(dict_b)
  214. cbd_obj = Equip.objects.get(equip_id=i.equip_id)
  215. # 需要发送短信的手机号码
  216. phone_numbers = msgconf["phone"]
  217. # 短信模板ID,需要在短信控制台中申请
  218. template_id = 435309 # NOTE: 这里的模板 ID`7839`只是示例,真实的模板 ID 需要在短信控制台中申请
  219. # 签名
  220. sms_sign = "云飞科技" # NOTE: 签名参数使用的是`签名内容`,而不是`签名ID`。这里的签名"腾讯云"只是示例,真实的签名需要在短信控制台中申请
  221. ssender = SmsSingleSender(appid, appkey)
  222. params = [equip_name,one_day_later.strftime("%Y-%m-%d"),str(pest_dict_new)] # 当模板没有参数时,`params = []`
  223. try:
  224. result = ssender.send_with_param(86, phone_numbers,
  225. template_id, params, sign=sms_sign, extend="", ext="") # 签名参数未提供或者为空时,会使用默认签名发送短信
  226. except HTTPError as e:
  227. print(e)
  228. except Exception as e:
  229. print(e)
  230. print(result)
  231. print("害虫总数为",pest_num)
  232. Msg_Send.objects.create(mobile=phone_numbers,equip_id=i.equip_id,send_dec=params,result_desc=result)
  233. pest_num = 0
  234. pest_name = []
  235. pest_nums = []
  236. pest_dict = {}
  237. pest_dict_new = {}
  238. print("----发送成功")
  239. time.sleep(3600)