|
|
@@ -2,10 +2,14 @@ from rest_framework.views import APIView
|
|
|
from rest_framework.response import Response
|
|
|
from django.core.cache import cache as default_cache
|
|
|
|
|
|
-from .serializers import SearchEquipSerializer, QxzDeviceDetailSerializer
|
|
|
+import json
|
|
|
+import ast
|
|
|
+
|
|
|
+from django.conf import settings
|
|
|
+from .serializers import SearchEquipSerializer, DeviceDetailSerializer
|
|
|
from utils.JWTAuthentication_diy import APIAuthentication
|
|
|
-from utils.permissions import QXZDeviceDetailPermission
|
|
|
-from utils.MyRateThrottle import QxzDeviceListRateThrottle, QxzDeviceDetailRateThrottle
|
|
|
+from utils.permissions import QXZDeviceDetailPermission, ScdDeviceDetailPermission, CbdDeviceDetailPermission, BzyDeviceDetailPermission, XycbDeviceDetailPermission
|
|
|
+from utils.MyRateThrottle import DeviceDetailRateThrottle, QxzDeviceListRateThrottle, ScdDeviceListRateThrottle, CbdDeviceListRateThrottle, DevicePhotoRateThrottle, BzyDeviceListRateThrottle, XycbDeviceListRateThrottle
|
|
|
from utils.utils import DeviceInfoUtils
|
|
|
from utils.db_utils import MongoDBTools
|
|
|
|
|
|
@@ -64,10 +68,10 @@ class QxzDeviceListView(APIView):
|
|
|
class QxzDeviceDetailView(APIView):
|
|
|
authentication_classes = [APIAuthentication]
|
|
|
permission_classes = [QXZDeviceDetailPermission]
|
|
|
- throttle_classes = [QxzDeviceDetailRateThrottle]
|
|
|
+ throttle_classes = [DeviceDetailRateThrottle]
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
- serializer = QxzDeviceDetailSerializer(data=request.query_params)
|
|
|
+ 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"]
|
|
|
@@ -100,3 +104,544 @@ class QxzDeviceDetailView(APIView):
|
|
|
result["items"].append(item)
|
|
|
return Response(result)
|
|
|
|
|
|
+
|
|
|
+class ScdDeviceListView(APIView):
|
|
|
+ authentication_classes = [APIAuthentication]
|
|
|
+ throttle_classes = [ScdDeviceListRateThrottle]
|
|
|
+
|
|
|
+ def get(self, request, *args, **kwargs):
|
|
|
+ """获取杀虫灯设备列表接口"""
|
|
|
+ uid = request.user
|
|
|
+
|
|
|
+ wheres = {
|
|
|
+ "device_type_id":2,
|
|
|
+ '$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":[]}
|
|
|
+ scd_dict_cache = {}
|
|
|
+ for item in data:
|
|
|
+ d_id = item.pop("id")
|
|
|
+ result["items"].append(item)
|
|
|
+ scd_dict_cache[item["device_id"]] = d_id
|
|
|
+ default_cache.set(str(uid)+"_scd_list", scd_dict_cache,60*5)
|
|
|
+ return Response(result)
|
|
|
+
|
|
|
+
|
|
|
+class ScdDeviceDetailView(APIView):
|
|
|
+ authentication_classes = [APIAuthentication]
|
|
|
+ permission_classes = [ScdDeviceDetailPermission]
|
|
|
+ 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
|
|
|
+ scd_dict_cache = default_cache.get(str(uid)+"_scd_list")
|
|
|
+ if scd_dict_cache:
|
|
|
+ d_id = scd_dict_cache[device_id]
|
|
|
+ else:
|
|
|
+ """避免缓存失效"""
|
|
|
+ wheres = {
|
|
|
+ 'device_type_id':2,
|
|
|
+ '$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)
|
|
|
+ scd_dict_cache = []
|
|
|
+ for item in data:
|
|
|
+ scd_dict_cache[item["device_id"]]=item["id"]
|
|
|
+ default_cache.set(str(uid)+"_scd_list", scd_dict_cache,60*5)
|
|
|
+ d_id = scd_dict_cache[device_id]
|
|
|
+
|
|
|
+ result = []
|
|
|
+
|
|
|
+ data_m = MongoDBTools(db_name='smartfarming', table_name='sa_device_scd_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 CbdDeviceListView(APIView):
|
|
|
+ authentication_classes = [APIAuthentication]
|
|
|
+ throttle_classes = [CbdDeviceListRateThrottle]
|
|
|
+
|
|
|
+ def get(self, request, *args, **kwargs):
|
|
|
+ """获取测报灯设备列表接口"""
|
|
|
+ uid = request.user
|
|
|
+
|
|
|
+ wheres = {
|
|
|
+ "device_type_id":3,
|
|
|
+ '$or': [
|
|
|
+ {'owner_uid': uid},
|
|
|
+ {'user_dealer': uid}
|
|
|
+ ]
|
|
|
+ }
|
|
|
+ project = {
|
|
|
+ 'id': '$id',
|
|
|
+ 'device_id': '$device_id',
|
|
|
+ 'disable': '$disable',
|
|
|
+ '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":[]}
|
|
|
+ cbd_dict_cache = {}
|
|
|
+ for item in data:
|
|
|
+ d_id = item.pop("id")
|
|
|
+ disable = item.pop("disable")
|
|
|
+ result["items"].append(item)
|
|
|
+ cbd_dict_cache[item["device_id"]] = {"d_id":d_id,"disable":disable}
|
|
|
+ default_cache.set(str(uid)+"_cbd_list", cbd_dict_cache,60*5)
|
|
|
+ return Response(result)
|
|
|
+
|
|
|
+
|
|
|
+class CbdDeviceDetailView(APIView):
|
|
|
+ authentication_classes = [APIAuthentication]
|
|
|
+ permission_classes = [CbdDeviceDetailPermission]
|
|
|
+ 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
|
|
|
+ cbd_dict_cache = default_cache.get(str(uid)+"_cbd_list")
|
|
|
+ if cbd_dict_cache:
|
|
|
+ d_id = cbd_dict_cache[device_id]["d_id"]
|
|
|
+ else:
|
|
|
+ """避免缓存失效"""
|
|
|
+ wheres = {
|
|
|
+ 'device_type_id':3,
|
|
|
+ '$or': [
|
|
|
+ {'owner_uid': uid},
|
|
|
+ {'user_dealer': uid}
|
|
|
+ ]
|
|
|
+ }
|
|
|
+ project = {
|
|
|
+ 'id': '$id',
|
|
|
+ 'device_id': '$device_id',
|
|
|
+ 'disable': '$disable'
|
|
|
+ }
|
|
|
+ m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
|
|
|
+ data = m.find_many(wheres=wheres, options=project)
|
|
|
+ cbd_dict_cache = {}
|
|
|
+ for item in data:
|
|
|
+ cbd_dict_cache[item["device_id"]]={"d_id":item["id"],"disable":item["disable"]}
|
|
|
+ default_cache.set(str(uid)+"_cbd_list", cbd_dict_cache,60*5)
|
|
|
+ d_id = cbd_dict_cache[device_id]["d_id"]
|
|
|
+
|
|
|
+ result = []
|
|
|
+
|
|
|
+ data_m = MongoDBTools(db_name='smartfarming', table_name='sa_device_cbd_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 CbdDevicePhotoView(APIView):
|
|
|
+ authentication_classes = [APIAuthentication]
|
|
|
+ permission_classes = [CbdDeviceDetailPermission]
|
|
|
+ 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
|
|
|
+ cbd_dict_cache = default_cache.get(str(uid)+"_cbd_list")
|
|
|
+ if cbd_dict_cache:
|
|
|
+ d_id = cbd_dict_cache[device_id]["d_id"]
|
|
|
+ disable = cbd_dict_cache[device_id]["disable"]
|
|
|
+ else:
|
|
|
+ """避免缓存失效"""
|
|
|
+ wheres = {
|
|
|
+ 'device_type_id':3,
|
|
|
+ '$or': [
|
|
|
+ {'owner_uid': uid},
|
|
|
+ {'user_dealer': uid}
|
|
|
+ ]
|
|
|
+ }
|
|
|
+ project = {
|
|
|
+ 'id': '$id',
|
|
|
+ 'device_id': '$device_id',
|
|
|
+ 'disable': '$disable'
|
|
|
+ }
|
|
|
+ m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
|
|
|
+ data = m.find_many(wheres=wheres, options=project)
|
|
|
+ cbd_dict_cache = {}
|
|
|
+ for item in data:
|
|
|
+ cbd_dict_cache[item["device_id"]]={"d_id":item["id"],"disable":item["disable"]}
|
|
|
+ default_cache.set(str(uid)+"_cbd_list", cbd_dict_cache,60*5)
|
|
|
+ d_id = cbd_dict_cache[device_id]["d_id"]
|
|
|
+ disable = cbd_dict_cache[device_id]["disable"]
|
|
|
+
|
|
|
+ result = []
|
|
|
+
|
|
|
+ data_m = MongoDBTools(db_name='smartfarming', table_name='sa_device_cbd_photo')
|
|
|
+ data_wheres = {
|
|
|
+ "device_id": str(d_id),
|
|
|
+ "addtime": {"$gt":start_time}
|
|
|
+ }
|
|
|
+ if disable == 1:
|
|
|
+ data_project = {
|
|
|
+ "addtime": "$addtime",
|
|
|
+ "addr": "$addr",
|
|
|
+ "indentify_photo":"$indentify_photo",
|
|
|
+ "indentify_result":"$indentify_result"
|
|
|
+ }
|
|
|
+ else:
|
|
|
+ data_project = {
|
|
|
+ "addtime": "$addtime",
|
|
|
+ "addr": "$addr"
|
|
|
+ }
|
|
|
+ data = data_m.find_many(wheres=data_wheres,options=data_project)
|
|
|
+
|
|
|
+ for item in data:
|
|
|
+ item_data = {}
|
|
|
+ addr = item["addr"]
|
|
|
+ if addr.startswith("http"):
|
|
|
+ Image = addr
|
|
|
+ elif addr.startswith("/"):
|
|
|
+ Image = settings.CONFIG["image_url"]["image_forward"] + addr
|
|
|
+ else:
|
|
|
+ Image = settings.CONFIG["image_url"]["image_forward"] + "/" +addr
|
|
|
+ item_data["uptime"] = item["addtime"]
|
|
|
+ item_data["Image"] = Image
|
|
|
+ if disable == 1:
|
|
|
+ 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"]["result_image_forward"] + indentify_photo
|
|
|
+ else:
|
|
|
+ Result_image = settings.CONFIG["image_url"]["result_image_forward"] + "/" + indentify_photo
|
|
|
+ else:
|
|
|
+ Result_image = "0"
|
|
|
+ item_data["Result_image"] = Result_image
|
|
|
+ item_data["Result"] = Result
|
|
|
+
|
|
|
+ result.append(item_data)
|
|
|
+ return Response(result)
|
|
|
+
|
|
|
+
|
|
|
+class BzyDeviceListView(APIView):
|
|
|
+ authentication_classes = [APIAuthentication]
|
|
|
+ throttle_classes = [BzyDeviceListRateThrottle]
|
|
|
+
|
|
|
+ def get(self, request, *args, **kwargs):
|
|
|
+ """获取孢子仪设备列表接口"""
|
|
|
+ uid = request.user
|
|
|
+
|
|
|
+ wheres = {
|
|
|
+ "device_type_id":7,
|
|
|
+ '$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":[]}
|
|
|
+ bzy_dict_cache = {}
|
|
|
+ for item in data:
|
|
|
+ d_id = item.pop("id")
|
|
|
+ result["items"].append(item)
|
|
|
+ bzy_dict_cache[item["device_id"]] = d_id
|
|
|
+ default_cache.set(str(uid)+"_bzy_list", bzy_dict_cache,60*5)
|
|
|
+ return Response(result)
|
|
|
+
|
|
|
+
|
|
|
+class BzyDeviceDetailView(APIView):
|
|
|
+ authentication_classes = [APIAuthentication]
|
|
|
+ permission_classes = [BzyDeviceDetailPermission]
|
|
|
+ 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
|
|
|
+ bzy_dict_cache = default_cache.get(str(uid)+"_bzy_list")
|
|
|
+ if bzy_dict_cache:
|
|
|
+ d_id = bzy_dict_cache[device_id]
|
|
|
+ else:
|
|
|
+ """避免缓存失效"""
|
|
|
+ wheres = {
|
|
|
+ 'device_type_id':7,
|
|
|
+ '$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)
|
|
|
+ bzy_dict_cache = []
|
|
|
+ for item in data:
|
|
|
+ bzy_dict_cache[item["device_id"]]=item["id"]
|
|
|
+ default_cache.set(str(uid)+"_bzy_list", bzy_dict_cache,60*5)
|
|
|
+ d_id = bzy_dict_cache[device_id]
|
|
|
+
|
|
|
+ result = []
|
|
|
+
|
|
|
+ data_m = MongoDBTools(db_name='smartfarming', table_name='sa_device_bzy_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 BzyDevicePhotoView(APIView):
|
|
|
+ authentication_classes = [APIAuthentication]
|
|
|
+ permission_classes = [BzyDeviceDetailPermission]
|
|
|
+ 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
|
|
|
+ bzy_dict_cache = default_cache.get(str(uid)+"_bzy_list")
|
|
|
+ if bzy_dict_cache:
|
|
|
+ d_id = bzy_dict_cache[device_id]
|
|
|
+ else:
|
|
|
+ """避免缓存失效"""
|
|
|
+ wheres = {
|
|
|
+ 'device_type_id':7,
|
|
|
+ '$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)
|
|
|
+ bzy_dict_cache = []
|
|
|
+ for item in data:
|
|
|
+ bzy_dict_cache[item["device_id"]]=item["id"]
|
|
|
+ default_cache.set(str(uid)+"_bzy_list", bzy_dict_cache,60*5)
|
|
|
+ d_id = bzy_dict_cache[device_id]
|
|
|
+
|
|
|
+ result = []
|
|
|
+
|
|
|
+ data_m = MongoDBTools(db_name='smartfarming', table_name='sa_device_bzyphoto')
|
|
|
+ data_wheres = {
|
|
|
+ "device_id": str(d_id),
|
|
|
+ "addtime": {"$gt":start_time}
|
|
|
+ }
|
|
|
+ data_project = {
|
|
|
+ "addtime": "$addtime",
|
|
|
+ "addr": "$addr"
|
|
|
+ }
|
|
|
+ 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"]["bzy_img_forward"] + item["addr"]
|
|
|
+ else:
|
|
|
+ Image = settings.CONFIG["image_url"]["bzy_img_forward"] + "/" + item["addr"]
|
|
|
+
|
|
|
+ result.append({"uptime":item["addtime"],"Image":Image})
|
|
|
+ return Response(result)
|
|
|
+
|
|
|
+
|
|
|
+class XycbDeviceListView(APIView):
|
|
|
+ authentication_classes = [APIAuthentication]
|
|
|
+ throttle_classes = [XycbDeviceListRateThrottle]
|
|
|
+
|
|
|
+ def get(self, request, *args, **kwargs):
|
|
|
+ """获取性诱设备列表接口"""
|
|
|
+ uid = request.user
|
|
|
+
|
|
|
+ wheres = {
|
|
|
+ "device_type_id":4,
|
|
|
+ '$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":[]}
|
|
|
+ xycb_dict_cache = {}
|
|
|
+ for item in data:
|
|
|
+ d_id = item.pop("id")
|
|
|
+ result["items"].append(item)
|
|
|
+ xycb_dict_cache[item["device_id"]] = d_id
|
|
|
+ default_cache.set(str(uid)+"_xycb_list", xycb_dict_cache,60*5)
|
|
|
+ return Response(result)
|
|
|
+
|
|
|
+
|
|
|
+class XycbDeviceDetailView(APIView):
|
|
|
+ authentication_classes = [APIAuthentication]
|
|
|
+ permission_classes = [XycbDeviceDetailPermission]
|
|
|
+ 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
|
|
|
+ xycb_dict_cache = default_cache.get(str(uid)+"_xycb_list")
|
|
|
+ if xycb_dict_cache:
|
|
|
+ d_id = xycb_dict_cache[device_id]
|
|
|
+ else:
|
|
|
+ """避免缓存失效"""
|
|
|
+ wheres = {
|
|
|
+ 'device_type_id':4,
|
|
|
+ '$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)
|
|
|
+ xycb_dict_cache = []
|
|
|
+ for item in data:
|
|
|
+ xycb_dict_cache[item["device_id"]]=item["id"]
|
|
|
+ default_cache.set(str(uid)+"_scd_list", xycb_dict_cache,60*5)
|
|
|
+ d_id = xycb_dict_cache[device_id]
|
|
|
+
|
|
|
+ result = []
|
|
|
+
|
|
|
+ data_m = MongoDBTools(db_name='smartfarming', table_name='sa_device_xycb_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)
|