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 start_time, device_id = request_data["start_timestamp"], request_data["device_id"] conf_wheres = { "device_id":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,"items":[]} data_m = MongoDBTools(db_name='smartfarming', table_name='sa_qxz_data') data_wheres = { "device_id": device_id, "uptime": {"$gt":start_time} } data = data_m.find_many(wheres=data_wheres) 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 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_cbdphoto') 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) 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 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.append({"uptime":item["addtime"],"Image":Image,"Result_image":Result_image,"Result":Result}) return Response(result)