views.py 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  1. from rest_framework.views import APIView
  2. from rest_framework.response import Response
  3. from django.core.cache import cache as default_cache
  4. import json
  5. import ast
  6. from django.conf import settings
  7. from .serializers import SearchEquipSerializer, DeviceDetailSerializer
  8. from utils.JWTAuthentication_diy import APIAuthentication
  9. from utils.permissions import QXZDeviceDetailPermission, ScdDeviceDetailPermission, CbdDeviceDetailPermission, BzyDeviceDetailPermission, XycbDeviceDetailPermission, XctDeviceDetailPermission, GssqDeviceDetailPermission
  10. from utils.MyRateThrottle import DeviceDetailRateThrottle, DevicePhotoRateThrottle, QxzDeviceListRateThrottle, ScdDeviceListRateThrottle, CbdDeviceListRateThrottle, BzyDeviceListRateThrottle, XycbDeviceListRateThrottle, XctDeviceListRateThrottle, GssqDeviceListRateThrottle
  11. from utils.utils import DeviceInfoUtils
  12. from utils.db_utils import MongoDBTools
  13. # Create your views here.
  14. class SearchEquip(APIView):
  15. def post(self, request):
  16. serializer = SearchEquipSerializer(data=request.data)
  17. serializer.is_valid(raise_exception=True)
  18. request_data = serializer.validated_data
  19. data = DeviceInfoUtils().get_equip_list(
  20. d_id=request_data.get("device_id"),
  21. isfullId=request_data.get("isfullId")
  22. )
  23. return Response(data)
  24. class QxzDeviceListView(APIView):
  25. authentication_classes = [APIAuthentication]
  26. throttle_classes = [QxzDeviceListRateThrottle]
  27. def get(self, request, *args, **kwargs):
  28. """获取气象站设备列表接口"""
  29. uid = request.user
  30. wheres = {
  31. "device_type_id":5,
  32. '$or': [
  33. {'owner_uid': uid},
  34. {'user_dealer': uid}
  35. ]
  36. }
  37. project = {
  38. 'device_id': '$device_id',
  39. 'uptime': '$uptime',
  40. 'device_status': '$device_status',
  41. 'lng': '$lng',
  42. 'lat': '$lat'
  43. }
  44. m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
  45. data = m.find_many(wheres=wheres, options=project)
  46. if data:
  47. total_counts = data.count()
  48. else:
  49. total_counts = 0
  50. result = {"total_counts":total_counts,"items":[]}
  51. qxz_list_cache = []
  52. for item in data:
  53. result["items"].append(item)
  54. qxz_list_cache.append(item["device_id"])
  55. default_cache.set(str(uid)+"_qxz_list", qxz_list_cache,60*5)
  56. return Response(result)
  57. class QxzDeviceDetailView(APIView):
  58. authentication_classes = [APIAuthentication]
  59. permission_classes = [QXZDeviceDetailPermission]
  60. throttle_classes = [DeviceDetailRateThrottle]
  61. def get(self, request, *args, **kwargs):
  62. serializer = DeviceDetailSerializer(data=request.query_params)
  63. serializer.is_valid(raise_exception=True)
  64. request_data = serializer.validated_data
  65. conf_wheres = {
  66. "device_id":request_data["device_id"]
  67. }
  68. conf_m = MongoDBTools(db_name='smartfarming', table_name='sa_qxz_conf')
  69. conf_data = conf_m.find_one(wheres=conf_wheres)
  70. if conf_data:
  71. conf_data.pop("id")
  72. conf_data.pop("device_id")
  73. conf_data.pop("uptime")
  74. conf_data = dict(sorted(conf_data.items(), key=lambda e:int(e[0].split("e")[1])))
  75. result = {"conf":conf_data,"total_counts":0,"items":[]}
  76. data_m = MongoDBTools(db_name='smartfarming', table_name='sa_qxz_data')
  77. if request_data.get("start_timestamp") == 0:
  78. data_wheres = {
  79. "device_id": request_data["device_id"]
  80. }
  81. else:
  82. data_wheres = {
  83. "device_id": request_data["device_id"],
  84. "uptime": {
  85. "$gt": request_data["start_timestamp"],
  86. "$lte": request_data["end_timestamp"]
  87. }
  88. }
  89. data = data_m.find_many(wheres=data_wheres,skip=request_data["page_start"],limit=request_data["page_size"])
  90. total_counts = data.count()
  91. result["total_counts"] = total_counts
  92. for item in data:
  93. item.pop("id")
  94. item.pop("device_id")
  95. uptime = item.pop("uptime")
  96. item = dict(sorted(item.items(), key=lambda e:int(e[0].split("e")[1])))
  97. result["items"].append({"uptime":uptime,"item_data":item})
  98. return Response(result)
  99. class ScdDeviceListView(APIView):
  100. authentication_classes = [APIAuthentication]
  101. throttle_classes = [ScdDeviceListRateThrottle]
  102. def get(self, request, *args, **kwargs):
  103. """获取杀虫灯设备列表接口"""
  104. uid = request.user
  105. wheres = {
  106. "device_type_id":2,
  107. '$or': [
  108. {'owner_uid': uid},
  109. {'user_dealer': uid}
  110. ]
  111. }
  112. project = {
  113. 'id': '$id',
  114. 'device_id': '$device_id',
  115. 'uptime': '$uptime',
  116. 'device_status': '$device_status',
  117. 'lng': '$lng',
  118. 'lat': '$lat'
  119. }
  120. m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
  121. data = m.find_many(wheres=wheres, options=project)
  122. if data:
  123. total_counts = data.count()
  124. else:
  125. total_counts = 0
  126. result = {"total_counts":total_counts,"items":[]}
  127. scd_dict_cache = {}
  128. for item in data:
  129. d_id = item.pop("id")
  130. result["items"].append(item)
  131. scd_dict_cache[item["device_id"]] = d_id
  132. default_cache.set(str(uid)+"_scd_list", scd_dict_cache,60*5)
  133. return Response(result)
  134. class ScdDeviceDetailView(APIView):
  135. authentication_classes = [APIAuthentication]
  136. permission_classes = [ScdDeviceDetailPermission]
  137. throttle_classes = [DeviceDetailRateThrottle]
  138. def get(self, request, *args, **kwargs):
  139. serializer = DeviceDetailSerializer(data=request.query_params)
  140. serializer.is_valid(raise_exception=True)
  141. request_data = serializer.validated_data
  142. uid = request.user
  143. scd_dict_cache = default_cache.get(str(uid)+"_scd_list")
  144. if scd_dict_cache:
  145. d_id = scd_dict_cache[request_data["device_id"]]
  146. else:
  147. """避免缓存失效"""
  148. wheres = {
  149. 'device_type_id':2,
  150. '$or': [
  151. {'owner_uid': uid},
  152. {'user_dealer': uid}
  153. ]
  154. }
  155. project = {
  156. 'id': '$id',
  157. 'device_id': '$device_id'
  158. }
  159. m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
  160. data = m.find_many(wheres=wheres, options=project)
  161. scd_dict_cache = []
  162. for item in data:
  163. scd_dict_cache[item["device_id"]]=item["id"]
  164. default_cache.set(str(uid)+"_scd_list", scd_dict_cache,60*5)
  165. d_id = scd_dict_cache[request_data["device_id"]]
  166. result = {"total_counts":0,"items":[]}
  167. data_project = {
  168. "addtime": "$addtime",
  169. "device_data": "$device_data"
  170. }
  171. data_m = MongoDBTools(db_name='smartfarming', table_name='sa_device_scd_data')
  172. if request_data.get("start_timestamp") == 0:
  173. data_wheres = {
  174. "device_id": d_id
  175. }
  176. else:
  177. data_wheres = {
  178. "device_id": d_id,
  179. "addtime": {
  180. "$gt": request_data["start_timestamp"],
  181. "$lte": request_data["end_timestamp"]
  182. }
  183. }
  184. data = data_m.find_many(wheres=data_wheres,options=data_project,skip=request_data["page_start"],limit=request_data["page_size"])
  185. total_counts = data.count()
  186. result["total_counts"] = total_counts
  187. for item in data:
  188. try:
  189. device_data = json.loads(item["device_data"])
  190. except:
  191. device_data = ast.literal_eval(item["device_data"])
  192. result["items"].append({"uptime":item["addtime"],"item_data":device_data})
  193. return Response(result)
  194. class CbdDeviceListView(APIView):
  195. authentication_classes = [APIAuthentication]
  196. throttle_classes = [CbdDeviceListRateThrottle]
  197. def get(self, request, *args, **kwargs):
  198. """获取测报灯设备列表接口"""
  199. uid = request.user
  200. wheres = {
  201. "device_type_id":3,
  202. '$or': [
  203. {'owner_uid': uid},
  204. {'user_dealer': uid}
  205. ]
  206. }
  207. project = {
  208. 'id': '$id',
  209. 'device_id': '$device_id',
  210. 'disable': '$disable',
  211. 'uptime': '$uptime',
  212. 'device_status': '$device_status',
  213. 'lng': '$lng',
  214. 'lat': '$lat'
  215. }
  216. m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
  217. data = m.find_many(wheres=wheres, options=project)
  218. if data:
  219. total_counts = data.count()
  220. else:
  221. total_counts = 0
  222. result = {"total_counts":total_counts,"items":[]}
  223. cbd_dict_cache = {}
  224. for item in data:
  225. d_id = item.pop("id")
  226. disable = item.pop("disable")
  227. result["items"].append(item)
  228. cbd_dict_cache[item["device_id"]] = {"d_id":d_id,"disable":disable}
  229. default_cache.set(str(uid)+"_cbd_list", cbd_dict_cache,60*5)
  230. return Response(result)
  231. class CbdDeviceDetailView(APIView):
  232. authentication_classes = [APIAuthentication]
  233. permission_classes = [CbdDeviceDetailPermission]
  234. throttle_classes = [DeviceDetailRateThrottle]
  235. def get(self, request, *args, **kwargs):
  236. serializer = DeviceDetailSerializer(data=request.query_params)
  237. serializer.is_valid(raise_exception=True)
  238. request_data = serializer.validated_data
  239. uid = request.user
  240. cbd_dict_cache = default_cache.get(str(uid)+"_cbd_list")
  241. if cbd_dict_cache:
  242. d_id = cbd_dict_cache[request_data["device_id"]]["d_id"]
  243. else:
  244. """避免缓存失效"""
  245. wheres = {
  246. 'device_type_id':3,
  247. '$or': [
  248. {'owner_uid': uid},
  249. {'user_dealer': uid}
  250. ]
  251. }
  252. project = {
  253. 'id': '$id',
  254. 'device_id': '$device_id',
  255. 'disable': '$disable'
  256. }
  257. m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
  258. data = m.find_many(wheres=wheres, options=project)
  259. cbd_dict_cache = {}
  260. for item in data:
  261. cbd_dict_cache[item["device_id"]]={"d_id":item["id"],"disable":item["disable"]}
  262. default_cache.set(str(uid)+"_cbd_list", cbd_dict_cache,60*5)
  263. d_id = cbd_dict_cache[request_data["device_id"]]["d_id"]
  264. result = {"total_counts":0,"items":[]}
  265. data_m = MongoDBTools(db_name='smartfarming', table_name='sa_device_cbd_data')
  266. data_project = {
  267. "addtime": "$addtime",
  268. "device_data": "$device_data"
  269. }
  270. if request_data.get("start_timestamp") == 0:
  271. data_wheres = {
  272. "device_id": d_id
  273. }
  274. else:
  275. data_wheres = {
  276. "device_id": d_id,
  277. "addtime": {
  278. "$gt": request_data["start_timestamp"],
  279. "$lte": request_data["end_timestamp"]
  280. }
  281. }
  282. data = data_m.find_many(wheres=data_wheres,options=data_project,skip=request_data["page_start"],limit=request_data["page_size"])
  283. total_counts = data.count()
  284. result["total_counts"] = total_counts
  285. for item in data:
  286. try:
  287. device_data = json.loads(item["device_data"])
  288. except:
  289. device_data = ast.literal_eval(item["device_data"])
  290. result["items"].append({"uptime":item["addtime"],"item_data":device_data})
  291. return Response(result)
  292. class CbdDevicePhotoView(APIView):
  293. authentication_classes = [APIAuthentication]
  294. permission_classes = [CbdDeviceDetailPermission]
  295. throttle_classes = [DevicePhotoRateThrottle]
  296. def get(self, request, *args, **kwargs):
  297. serializer = DeviceDetailSerializer(data=request.query_params)
  298. serializer.is_valid(raise_exception=True)
  299. request_data = serializer.validated_data
  300. uid = request.user
  301. cbd_dict_cache = default_cache.get(str(uid)+"_cbd_list")
  302. if cbd_dict_cache:
  303. d_id = cbd_dict_cache[request_data["device_id"]]["d_id"]
  304. disable = cbd_dict_cache[request_data["device_id"]]["disable"]
  305. else:
  306. """避免缓存失效"""
  307. wheres = {
  308. 'device_type_id':3,
  309. '$or': [
  310. {'owner_uid': uid},
  311. {'user_dealer': uid}
  312. ]
  313. }
  314. project = {
  315. 'id': '$id',
  316. 'device_id': '$device_id',
  317. 'disable': '$disable'
  318. }
  319. m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
  320. data = m.find_many(wheres=wheres, options=project)
  321. cbd_dict_cache = {}
  322. for item in data:
  323. cbd_dict_cache[item["device_id"]]={"d_id":item["id"],"disable":item["disable"]}
  324. default_cache.set(str(uid)+"_cbd_list", cbd_dict_cache,60*5)
  325. d_id = cbd_dict_cache[request_data["device_id"]]["d_id"]
  326. disable = cbd_dict_cache[request_data["device_id"]]["disable"]
  327. result = {"total_counts":0,"items":[]}
  328. data_m = MongoDBTools(db_name='smartfarming', table_name='sa_device_cbdphoto')
  329. if disable == 1:
  330. data_project = {
  331. "addtime": "$addtime",
  332. "addr": "$addr",
  333. "indentify_photo":"$indentify_photo",
  334. "indentify_result":"$indentify_result"
  335. }
  336. else:
  337. data_project = {
  338. "addtime": "$addtime",
  339. "addr": "$addr"
  340. }
  341. if request_data.get("start_timestamp") == 0:
  342. data_wheres = {
  343. "device_id": str(d_id)
  344. }
  345. else:
  346. data_wheres = {
  347. "device_id": str(d_id),
  348. "addtime": {
  349. "$gt": request_data["start_timestamp"],
  350. "$lte": request_data["end_timestamp"]
  351. }
  352. }
  353. data = data_m.find_many(wheres=data_wheres,options=data_project,skip=request_data["page_start"],limit=request_data["page_size"])
  354. total_counts = data.count()
  355. result["total_counts"] = total_counts
  356. for item in data:
  357. item_data = {}
  358. addr = item["addr"]
  359. if addr.startswith("http"):
  360. Image = addr
  361. elif addr.startswith("/"):
  362. Image = settings.CONFIG["image_url"]["image_forward"] + addr
  363. else:
  364. Image = settings.CONFIG["image_url"]["image_forward"] + "/" +addr
  365. item_data["uptime"] = item["addtime"]
  366. item_data["Image"] = Image
  367. if disable == 1:
  368. Result = item["indentify_result"] if item["indentify_result"] else "0"
  369. indentify_photo = item["indentify_photo"]
  370. if indentify_photo:
  371. if indentify_photo.startswith("http"):
  372. Result_image = indentify_photo
  373. elif indentify_photo.startswith("/"):
  374. Result_image = settings.CONFIG["image_url"]["result_image_forward"] + indentify_photo
  375. else:
  376. Result_image = settings.CONFIG["image_url"]["result_image_forward"] + "/" + indentify_photo
  377. else:
  378. Result_image = "0"
  379. item_data["Result_image"] = Result_image
  380. item_data["Result"] = Result
  381. result["items"].append(item_data)
  382. return Response(result)
  383. class BzyDeviceListView(APIView):
  384. authentication_classes = [APIAuthentication]
  385. throttle_classes = [BzyDeviceListRateThrottle]
  386. def get(self, request, *args, **kwargs):
  387. """获取孢子仪设备列表接口"""
  388. uid = request.user
  389. wheres = {
  390. "device_type_id":7,
  391. '$or': [
  392. {'owner_uid': uid},
  393. {'user_dealer': uid}
  394. ]
  395. }
  396. project = {
  397. 'id': '$id',
  398. 'device_id': '$device_id',
  399. 'uptime': '$uptime',
  400. 'device_status': '$device_status',
  401. 'lng': '$lng',
  402. 'lat': '$lat'
  403. }
  404. m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
  405. data = m.find_many(wheres=wheres, options=project)
  406. if data:
  407. total_counts = data.count()
  408. else:
  409. total_counts = 0
  410. result = {"total_counts":total_counts,"items":[]}
  411. bzy_dict_cache = {}
  412. for item in data:
  413. d_id = item.pop("id")
  414. result["items"].append(item)
  415. bzy_dict_cache[item["device_id"]] = d_id
  416. default_cache.set(str(uid)+"_bzy_list", bzy_dict_cache,60*5)
  417. return Response(result)
  418. class BzyDeviceDetailView(APIView):
  419. authentication_classes = [APIAuthentication]
  420. permission_classes = [BzyDeviceDetailPermission]
  421. throttle_classes = [DeviceDetailRateThrottle]
  422. def get(self, request, *args, **kwargs):
  423. serializer = DeviceDetailSerializer(data=request.query_params)
  424. serializer.is_valid(raise_exception=True)
  425. request_data = serializer.validated_data
  426. uid = request.user
  427. bzy_dict_cache = default_cache.get(str(uid)+"_bzy_list")
  428. if bzy_dict_cache:
  429. d_id = bzy_dict_cache[request_data["device_id"]]
  430. else:
  431. """避免缓存失效"""
  432. wheres = {
  433. 'device_type_id':7,
  434. '$or': [
  435. {'owner_uid': uid},
  436. {'user_dealer': uid}
  437. ]
  438. }
  439. project = {
  440. 'id': '$id',
  441. 'device_id': '$device_id'
  442. }
  443. m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
  444. data = m.find_many(wheres=wheres, options=project)
  445. bzy_dict_cache = []
  446. for item in data:
  447. bzy_dict_cache[item["device_id"]]=item["id"]
  448. default_cache.set(str(uid)+"_bzy_list", bzy_dict_cache,60*5)
  449. d_id = bzy_dict_cache[request_data["device_id"]]
  450. result = {"total_counts":0,"items":[]}
  451. data_m = MongoDBTools(db_name='smartfarming', table_name='sa_device_bzy_data')
  452. data_project = {
  453. "addtime": "$addtime",
  454. "device_data": "$device_data"
  455. }
  456. if request_data.get("start_timestamp") == 0:
  457. data_wheres = {
  458. "device_id": d_id
  459. }
  460. else:
  461. data_wheres = {
  462. "device_id": d_id,
  463. "addtime": {
  464. "$gt": request_data["start_timestamp"],
  465. "$lte": request_data["end_timestamp"]
  466. }
  467. }
  468. data = data_m.find_many(wheres=data_wheres,options=data_project,skip=request_data["page_start"],limit=request_data["page_size"])
  469. total_counts = data.count()
  470. result["total_counts"] = total_counts
  471. for item in data:
  472. try:
  473. device_data = json.loads(item["device_data"])
  474. except:
  475. device_data = ast.literal_eval(item["device_data"])
  476. result["items"].append({"uptime":item["addtime"],"item_data":device_data})
  477. return Response(result)
  478. class BzyDevicePhotoView(APIView):
  479. authentication_classes = [APIAuthentication]
  480. permission_classes = [BzyDeviceDetailPermission]
  481. throttle_classes = [DevicePhotoRateThrottle]
  482. def get(self, request, *args, **kwargs):
  483. serializer = DeviceDetailSerializer(data=request.query_params)
  484. serializer.is_valid(raise_exception=True)
  485. request_data = serializer.validated_data
  486. uid = request.user
  487. bzy_dict_cache = default_cache.get(str(uid)+"_bzy_list")
  488. if bzy_dict_cache:
  489. d_id = bzy_dict_cache[request_data["device_id"]]
  490. else:
  491. """避免缓存失效"""
  492. wheres = {
  493. 'device_type_id':7,
  494. '$or': [
  495. {'owner_uid': uid},
  496. {'user_dealer': uid}
  497. ]
  498. }
  499. project = {
  500. 'id': '$id',
  501. 'device_id': '$device_id'
  502. }
  503. m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
  504. data = m.find_many(wheres=wheres, options=project)
  505. bzy_dict_cache = []
  506. for item in data:
  507. bzy_dict_cache[item["device_id"]]=item["id"]
  508. default_cache.set(str(uid)+"_bzy_list", bzy_dict_cache,60*5)
  509. d_id = bzy_dict_cache[request_data["device_id"]]
  510. result = {"total_counts":0,"items":[]}
  511. data_m = MongoDBTools(db_name='smartfarming', table_name='sa_device_bzyphoto')
  512. data_project = {
  513. "addtime": "$addtime",
  514. "addr": "$addr"
  515. }
  516. if request_data.get("start_timestamp") == 0:
  517. data_wheres = {
  518. "device_id": str(d_id)
  519. }
  520. else:
  521. data_wheres = {
  522. "device_id": str(d_id),
  523. "addtime": {
  524. "$gt": request_data["start_timestamp"],
  525. "$lte": request_data["end_timestamp"]
  526. }
  527. }
  528. data = data_m.find_many(wheres=data_wheres,options=data_project,skip=request_data["page_start"],limit=request_data["page_size"])
  529. total_counts = data.count()
  530. result["total_counts"] = total_counts
  531. for item in data:
  532. if item["addr"].startswith("http"):
  533. Image = item["addr"]
  534. elif item["addr"].startswith("/"):
  535. Image = settings.CONFIG["image_url"]["bzy_img_forward"] + item["addr"]
  536. else:
  537. Image = settings.CONFIG["image_url"]["bzy_img_forward"] + "/" + item["addr"]
  538. result["items"].append({"uptime":item["addtime"],"Image":Image})
  539. return Response(result)
  540. class XycbDeviceListView(APIView):
  541. authentication_classes = [APIAuthentication]
  542. throttle_classes = [XycbDeviceListRateThrottle]
  543. def get(self, request, *args, **kwargs):
  544. """获取性诱设备列表接口"""
  545. uid = request.user
  546. wheres = {
  547. "device_type_id":4,
  548. '$or': [
  549. {'owner_uid': uid},
  550. {'user_dealer': uid}
  551. ]
  552. }
  553. project = {
  554. 'id': '$id',
  555. 'device_id': '$device_id',
  556. 'uptime': '$uptime',
  557. 'device_status': '$device_status',
  558. 'lng': '$lng',
  559. 'lat': '$lat'
  560. }
  561. m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
  562. data = m.find_many(wheres=wheres, options=project)
  563. if data:
  564. total_counts = data.count()
  565. else:
  566. total_counts = 0
  567. result = {"total_counts":total_counts,"items":[]}
  568. xycb_dict_cache = {}
  569. for item in data:
  570. d_id = item.pop("id")
  571. result["items"].append(item)
  572. xycb_dict_cache[item["device_id"]] = d_id
  573. default_cache.set(str(uid)+"_xycb_list", xycb_dict_cache,60*5)
  574. return Response(result)
  575. class XycbDeviceDetailView(APIView):
  576. authentication_classes = [APIAuthentication]
  577. permission_classes = [XycbDeviceDetailPermission]
  578. throttle_classes = [DeviceDetailRateThrottle]
  579. def get(self, request, *args, **kwargs):
  580. serializer = DeviceDetailSerializer(data=request.query_params)
  581. serializer.is_valid(raise_exception=True)
  582. request_data = serializer.validated_data
  583. uid = request.user
  584. xycb_dict_cache = default_cache.get(str(uid)+"_xycb_list")
  585. if xycb_dict_cache:
  586. d_id = xycb_dict_cache[request_data["device_id"]]
  587. else:
  588. """避免缓存失效"""
  589. wheres = {
  590. 'device_type_id':4,
  591. '$or': [
  592. {'owner_uid': uid},
  593. {'user_dealer': uid}
  594. ]
  595. }
  596. project = {
  597. 'id': '$id',
  598. 'device_id': '$device_id'
  599. }
  600. m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
  601. data = m.find_many(wheres=wheres, options=project)
  602. xycb_dict_cache = []
  603. for item in data:
  604. xycb_dict_cache[item["device_id"]]=item["id"]
  605. default_cache.set(str(uid)+"_scd_list", xycb_dict_cache,60*5)
  606. d_id = xycb_dict_cache[request_data["device_id"]]
  607. result = {"total_counts":0,"items":[]}
  608. data_m = MongoDBTools(db_name='smartfarming', table_name='sa_device_xycb_data')
  609. data_project = {
  610. "addtime": "$addtime",
  611. "device_data": "$device_data"
  612. }
  613. if request_data.get("start_timestamp") == 0:
  614. data_wheres = {
  615. "device_id": d_id
  616. }
  617. else:
  618. data_wheres = {
  619. "device_id": d_id,
  620. "addtime": {
  621. "$gt": request_data["start_timestamp"],
  622. "$lte": request_data["end_timestamp"]
  623. }
  624. }
  625. data = data_m.find_many(wheres=data_wheres,options=data_project,skip=request_data["page_start"],limit=request_data["page_size"])
  626. total_counts = data.count()
  627. result["total_counts"] = total_counts
  628. for item in data:
  629. try:
  630. device_data = json.loads(item["device_data"])
  631. except:
  632. device_data = ast.literal_eval(item["device_data"])
  633. result["items"].append({"uptime":item["addtime"],"item_data":device_data})
  634. return Response(result)
  635. class XctDeviceListView(APIView):
  636. authentication_classes = [APIAuthentication]
  637. throttle_classes = [XctDeviceListRateThrottle]
  638. def get(self, request, *args, **kwargs):
  639. """获取吸虫塔设备列表接口"""
  640. uid = request.user
  641. wheres = {
  642. "device_type_id":12,
  643. '$or': [
  644. {'owner_uid': uid},
  645. {'user_dealer': uid}
  646. ]
  647. }
  648. project = {
  649. 'id': '$id',
  650. 'device_id': '$device_id',
  651. 'uptime': '$uptime',
  652. 'device_status': '$device_status',
  653. 'lng': '$lng',
  654. 'lat': '$lat'
  655. }
  656. m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
  657. data = m.find_many(wheres=wheres, options=project)
  658. if data:
  659. total_counts = data.count()
  660. else:
  661. total_counts = 0
  662. result = {"total_counts":total_counts,"items":[]}
  663. xct_dict_cache = {}
  664. for item in data:
  665. d_id = item.pop("id")
  666. result["items"].append(item)
  667. xct_dict_cache[item["device_id"]] = d_id
  668. default_cache.set(str(uid)+"_xct_list", xct_dict_cache,60*5)
  669. return Response(result)
  670. class XctDeviceDetailView(APIView):
  671. authentication_classes = [APIAuthentication]
  672. permission_classes = [XctDeviceDetailPermission]
  673. throttle_classes = [DeviceDetailRateThrottle]
  674. def get(self, request, *args, **kwargs):
  675. serializer = DeviceDetailSerializer(data=request.query_params)
  676. serializer.is_valid(raise_exception=True)
  677. request_data = serializer.validated_data
  678. uid = request.user
  679. xct_dict_cache = default_cache.get(str(uid)+"_xct_list")
  680. if xct_dict_cache:
  681. d_id = xct_dict_cache[request_data["device_id"]]
  682. else:
  683. """避免缓存失效"""
  684. wheres = {
  685. 'device_type_id':12,
  686. '$or': [
  687. {'owner_uid': uid},
  688. {'user_dealer': uid}
  689. ]
  690. }
  691. project = {
  692. 'id': '$id',
  693. 'device_id': '$device_id'
  694. }
  695. m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
  696. data = m.find_many(wheres=wheres, options=project)
  697. xct_dict_cache = []
  698. for item in data:
  699. xct_dict_cache[item["device_id"]]=item["id"]
  700. default_cache.set(str(uid)+"_xct_list", xct_dict_cache,60*5)
  701. d_id = xct_dict_cache[request_data["device_id"]]
  702. result = {"total_counts":0,"items":[]}
  703. data_m = MongoDBTools(db_name='smartfarming', table_name='sa_device_xct_data')
  704. data_project = {
  705. "addtime": "$addtime",
  706. "device_data": "$device_data"
  707. }
  708. if request_data.get("start_timestamp") == 0:
  709. data_wheres = {
  710. "device_id": d_id
  711. }
  712. else:
  713. data_wheres = {
  714. "device_id": d_id,
  715. "addtime": {
  716. "$gt": request_data["start_timestamp"],
  717. "$lte": request_data["end_timestamp"]
  718. }
  719. }
  720. data = data_m.find_many(wheres=data_wheres,options=data_project,skip=request_data["page_start"],limit=request_data["page_size"])
  721. total_counts = data.count()
  722. result["total_counts"] = total_counts
  723. for item in data:
  724. try:
  725. device_data = json.loads(item["device_data"])
  726. except:
  727. device_data = ast.literal_eval(item["device_data"])
  728. result["items"].append({"uptime":item["addtime"],"item_data":device_data})
  729. return Response(result)
  730. class XctDevicePhotoView(APIView):
  731. authentication_classes = [APIAuthentication]
  732. permission_classes = [XctDeviceDetailPermission]
  733. throttle_classes = [DevicePhotoRateThrottle]
  734. def get(self, request, *args, **kwargs):
  735. serializer = DeviceDetailSerializer(data=request.query_params)
  736. serializer.is_valid(raise_exception=True)
  737. request_data = serializer.validated_data
  738. uid = request.user
  739. xct_dict_cache = default_cache.get(str(uid)+"_xct_list")
  740. if xct_dict_cache:
  741. d_id = xct_dict_cache[request_data["device_id"]]
  742. else:
  743. """避免缓存失效"""
  744. wheres = {
  745. 'device_type_id':12,
  746. '$or': [
  747. {'owner_uid': uid},
  748. {'user_dealer': uid}
  749. ]
  750. }
  751. project = {
  752. 'id': '$id',
  753. 'device_id': '$device_id'
  754. }
  755. m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
  756. data = m.find_many(wheres=wheres, options=project)
  757. xct_dict_cache = []
  758. for item in data:
  759. xct_dict_cache[item["device_id"]]=item["id"]
  760. default_cache.set(str(uid)+"_xct_list", xct_dict_cache,60*5)
  761. d_id = xct_dict_cache[request_data["device_id"]]
  762. result = {"total_counts":0,"itmes":[]}
  763. data_m = MongoDBTools(db_name='smartfarming', table_name='sa_device_xct_photo')
  764. data_project = {
  765. "addtime": "$addtime",
  766. "addr": "$addr",
  767. "indentify_photo":"$indentify_photo",
  768. "indentify_result":"$indentify_result"
  769. }
  770. if request_data.get("start_timestamp") == 0:
  771. data_wheres = {
  772. "device_id": str(d_id)
  773. }
  774. else:
  775. data_wheres = {
  776. "device_id": str(d_id),
  777. "addtime": {
  778. "$gt": request_data["start_timestamp"],
  779. "$lte": request_data["end_timestamp"]
  780. }
  781. }
  782. data = data_m.find_many(wheres=data_wheres,options=data_project,skip=request_data["page_start"],limit=request_data["page_size"])
  783. total_counts = data.count()
  784. result["total_counts"] = total_counts
  785. for item in data:
  786. if item["addr"].startswith("http"):
  787. Image = item["addr"]
  788. elif item["addr"].startswith("/"):
  789. Image = settings.CONFIG["image_url"]["xct_img_forward"] + item["addr"]
  790. else:
  791. Image = settings.CONFIG["image_url"]["xct_img_forward"] + "/" + item["addr"]
  792. Result = item["indentify_result"] if item["indentify_result"] else "0"
  793. indentify_photo = item["indentify_photo"]
  794. if indentify_photo and indentify_photo!="0":
  795. if indentify_photo == "0":
  796. Result_image = "0"
  797. else:
  798. if indentify_photo.startswith("http"):
  799. Result_image = indentify_photo
  800. elif indentify_photo.startswith("/"):
  801. Result_image = settings.CONFIG["image_url"]["xct_img_result_forward"] + indentify_photo
  802. else:
  803. Result_image = settings.CONFIG["image_url"]["xct_img_result_forward"] + "/" + indentify_photo
  804. else:
  805. Result_image = "0"
  806. result["itmes"].append({"uptime":item["addtime"],"Image":Image,"Result_image":Result_image,"Result":Result})
  807. return Response(result)
  808. class GssqDeviceListView(APIView):
  809. authentication_classes = [APIAuthentication]
  810. throttle_classes = [GssqDeviceListRateThrottle]
  811. def get(self, request, *args, **kwargs):
  812. """获取管式墒情列表接口"""
  813. uid = request.user
  814. wheres = {
  815. "device_type_id":15,
  816. '$or': [
  817. {'owner_uid': uid},
  818. {'user_dealer': uid}
  819. ]
  820. }
  821. project = {
  822. 'id': '$id',
  823. 'device_id': '$device_id',
  824. 'uptime': '$uptime',
  825. 'device_status': '$device_status',
  826. 'lng': '$lng',
  827. 'lat': '$lat'
  828. }
  829. m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
  830. data = m.find_many(wheres=wheres, options=project)
  831. if data:
  832. total_counts = data.count()
  833. else:
  834. total_counts = 0
  835. result = {"total_counts":total_counts,"items":[]}
  836. gssq_dict_cache = {}
  837. for item in data:
  838. d_id = item.pop("id")
  839. result["items"].append(item)
  840. gssq_dict_cache[item["device_id"]] = d_id
  841. default_cache.set(str(uid)+"_gssq_list", gssq_dict_cache,60*5)
  842. return Response(result)
  843. class GssqDeviceDetailView(APIView):
  844. authentication_classes = [APIAuthentication]
  845. permission_classes = [GssqDeviceDetailPermission]
  846. throttle_classes = [DeviceDetailRateThrottle]
  847. def get(self, request, *args, **kwargs):
  848. serializer = DeviceDetailSerializer(data=request.query_params)
  849. serializer.is_valid(raise_exception=True)
  850. request_data = serializer.validated_data
  851. uid = request.user
  852. gssq_dict_cache = default_cache.get(str(uid)+"_gssq_list")
  853. if gssq_dict_cache:
  854. d_id = gssq_dict_cache[request_data["device_id"]]
  855. else:
  856. """避免缓存失效"""
  857. wheres = {
  858. 'device_type_id':15,
  859. '$or': [
  860. {'owner_uid': uid},
  861. {'user_dealer': uid}
  862. ]
  863. }
  864. project = {
  865. 'id': '$id',
  866. 'device_id': '$device_id'
  867. }
  868. m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
  869. data = m.find_many(wheres=wheres, options=project)
  870. gssq_dict_cache = []
  871. for item in data:
  872. gssq_dict_cache[item["device_id"]]=item["id"]
  873. default_cache.set(str(uid)+"_gssq_list", gssq_dict_cache,60*5)
  874. d_id = gssq_dict_cache[request_data["device_id"]]
  875. result = {"total_counts":0,"items":[]}
  876. data_m = MongoDBTools(db_name='smartfarming', table_name='sa_nd_qxz_data')
  877. data_project = {
  878. "upl_time": "$upl_time",
  879. "id" : "$id",
  880. "at" : "$at",
  881. "atm" : "$atm",
  882. "ats" : "$ats",
  883. "csq" : "$csq",
  884. "device_id" : "$device_id",
  885. "swc" : "$swc",
  886. "temp" : "$temp"
  887. }
  888. if request_data.get("start_timestamp") == 0:
  889. data_wheres = {
  890. "device_id": request_data["device_id"]
  891. }
  892. else:
  893. data_wheres = {
  894. "device_id": request_data["device_id"],
  895. "upl_time": {
  896. "$gt": request_data["start_timestamp"],
  897. "$lte": request_data["end_timestamp"]
  898. }
  899. }
  900. data = data_m.find_many(wheres=data_wheres,options=data_project,skip=request_data["page_start"],limit=request_data["page_size"])
  901. total_counts = data.count()
  902. result["total_counts"] = total_counts
  903. for item in data:
  904. try:
  905. device_data = item
  906. except:
  907. device_data = ast.literal_eval(item)
  908. result["items"].append({"uptime":item["upl_time"],"item_data":device_data})
  909. return Response(result)