cbd_msg.py 4.8 KB

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