views.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  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. start_time, device_id = request_data["start_timestamp"], request_data["device_id"]
  66. conf_wheres = {
  67. "device_id":device_id,
  68. }
  69. conf_m = MongoDBTools(db_name='smartfarming', table_name='sa_qxz_conf')
  70. conf_data = conf_m.find_one(wheres=conf_wheres)
  71. if conf_data:
  72. conf_data.pop("id")
  73. conf_data.pop("device_id")
  74. conf_data.pop("uptime")
  75. conf_data = dict(sorted(conf_data.items(), key=lambda e:int(e[0].split("e")[1])))
  76. result = {"conf":conf_data,"items":[]}
  77. data_m = MongoDBTools(db_name='smartfarming', table_name='sa_qxz_data')
  78. data_wheres = {
  79. "device_id": device_id,
  80. "uptime": {"$gt":start_time}
  81. }
  82. data = data_m.find_many(wheres=data_wheres)
  83. for item in data:
  84. item.pop("id")
  85. item.pop("device_id")
  86. uptime = item.pop("uptime")
  87. item = dict(sorted(item.items(), key=lambda e:int(e[0].split("e")[1])))
  88. item["uptime"] = uptime
  89. result["items"].append(item)
  90. return Response(result)
  91. class ScdDeviceListView(APIView):
  92. authentication_classes = [APIAuthentication]
  93. throttle_classes = [ScdDeviceListRateThrottle]
  94. def get(self, request, *args, **kwargs):
  95. """获取杀虫灯设备列表接口"""
  96. uid = request.user
  97. wheres = {
  98. "device_type_id":2,
  99. '$or': [
  100. {'owner_uid': uid},
  101. {'user_dealer': uid}
  102. ]
  103. }
  104. project = {
  105. 'id': '$id',
  106. 'device_id': '$device_id',
  107. 'uptime': '$uptime',
  108. 'device_status': '$device_status',
  109. 'lng': '$lng',
  110. 'lat': '$lat'
  111. }
  112. m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
  113. data = m.find_many(wheres=wheres, options=project)
  114. if data:
  115. total_counts = data.count()
  116. else:
  117. total_counts = 0
  118. result = {"total_counts":total_counts,"items":[]}
  119. scd_dict_cache = {}
  120. for item in data:
  121. d_id = item.pop("id")
  122. result["items"].append(item)
  123. scd_dict_cache[item["device_id"]] = d_id
  124. default_cache.set(str(uid)+"_scd_list", scd_dict_cache,60*5)
  125. return Response(result)
  126. class ScdDeviceDetailView(APIView):
  127. authentication_classes = [APIAuthentication]
  128. permission_classes = [ScdDeviceDetailPermission]
  129. throttle_classes = [DeviceDetailRateThrottle]
  130. def get(self, request, *args, **kwargs):
  131. serializer = DeviceDetailSerializer(data=request.query_params)
  132. serializer.is_valid(raise_exception=True)
  133. request_data = serializer.validated_data
  134. start_time, device_id = request_data["start_timestamp"], request_data["device_id"]
  135. uid = request.user
  136. scd_dict_cache = default_cache.get(str(uid)+"_scd_list")
  137. if scd_dict_cache:
  138. d_id = scd_dict_cache[device_id]
  139. else:
  140. """避免缓存失效"""
  141. wheres = {
  142. 'device_type_id':2,
  143. '$or': [
  144. {'owner_uid': uid},
  145. {'user_dealer': uid}
  146. ]
  147. }
  148. project = {
  149. 'id': '$id',
  150. 'device_id': '$device_id'
  151. }
  152. m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
  153. data = m.find_many(wheres=wheres, options=project)
  154. scd_dict_cache = []
  155. for item in data:
  156. scd_dict_cache[item["device_id"]]=item["id"]
  157. default_cache.set(str(uid)+"_scd_list", scd_dict_cache,60*5)
  158. d_id = scd_dict_cache[device_id]
  159. result = []
  160. data_m = MongoDBTools(db_name='smartfarming', table_name='sa_device_scd_data')
  161. data_wheres = {
  162. "device_id": d_id,
  163. "addtime": {"$gt":start_time}
  164. }
  165. data_project = {
  166. "addtime": "$addtime",
  167. "device_data": "$device_data"
  168. }
  169. data = data_m.find_many(wheres=data_wheres,options=data_project)
  170. for item in data:
  171. try:
  172. device_data = json.loads(item["device_data"])
  173. except:
  174. device_data = ast.literal_eval(item["device_data"])
  175. result.append({"uptime":item["addtime"],"item_data":device_data})
  176. return Response(result)
  177. class CbdDeviceListView(APIView):
  178. authentication_classes = [APIAuthentication]
  179. throttle_classes = [CbdDeviceListRateThrottle]
  180. def get(self, request, *args, **kwargs):
  181. """获取测报灯设备列表接口"""
  182. uid = request.user
  183. wheres = {
  184. "device_type_id":3,
  185. '$or': [
  186. {'owner_uid': uid},
  187. {'user_dealer': uid}
  188. ]
  189. }
  190. project = {
  191. 'id': '$id',
  192. 'device_id': '$device_id',
  193. 'disable': '$disable',
  194. 'uptime': '$uptime',
  195. 'device_status': '$device_status',
  196. 'lng': '$lng',
  197. 'lat': '$lat'
  198. }
  199. m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
  200. data = m.find_many(wheres=wheres, options=project)
  201. if data:
  202. total_counts = data.count()
  203. else:
  204. total_counts = 0
  205. result = {"total_counts":total_counts,"items":[]}
  206. cbd_dict_cache = {}
  207. for item in data:
  208. d_id = item.pop("id")
  209. disable = item.pop("disable")
  210. result["items"].append(item)
  211. cbd_dict_cache[item["device_id"]] = {"d_id":d_id,"disable":disable}
  212. default_cache.set(str(uid)+"_cbd_list", cbd_dict_cache,60*5)
  213. return Response(result)
  214. class CbdDeviceDetailView(APIView):
  215. authentication_classes = [APIAuthentication]
  216. permission_classes = [CbdDeviceDetailPermission]
  217. throttle_classes = [DeviceDetailRateThrottle]
  218. def get(self, request, *args, **kwargs):
  219. serializer = DeviceDetailSerializer(data=request.query_params)
  220. serializer.is_valid(raise_exception=True)
  221. request_data = serializer.validated_data
  222. start_time, device_id = request_data["start_timestamp"], request_data["device_id"]
  223. uid = request.user
  224. cbd_dict_cache = default_cache.get(str(uid)+"_cbd_list")
  225. if cbd_dict_cache:
  226. d_id = cbd_dict_cache[device_id]["d_id"]
  227. else:
  228. """避免缓存失效"""
  229. wheres = {
  230. 'device_type_id':3,
  231. '$or': [
  232. {'owner_uid': uid},
  233. {'user_dealer': uid}
  234. ]
  235. }
  236. project = {
  237. 'id': '$id',
  238. 'device_id': '$device_id',
  239. 'disable': '$disable'
  240. }
  241. m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
  242. data = m.find_many(wheres=wheres, options=project)
  243. cbd_dict_cache = {}
  244. for item in data:
  245. cbd_dict_cache[item["device_id"]]={"d_id":item["id"],"disable":item["disable"]}
  246. default_cache.set(str(uid)+"_cbd_list", cbd_dict_cache,60*5)
  247. d_id = cbd_dict_cache[device_id]["d_id"]
  248. result = []
  249. data_m = MongoDBTools(db_name='smartfarming', table_name='sa_device_cbd_data')
  250. data_wheres = {
  251. "device_id": d_id,
  252. "addtime": {"$gt":start_time}
  253. }
  254. data_project = {
  255. "addtime": "$addtime",
  256. "device_data": "$device_data"
  257. }
  258. data = data_m.find_many(wheres=data_wheres,options=data_project)
  259. for item in data:
  260. try:
  261. device_data = json.loads(item["device_data"])
  262. except:
  263. device_data = ast.literal_eval(item["device_data"])
  264. result.append({"uptime":item["addtime"],"item_data":device_data})
  265. return Response(result)
  266. class CbdDevicePhotoView(APIView):
  267. authentication_classes = [APIAuthentication]
  268. permission_classes = [CbdDeviceDetailPermission]
  269. throttle_classes = [DevicePhotoRateThrottle]
  270. def get(self, request, *args, **kwargs):
  271. serializer = DeviceDetailSerializer(data=request.query_params)
  272. serializer.is_valid(raise_exception=True)
  273. request_data = serializer.validated_data
  274. start_time, device_id = request_data["start_timestamp"], request_data["device_id"]
  275. uid = request.user
  276. cbd_dict_cache = default_cache.get(str(uid)+"_cbd_list")
  277. if cbd_dict_cache:
  278. d_id = cbd_dict_cache[device_id]["d_id"]
  279. disable = cbd_dict_cache[device_id]["disable"]
  280. else:
  281. """避免缓存失效"""
  282. wheres = {
  283. 'device_type_id':3,
  284. '$or': [
  285. {'owner_uid': uid},
  286. {'user_dealer': uid}
  287. ]
  288. }
  289. project = {
  290. 'id': '$id',
  291. 'device_id': '$device_id',
  292. 'disable': '$disable'
  293. }
  294. m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
  295. data = m.find_many(wheres=wheres, options=project)
  296. cbd_dict_cache = {}
  297. for item in data:
  298. cbd_dict_cache[item["device_id"]]={"d_id":item["id"],"disable":item["disable"]}
  299. default_cache.set(str(uid)+"_cbd_list", cbd_dict_cache,60*5)
  300. d_id = cbd_dict_cache[device_id]["d_id"]
  301. disable = cbd_dict_cache[device_id]["disable"]
  302. result = []
  303. print(d_id,type(d_id))
  304. print(start_time,type(start_time))
  305. data_m = MongoDBTools(db_name='smartfarming', table_name='sa_device_cbd_photo')
  306. data_wheres = {
  307. "device_id": str(d_id),
  308. "addtime": {"$gt":start_time}
  309. }
  310. if disable == 1:
  311. data_project = {
  312. "addtime": "$addtime",
  313. "addr": "$addr",
  314. "indentify_photo":"$indentify_photo",
  315. "indentify_result":"$indentify_result"
  316. }
  317. else:
  318. data_project = {
  319. "addtime": "$addtime",
  320. "addr": "$addr"
  321. }
  322. data = data_m.find_many(wheres=data_wheres,options=data_project)
  323. for item in data:
  324. item_data = {}
  325. addr = item["addr"]
  326. if addr.startswith("http"):
  327. Image = addr
  328. elif addr.startswith("/"):
  329. Image = settings.CONFIG["image_url"]["image_forward"] + addr
  330. else:
  331. Image = settings.CONFIG["image_url"]["image_forward"] + "/" +addr
  332. item_data["uptime"] = item["addtime"]
  333. item_data["Image"] = Image
  334. if disable == 1:
  335. Result = item["indentify_result"] if item["indentify_result"] else "0"
  336. indentify_photo = item["indentify_photo"]
  337. if indentify_photo:
  338. if indentify_photo.startswith("http"):
  339. Result_image = indentify_photo
  340. elif indentify_photo.startswith("/"):
  341. Result_image = settings.CONFIG["image_url"]["result_image_forward"] + indentify_photo
  342. else:
  343. Result_image = settings.CONFIG["image_url"]["result_image_forward"] + "/" + indentify_photo
  344. else:
  345. Result_image = "0"
  346. item_data["Result_image"] = Result_image
  347. item_data["Result"] = Result
  348. result.append(item_data)
  349. return Response(result)
  350. class BzyDeviceListView(APIView):
  351. authentication_classes = [APIAuthentication]
  352. throttle_classes = [BzyDeviceListRateThrottle]
  353. def get(self, request, *args, **kwargs):
  354. """获取孢子仪设备列表接口"""
  355. uid = request.user
  356. wheres = {
  357. "device_type_id":7,
  358. '$or': [
  359. {'owner_uid': uid},
  360. {'user_dealer': uid}
  361. ]
  362. }
  363. project = {
  364. 'id': '$id',
  365. 'device_id': '$device_id',
  366. 'uptime': '$uptime',
  367. 'device_status': '$device_status',
  368. 'lng': '$lng',
  369. 'lat': '$lat'
  370. }
  371. m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
  372. data = m.find_many(wheres=wheres, options=project)
  373. if data:
  374. total_counts = data.count()
  375. else:
  376. total_counts = 0
  377. result = {"total_counts":total_counts,"items":[]}
  378. bzy_dict_cache = {}
  379. for item in data:
  380. d_id = item.pop("id")
  381. result["items"].append(item)
  382. bzy_dict_cache[item["device_id"]] = d_id
  383. default_cache.set(str(uid)+"_bzy_list", bzy_dict_cache,60*5)
  384. return Response(result)
  385. class BzyDeviceDetailView(APIView):
  386. authentication_classes = [APIAuthentication]
  387. permission_classes = [BzyDeviceDetailPermission]
  388. throttle_classes = [DeviceDetailRateThrottle]
  389. def get(self, request, *args, **kwargs):
  390. serializer = DeviceDetailSerializer(data=request.query_params)
  391. serializer.is_valid(raise_exception=True)
  392. request_data = serializer.validated_data
  393. start_time, device_id = request_data["start_timestamp"], request_data["device_id"]
  394. uid = request.user
  395. bzy_dict_cache = default_cache.get(str(uid)+"_bzy_list")
  396. if bzy_dict_cache:
  397. d_id = bzy_dict_cache[device_id]
  398. else:
  399. """避免缓存失效"""
  400. wheres = {
  401. 'device_type_id':7,
  402. '$or': [
  403. {'owner_uid': uid},
  404. {'user_dealer': uid}
  405. ]
  406. }
  407. project = {
  408. 'id': '$id',
  409. 'device_id': '$device_id'
  410. }
  411. m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
  412. data = m.find_many(wheres=wheres, options=project)
  413. bzy_dict_cache = []
  414. for item in data:
  415. bzy_dict_cache[item["device_id"]]=item["id"]
  416. default_cache.set(str(uid)+"_bzy_list", bzy_dict_cache,60*5)
  417. d_id = bzy_dict_cache[device_id]
  418. result = []
  419. data_m = MongoDBTools(db_name='smartfarming', table_name='sa_device_bzy_data')
  420. data_wheres = {
  421. "device_id": d_id,
  422. "addtime": {"$gt":start_time}
  423. }
  424. data_project = {
  425. "addtime": "$addtime",
  426. "device_data": "$device_data"
  427. }
  428. data = data_m.find_many(wheres=data_wheres,options=data_project)
  429. for item in data:
  430. try:
  431. device_data = json.loads(item["device_data"])
  432. except:
  433. device_data = ast.literal_eval(item["device_data"])
  434. result.append({"uptime":item["addtime"],"item_data":device_data})
  435. return Response(result)
  436. class BzyDevicePhotoView(APIView):
  437. authentication_classes = [APIAuthentication]
  438. permission_classes = [BzyDeviceDetailPermission]
  439. throttle_classes = [DevicePhotoRateThrottle]
  440. def get(self, request, *args, **kwargs):
  441. serializer = DeviceDetailSerializer(data=request.query_params)
  442. serializer.is_valid(raise_exception=True)
  443. request_data = serializer.validated_data
  444. start_time, device_id = request_data["start_timestamp"], request_data["device_id"]
  445. uid = request.user
  446. bzy_dict_cache = default_cache.get(str(uid)+"_bzy_list")
  447. if bzy_dict_cache:
  448. d_id = bzy_dict_cache[device_id]
  449. else:
  450. """避免缓存失效"""
  451. wheres = {
  452. 'device_type_id':7,
  453. '$or': [
  454. {'owner_uid': uid},
  455. {'user_dealer': uid}
  456. ]
  457. }
  458. project = {
  459. 'id': '$id',
  460. 'device_id': '$device_id'
  461. }
  462. m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
  463. data = m.find_many(wheres=wheres, options=project)
  464. bzy_dict_cache = []
  465. for item in data:
  466. bzy_dict_cache[item["device_id"]]=item["id"]
  467. default_cache.set(str(uid)+"_bzy_list", bzy_dict_cache,60*5)
  468. d_id = bzy_dict_cache[device_id]
  469. result = []
  470. data_m = MongoDBTools(db_name='smartfarming', table_name='sa_device_bzyphoto')
  471. data_wheres = {
  472. "device_id": str(d_id),
  473. "addtime": {"$gt":start_time}
  474. }
  475. data_project = {
  476. "addtime": "$addtime",
  477. "addr": "$addr"
  478. }
  479. data = data_m.find_many(wheres=data_wheres,options=data_project)
  480. for item in data:
  481. if item["addr"].startswith("http"):
  482. Image = item["addr"]
  483. elif item["addr"].startswith("/"):
  484. Image = settings.CONFIG["image_url"]["bzy_img_forward"] + item["addr"]
  485. else:
  486. Image = settings.CONFIG["image_url"]["bzy_img_forward"] + "/" + item["addr"]
  487. result.append({"uptime":item["addtime"],"Image":Image})
  488. return Response(result)
  489. class XycbDeviceListView(APIView):
  490. authentication_classes = [APIAuthentication]
  491. throttle_classes = [XycbDeviceListRateThrottle]
  492. def get(self, request, *args, **kwargs):
  493. """获取性诱设备列表接口"""
  494. uid = request.user
  495. wheres = {
  496. "device_type_id":4,
  497. '$or': [
  498. {'owner_uid': uid},
  499. {'user_dealer': uid}
  500. ]
  501. }
  502. project = {
  503. 'id': '$id',
  504. 'device_id': '$device_id',
  505. 'uptime': '$uptime',
  506. 'device_status': '$device_status',
  507. 'lng': '$lng',
  508. 'lat': '$lat'
  509. }
  510. m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
  511. data = m.find_many(wheres=wheres, options=project)
  512. if data:
  513. total_counts = data.count()
  514. else:
  515. total_counts = 0
  516. result = {"total_counts":total_counts,"items":[]}
  517. xycb_dict_cache = {}
  518. for item in data:
  519. d_id = item.pop("id")
  520. result["items"].append(item)
  521. xycb_dict_cache[item["device_id"]] = d_id
  522. default_cache.set(str(uid)+"_xycb_list", xycb_dict_cache,60*5)
  523. return Response(result)
  524. class XycbDeviceDetailView(APIView):
  525. authentication_classes = [APIAuthentication]
  526. permission_classes = [XycbDeviceDetailPermission]
  527. throttle_classes = [DeviceDetailRateThrottle]
  528. def get(self, request, *args, **kwargs):
  529. serializer = DeviceDetailSerializer(data=request.query_params)
  530. serializer.is_valid(raise_exception=True)
  531. request_data = serializer.validated_data
  532. start_time, device_id = request_data["start_timestamp"], request_data["device_id"]
  533. uid = request.user
  534. xycb_dict_cache = default_cache.get(str(uid)+"_xycb_list")
  535. if xycb_dict_cache:
  536. d_id = xycb_dict_cache[device_id]
  537. else:
  538. """避免缓存失效"""
  539. wheres = {
  540. 'device_type_id':4,
  541. '$or': [
  542. {'owner_uid': uid},
  543. {'user_dealer': uid}
  544. ]
  545. }
  546. project = {
  547. 'id': '$id',
  548. 'device_id': '$device_id'
  549. }
  550. m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
  551. data = m.find_many(wheres=wheres, options=project)
  552. xycb_dict_cache = []
  553. for item in data:
  554. xycb_dict_cache[item["device_id"]]=item["id"]
  555. default_cache.set(str(uid)+"_scd_list", xycb_dict_cache,60*5)
  556. d_id = xycb_dict_cache[device_id]
  557. result = []
  558. data_m = MongoDBTools(db_name='smartfarming', table_name='sa_device_xycb_data')
  559. data_wheres = {
  560. "device_id": d_id,
  561. "addtime": {"$gt":start_time}
  562. }
  563. data_project = {
  564. "addtime": "$addtime",
  565. "device_data": "$device_data"
  566. }
  567. data = data_m.find_many(wheres=data_wheres,options=data_project)
  568. for item in data:
  569. try:
  570. device_data = json.loads(item["device_data"])
  571. except:
  572. device_data = ast.literal_eval(item["device_data"])
  573. result.append({"uptime":item["addtime"],"item_data":device_data})
  574. return Response(result)
  575. class XctDeviceListView(APIView):
  576. authentication_classes = [APIAuthentication]
  577. throttle_classes = [XctDeviceListRateThrottle]
  578. def get(self, request, *args, **kwargs):
  579. """获取吸虫塔设备列表接口"""
  580. uid = request.user
  581. wheres = {
  582. "device_type_id":12,
  583. '$or': [
  584. {'owner_uid': uid},
  585. {'user_dealer': uid}
  586. ]
  587. }
  588. project = {
  589. 'id': '$id',
  590. 'device_id': '$device_id',
  591. 'uptime': '$uptime',
  592. 'device_status': '$device_status',
  593. 'lng': '$lng',
  594. 'lat': '$lat'
  595. }
  596. m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
  597. data = m.find_many(wheres=wheres, options=project)
  598. if data:
  599. total_counts = data.count()
  600. else:
  601. total_counts = 0
  602. result = {"total_counts":total_counts,"items":[]}
  603. xct_dict_cache = {}
  604. for item in data:
  605. d_id = item.pop("id")
  606. result["items"].append(item)
  607. xct_dict_cache[item["device_id"]] = d_id
  608. default_cache.set(str(uid)+"_xct_list", xct_dict_cache,60*5)
  609. return Response(result)
  610. class XctDeviceDetailView(APIView):
  611. authentication_classes = [APIAuthentication]
  612. permission_classes = [XctDeviceDetailPermission]
  613. throttle_classes = [DeviceDetailRateThrottle]
  614. def get(self, request, *args, **kwargs):
  615. serializer = DeviceDetailSerializer(data=request.query_params)
  616. serializer.is_valid(raise_exception=True)
  617. request_data = serializer.validated_data
  618. start_time, device_id = request_data["start_timestamp"], request_data["device_id"]
  619. uid = request.user
  620. xct_dict_cache = default_cache.get(str(uid)+"_xct_list")
  621. if xct_dict_cache:
  622. d_id = xct_dict_cache[device_id]
  623. else:
  624. """避免缓存失效"""
  625. wheres = {
  626. 'device_type_id':12,
  627. '$or': [
  628. {'owner_uid': uid},
  629. {'user_dealer': uid}
  630. ]
  631. }
  632. project = {
  633. 'id': '$id',
  634. 'device_id': '$device_id'
  635. }
  636. m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
  637. data = m.find_many(wheres=wheres, options=project)
  638. xct_dict_cache = []
  639. for item in data:
  640. xct_dict_cache[item["device_id"]]=item["id"]
  641. default_cache.set(str(uid)+"_xct_list", xct_dict_cache,60*5)
  642. d_id = xct_dict_cache[device_id]
  643. result = []
  644. data_m = MongoDBTools(db_name='smartfarming', table_name='sa_device_xct_data')
  645. data_wheres = {
  646. "device_id": d_id,
  647. "addtime": {"$gt":start_time}
  648. }
  649. data_project = {
  650. "addtime": "$addtime",
  651. "device_data": "$device_data"
  652. }
  653. data = data_m.find_many(wheres=data_wheres,options=data_project)
  654. for item in data:
  655. try:
  656. device_data = json.loads(item["device_data"])
  657. except:
  658. device_data = ast.literal_eval(item["device_data"])
  659. result.append({"uptime":item["addtime"],"item_data":device_data})
  660. return Response(result)
  661. class XctDevicePhotoView(APIView):
  662. authentication_classes = [APIAuthentication]
  663. permission_classes = [XctDeviceDetailPermission]
  664. throttle_classes = [DevicePhotoRateThrottle]
  665. def get(self, request, *args, **kwargs):
  666. serializer = DeviceDetailSerializer(data=request.query_params)
  667. serializer.is_valid(raise_exception=True)
  668. request_data = serializer.validated_data
  669. start_time, device_id = request_data["start_timestamp"], request_data["device_id"]
  670. uid = request.user
  671. xct_dict_cache = default_cache.get(str(uid)+"_xct_list")
  672. if xct_dict_cache:
  673. d_id = xct_dict_cache[device_id]
  674. else:
  675. """避免缓存失效"""
  676. wheres = {
  677. 'device_type_id':12,
  678. '$or': [
  679. {'owner_uid': uid},
  680. {'user_dealer': uid}
  681. ]
  682. }
  683. project = {
  684. 'id': '$id',
  685. 'device_id': '$device_id'
  686. }
  687. m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
  688. data = m.find_many(wheres=wheres, options=project)
  689. xct_dict_cache = []
  690. for item in data:
  691. xct_dict_cache[item["device_id"]]=item["id"]
  692. default_cache.set(str(uid)+"_xct_list", xct_dict_cache,60*5)
  693. d_id = xct_dict_cache[device_id]
  694. result = []
  695. data_m = MongoDBTools(db_name='smartfarming', table_name='sa_device_xct_photo')
  696. data_wheres = {
  697. "device_id": str(d_id),
  698. "addtime": {"$gt":start_time}
  699. }
  700. data_project = {
  701. "addtime": "$addtime",
  702. "addr": "$addr",
  703. "indentify_photo":"$indentify_photo",
  704. "indentify_result":"$indentify_result"
  705. }
  706. data = data_m.find_many(wheres=data_wheres,options=data_project)
  707. for item in data:
  708. if item["addr"].startswith("http"):
  709. Image = item["addr"]
  710. elif item["addr"].startswith("/"):
  711. Image = settings.CONFIG["image_url"]["xct_img_forward"] + item["addr"]
  712. else:
  713. Image = settings.CONFIG["image_url"]["xct_img_forward"] + "/" + item["addr"]
  714. Result = item["indentify_result"] if item["indentify_result"] else "0"
  715. indentify_photo = item["indentify_photo"]
  716. if indentify_photo:
  717. if indentify_photo.startswith("http"):
  718. Result_image = indentify_photo
  719. elif indentify_photo.startswith("/"):
  720. Result_image = settings.CONFIG["image_url"]["xct_img_result_forward"] + indentify_photo
  721. else:
  722. Result_image = settings.CONFIG["image_url"]["xct_img_result_forward"] + "/" + indentify_photo
  723. else:
  724. Result_image = "0"
  725. result.append({"uptime":item["addtime"],"Image":Image,"Result_image":Result_image,"Result":Result})
  726. return Response(result)