views.py 33 KB

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