views.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  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
  10. from utils.MyRateThrottle import DeviceDetailRateThrottle, DevicePhotoRateThrottle, QxzDeviceListRateThrottle, ScdDeviceListRateThrottle, CbdDeviceListRateThrottle, BzyDeviceListRateThrottle, XycbDeviceListRateThrottle, XctDeviceListRateThrottle
  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)