views.py 34 KB

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