views.py 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  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. import requests
  7. from django.conf import settings
  8. from .serializers import SearchEquipSerializer, DeviceDetailSerializer
  9. from utils.JWTAuthentication_diy import APIAuthentication
  10. from utils.permissions import QXZDeviceDetailPermission, ScdDeviceDetailPermission, CbdDeviceDetailPermission, BzyDeviceDetailPermission, XycbDeviceDetailPermission, XctDeviceDetailPermission, GssqDeviceDetailPermission, JkDeviceDetailPermission
  11. from utils.MyRateThrottle import DeviceDetailRateThrottle, DevicePhotoRateThrottle, QxzDeviceListRateThrottle, ScdDeviceListRateThrottle, CbdDeviceListRateThrottle, BzyDeviceListRateThrottle, XycbDeviceListRateThrottle, XctDeviceListRateThrottle, GssqDeviceListRateThrottle
  12. from utils.utils import DeviceInfoUtils
  13. from utils.db_utils import MongoDBTools
  14. # Create your views here.
  15. class SearchEquip(APIView):
  16. def post(self, request):
  17. serializer = SearchEquipSerializer(data=request.data)
  18. serializer.is_valid(raise_exception=True)
  19. request_data = serializer.validated_data
  20. data = DeviceInfoUtils().get_equip_list(
  21. d_id=request_data.get("device_id"),
  22. isfullId=request_data.get("isfullId")
  23. )
  24. return Response(data)
  25. class QxzDeviceListView(APIView):
  26. authentication_classes = [APIAuthentication]
  27. throttle_classes = [QxzDeviceListRateThrottle]
  28. def get(self, request, *args, **kwargs):
  29. """获取气象站设备列表接口"""
  30. uid = request.user
  31. wheres = {
  32. "device_type_id":5,
  33. '$or': [
  34. {'owner_uid': uid},
  35. {'user_dealer': uid}
  36. ]
  37. }
  38. project = {
  39. 'device_id': '$device_id',
  40. 'uptime': '$uptime',
  41. 'device_status': '$device_status',
  42. 'lng': '$lng',
  43. 'lat': '$lat'
  44. }
  45. m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
  46. data = m.find_many(wheres=wheres, options=project)
  47. if data:
  48. total_counts = data.count()
  49. else:
  50. total_counts = 0
  51. result = {"total_counts":total_counts,"items":[]}
  52. qxz_list_cache = []
  53. for item in data:
  54. result["items"].append(item)
  55. qxz_list_cache.append(item["device_id"])
  56. default_cache.set(str(uid)+"_qxz_list", qxz_list_cache,60*5)
  57. return Response(result)
  58. class QxzDeviceDetailView(APIView):
  59. authentication_classes = [APIAuthentication]
  60. permission_classes = [QXZDeviceDetailPermission]
  61. throttle_classes = [DeviceDetailRateThrottle]
  62. def get(self, request, *args, **kwargs):
  63. serializer = DeviceDetailSerializer(data=request.query_params)
  64. serializer.is_valid(raise_exception=True)
  65. request_data = serializer.validated_data
  66. conf_wheres = {
  67. "device_id":request_data["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_temp = {}
  73. for k, v in conf_data.items():
  74. if v:
  75. conf_data_temp[k] = v
  76. conf_data_temp.pop("id")
  77. conf_data_temp.pop("device_id")
  78. conf_data_temp.pop("uptime")
  79. conf_data = dict(sorted(conf_data_temp.items(), key=lambda e:int(e[0].split("e")[1])))
  80. result = {"conf":conf_data,"total_counts":0,"items":[]}
  81. data_m = MongoDBTools(db_name='smartfarming', table_name='sa_qxz_data')
  82. if request_data.get("start_timestamp") == 0:
  83. data_wheres = {
  84. "device_id": request_data["device_id"]
  85. }
  86. else:
  87. data_wheres = {
  88. "device_id": request_data["device_id"],
  89. "uptime": {
  90. "$gt": request_data["start_timestamp"],
  91. "$lte": request_data["end_timestamp"]
  92. }
  93. }
  94. data = data_m.find_many(wheres=data_wheres,skip=request_data["page_start"],limit=request_data["page_size"])
  95. total_counts = data.count()
  96. result["total_counts"] = total_counts
  97. for item in data:
  98. item.pop("id")
  99. item.pop("device_id")
  100. uptime = item.pop("uptime")
  101. item = dict(sorted(item.items(), key=lambda e:int(e[0].split("e")[1])))
  102. result["items"].append({"uptime":uptime,"item_data":item})
  103. return Response(result)
  104. class ScdDeviceListView(APIView):
  105. authentication_classes = [APIAuthentication]
  106. throttle_classes = [ScdDeviceListRateThrottle]
  107. def get(self, request, *args, **kwargs):
  108. """获取杀虫灯设备列表接口"""
  109. uid = request.user
  110. wheres = {
  111. "device_type_id":2,
  112. '$or': [
  113. {'owner_uid': uid},
  114. {'user_dealer': uid}
  115. ]
  116. }
  117. project = {
  118. 'id': '$id',
  119. 'device_id': '$device_id',
  120. 'uptime': '$uptime',
  121. 'device_status': '$device_status',
  122. 'lng': '$lng',
  123. 'lat': '$lat'
  124. }
  125. m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
  126. data = m.find_many(wheres=wheres, options=project)
  127. if data:
  128. total_counts = data.count()
  129. else:
  130. total_counts = 0
  131. result = {"total_counts":total_counts,"items":[]}
  132. scd_dict_cache = {}
  133. for item in data:
  134. d_id = item.pop("id")
  135. result["items"].append(item)
  136. scd_dict_cache[item["device_id"]] = d_id
  137. default_cache.set(str(uid)+"_scd_list", scd_dict_cache,60*5)
  138. return Response(result)
  139. class ScdDeviceDetailView(APIView):
  140. authentication_classes = [APIAuthentication]
  141. permission_classes = [ScdDeviceDetailPermission]
  142. throttle_classes = [DeviceDetailRateThrottle]
  143. def get(self, request, *args, **kwargs):
  144. serializer = DeviceDetailSerializer(data=request.query_params)
  145. serializer.is_valid(raise_exception=True)
  146. request_data = serializer.validated_data
  147. uid = request.user
  148. scd_dict_cache = default_cache.get(str(uid)+"_scd_list")
  149. if scd_dict_cache:
  150. d_id = scd_dict_cache[request_data["device_id"]]
  151. else:
  152. """避免缓存失效"""
  153. wheres = {
  154. 'device_type_id':2,
  155. '$or': [
  156. {'owner_uid': uid},
  157. {'user_dealer': uid}
  158. ]
  159. }
  160. project = {
  161. 'id': '$id',
  162. 'device_id': '$device_id'
  163. }
  164. m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
  165. data = m.find_many(wheres=wheres, options=project)
  166. scd_dict_cache = []
  167. for item in data:
  168. scd_dict_cache[item["device_id"]]=item["id"]
  169. default_cache.set(str(uid)+"_scd_list", scd_dict_cache,60*5)
  170. d_id = scd_dict_cache[request_data["device_id"]]
  171. result = {"total_counts":0,"items":[]}
  172. data_project = {
  173. "addtime": "$addtime",
  174. "device_data": "$device_data"
  175. }
  176. data_m = MongoDBTools(db_name='smartfarming', table_name='sa_device_scd_data')
  177. if request_data.get("start_timestamp") == 0:
  178. data_wheres = {
  179. "device_id": d_id
  180. }
  181. else:
  182. data_wheres = {
  183. "device_id": d_id,
  184. "addtime": {
  185. "$gt": request_data["start_timestamp"],
  186. "$lte": request_data["end_timestamp"]
  187. }
  188. }
  189. data = data_m.find_many(wheres=data_wheres,options=data_project,skip=request_data["page_start"],limit=request_data["page_size"])
  190. total_counts = data.count()
  191. result["total_counts"] = total_counts
  192. for item in data:
  193. try:
  194. device_data = json.loads(item["device_data"])
  195. except:
  196. device_data = ast.literal_eval(item["device_data"])
  197. result["items"].append({"uptime":item["addtime"],"item_data":device_data})
  198. return Response(result)
  199. class CbdDeviceListView(APIView):
  200. authentication_classes = [APIAuthentication]
  201. throttle_classes = [CbdDeviceListRateThrottle]
  202. def get(self, request, *args, **kwargs):
  203. """获取测报灯设备列表接口"""
  204. uid = request.user
  205. wheres = {
  206. "device_type_id":3,
  207. '$or': [
  208. {'owner_uid': uid},
  209. {'user_dealer': uid}
  210. ]
  211. }
  212. project = {
  213. 'id': '$id',
  214. 'device_id': '$device_id',
  215. 'disable': '$disable',
  216. 'uptime': '$uptime',
  217. 'device_status': '$device_status',
  218. 'lng': '$lng',
  219. 'lat': '$lat'
  220. }
  221. m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
  222. data = m.find_many(wheres=wheres, options=project)
  223. if data:
  224. total_counts = data.count()
  225. else:
  226. total_counts = 0
  227. result = {"total_counts":total_counts,"items":[]}
  228. cbd_dict_cache = {}
  229. for item in data:
  230. d_id = item.pop("id")
  231. disable = item.pop("disable")
  232. result["items"].append(item)
  233. cbd_dict_cache[item["device_id"]] = {"d_id":d_id,"disable":disable}
  234. default_cache.set(str(uid)+"_cbd_list", cbd_dict_cache,60*5)
  235. return Response(result)
  236. class CbdDeviceDetailView(APIView):
  237. authentication_classes = [APIAuthentication]
  238. permission_classes = [CbdDeviceDetailPermission]
  239. throttle_classes = [DeviceDetailRateThrottle]
  240. def get(self, request, *args, **kwargs):
  241. serializer = DeviceDetailSerializer(data=request.query_params)
  242. serializer.is_valid(raise_exception=True)
  243. request_data = serializer.validated_data
  244. uid = request.user
  245. cbd_dict_cache = default_cache.get(str(uid)+"_cbd_list")
  246. if cbd_dict_cache:
  247. d_id = cbd_dict_cache[request_data["device_id"]]["d_id"]
  248. else:
  249. """避免缓存失效"""
  250. wheres = {
  251. 'device_type_id':3,
  252. '$or': [
  253. {'owner_uid': uid},
  254. {'user_dealer': uid}
  255. ]
  256. }
  257. project = {
  258. 'id': '$id',
  259. 'device_id': '$device_id',
  260. 'disable': '$disable'
  261. }
  262. m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
  263. data = m.find_many(wheres=wheres, options=project)
  264. cbd_dict_cache = {}
  265. for item in data:
  266. cbd_dict_cache[item["device_id"]]={"d_id":item["id"],"disable":item["disable"]}
  267. default_cache.set(str(uid)+"_cbd_list", cbd_dict_cache,60*5)
  268. d_id = cbd_dict_cache[request_data["device_id"]]["d_id"]
  269. result = {"total_counts":0,"items":[]}
  270. data_m = MongoDBTools(db_name='smartfarming', table_name='sa_device_cbd_data')
  271. data_project = {
  272. "addtime": "$addtime",
  273. "device_data": "$device_data"
  274. }
  275. if request_data.get("start_timestamp") == 0:
  276. data_wheres = {
  277. "device_id": d_id
  278. }
  279. else:
  280. data_wheres = {
  281. "device_id": d_id,
  282. "addtime": {
  283. "$gt": request_data["start_timestamp"],
  284. "$lte": request_data["end_timestamp"]
  285. }
  286. }
  287. data = data_m.find_many(wheres=data_wheres,options=data_project,skip=request_data["page_start"],limit=request_data["page_size"])
  288. total_counts = data.count()
  289. result["total_counts"] = total_counts
  290. for item in data:
  291. try:
  292. device_data = json.loads(item["device_data"])
  293. except:
  294. device_data = ast.literal_eval(item["device_data"])
  295. result["items"].append({"uptime":item["addtime"],"item_data":device_data})
  296. return Response(result)
  297. class CbdDevicePhotoView(APIView):
  298. authentication_classes = [APIAuthentication]
  299. permission_classes = [CbdDeviceDetailPermission]
  300. throttle_classes = [DevicePhotoRateThrottle]
  301. def get(self, request, *args, **kwargs):
  302. serializer = DeviceDetailSerializer(data=request.query_params)
  303. serializer.is_valid(raise_exception=True)
  304. request_data = serializer.validated_data
  305. uid = request.user
  306. cbd_dict_cache = default_cache.get(str(uid)+"_cbd_list")
  307. if cbd_dict_cache:
  308. d_id = cbd_dict_cache[request_data["device_id"]]["d_id"]
  309. disable = cbd_dict_cache[request_data["device_id"]]["disable"]
  310. else:
  311. """避免缓存失效"""
  312. wheres = {
  313. 'device_type_id':3,
  314. '$or': [
  315. {'owner_uid': uid},
  316. {'user_dealer': uid}
  317. ]
  318. }
  319. project = {
  320. 'id': '$id',
  321. 'device_id': '$device_id',
  322. 'disable': '$disable'
  323. }
  324. m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
  325. data = m.find_many(wheres=wheres, options=project)
  326. cbd_dict_cache = {}
  327. for item in data:
  328. cbd_dict_cache[item["device_id"]]={"d_id":item["id"],"disable":item["disable"]}
  329. default_cache.set(str(uid)+"_cbd_list", cbd_dict_cache,60*5)
  330. d_id = cbd_dict_cache[request_data["device_id"]]["d_id"]
  331. disable = cbd_dict_cache[request_data["device_id"]]["disable"]
  332. result = {"total_counts":0,"items":[]}
  333. data_m = MongoDBTools(db_name='smartfarming', table_name='sa_device_cbdphoto')
  334. if disable == 1:
  335. data_project = {
  336. "addtime": "$addtime",
  337. "addr": "$addr",
  338. "indentify_photo":"$indentify_photo",
  339. "indentify_result":"$indentify_result"
  340. }
  341. else:
  342. data_project = {
  343. "addtime": "$addtime",
  344. "addr": "$addr"
  345. }
  346. if request_data.get("start_timestamp") == 0:
  347. data_wheres = {
  348. "device_id": str(d_id)
  349. }
  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,skip=request_data["page_start"],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.get("start_timestamp") == 0:
  462. data_wheres = {
  463. "device_id": d_id
  464. }
  465. else:
  466. data_wheres = {
  467. "device_id": d_id,
  468. "addtime": {
  469. "$gt": request_data["start_timestamp"],
  470. "$lte": request_data["end_timestamp"]
  471. }
  472. }
  473. data = data_m.find_many(wheres=data_wheres,options=data_project,skip=request_data["page_start"],limit=request_data["page_size"])
  474. total_counts = data.count()
  475. result["total_counts"] = total_counts
  476. for item in data:
  477. try:
  478. device_data = json.loads(item["device_data"])
  479. except:
  480. device_data = ast.literal_eval(item["device_data"])
  481. result["items"].append({"uptime":item["addtime"],"item_data":device_data})
  482. return Response(result)
  483. class BzyDevicePhotoView(APIView):
  484. authentication_classes = [APIAuthentication]
  485. permission_classes = [BzyDeviceDetailPermission]
  486. throttle_classes = [DevicePhotoRateThrottle]
  487. def get(self, request, *args, **kwargs):
  488. serializer = DeviceDetailSerializer(data=request.query_params)
  489. serializer.is_valid(raise_exception=True)
  490. request_data = serializer.validated_data
  491. uid = request.user
  492. bzy_dict_cache = default_cache.get(str(uid)+"_bzy_list")
  493. if bzy_dict_cache:
  494. d_id = bzy_dict_cache[request_data["device_id"]]
  495. else:
  496. """避免缓存失效"""
  497. wheres = {
  498. 'device_type_id':7,
  499. '$or': [
  500. {'owner_uid': uid},
  501. {'user_dealer': uid}
  502. ]
  503. }
  504. project = {
  505. 'id': '$id',
  506. 'device_id': '$device_id'
  507. }
  508. m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
  509. data = m.find_many(wheres=wheres, options=project)
  510. bzy_dict_cache = []
  511. for item in data:
  512. bzy_dict_cache[item["device_id"]]=item["id"]
  513. default_cache.set(str(uid)+"_bzy_list", bzy_dict_cache,60*5)
  514. d_id = bzy_dict_cache[request_data["device_id"]]
  515. result = {"total_counts":0,"items":[]}
  516. data_m = MongoDBTools(db_name='smartfarming', table_name='sa_device_bzyphoto')
  517. data_project = {
  518. "addtime": "$addtime",
  519. "addr": "$addr"
  520. }
  521. if request_data.get("start_timestamp") == 0:
  522. data_wheres = {
  523. "device_id": str(d_id)
  524. }
  525. else:
  526. data_wheres = {
  527. "device_id": str(d_id),
  528. "addtime": {
  529. "$gt": request_data["start_timestamp"],
  530. "$lte": request_data["end_timestamp"]
  531. }
  532. }
  533. data = data_m.find_many(wheres=data_wheres,options=data_project,skip=request_data["page_start"],limit=request_data["page_size"])
  534. total_counts = data.count()
  535. result["total_counts"] = total_counts
  536. for item in data:
  537. if item["addr"].startswith("http"):
  538. Image = item["addr"]
  539. elif item["addr"].startswith("/"):
  540. Image = settings.CONFIG["image_url"]["bzy_img_forward"] + item["addr"]
  541. else:
  542. Image = settings.CONFIG["image_url"]["bzy_img_forward"] + "/" + item["addr"]
  543. result["items"].append({"uptime":item["addtime"],"Image":Image})
  544. return Response(result)
  545. class XycbDeviceListView(APIView):
  546. authentication_classes = [APIAuthentication]
  547. throttle_classes = [XycbDeviceListRateThrottle]
  548. def get(self, request, *args, **kwargs):
  549. """获取性诱设备列表接口"""
  550. uid = request.user
  551. wheres = {
  552. "device_type_id":4,
  553. '$or': [
  554. {'owner_uid': uid},
  555. {'user_dealer': uid}
  556. ]
  557. }
  558. project = {
  559. 'id': '$id',
  560. 'device_id': '$device_id',
  561. 'uptime': '$uptime',
  562. 'device_status': '$device_status',
  563. 'lng': '$lng',
  564. 'lat': '$lat'
  565. }
  566. m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
  567. data = m.find_many(wheres=wheres, options=project)
  568. if data:
  569. total_counts = data.count()
  570. else:
  571. total_counts = 0
  572. result = {"total_counts":total_counts,"items":[]}
  573. xycb_dict_cache = {}
  574. for item in data:
  575. d_id = item.pop("id")
  576. result["items"].append(item)
  577. xycb_dict_cache[item["device_id"]] = d_id
  578. default_cache.set(str(uid)+"_xycb_list", xycb_dict_cache,60*5)
  579. return Response(result)
  580. class XycbDeviceDetailView(APIView):
  581. authentication_classes = [APIAuthentication]
  582. permission_classes = [XycbDeviceDetailPermission]
  583. throttle_classes = [DeviceDetailRateThrottle]
  584. def get(self, request, *args, **kwargs):
  585. serializer = DeviceDetailSerializer(data=request.query_params)
  586. serializer.is_valid(raise_exception=True)
  587. request_data = serializer.validated_data
  588. uid = request.user
  589. xycb_dict_cache = default_cache.get(str(uid)+"_xycb_list")
  590. if xycb_dict_cache:
  591. d_id = xycb_dict_cache[request_data["device_id"]]
  592. else:
  593. """避免缓存失效"""
  594. wheres = {
  595. 'device_type_id':4,
  596. '$or': [
  597. {'owner_uid': uid},
  598. {'user_dealer': uid}
  599. ]
  600. }
  601. project = {
  602. 'id': '$id',
  603. 'device_id': '$device_id'
  604. }
  605. m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
  606. data = m.find_many(wheres=wheres, options=project)
  607. xycb_dict_cache = []
  608. for item in data:
  609. xycb_dict_cache[item["device_id"]]=item["id"]
  610. default_cache.set(str(uid)+"_scd_list", xycb_dict_cache,60*5)
  611. d_id = xycb_dict_cache[request_data["device_id"]]
  612. result = {"total_counts":0,"items":[]}
  613. data_m = MongoDBTools(db_name='smartfarming', table_name='sa_device_xycb_data')
  614. data_project = {
  615. "addtime": "$addtime",
  616. "device_data": "$device_data"
  617. }
  618. if request_data.get("start_timestamp") == 0:
  619. data_wheres = {
  620. "device_id": d_id
  621. }
  622. else:
  623. data_wheres = {
  624. "device_id": d_id,
  625. "addtime": {
  626. "$gt": request_data["start_timestamp"],
  627. "$lte": request_data["end_timestamp"]
  628. }
  629. }
  630. data = data_m.find_many(wheres=data_wheres,options=data_project,skip=request_data["page_start"],limit=request_data["page_size"])
  631. total_counts = data.count()
  632. result["total_counts"] = total_counts
  633. for item in data:
  634. try:
  635. device_data = json.loads(item["device_data"])
  636. except:
  637. device_data = ast.literal_eval(item["device_data"])
  638. result["items"].append({"uptime":item["addtime"],"item_data":device_data})
  639. return Response(result)
  640. class XctDeviceListView(APIView):
  641. authentication_classes = [APIAuthentication]
  642. throttle_classes = [XctDeviceListRateThrottle]
  643. def get(self, request, *args, **kwargs):
  644. """获取吸虫塔设备列表接口"""
  645. uid = request.user
  646. wheres = {
  647. "device_type_id":12,
  648. '$or': [
  649. {'owner_uid': uid},
  650. {'user_dealer': uid}
  651. ]
  652. }
  653. project = {
  654. 'id': '$id',
  655. 'device_id': '$device_id',
  656. 'uptime': '$uptime',
  657. 'device_status': '$device_status',
  658. 'lng': '$lng',
  659. 'lat': '$lat'
  660. }
  661. m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
  662. data = m.find_many(wheres=wheres, options=project)
  663. if data:
  664. total_counts = data.count()
  665. else:
  666. total_counts = 0
  667. result = {"total_counts":total_counts,"items":[]}
  668. xct_dict_cache = {}
  669. for item in data:
  670. d_id = item.pop("id")
  671. result["items"].append(item)
  672. xct_dict_cache[item["device_id"]] = d_id
  673. default_cache.set(str(uid)+"_xct_list", xct_dict_cache,60*5)
  674. return Response(result)
  675. class XctDeviceDetailView(APIView):
  676. authentication_classes = [APIAuthentication]
  677. permission_classes = [XctDeviceDetailPermission]
  678. throttle_classes = [DeviceDetailRateThrottle]
  679. def get(self, request, *args, **kwargs):
  680. serializer = DeviceDetailSerializer(data=request.query_params)
  681. serializer.is_valid(raise_exception=True)
  682. request_data = serializer.validated_data
  683. uid = request.user
  684. xct_dict_cache = default_cache.get(str(uid)+"_xct_list")
  685. if xct_dict_cache:
  686. d_id = xct_dict_cache[request_data["device_id"]]
  687. else:
  688. """避免缓存失效"""
  689. wheres = {
  690. 'device_type_id':12,
  691. '$or': [
  692. {'owner_uid': uid},
  693. {'user_dealer': uid}
  694. ]
  695. }
  696. project = {
  697. 'id': '$id',
  698. 'device_id': '$device_id'
  699. }
  700. m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
  701. data = m.find_many(wheres=wheres, options=project)
  702. xct_dict_cache = []
  703. for item in data:
  704. xct_dict_cache[item["device_id"]]=item["id"]
  705. default_cache.set(str(uid)+"_xct_list", xct_dict_cache,60*5)
  706. d_id = xct_dict_cache[request_data["device_id"]]
  707. result = {"total_counts":0,"items":[]}
  708. data_m = MongoDBTools(db_name='smartfarming', table_name='sa_device_xct_data')
  709. data_project = {
  710. "addtime": "$addtime",
  711. "device_data": "$device_data"
  712. }
  713. if request_data.get("start_timestamp") == 0:
  714. data_wheres = {
  715. "device_id": d_id
  716. }
  717. else:
  718. data_wheres = {
  719. "device_id": d_id,
  720. "addtime": {
  721. "$gt": request_data["start_timestamp"],
  722. "$lte": request_data["end_timestamp"]
  723. }
  724. }
  725. data = data_m.find_many(wheres=data_wheres,options=data_project,skip=request_data["page_start"],limit=request_data["page_size"])
  726. total_counts = data.count()
  727. result["total_counts"] = total_counts
  728. for item in data:
  729. try:
  730. device_data = json.loads(item["device_data"])
  731. except:
  732. device_data = ast.literal_eval(item["device_data"])
  733. result["items"].append({"uptime":item["addtime"],"item_data":device_data})
  734. return Response(result)
  735. class XctDevicePhotoView(APIView):
  736. authentication_classes = [APIAuthentication]
  737. permission_classes = [XctDeviceDetailPermission]
  738. throttle_classes = [DevicePhotoRateThrottle]
  739. def get(self, request, *args, **kwargs):
  740. serializer = DeviceDetailSerializer(data=request.query_params)
  741. serializer.is_valid(raise_exception=True)
  742. request_data = serializer.validated_data
  743. uid = request.user
  744. xct_dict_cache = default_cache.get(str(uid)+"_xct_list")
  745. if xct_dict_cache:
  746. d_id = xct_dict_cache[request_data["device_id"]]
  747. else:
  748. """避免缓存失效"""
  749. wheres = {
  750. 'device_type_id':12,
  751. '$or': [
  752. {'owner_uid': uid},
  753. {'user_dealer': uid}
  754. ]
  755. }
  756. project = {
  757. 'id': '$id',
  758. 'device_id': '$device_id'
  759. }
  760. m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
  761. data = m.find_many(wheres=wheres, options=project)
  762. xct_dict_cache = []
  763. for item in data:
  764. xct_dict_cache[item["device_id"]]=item["id"]
  765. default_cache.set(str(uid)+"_xct_list", xct_dict_cache,60*5)
  766. d_id = xct_dict_cache[request_data["device_id"]]
  767. result = {"total_counts":0,"itmes":[]}
  768. data_m = MongoDBTools(db_name='smartfarming', table_name='sa_device_xct_photo')
  769. data_project = {
  770. "addtime": "$addtime",
  771. "addr": "$addr",
  772. "indentify_photo":"$indentify_photo",
  773. "indentify_result":"$indentify_result"
  774. }
  775. if request_data.get("start_timestamp") == 0:
  776. data_wheres = {
  777. "device_id": str(d_id)
  778. }
  779. else:
  780. data_wheres = {
  781. "device_id": str(d_id),
  782. "addtime": {
  783. "$gt": request_data["start_timestamp"],
  784. "$lte": request_data["end_timestamp"]
  785. }
  786. }
  787. data = data_m.find_many(wheres=data_wheres,options=data_project,skip=request_data["page_start"],limit=request_data["page_size"])
  788. total_counts = data.count()
  789. result["total_counts"] = total_counts
  790. for item in data:
  791. if item["addr"].startswith("http"):
  792. Image = item["addr"]
  793. elif item["addr"].startswith("/"):
  794. Image = settings.CONFIG["image_url"]["xct_img_forward"] + item["addr"]
  795. else:
  796. Image = settings.CONFIG["image_url"]["xct_img_forward"] + "/" + item["addr"]
  797. Result = item["indentify_result"] if item["indentify_result"] else "0"
  798. indentify_photo = item["indentify_photo"]
  799. if indentify_photo and indentify_photo!="0":
  800. if indentify_photo == "0":
  801. Result_image = "0"
  802. else:
  803. if indentify_photo.startswith("http"):
  804. Result_image = indentify_photo
  805. elif indentify_photo.startswith("/"):
  806. Result_image = settings.CONFIG["image_url"]["xct_img_result_forward"] + indentify_photo
  807. else:
  808. Result_image = settings.CONFIG["image_url"]["xct_img_result_forward"] + "/" + indentify_photo
  809. else:
  810. Result_image = "0"
  811. result["itmes"].append({"uptime":item["addtime"],"Image":Image,"Result_image":Result_image,"Result":Result})
  812. return Response(result)
  813. class GssqDeviceListView(APIView):
  814. authentication_classes = [APIAuthentication]
  815. throttle_classes = [GssqDeviceListRateThrottle]
  816. def get(self, request, *args, **kwargs):
  817. """获取管式墒情列表接口"""
  818. uid = request.user
  819. wheres = {
  820. "device_type_id":15,
  821. '$or': [
  822. {'owner_uid': uid},
  823. {'user_dealer': uid}
  824. ]
  825. }
  826. project = {
  827. 'id': '$id',
  828. 'device_id': '$device_id',
  829. 'uptime': '$uptime',
  830. 'device_status': '$device_status',
  831. 'lng': '$lng',
  832. 'lat': '$lat'
  833. }
  834. m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
  835. data = m.find_many(wheres=wheres, options=project)
  836. if data:
  837. total_counts = data.count()
  838. else:
  839. total_counts = 0
  840. result = {"total_counts":total_counts,"items":[]}
  841. gssq_dict_cache = {}
  842. for item in data:
  843. d_id = item.pop("id")
  844. result["items"].append(item)
  845. gssq_dict_cache[item["device_id"]] = d_id
  846. default_cache.set(str(uid)+"_gssq_list", gssq_dict_cache,60*5)
  847. return Response(result)
  848. class GssqDeviceDetailView(APIView):
  849. authentication_classes = [APIAuthentication]
  850. permission_classes = [GssqDeviceDetailPermission]
  851. throttle_classes = [DeviceDetailRateThrottle]
  852. def get(self, request, *args, **kwargs):
  853. serializer = DeviceDetailSerializer(data=request.query_params)
  854. serializer.is_valid(raise_exception=True)
  855. request_data = serializer.validated_data
  856. uid = request.user
  857. gssq_dict_cache = default_cache.get(str(uid)+"_gssq_list")
  858. if gssq_dict_cache:
  859. d_id = gssq_dict_cache[request_data["device_id"]]
  860. else:
  861. """避免缓存失效"""
  862. wheres = {
  863. 'device_type_id':15,
  864. '$or': [
  865. {'owner_uid': uid},
  866. {'user_dealer': uid}
  867. ]
  868. }
  869. project = {
  870. 'id': '$id',
  871. 'device_id': '$device_id'
  872. }
  873. m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
  874. data = m.find_many(wheres=wheres, options=project)
  875. gssq_dict_cache = []
  876. for item in data:
  877. gssq_dict_cache[item["device_id"]]=item["id"]
  878. default_cache.set(str(uid)+"_gssq_list", gssq_dict_cache,60*5)
  879. d_id = gssq_dict_cache[request_data["device_id"]]
  880. result = {"total_counts":0,"items":[]}
  881. data_m = MongoDBTools(db_name='smartfarming', table_name='sa_nd_qxz_data')
  882. data_project = {
  883. "upl_time": "$upl_time",
  884. "id" : "$id",
  885. "at" : "$at",
  886. "atm" : "$atm",
  887. "ats" : "$ats",
  888. "csq" : "$csq",
  889. "device_id" : "$device_id",
  890. "swc" : "$swc",
  891. "temp" : "$temp"
  892. }
  893. if request_data.get("start_timestamp") == 0:
  894. data_wheres = {
  895. "device_id": request_data["device_id"]
  896. }
  897. else:
  898. data_wheres = {
  899. "device_id": request_data["device_id"],
  900. "upl_time": {
  901. "$gt": request_data["start_timestamp"],
  902. "$lte": request_data["end_timestamp"]
  903. }
  904. }
  905. data = data_m.find_many(wheres=data_wheres,options=data_project,skip=request_data["page_start"],limit=request_data["page_size"])
  906. total_counts = data.count()
  907. result["total_counts"] = total_counts
  908. for item in data:
  909. try:
  910. device_data = item
  911. except:
  912. device_data = ast.literal_eval(item)
  913. result["items"].append({"uptime":item["upl_time"],"item_data":device_data})
  914. return Response(result)
  915. class JkDevicePlayAddressView(APIView):
  916. authentication_classes = [APIAuthentication]
  917. permission_classes = [JkDeviceDetailPermission]
  918. def get(self, request, *args, **kwargs):
  919. serializer = DeviceDetailSerializer(data=request.query_params)
  920. serializer.is_valid(raise_exception=True)
  921. request_data = serializer.validated_data
  922. print(request_data)
  923. uid = request.user
  924. jk_dict_cache = default_cache.get(str(uid)+"_jk_list")
  925. if jk_dict_cache:
  926. d_id = jk_dict_cache[request_data["device_id"]]
  927. else:
  928. """避免缓存失效"""
  929. wheres = {
  930. 'device_type_id':12,
  931. '$or': [
  932. {'owner_uid': uid},
  933. {'user_dealer': uid}
  934. ]
  935. }
  936. project = {
  937. 'id': '$id',
  938. 'device_id': '$device_id'
  939. }
  940. m = MongoDBTools(db_name='smartfarming', table_name='sa_device')
  941. data = m.find_many(wheres=wheres, options=project)
  942. jk_dict_cache = []
  943. for item in data:
  944. jk_dict_cache[item["device_id"]]=item["id"]
  945. default_cache.set(str(uid)+"_jk_list", jk_dict_cache,60*5)
  946. d_id = jk_dict_cache[request_data["device_id"]]
  947. result = {"total_counts":0,"itmes":[]}
  948. data_m = MongoDBTools(db_name='smartfarming', table_name='sa_device_camera')
  949. data_wheres = {
  950. "device_id": request_data["device_id"],
  951. }
  952. print(data_wheres)
  953. data = data_m.find_one(data_wheres)
  954. print(data)
  955. addr = data["device_info"]
  956. account_id = data["account_id"]
  957. MongoCameraAccount = MongoDBTools(db_name='smartfarming', table_name='sa_device_camera_account')
  958. CameraAccount_data = MongoCameraAccount.find_many({"id":int(account_id)})
  959. if CameraAccount_data.count() > 0:
  960. account_obj = CameraAccount_data[0]
  961. account_type = account_obj["account_type"]
  962. token = account_obj["token"]
  963. if account_type == 0:
  964. device_info = request_data["device_id"].split("-")
  965. if len(device_info) == 2:
  966. device = device_info[0]
  967. channel = device_info[1]
  968. elif len(device_info) == 1:
  969. device = device_info[0]
  970. channel = 1
  971. else:
  972. return Response({"error":"监控地址有误"})
  973. yz_url = "https://open.ys7.com/api/lapp/v2/live/address/get"
  974. # ezopen 播放地址
  975. res = requests.post(f"{yz_url}?accessToken={token}&deviceSerial={device}&channelNo={channel}")
  976. res_data = res.json()
  977. print(res_data)
  978. if res_data.get("code") == '200':
  979. ezopen = res_data.get("data", {}).get("url", "")
  980. else:
  981. return Response({"error":res_data.get("msg")})
  982. # hls 播放地址
  983. res = requests.post(f"{yz_url}?accessToken={token}&deviceSerial={device}&channelNo={channel}&protocol=2")
  984. hls = (res.json()).get("data", {}).get("url", "")
  985. if not hls:
  986. # 海康设备
  987. addr = addr.replace("http://", "https://")
  988. data = {
  989. "ezopen": ezopen,
  990. "token": token,
  991. "hls": hls,
  992. "type_id": 0
  993. }
  994. elif account_type == 1:
  995. # 大华设备
  996. addr = addr.replace("http://cmgw-vpc.lechange.com:8888", "https://cmgw-vpc.lechange.com:8890")
  997. addr = eval(addr)
  998. addr["type_id"] = -1
  999. return str(addr)
  1000. else:
  1001. addr = addr.replace("http://", "https://")
  1002. data = {
  1003. "hls": addr,
  1004. "type_id": 0
  1005. }
  1006. return Response(data)