| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015 |
- 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, GssqDeviceDetailPermission
- from utils.MyRateThrottle import DeviceDetailRateThrottle, DevicePhotoRateThrottle, QxzDeviceListRateThrottle, ScdDeviceListRateThrottle, CbdDeviceListRateThrottle, BzyDeviceListRateThrottle, XycbDeviceListRateThrottle, XctDeviceListRateThrottle, GssqDeviceListRateThrottle
- 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.get("start_timestamp") == 0:
- data_wheres = {
- "device_id": request_data["device_id"]
- }
- 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,skip=request_data["page_start"],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])))
- result["items"].append({"uptime":uptime,"item_data":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.get("start_timestamp") == 0:
- data_wheres = {
- "device_id": d_id
- }
- 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,skip=request_data["page_start"],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.get("start_timestamp") == 0:
- data_wheres = {
- "device_id": d_id
- }
- 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,skip=request_data["page_start"],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.get("start_timestamp") == 0:
- data_wheres = {
- "device_id": str(d_id)
- }
- 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,skip=request_data["page_start"],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.get("start_timestamp") == 0:
- data_wheres = {
- "device_id": d_id
- }
- 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,skip=request_data["page_start"],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.get("start_timestamp") == 0:
- data_wheres = {
- "device_id": str(d_id)
- }
- 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,skip=request_data["page_start"],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.get("start_timestamp") == 0:
- data_wheres = {
- "device_id": d_id
- }
- 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,skip=request_data["page_start"],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.get("start_timestamp") == 0:
- data_wheres = {
- "device_id": d_id
- }
- 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,skip=request_data["page_start"],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.get("start_timestamp") == 0:
- data_wheres = {
- "device_id": str(d_id)
- }
- 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,skip=request_data["page_start"],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)
- class GssqDeviceListView(APIView):
- authentication_classes = [APIAuthentication]
- throttle_classes = [GssqDeviceListRateThrottle]
- def get(self, request, *args, **kwargs):
- """获取管式墒情列表接口"""
- uid = request.user
-
- wheres = {
- "device_type_id":15,
- '$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":[]}
- gssq_dict_cache = {}
- for item in data:
- d_id = item.pop("id")
- result["items"].append(item)
- gssq_dict_cache[item["device_id"]] = d_id
- default_cache.set(str(uid)+"_gssq_list", gssq_dict_cache,60*5)
- return Response(result)
- class GssqDeviceDetailView(APIView):
- authentication_classes = [APIAuthentication]
- permission_classes = [GssqDeviceDetailPermission]
- 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
- gssq_dict_cache = default_cache.get(str(uid)+"_gssq_list")
- if gssq_dict_cache:
- d_id = gssq_dict_cache[request_data["device_id"]]
- else:
- """避免缓存失效"""
- wheres = {
- 'device_type_id':15,
- '$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)
- gssq_dict_cache = []
- for item in data:
- gssq_dict_cache[item["device_id"]]=item["id"]
- default_cache.set(str(uid)+"_gssq_list", gssq_dict_cache,60*5)
- d_id = gssq_dict_cache[request_data["device_id"]]
- result = {"total_counts":0,"items":[]}
- data_m = MongoDBTools(db_name='smartfarming', table_name='sa_nd_qxz_data')
- data_project = {
- "upl_time": "$upl_time",
- "id" : "$id",
- "at" : "$at",
- "atm" : "$atm",
- "ats" : "$ats",
- "csq" : "$csq",
- "device_id" : "$device_id",
- "swc" : "$swc",
- "temp" : "$temp"
- }
- if request_data.get("start_timestamp") == 0:
- data_wheres = {
- "device_id": request_data["device_id"]
- }
- else:
- data_wheres = {
- "device_id": request_data["device_id"],
- "upl_time": {
- "$gt": request_data["start_timestamp"],
- "$lte": request_data["end_timestamp"]
- }
- }
- data = data_m.find_many(wheres=data_wheres,options=data_project,skip=request_data["page_start"],limit=request_data["page_size"])
- total_counts = data.count()
- result["total_counts"] = total_counts
- for item in data:
- try:
- device_data = item
- except:
- device_data = ast.literal_eval(item)
-
- result["items"].append({"uptime":item["upl_time"],"item_data":device_data})
- return Response(result)
|