|
|
@@ -1,4 +1,9 @@
|
|
|
# coding=utf-8
|
|
|
+from smartfarming.api.views.forecast.all_dict import insect_dict
|
|
|
+from smartfarming.models.pest_count import MongoCBDPestWarning
|
|
|
+from smartfarming.models.worm_forecast import MongoCBDphoto
|
|
|
+from smartfarming.models.sim_card import MongoMsg_Conf
|
|
|
+from django.conf import settings
|
|
|
import os
|
|
|
import sys
|
|
|
import time
|
|
|
@@ -11,29 +16,28 @@ if local_path not in sys.path:
|
|
|
sys.path.append(local_path)
|
|
|
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "kedong.settings")
|
|
|
django.setup()
|
|
|
-from django.conf import settings
|
|
|
-from smartfarming.models.sim_card import MongoMsg_Conf
|
|
|
-from smartfarming.models.worm_forecast import MongoCBDphoto
|
|
|
-from smartfarming.models.pest_count import MongoCBDPestWarning
|
|
|
-from smartfarming.api.views.forecast.all_dict import insect_dict
|
|
|
|
|
|
|
|
|
logger = logging.getLogger("other")
|
|
|
|
|
|
+
|
|
|
def product_cbd_alarm():
|
|
|
# 获取所有的预警配置
|
|
|
msg_conf = MongoMsg_Conf.objects.all()
|
|
|
for msg in msg_conf:
|
|
|
# 获取device_id conf
|
|
|
device_id = msg.device_id
|
|
|
- conf = eval(msg.conf)
|
|
|
+ conf = eval(msg.conf)
|
|
|
# 获取该device_id 下前一天的害虫情况
|
|
|
now = datetime.now()
|
|
|
now_stamp = int(time.time())
|
|
|
previous_day = now - timedelta(days=1)
|
|
|
- start_time = datetime(previous_day.year, previous_day.month, previous_day.day, 0, 0, 0).timestamp()
|
|
|
- end_time = datetime(now.year, now.month, now.day, 23, 59, 59).timestamp()
|
|
|
- photo_data = MongoCBDphoto.objects.filter(addtime__range = [start_time, end_time]).values_list("indentify_result", flat=True)
|
|
|
+ start_time = datetime(
|
|
|
+ previous_day.year, previous_day.month, previous_day.day, 0, 0, 0).timestamp()
|
|
|
+ end_time = datetime(now.year, now.month, now.day,
|
|
|
+ 23, 59, 59).timestamp()
|
|
|
+ photo_data = MongoCBDphoto.objects.filter(
|
|
|
+ addtime__range=[start_time, end_time]).values_list("indentify_result", flat=True)
|
|
|
# 把当天的害虫统计出来
|
|
|
indentify_result = {}
|
|
|
for p in photo_data:
|
|
|
@@ -62,27 +66,27 @@ def product_cbd_alarm():
|
|
|
# 写入数据库
|
|
|
if warning_result:
|
|
|
alarm_obj = MongoCBDPestWarning.objects.create(
|
|
|
- device_id = device_id,
|
|
|
- warning_content = f"{warning_result}请注意防范",
|
|
|
- warning_types = "2",
|
|
|
- upltime = now_stamp
|
|
|
- )
|
|
|
- logger.warning(f"指定害虫数量预警: {alarm_obj.id}")
|
|
|
+ device_id=device_id,
|
|
|
+ warning_content=f"{warning_result}请注意防范",
|
|
|
+ warning_types="2",
|
|
|
+ upltime=now_stamp
|
|
|
+ )
|
|
|
+ logger.warning(f"指定害虫数量预警: {alarm_obj.id}")
|
|
|
|
|
|
# 害虫种类
|
|
|
pestCategory = conf.get("pestCategory")
|
|
|
if pestCategory == "on":
|
|
|
- pestCategoryNum = conf.get("pestCategoryNum")
|
|
|
+ pestCategoryNum = conf.get("pestCategoryNum")
|
|
|
pest_cg_count = len(indentify_result.keys())
|
|
|
if pest_cg_count > int(pestCategoryNum):
|
|
|
# 写入数据库
|
|
|
alarm_obj = MongoCBDPestWarning.objects.create(
|
|
|
- device_id = device_id,
|
|
|
- warning_content = f"害虫种类{pest_cg_count},请注意防范",
|
|
|
- warning_types = "1",
|
|
|
- upltime = now_stamp
|
|
|
- )
|
|
|
- logger.warning(f"害虫种类: {alarm_obj.id}")
|
|
|
+ device_id=device_id,
|
|
|
+ warning_content=f"害虫种类{pest_cg_count},请注意防范",
|
|
|
+ warning_types="1",
|
|
|
+ upltime=now_stamp
|
|
|
+ )
|
|
|
+ logger.warning(f"害虫种类: {alarm_obj.id}")
|
|
|
|
|
|
# 害虫总数
|
|
|
pestTotal = conf.get("pestTotal")
|
|
|
@@ -94,29 +98,29 @@ def product_cbd_alarm():
|
|
|
if ct > int(pestTotalNum):
|
|
|
# 写入数据库
|
|
|
alarm_obj = MongoCBDPestWarning.objects.create(
|
|
|
- device_id = device_id,
|
|
|
- warning_content = f"害虫总数{str(ct)},请注意防范",
|
|
|
- warning_types = "3",
|
|
|
- upltime = now_stamp
|
|
|
- )
|
|
|
- logger.warning(f"害虫总数: {alarm_obj.id}")
|
|
|
-
|
|
|
+ device_id=device_id,
|
|
|
+ warning_content=f"害虫总数{str(ct)},请注意防范",
|
|
|
+ warning_types="3",
|
|
|
+ upltime=now_stamp
|
|
|
+ )
|
|
|
+ logger.warning(f"害虫总数: {alarm_obj.id}")
|
|
|
|
|
|
# 综合预警
|
|
|
total_warning = ""
|
|
|
pestWarn = conf.get("pestWarn")
|
|
|
if pestWarn == "on":
|
|
|
for m, n in indentify_result.items():
|
|
|
- total_warning += f"害虫{insect_dict.get(m)},数量{str(n)};"
|
|
|
+ total_warning += f"害虫{insect_dict.get(m)},数量{str(n)};"
|
|
|
# 写入数据库
|
|
|
if total_warning:
|
|
|
alarm_obj = MongoCBDPestWarning.objects.create(
|
|
|
- device_id = device_id,
|
|
|
- warning_content = f"{total_warning}请注意防范!",
|
|
|
- warning_types = "4",
|
|
|
- upltime = now_stamp
|
|
|
- )
|
|
|
- logger.warning(f"综合预警: {alarm_obj.id}")
|
|
|
+ device_id=device_id,
|
|
|
+ warning_content=f"{total_warning}请注意防范!",
|
|
|
+ warning_types="4",
|
|
|
+ upltime=now_stamp
|
|
|
+ )
|
|
|
+ logger.warning(f"综合预警: {alarm_obj.id}")
|
|
|
+
|
|
|
|
|
|
# 每天的8点执行任务
|
|
|
schedule.every().day.at("05:00").do(product_cbd_alarm)
|