|
|
@@ -0,0 +1,151 @@
|
|
|
+from rest_framework.generics import GenericAPIView
|
|
|
+from rest_framework.response import Response
|
|
|
+import re
|
|
|
+import math
|
|
|
+
|
|
|
+from .serializers import ZhiBaoSelectViewSerializer
|
|
|
+from utils.JWTAuthentication_diy import MyJWTAuthentication
|
|
|
+from utils.permissions import ModulePermission
|
|
|
+
|
|
|
+from utils.db_utils import MongoDBTools
|
|
|
+from utils.all_dict import insect_dict
|
|
|
+from django.conf import settings
|
|
|
+
|
|
|
+
|
|
|
+class PestSelectView(GenericAPIView):
|
|
|
+ authentication_classes = [MyJWTAuthentication]
|
|
|
+ permission_classes = [ModulePermission]
|
|
|
+ serializer_class = ZhiBaoSelectViewSerializer
|
|
|
+
|
|
|
+ def get(self, request, *args, **kwargs):
|
|
|
+ serializer = self.get_serializer(data=request.query_params)
|
|
|
+ serializer.is_valid(raise_exception=True)
|
|
|
+ data = serializer.validated_data
|
|
|
+ identify_model = data["identify_model"]
|
|
|
+ start_time = data["start_time"]
|
|
|
+ end_time = data["end_time"]
|
|
|
+ page = data["page"]
|
|
|
+ page_size = data["page_size"]
|
|
|
+ select_name = data.get("pest_name",'')
|
|
|
+
|
|
|
+ if select_name:
|
|
|
+ key_list=list(insect_dict.keys())
|
|
|
+ val_list=list(insect_dict.values())
|
|
|
+ ind = val_list.index(select_name)
|
|
|
+ key = key_list[ind]+"," # 1665772530,1666125101
|
|
|
+ wheres = {
|
|
|
+ "photo_status": 1,
|
|
|
+ "addtime": {
|
|
|
+ "$gte": start_time,
|
|
|
+ "$lt": end_time
|
|
|
+ },
|
|
|
+ "indentify_result": re.compile("#{}|^{}".format(key,key))
|
|
|
+ }
|
|
|
+ else:
|
|
|
+ wheres = {
|
|
|
+ "photo_status": 1,
|
|
|
+ "addtime":{
|
|
|
+ "$gte": start_time,
|
|
|
+ "$lt": end_time
|
|
|
+ },
|
|
|
+ "indentify_result": {
|
|
|
+ "$nin": ["", "0", None]
|
|
|
+ }
|
|
|
+ }
|
|
|
+ project = {
|
|
|
+ "device_id":"$device_id",
|
|
|
+ "addr":"$addr",
|
|
|
+ "indentify_photo":"$indentify_photo",
|
|
|
+ "indentify_result": "$indentify_result",
|
|
|
+ "addtime":"$addtime"
|
|
|
+ }
|
|
|
+ skip = (page-1)*page_size
|
|
|
+ limit = page_size
|
|
|
+ model = MongoDBTools(db_name='smartfarming', table_name='sa_device_cbdphoto') if identify_model == "A" else MongoDBTools(db_name='smartfarming', table_name='sa_device_cbdphoto_b')
|
|
|
+ photo_data = model.find_many(wheres=wheres, options=project, skip=skip, limit=limit)
|
|
|
+ if photo_data:
|
|
|
+ total_counts = photo_data.count()
|
|
|
+ else:
|
|
|
+ total_counts = 0
|
|
|
+ data = {"total_page":math.ceil(total_counts/page_size), "total_counts":total_counts, "page_size":page_size, "current_page":page, "current_counts":0, "items":[]}
|
|
|
+ d_id_list = []
|
|
|
+ items = []
|
|
|
+ for i in photo_data:
|
|
|
+ d_id_list.append(int(i["device_id"]))
|
|
|
+ items.append(i)
|
|
|
+ data["current_counts"] += 1
|
|
|
+
|
|
|
+ wheres = {
|
|
|
+ "device_type_id":3,
|
|
|
+ "id": {
|
|
|
+ "$in": d_id_list
|
|
|
+ }
|
|
|
+ }
|
|
|
+ project = {
|
|
|
+ 'id': "$id",
|
|
|
+ 'device_id': '$device_id',
|
|
|
+ 'device_name': '$device_name',
|
|
|
+ 'device_code': '$device_code',
|
|
|
+ 'province': '$province',
|
|
|
+ 'city': '$city',
|
|
|
+ 'district': '$district'
|
|
|
+ }
|
|
|
+ m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
|
|
|
+ device_data = m.find_many(wheres=wheres, options=project)
|
|
|
+ d_id_dicts = {}
|
|
|
+ for device_item in device_data:
|
|
|
+ d_id_dicts[device_item["id"]] = {"location":device_item["province"]+device_item["city"]+device_item["district"],
|
|
|
+ "device_id":device_item["device_id"],
|
|
|
+ "device_code":device_item["device_code"],
|
|
|
+ "device_name":device_item["device_name"] if device_item["device_name"] else "测报灯"
|
|
|
+ }
|
|
|
+ pest_image_data = []
|
|
|
+ for photo_object in items:
|
|
|
+ indentify_result = photo_object["indentify_result"]
|
|
|
+ pest_string = ""
|
|
|
+ pest_dict = {}
|
|
|
+ for index,result in enumerate(indentify_result.split("#")) :
|
|
|
+ if index != 0:
|
|
|
+ pest_string += "、"
|
|
|
+ tuple_result = result.split(",")
|
|
|
+ pest_name = insect_dict.get(tuple_result[0],"未命名")
|
|
|
+ pest_string+=pest_name
|
|
|
+ pest_dict[pest_name] = int(tuple_result[1])
|
|
|
+
|
|
|
+ addtime = photo_object["addtime"]
|
|
|
+ d_id = int(photo_object["device_id"])
|
|
|
+ device_code = d_id_dicts.get(d_id,{"device_code":0}).get("device_code",0)
|
|
|
+
|
|
|
+ img_path = settings.CONFIG["image_url"]["image"]
|
|
|
+ if identify_model == "A":
|
|
|
+ indentify_path = settings.CONFIG["image_url"]["discern"]
|
|
|
+ elif identify_model == "B" and device_code == 4: # 水稻
|
|
|
+ indentify_path = settings.CONFIG["image_url"]["discern"]
|
|
|
+ else:
|
|
|
+ indentify_path = settings.CONFIG["image_url"]["discernB"]
|
|
|
+ if photo_object["addr"].startswith("http"):
|
|
|
+ img_url = photo_object["addr"]
|
|
|
+ elif photo_object["addr"].startswith('/'):
|
|
|
+ img_url = img_path + photo_object["addr"]
|
|
|
+ else:
|
|
|
+ img_url = img_path + "/" + photo_object["addr"]
|
|
|
+ if photo_object["indentify_photo"]:
|
|
|
+ if photo_object["indentify_photo"].startswith("http"):
|
|
|
+ indentify_photo = photo_object["indentify_photo"]
|
|
|
+ elif photo_object["indentify_photo"].startswith('/'):
|
|
|
+ indentify_photo = indentify_path + photo_object["indentify_photo"]
|
|
|
+ else:
|
|
|
+ indentify_photo = indentify_path + "/" + photo_object["indentify_photo"]
|
|
|
+ else:
|
|
|
+ indentify_photo = ""
|
|
|
+ pest_image_data.append({"deviceId":d_id_dicts.get(d_id,{"device_id":d_id})["device_id"],
|
|
|
+ "deviceName":d_id_dicts.get(d_id,{"device_name":"测报灯"})["device_name"],
|
|
|
+ "pestName":pest_string,
|
|
|
+ "addtime":addtime,
|
|
|
+ "location":d_id_dicts.get(d_id,{"location":""})["location"],
|
|
|
+ "img_url":img_url,
|
|
|
+ "indentify_photo":indentify_photo,
|
|
|
+ "pest_dict":pest_dict
|
|
|
+ })
|
|
|
+ data["items"] = pest_image_data
|
|
|
+ return Response(data)
|