Browse Source

吸虫塔对接接口实现

yf_fyh 2 năm trước cách đây
mục cha
commit
6c762c6642
6 tập tin đã thay đổi với 229 bổ sung6 xóa
  1. 4 0
      apps/Equipment/urls.py
  2. 172 2
      apps/Equipment/views.py
  3. 3 1
      formal.json
  4. 3 1
      test.json
  5. 9 0
      utils/MyRateThrottle.py
  6. 38 2
      utils/permissions.py

+ 4 - 0
apps/Equipment/urls.py

@@ -21,5 +21,9 @@ urlpatterns = [
 
     url(r'^xycb/list/$', views.XycbDeviceListView.as_view(), name='scd_list'),
     url(r'^xycb/detail/$', views.XycbDeviceDetailView.as_view(), name='scd_detail'),
+
+    url(r'^xct/list/$', views.XctDeviceListView.as_view(), name='xct_list'),
+    url(r'^xct/detail/$', views.XctDeviceDetailView.as_view(), name='xct_detail'),
+    url(r'^xct/photo/$', views.XctDevicePhotoView.as_view(), name='xct_photo'),
     
 ]

+ 172 - 2
apps/Equipment/views.py

@@ -8,8 +8,8 @@ import ast
 from django.conf import settings
 from .serializers import SearchEquipSerializer, DeviceDetailSerializer
 from utils.JWTAuthentication_diy import APIAuthentication
-from utils.permissions import QXZDeviceDetailPermission, ScdDeviceDetailPermission, CbdDeviceDetailPermission, BzyDeviceDetailPermission, XycbDeviceDetailPermission
-from utils.MyRateThrottle import DeviceDetailRateThrottle, QxzDeviceListRateThrottle, ScdDeviceListRateThrottle, CbdDeviceListRateThrottle, DevicePhotoRateThrottle, BzyDeviceListRateThrottle, XycbDeviceListRateThrottle
+from utils.permissions import QXZDeviceDetailPermission, ScdDeviceDetailPermission, CbdDeviceDetailPermission, BzyDeviceDetailPermission, XycbDeviceDetailPermission, XctDeviceDetailPermission
+from utils.MyRateThrottle import DeviceDetailRateThrottle, DevicePhotoRateThrottle, QxzDeviceListRateThrottle, ScdDeviceListRateThrottle, CbdDeviceListRateThrottle, BzyDeviceListRateThrottle, XycbDeviceListRateThrottle, XctDeviceListRateThrottle
 from utils.utils import DeviceInfoUtils
 from utils.db_utils import MongoDBTools
 
@@ -644,4 +644,174 @@ class XycbDeviceDetailView(APIView):
             except:
                 device_data = ast.literal_eval(item["device_data"])
             result.append({"uptime":item["addtime"],"item_data":device_data})
+        return Response(result)
+
+
+class XctDeviceListView(APIView):
+    authentication_classes = [APIAuthentication]
+    throttle_classes = [XctDeviceListRateThrottle]
+
+    def get(self, request, *args, **kwargs):
+        """获取吸虫塔设备列表接口"""
+        uid = request.user
+        
+        wheres = {
+            "device_type_id":12,
+            '$or': [
+                {'owner_uid': uid},
+                {'user_dealer': uid}
+            ]
+        }
+        project = {
+            'id': '$id',
+            'device_id': '$device_id',
+            'uptime': '$uptime',
+            'device_status': '$device_status',
+            'lng': '$lng',
+            'lat': '$lat'
+        }
+        m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
+        data = m.find_many(wheres=wheres, options=project)
+        if data:
+            total_counts = data.count()
+        else:
+            total_counts = 0
+        result = {"total_counts":total_counts,"items":[]}
+        xct_dict_cache = {}
+        for item in data:
+            d_id = item.pop("id")
+            result["items"].append(item)
+            xct_dict_cache[item["device_id"]] = d_id
+        default_cache.set(str(uid)+"_xct_list", xct_dict_cache,60*5)
+        return Response(result)
+
+
+class XctDeviceDetailView(APIView):
+    authentication_classes = [APIAuthentication]
+    permission_classes = [XctDeviceDetailPermission]
+    throttle_classes = [DeviceDetailRateThrottle]
+    
+    def get(self, request, *args, **kwargs):
+        serializer = DeviceDetailSerializer(data=request.query_params)
+        serializer.is_valid(raise_exception=True)
+        request_data = serializer.validated_data
+        start_time, device_id = request_data["start_timestamp"], request_data["device_id"]
+
+        uid = request.user
+        xct_dict_cache = default_cache.get(str(uid)+"_xct_list")
+        if xct_dict_cache:
+            d_id = xct_dict_cache[device_id]
+        else:
+            """避免缓存失效"""
+            wheres = {
+                'device_type_id':12,
+                '$or': [
+                    {'owner_uid': uid},
+                    {'user_dealer': uid}
+                ]
+            }
+            project = {
+                'id': '$id',
+                'device_id': '$device_id'
+            }
+            m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
+            data = m.find_many(wheres=wheres, options=project)
+            xct_dict_cache = []
+            for item in data:
+                xct_dict_cache[item["device_id"]]=item["id"]
+            default_cache.set(str(uid)+"_xct_list", xct_dict_cache,60*5)
+            d_id = xct_dict_cache[device_id]
+
+        result = []
+
+        data_m = MongoDBTools(db_name='smartfarming', table_name='sa_device_xct_data')
+        data_wheres = {
+            "device_id": d_id,
+            "addtime": {"$gt":start_time}
+        }
+        data_project = {
+            "addtime": "$addtime",
+            "device_data": "$device_data"
+        }
+        data = data_m.find_many(wheres=data_wheres,options=data_project)
+
+        for item in data:
+            try:
+                device_data = json.loads(item["device_data"])
+            except:
+                device_data = ast.literal_eval(item["device_data"])
+            result.append({"uptime":item["addtime"],"item_data":device_data})
+        return Response(result)
+
+
+class XctDevicePhotoView(APIView):
+    authentication_classes = [APIAuthentication]
+    permission_classes = [XctDeviceDetailPermission]
+    throttle_classes = [DevicePhotoRateThrottle]
+    
+    def get(self, request, *args, **kwargs):
+        serializer = DeviceDetailSerializer(data=request.query_params)
+        serializer.is_valid(raise_exception=True)
+        request_data = serializer.validated_data
+        start_time, device_id = request_data["start_timestamp"], request_data["device_id"]
+
+        uid = request.user
+        xct_dict_cache = default_cache.get(str(uid)+"_xct_list")
+        if xct_dict_cache:
+            d_id = xct_dict_cache[device_id]
+        else:
+            """避免缓存失效"""
+            wheres = {
+                'device_type_id':12,
+                '$or': [
+                    {'owner_uid': uid},
+                    {'user_dealer': uid}
+                ]
+            }
+            project = {
+                'id': '$id',
+                'device_id': '$device_id'
+            }
+            m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
+            data = m.find_many(wheres=wheres, options=project)
+            xct_dict_cache = []
+            for item in data:
+                xct_dict_cache[item["device_id"]]=item["id"]
+            default_cache.set(str(uid)+"_xct_list", xct_dict_cache,60*5)
+            d_id = xct_dict_cache[device_id]
+
+        result = []
+
+        data_m = MongoDBTools(db_name='smartfarming', table_name='sa_device_xct_photo')
+        data_wheres = {
+            "device_id": str(d_id),
+            "addtime": {"$gt":start_time}
+        }
+        data_project = {
+            "addtime": "$addtime",
+            "addr": "$addr",
+            "indentify_photo":"$indentify_photo",
+            "indentify_result":"$indentify_result"
+        }
+        data = data_m.find_many(wheres=data_wheres,options=data_project)
+
+        for item in data:
+            if item["addr"].startswith("http"):
+                Image = item["addr"]
+            elif item["addr"].startswith("/"):
+                Image = settings.CONFIG["image_url"]["xct_img_forward"] + item["addr"]
+            else:
+                Image = settings.CONFIG["image_url"]["xct_img_forward"] + "/" + item["addr"]
+            Result = item["indentify_result"] if item["indentify_result"] else "0"
+            indentify_photo = item["indentify_photo"]
+            if indentify_photo:
+                if indentify_photo.startswith("http"):
+                    Result_image = indentify_photo
+                elif indentify_photo.startswith("/"):
+                    Result_image = settings.CONFIG["image_url"]["xct_img_result_forward"] + indentify_photo
+                else:
+                    Result_image = settings.CONFIG["image_url"]["xct_img_result_forward"] + "/" + indentify_photo
+            else:
+                Result_image = "0"
+            result.append({"uptime":item["addtime"],"Image":Image,"Result_image":Result_image,"Result":Result})
         return Response(result)

+ 3 - 1
formal.json

@@ -29,6 +29,8 @@
 		"discernB" : "https://bigdata-image.oss-cn-hangzhou.aliyuncs.com/ResultB/cbd",
 		"image_forward": "http://8.136.98.49:8003/Basics/cbd",
 		"result_image_forward": "http://8.136.98.49:8003/Result/cbd",
-		"bzy_img_forward": "http://8.136.98.49:8003/bzy_photo"
+		"bzy_img_forward": "http://8.136.98.49:8003/bzy_photo",
+		"xct_img_forward": "http://8.136.98.49:8003/Basics/xct",
+    	"xct_img_result_forward": "http://8.136.98.49:8003/Result/xct"
 	}
 }

+ 3 - 1
test.json

@@ -29,6 +29,8 @@
 		"discernB": "http://bigdata-all.oss-accelerate.aliyuncs.com/ResultB/cbd",
 		"image_forward": "http://114.115.147.140:8004/Basics/cbd",
 		"result_image_forward": "http://114.115.147.140:8004/Result/cbd",
-		"bzy_img_forward": "http://114.115.147.140/bzy_photo"
+		"bzy_img_forward": "http://114.115.147.140/bzy_photo",
+		"xct_img_forward": "http://114.115.147.140:8004/Basics/xct",
+    	"xct_img_result_forward": "http://8.136.98.49:8004/Result/xct"
 	}
 }

+ 9 - 0
utils/MyRateThrottle.py

@@ -62,4 +62,13 @@ class XycbDeviceListRateThrottle(SimpleRateThrottle):
     def get_cache_key(self, request, view):
         uid = request.user
         key = str(uid) + "_xycblist_rate"
+        return key
+
+
+class XctDeviceListRateThrottle(SimpleRateThrottle):
+    scope = "devicelist"
+
+    def get_cache_key(self, request, view):
+        uid = request.user
+        key = str(uid) + "_xctlist_rate"
         return key

+ 38 - 2
utils/permissions.py

@@ -154,8 +154,6 @@ class BzyDeviceDetailPermission(BasePermission):
         if device_id is None:
             return True
         bzy_dict_cache = default_cache.get(str(uid)+"_bzy_list")
-        print(type(bzy_dict_cache))
-        print(bzy_dict_cache)
         if bzy_dict_cache:
             if device_id in bzy_dict_cache:
                 return True
@@ -220,4 +218,42 @@ class XycbDeviceDetailPermission(BasePermission):
             if device_id in xycb_dict_cache:
                 return True
             else:
+                return False
+
+
+class XctDeviceDetailPermission(BasePermission):
+    message = "非此账户下设备"
+    
+    def has_permission(self, request, view):
+        uid = request.user
+        device_id = request.query_params.get("device_id")
+        if device_id is None:
+            return True
+        xct_dict_cache = default_cache.get(str(uid)+"_xct_list")
+        if xct_dict_cache:
+            if device_id in xct_dict_cache:
+                return True
+            else:
+                return False
+        else:
+            wheres = {
+                'device_type_id':12,
+                '$or': [
+                    {'owner_uid': uid},
+                    {'user_dealer': uid}
+                ]
+            }
+            project = {
+                'id': '$id',
+                'device_id': '$device_id'
+            }
+            m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
+            data = m.find_many(wheres=wheres, options=project)
+            xct_dict_cache = {}
+            for item in data:
+                xct_dict_cache[item["device_id"]]=item["id"]
+            default_cache.set(str(uid)+"_xct_list", xct_dict_cache,60*5)
+            if device_id in xct_dict_cache:
+                return True
+            else:
                 return False