| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912 |
- from rest_framework.views import APIView
- from rest_framework.response import Response
- from django.core.cache import cache as default_cache
- 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, 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
- # Create your views here.
- class SearchEquip(APIView):
- def post(self, request):
- serializer = SearchEquipSerializer(data=request.data)
- serializer.is_valid(raise_exception=True)
- request_data = serializer.validated_data
- data = DeviceInfoUtils().get_equip_list(
- d_id=request_data.get("device_id"),
- isfullId=request_data.get("isfullId")
- )
- return Response(data)
- class QxzDeviceListView(APIView):
- authentication_classes = [APIAuthentication]
- throttle_classes = [QxzDeviceListRateThrottle]
- def get(self, request, *args, **kwargs):
- """获取气象站设备列表接口"""
- uid = request.user
-
- wheres = {
- "device_type_id":5,
- '$or': [
- {'owner_uid': uid},
- {'user_dealer': uid}
- ]
- }
- project = {
- '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":[]}
- qxz_list_cache = []
- for item in data:
- result["items"].append(item)
- qxz_list_cache.append(item["device_id"])
- default_cache.set(str(uid)+"_qxz_list", qxz_list_cache,60*5)
- return Response(result)
- class QxzDeviceDetailView(APIView):
- authentication_classes = [APIAuthentication]
- permission_classes = [QXZDeviceDetailPermission]
- 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
- conf_wheres = {
- "device_id":request_data["device_id"],
- }
- conf_m = MongoDBTools(db_name='smartfarming', table_name='sa_qxz_conf')
- conf_data = conf_m.find_one(wheres=conf_wheres)
- if conf_data:
- conf_data.pop("id")
- conf_data.pop("device_id")
- conf_data.pop("uptime")
- conf_data = dict(sorted(conf_data.items(), key=lambda e:int(e[0].split("e")[1])))
- result = {"conf":conf_data,"total_counts":0,"items":[]}
- data_m = MongoDBTools(db_name='smartfarming', table_name='sa_qxz_data')
- if request_data["req"] == "new":
- data_wheres = {
- "device_id": request_data["device_id"]
- }
- data = data_m.find_many(wheres=data_wheres,skip=request_data["offset"],limit=request_data["page_size"])
- else:
- data_wheres = {
- "device_id": request_data["device_id"],
- "uptime": {
- "$gt": request_data["start_timestamp"],
- "$lte": request_data["end_timestamp"]
- }
- }
- data = data_m.find_many(wheres=data_wheres,is_reverse=False,skip=(request_data["page"]-1)*request_data["page_size"],limit=request_data["page_size"])
- total_counts = data.count()
- result["total_counts"] = total_counts
- for item in data:
- item.pop("id")
- item.pop("device_id")
- uptime = item.pop("uptime")
- item = dict(sorted(item.items(), key=lambda e:int(e[0].split("e")[1])))
- item["uptime"] = uptime
- 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
- uid = request.user
- scd_dict_cache = default_cache.get(str(uid)+"_scd_list")
- if scd_dict_cache:
- d_id = scd_dict_cache[request_data["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[request_data["device_id"]]
- result = {"total_counts":0,"items":[]}
- data_project = {
- "addtime": "$addtime",
- "device_data": "$device_data"
- }
- data_m = MongoDBTools(db_name='smartfarming', table_name='sa_device_scd_data')
- if request_data["req"] == "new":
- data_wheres = {
- "device_id": d_id
- }
-
- data = data_m.find_many(wheres=data_wheres,options=data_project,skip=request_data["offset"],limit=request_data["page_size"])
- else:
- data_wheres = {
- "device_id": d_id,
- "addtime": {
- "$gt": request_data["start_timestamp"],
- "$lte": request_data["end_timestamp"]
- }
- }
- data = data_m.find_many(wheres=data_wheres,options=data_project,is_reverse=False,skip=(request_data["page"]-1)*request_data["page_size"],limit=request_data["page_size"])
- total_counts = data.count()
- result["total_counts"] = total_counts
-
- for item in data:
- try:
- device_data = json.loads(item["device_data"])
- except:
- device_data = ast.literal_eval(item["device_data"])
- result["items"].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
- uid = request.user
- cbd_dict_cache = default_cache.get(str(uid)+"_cbd_list")
- if cbd_dict_cache:
- d_id = cbd_dict_cache[request_data["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[request_data["device_id"]]["d_id"]
- result = {"total_counts":0,"items":[]}
- data_m = MongoDBTools(db_name='smartfarming', table_name='sa_device_cbd_data')
-
- data_project = {
- "addtime": "$addtime",
- "device_data": "$device_data"
- }
- if request_data["req"] == "new":
- data_wheres = {
- "device_id": d_id
- }
- data = data_m.find_many(wheres=data_wheres,options=data_project,skip=request_data["offset"],limit=request_data["page_size"])
- else:
- data_wheres = {
- "device_id": d_id,
- "addtime": {
- "$gt": request_data["start_timestamp"],
- "$lte": request_data["end_timestamp"]
- }
- }
- data = data_m.find_many(wheres=data_wheres,options=data_project,is_reverse=False,skip=(request_data["page"]-1)*request_data["page_size"],limit=request_data["page_size"])
- total_counts = data.count()
- result["total_counts"] = total_counts
- for item in data:
- try:
- device_data = json.loads(item["device_data"])
- except:
- device_data = ast.literal_eval(item["device_data"])
- result["items"].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
- uid = request.user
- cbd_dict_cache = default_cache.get(str(uid)+"_cbd_list")
- if cbd_dict_cache:
- d_id = cbd_dict_cache[request_data["device_id"]]["d_id"]
- disable = cbd_dict_cache[request_data["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[request_data["device_id"]]["d_id"]
- disable = cbd_dict_cache[request_data["device_id"]]["disable"]
- result = {"total_counts":0,"items":[]}
- data_m = MongoDBTools(db_name='smartfarming', table_name='sa_device_cbdphoto')
- if disable == 1:
- data_project = {
- "addtime": "$addtime",
- "addr": "$addr",
- "indentify_photo":"$indentify_photo",
- "indentify_result":"$indentify_result"
- }
- else:
- data_project = {
- "addtime": "$addtime",
- "addr": "$addr"
- }
- if request_data["req"] == "new":
- data_wheres = {
- "device_id": str(d_id)
- }
- data = data_m.find_many(wheres=data_wheres,options=data_project,skip=request_data["offset"],limit=request_data["page_size"])
- else:
- data_wheres = {
- "device_id": str(d_id),
- "addtime": {
- "$gt": request_data["start_timestamp"],
- "$lte": request_data["end_timestamp"]
- }
- }
- data = data_m.find_many(wheres=data_wheres,options=data_project,is_reverse=False,skip=(request_data["page"]-1)*request_data["page_size"],limit=request_data["page_size"])
- total_counts = data.count()
- result["total_counts"] = total_counts
- 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["items"].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
- uid = request.user
- bzy_dict_cache = default_cache.get(str(uid)+"_bzy_list")
- if bzy_dict_cache:
- d_id = bzy_dict_cache[request_data["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[request_data["device_id"]]
- result = {"total_counts":0,"items":[]}
- data_m = MongoDBTools(db_name='smartfarming', table_name='sa_device_bzy_data')
- data_project = {
- "addtime": "$addtime",
- "device_data": "$device_data"
- }
- if request_data["req"] == "new":
- data_wheres = {
- "device_id": d_id
- }
- data = data_m.find_many(wheres=data_wheres,options=data_project,skip=request_data["offset"],limit=request_data["page_size"])
- else:
- data_wheres = {
- "device_id": d_id,
- "addtime": {
- "$gt": request_data["start_timestamp"],
- "$lte": request_data["end_timestamp"]
- }
- }
- data = data_m.find_many(wheres=data_wheres,options=data_project,is_reverse=False,skip=(request_data["page"]-1)*request_data["page_size"],limit=request_data["page_size"])
- total_counts = data.count()
- result["total_counts"] = total_counts
- for item in data:
- try:
- device_data = json.loads(item["device_data"])
- except:
- device_data = ast.literal_eval(item["device_data"])
- result["items"].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
- uid = request.user
- bzy_dict_cache = default_cache.get(str(uid)+"_bzy_list")
- if bzy_dict_cache:
- d_id = bzy_dict_cache[request_data["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[request_data["device_id"]]
- result = {"total_counts":0,"items":[]}
- data_m = MongoDBTools(db_name='smartfarming', table_name='sa_device_bzyphoto')
- data_project = {
- "addtime": "$addtime",
- "addr": "$addr"
- }
- if request_data["req"] == "new":
- data_wheres = {
- "device_id": str(d_id)
- }
- data = data_m.find_many(wheres=data_wheres,options=data_project,skip=request_data["offset"],limit=request_data["page_size"])
- else:
- data_wheres = {
- "device_id": str(d_id),
- "addtime": {
- "$gt": request_data["start_timestamp"],
- "$lte": request_data["end_timestamp"]
- }
- }
- data = data_m.find_many(wheres=data_wheres,options=data_project,is_reverse=False,skip=(request_data["page"]-1)*request_data["page_size"],limit=request_data["page_size"])
- total_counts = data.count()
- result["total_counts"] = total_counts
- 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["items"].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
- uid = request.user
- xycb_dict_cache = default_cache.get(str(uid)+"_xycb_list")
- if xycb_dict_cache:
- d_id = xycb_dict_cache[request_data["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[request_data["device_id"]]
- result = {"total_counts":0,"items":[]}
- data_m = MongoDBTools(db_name='smartfarming', table_name='sa_device_xycb_data')
- data_project = {
- "addtime": "$addtime",
- "device_data": "$device_data"
- }
- if request_data["req"] == "new":
- data_wheres = {
- "device_id": d_id
- }
- data = data_m.find_many(wheres=data_wheres,options=data_project,skip=request_data["offset"],limit=request_data["page_size"])
- else:
- data_wheres = {
- "device_id": d_id,
- "addtime": {
- "$gt": request_data["start_timestamp"],
- "$lte": request_data["end_timestamp"]
- }
- }
- data = data_m.find_many(wheres=data_wheres,options=data_project,is_reverse=False,skip=(request_data["page"]-1)*request_data["page_size"],limit=request_data["page_size"])
- total_counts = data.count()
- result["total_counts"] = total_counts
- for item in data:
- try:
- device_data = json.loads(item["device_data"])
- except:
- device_data = ast.literal_eval(item["device_data"])
- result["items"].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
- uid = request.user
- xct_dict_cache = default_cache.get(str(uid)+"_xct_list")
- if xct_dict_cache:
- d_id = xct_dict_cache[request_data["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[request_data["device_id"]]
- result = {"total_counts":0,"items":[]}
- data_m = MongoDBTools(db_name='smartfarming', table_name='sa_device_xct_data')
- data_project = {
- "addtime": "$addtime",
- "device_data": "$device_data"
- }
- if request_data["req"] == "new":
- data_wheres = {
- "device_id": d_id
- }
- data = data_m.find_many(wheres=data_wheres,options=data_project,skip=request_data["offset"],limit=request_data["page_size"])
- else:
- data_wheres = {
- "device_id": d_id,
- "addtime": {
- "$gt": request_data["start_timestamp"],
- "$lte": request_data["end_timestamp"]
- }
- }
- data = data_m.find_many(wheres=data_wheres,options=data_project,is_reverse=False,skip=(request_data["page"]-1)*request_data["page_size"],limit=request_data["page_size"])
- total_counts = data.count()
- result["total_counts"] = total_counts
- for item in data:
- try:
- device_data = json.loads(item["device_data"])
- except:
- device_data = ast.literal_eval(item["device_data"])
- result["items"].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
- uid = request.user
- xct_dict_cache = default_cache.get(str(uid)+"_xct_list")
- if xct_dict_cache:
- d_id = xct_dict_cache[request_data["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[request_data["device_id"]]
- result = {"total_counts":0,"itmes":[]}
- data_m = MongoDBTools(db_name='smartfarming', table_name='sa_device_xct_photo')
- data_project = {
- "addtime": "$addtime",
- "addr": "$addr",
- "indentify_photo":"$indentify_photo",
- "indentify_result":"$indentify_result"
- }
- if request_data["req"] == "new":
- data_wheres = {
- "device_id": str(d_id)
- }
- data = data_m.find_many(wheres=data_wheres,options=data_project,skip=request_data["offset"],limit=request_data["page_size"])
- else:
- data_wheres = {
- "device_id": str(d_id),
- "addtime": {
- "$gt": request_data["start_timestamp"],
- "$lte": request_data["end_timestamp"]
- }
- }
- data = data_m.find_many(wheres=data_wheres,options=data_project,is_reverse=False,skip=(request_data["page"]-1)*request_data["page_size"],limit=request_data["page_size"])
- total_counts = data.count()
- result["total_counts"] = total_counts
- 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 and indentify_photo!="0":
- if indentify_photo == "0":
- Result_image = "0"
- else:
- 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["itmes"].append({"uptime":item["addtime"],"Image":Image,"Result_image":Result_image,"Result":Result})
- return Response(result)
|