cbd_msg.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import os
  2. import sys
  3. import time
  4. import logging
  5. import django
  6. import schedule
  7. from datetime import datetime, timedelta
  8. local_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  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. logger = logging.getLogger("other")
  19. def product_cbd_alarm():
  20. # 获取所有的预警配置
  21. msg_conf = MongoMsg_Conf.objects.all()
  22. for msg in msg_conf:
  23. # 获取device_id conf
  24. device_id = msg.device_id
  25. conf = eval(msg.conf)
  26. # 获取该device_id 下前一天的害虫情况
  27. now = datetime.now()
  28. now_stamp = int(time.time())
  29. previous_day = now - timedelta(days=1)
  30. start_time = datetime(previous_day.year, previous_day.month, previous_day.day, 0, 0, 0).timestamp()
  31. end_time = datetime(now.year, now.month, now.day, 23, 59, 59).timestamp()
  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. alarm_obj = MongoCBDPestWarning.objects.create(
  60. device_id = device_id,
  61. warning_content = f"{warning_result}请注意防范",
  62. warning_types = "2",
  63. upltime = now_stamp
  64. )
  65. logger.warning(f"指定害虫数量预警: {alarm_obj.id}")
  66. # 害虫种类
  67. pestCategory = conf.get("pestCategory")
  68. if pestCategory == "on":
  69. pestCategoryNum = conf.get("pestCategoryNum")
  70. pest_cg_count = len(indentify_result.keys())
  71. if pest_cg_count > int(pestCategoryNum):
  72. # 写入数据库
  73. alarm_obj = MongoCBDPestWarning.objects.create(
  74. device_id = device_id,
  75. warning_content = f"害虫种类{pest_cg_count},请注意防范",
  76. warning_types = "1",
  77. upltime = now_stamp
  78. )
  79. logger.warning(f"害虫种类: {alarm_obj.id}")
  80. # 害虫总数
  81. pestTotal = conf.get("pestTotal")
  82. if pestTotal == "on":
  83. pestTotalNum = conf.get("pestTotalNum")
  84. ct = 0
  85. for m, n in indentify_result.items():
  86. ct += int(n)
  87. if ct > int(pestTotalNum):
  88. # 写入数据库
  89. alarm_obj = MongoCBDPestWarning.objects.create(
  90. device_id = device_id,
  91. warning_content = f"害虫总数{str(ct)},请注意防范",
  92. warning_types = "3",
  93. upltime = now_stamp
  94. )
  95. logger.warning(f"害虫总数: {alarm_obj.id}")
  96. # 综合预警
  97. total_warning = ""
  98. pestWarn = conf.get("pestWarn")
  99. if pestWarn == "on":
  100. for m, n in indentify_result.items():
  101. total_warning += f"害虫{insect_dict.get(m)},数量{str(n)};"
  102. # 写入数据库
  103. alarm_obj = MongoCBDPestWarning.objects.create(
  104. device_id = device_id,
  105. warning_content = f"{total_warning}请注意防范!",
  106. warning_types = "4",
  107. upltime = now_stamp
  108. )
  109. logger.warning(f"综合预警: {alarm_obj.id}")
  110. # 每天的8点执行任务
  111. schedule.every().day.at("05:00").do(product_cbd_alarm)
  112. while True:
  113. schedule.run_pending()
  114. time.sleep(1)