Explorar o código

设备控制更新

yf_yzl %!s(int64=2) %!d(string=hai) anos
pai
achega
4b724a2e36
Modificáronse 1 ficheiros con 45 adicións e 24 borrados
  1. 45 24
      smartfarming/views/counts_views.py

+ 45 - 24
smartfarming/views/counts_views.py

@@ -1,5 +1,5 @@
 from django.core.paginator import Paginator
-import time
+import logging
 import datetime
 from operator import itemgetter
 from django.db.models import Q, Sum, Count
@@ -20,6 +20,8 @@ from smartfarming.models.device import MongoDevice
 from smartfarming.models.ascend import MongoBase
 
 
+logger = logging.getLogger("myapp")
+
 class LandPlanInfoAPIView(APIView):
 
     def post(self, request):
@@ -118,6 +120,7 @@ class PlanNameAPIView(APIView):
             plans = MongoPlantInfo.objects.filter(is_delete=1, id__in=plan_ids).values_list("plantname", flat=True).distinct()
             return Response({"code": 0, "msg": "success", "data": plans})
         except Exception as e:
+            logger.error(e)
             return Response({"code": 0, "msg": "success", "data": []})
         
 
@@ -125,24 +128,28 @@ class PlanAreaAPIView(APIView):
 
     def post(self, request):
         # 种植面积与作物个数统计
-        land_plan_ids = LandPlanInfo.objects.filter(status="采收").filter(recovery_kg=0).values("land_id", "plan_id")
-        plan_land = []
-        plans = []
-        plan_area = 0
-        for i in land_plan_ids:
-            landarea = MongoLandInfo.objects.get(id=i.get("land_id")).landarea
-            plant = MongoPlantInfo.objects.get(id=i.get("plan_id"))
-            plantname = f"{plant.plantname}({plant.planttype})"
-            plan_land.append(
-                {
-                    "name": plantname,
-                    "value": round(float(landarea), 2)
-                }
-            )
-            plans.append(plantname) if plantname not in plans else None 
-            plan_area += round(float(landarea), 2)
-        return Response({"code": 0, "msg": "success", "data": {"lands_area": round(plan_area, 2), "plans_count": len(plans), "p_list": plan_land}})
-    
+        try:
+            land_plan_ids = LandPlanInfo.objects.filter(status="采收").filter(recovery_kg=0).values("land_id", "plan_id")
+            plan_land = []
+            plans = []
+            plan_area = 0
+            for i in land_plan_ids:
+                landarea = MongoLandInfo.objects.get(id=i.get("land_id")).landarea
+                plant = MongoPlantInfo.objects.get(id=i.get("plan_id"))
+                plantname = f"{plant.plantname}({plant.planttype})"
+                plan_land.append(
+                    {
+                        "name": plantname,
+                        "value": round(float(landarea), 2)
+                    }
+                )
+                plans.append(plantname) if plantname not in plans else None 
+                plan_area += round(float(landarea), 2)
+            return Response({"code": 0, "msg": "success", "data": {"lands_area": round(plan_area, 2), "plans_count": len(plans), "p_list": plan_land}})
+        except Exception as e:
+            logger.error(e)
+            return Response({"code": 0, "msg": "请检查数据"})
+
 
 class DeviceCountAPIView(APIView):
 
@@ -194,6 +201,7 @@ class RecentPestCountAPIView(APIView):
             result = {m[0]:m[1] for m in result[-10:]}
             return Response({"code": 0, "msg": "success", "data": result})
         except Exception as e:
+            logger.error(e)
             return Response({"code": 2, "msg": "数据有误,请核查"})
 
 
@@ -239,7 +247,7 @@ class AlermNewsAPIView(APIView):
                     data.append(i)
             return Response({"code": 0,"msg":"success", "data": data})
         except Exception as e:
-            print(e)
+            logger.error(e)
             return Response({"code": 2, "msg": "获取数据失败"})
         
 class APPAlarmAPIView(APIView):
@@ -292,6 +300,7 @@ class QxzCameraUpdate(APIView):
                 ca_obj.save()
             return Response({"code": 0,"msg":"success"})
         except Exception as e:
+            logger.error(e)
             return Response({"code": 2,"msg":"失败"})
         
     
@@ -299,8 +308,20 @@ class KeDongOverAPIView(APIView):
 
     def post(self, request): 
         # 我的信息
-        country_count = CountryModel.objects.all().count()
-        area = MongoBase.objects.all().first().base_area
-        device = MongoDevice.objects.all().count()
-        return Response({"code": 0,"msg":"success", "data": {"country_count": country_count, "area": area, "device": device}})
+        try:
+            country_count = CountryModel.objects.all().count()
+            area = MongoBase.objects.all().first().base_area
+            device = MongoDevice.objects.all().count()
+            return Response({
+                "code": 0,
+                "msg":"success", 
+                "data": {
+                    "country_count": country_count, 
+                    "area": area, 
+                    "device": device
+                }
+            })
+        except Exception as e:
+            logger.error(e)
+            return Response({"code": 2,"msg":"失败"})