cbd_msg.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import os
  2. import sys
  3. import time
  4. import django
  5. import json
  6. from datetime import datetime, timedelta
  7. local_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  8. print(local_path)
  9. if local_path not in sys.path:
  10. sys.path.append(local_path)
  11. os.environ.setdefault("DJANGO_SETTINGS_MODULE", "kedong.settings")
  12. django.setup()
  13. from django.conf import settings
  14. from smartfarming.models.sim_card import MongoMsg_Conf
  15. from smartfarming.models.worm_forecast import MongoCBDphoto
  16. from smartfarming.models.pest_count import MongoCBDPestWarning
  17. from smartfarming.api.views.forecast.all_dict import insect_dict
  18. # 获取所有的预警配置
  19. msg_conf = MongoMsg_Conf.objects.all()
  20. for msg in msg_conf:
  21. # 获取device_id conf
  22. device_id = msg.device_id
  23. conf = eval(msg.conf)
  24. # 获取该device_id 下前一天的害虫情况
  25. now = datetime.now()
  26. now_stamp = int(time.time())
  27. previous_day = now - timedelta(days=1)
  28. start_time = datetime(previous_day.year, previous_day.month, previous_day.day, 0, 0, 0).timestamp()
  29. end_time = datetime(now.year, now.month, now.day, 23, 59, 59).timestamp()
  30. print("起始时间:", start_time)
  31. print("结束时间:", end_time)
  32. photo_data = MongoCBDphoto.objects.filter(addtime__range = [start_time, end_time]).values_list("indentify_result", flat=True)
  33. # 把当天的害虫统计出来
  34. indentify_result = {}
  35. for p in photo_data:
  36. tmp = p.split("#")
  37. for tp in tmp:
  38. k = tp.split(",")
  39. if k[0] in indentify_result.keys():
  40. indentify_result[k[0]] += int(k[1])
  41. else:
  42. indentify_result[k[0]] = int(k[1])
  43. # 定义预警变量
  44. warning_result = ""
  45. # 指定害虫数量预警
  46. appointPest = conf.get("appointPest")
  47. if appointPest == "on":
  48. appointPestName = conf.get("appointPestName")
  49. appointPestNum = conf.get("appointPestNum")
  50. pest_cg = appointPestName.split("#")
  51. pest_nu = appointPestNum.split("#")
  52. for i in pest_cg:
  53. if i in indentify_result.keys():
  54. if indentify_result[i] > int(pest_nu[pest_cg.index(i)]):
  55. zh = insect_dict.get(i, "未知")
  56. warning_result += f"害虫{zh}数量{str(indentify_result[i])},"
  57. # 写入数据库
  58. if warning_result:
  59. MongoCBDPestWarning.objects.create(
  60. device_id = device_id,
  61. warning_content = f"{warning_result}请注意防范",
  62. warning_types = "2",
  63. upltime = now_stamp
  64. )
  65. # 害虫种类
  66. pestCategory = conf.get("pestCategory")
  67. if pestCategory == "on":
  68. pestCategoryNum = conf.get("pestCategoryNum")
  69. pest_cg_count = len(indentify_result.keys())
  70. if pest_cg_count > int(pestCategoryNum):
  71. # 写入数据库
  72. MongoCBDPestWarning.objects.create(
  73. device_id = device_id,
  74. warning_content = f"害虫种类{pest_cg_count},请注意防范",
  75. warning_types = "1",
  76. upltime = now_stamp
  77. )
  78. # 害虫总数
  79. pestTotal = conf.get("pestTotal")
  80. if pestTotal == "on":
  81. pestTotalNum = conf.get("pestTotalNum")
  82. ct = 0
  83. for m, n in indentify_result.items():
  84. ct += int(n)
  85. if ct > int(pestTotalNum):
  86. # 写入数据库
  87. MongoCBDPestWarning.objects.create(
  88. device_id = device_id,
  89. warning_content = f"害虫总数{str(ct)},请注意防范",
  90. warning_types = "3",
  91. upltime = now_stamp
  92. )
  93. # 综合预警
  94. total_warning = ""
  95. pestWarn = conf.get("pestWarn")
  96. if pestWarn == "on":
  97. for m, n in indentify_result.items():
  98. total_warning += f"害虫{insect_dict.get(m)},数量{str(n)};"
  99. # 写入数据库
  100. MongoCBDPestWarning.objects.create(
  101. device_id = device_id,
  102. warning_content = f"{total_warning}请注意防范!",
  103. warning_types = "4",
  104. upltime = now_stamp
  105. )