yf_yzl пре 2 година
родитељ
комит
c126de8687
2 измењених фајлова са 53 додато и 34 уклоњено
  1. 42 21
      scripts/cbd_msg.py
  2. 11 13
      smartfarming/api/views/device/device_sms_alert.py

+ 42 - 21
scripts/cbd_msg.py

@@ -4,7 +4,7 @@ import time
 import django
 import json
 from datetime import datetime, timedelta
-local_path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
+local_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
 print(local_path)
 if local_path not in sys.path:
     sys.path.append(local_path)
@@ -22,10 +22,11 @@ from smartfarming.api.views.forecast.all_dict import insect_dict
 msg_conf = MongoMsg_Conf.objects.all()
 for msg in msg_conf:
     # 获取device_id  conf
-    device_id = msg_conf.device_id
-    conf = json.load(msg_conf.conf) 
+    device_id = msg.device_id
+    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()
@@ -36,10 +37,12 @@ for msg in msg_conf:
     indentify_result = {}
     for p in photo_data:
         tmp = p.split("#")
-        if tmp[0] in indentify_result.keys():
-            indentify_result[tmp[0]] += int(tmp[1])
-        else:
-            indentify_result[tmp[0]] = int(tmp[1])
+        for tp in tmp:
+            k = tp.split(",")
+            if k[0] in indentify_result.keys():
+                indentify_result[k[0]] += int(k[1])
+            else:
+                indentify_result[k[0]] = int(k[1])
 
     # 定义预警变量
     warning_result = ""
@@ -49,12 +52,20 @@ for msg in msg_conf:
         appointPestName = conf.get("appointPestName")
         appointPestNum = conf.get("appointPestNum")
         pest_cg = appointPestName.split("#")
-        pest_nu = appointPestName.split("#")
+        pest_nu = appointPestNum.split("#")
         for i in pest_cg:
             if i in indentify_result.keys():
                 if indentify_result[i] > int(pest_nu[pest_cg.index(i)]):
                     zh = insect_dict.get(i, "未知")
                     warning_result += f"害虫{zh}数量{str(indentify_result[i])},"
+        # 写入数据库
+        if warning_result:
+            MongoCBDPestWarning.objects.create(
+                device_id = device_id,
+                warning_content = f"{warning_result}请注意防范",
+                warning_types = "2",
+                upltime = now_stamp
+            )        
 
     # 害虫种类
     pestCategory = conf.get("pestCategory")
@@ -62,7 +73,13 @@ for msg in msg_conf:
         pestCategoryNum = conf.get("pestCategoryNum") 
         pest_cg_count = len(indentify_result.keys())
         if pest_cg_count > int(pestCategoryNum):
-            warning_result += f"害虫种类{pest_cg_count},"
+            # 写入数据库
+            MongoCBDPestWarning.objects.create(
+                device_id = device_id,
+                warning_content = f"害虫种类{pest_cg_count},请注意防范",
+                warning_types = "1",
+                upltime = now_stamp
+            )  
 
     # 害虫总数
     pestTotal = conf.get("pestTotal")
@@ -72,20 +89,24 @@ for msg in msg_conf:
         for m, n in indentify_result.items():
             ct += int(n)
         if ct > int(pestTotalNum):
-            warning_result += f"害虫总数{str(ct)},"
+            # 写入数据库
+            MongoCBDPestWarning.objects.create(
+                device_id = device_id,
+                warning_content = f"害虫总数{str(ct)},请注意防范",
+                warning_types = "3",
+                upltime = now_stamp
+            )  
 
     # 综合预警
+    total_warning = ""
     pestWarn = conf.get("pestWarn")
     if pestWarn == "on":
         for m, n in indentify_result.items():
-            warning_result += f"{insect_dict.get(m)},数量{str(n)}," 
-    if warning_result:
-        warning_result += "请注意防范"
-    # 写入数据库
-    MongoCBDPestWarning.objects.create(
-        
-    )
-
-
-
-
+            total_warning += f"害虫{insect_dict.get(m)},数量{str(n)};" 
+        # 写入数据库
+        MongoCBDPestWarning.objects.create(
+            device_id = device_id,
+            warning_content = f"{total_warning}请注意防范!",
+            warning_types = "4",
+            upltime = now_stamp
+        )  

+ 11 - 13
smartfarming/api/views/device/device_sms_alert.py

@@ -70,20 +70,18 @@ def user_cbd_pest_warning_record_read(request):
     req                      非必传(str)            一键已读必传 "all"   单个已读无需传递
     """
     req = request.POST.get("req")
-    cbd_pest_warnig_query = MongoCBDPestWarning.objects.filter(user_id=request.myuser.id)
-    if req and req=="all":
-        cbd_pest_warnig_query.update(status=1)
-    else:
-        d_id = request.POST.get("id")
-        if not d_id:
-            raise PortError("id","参数缺失")
-        try:
+    cbd_pest_warnig_query = MongoCBDPestWarning.objects.all()
+    try:
+        if req and req=="all":
+            cbd_pest_warnig_query.update(status=1)
+        else:
+            d_id = request.POST.get("id")
             cbd_pest_warnig_query = cbd_pest_warnig_query.get(id=d_id)
-        except:
-            raise PortError("id","未找到此数据")
-        cbd_pest_warnig_query.status = 1
-        cbd_pest_warnig_query.save()
-    return True
+            cbd_pest_warnig_query.status = 1
+            cbd_pest_warnig_query.save()
+        return True
+    except Exception as e:
+        raise PortError("start_time&end_time","参数有误") 
 
 
 @kedong_deco(login_required=True)