views.py 37 KB

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