men_tou_gou.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. from rest_framework.views import APIView
  2. from rest_framework.response import Response
  3. import pymongo
  4. from urllib import parse
  5. import datetime
  6. import re
  7. import json
  8. def get_device(device_type=None):
  9. device = {
  10. "864865060482180": "5",
  11. "860181067701570": "15",
  12. "L31671797-1": "6",
  13. "L42185263-1": "6"
  14. }
  15. if not device_type:
  16. return device
  17. else:
  18. new_device = {}
  19. for k, v in device:
  20. if v == device_type:
  21. new_device[k] = v
  22. return new_device
  23. def get_table():
  24. passwd = parse.quote_plus("yfkj@6020")
  25. user = parse.quote_plus("root")
  26. myclient = pymongo.MongoClient("mongodb://{0}:{1}@8.136.98.49:57017/".format(user,passwd))
  27. return myclient
  28. class CountDeviceStatus(APIView):
  29. # 设备状态,在线与离线数量
  30. def post(self, request):
  31. devices = get_device()
  32. device_ids = devices.keys()
  33. # 获取设备的版本,经纬度
  34. myclient = get_table()
  35. db = myclient.smartfarming
  36. sa_device = db.sa_device
  37. query = {
  38. 'device_id': {
  39. "$in": list(device_ids)
  40. }
  41. }
  42. device = list(sa_device.find(query, {"device_id": 1,"lng":1, "lat": 1, "device_status": 1, "_id": 0, "uptime": 1, "province": 1, "city":1, "district": 1, "device_name": 1}))
  43. offline = 0
  44. online = 0
  45. new = []
  46. for k in device:
  47. if k.get("device_status") == 1:
  48. online += 1
  49. else:
  50. offline += 1
  51. new.append(k)
  52. myclient.close()
  53. return Response({"count": offline + online, "online": online, "offline": offline, "device": new, "mtg_name": True})
  54. class QxData(APIView):
  55. # 气象、墒情监测数据
  56. def post(self, request):
  57. print(1/0)
  58. result = {}
  59. device_id = {
  60. "气象站": "864865060482180",
  61. "墒情站": "860181067701570"
  62. }
  63. myclient = get_table()
  64. db = myclient.smartfarming
  65. qxz_conf = db.sa_qxz_conf
  66. query = {'device_id': "864865060482180"}
  67. conf = qxz_conf.find_one(query, {"_id": 0, "uptime": 0, "device_id": 0, "id": 0})
  68. conf = {k: v for k, v in conf.items() if v is not None and v != ""}
  69. qxz_data = db.sa_qxz_data
  70. query = {'device_id': "864865060482180"}
  71. field = conf.keys()
  72. fields = {i: 1 for i in field}
  73. fields.update({"_id": 0, "uptime": 1})
  74. data = qxz_data.find(query, fields).sort("uptime", pymongo.DESCENDING).limit(1)
  75. data = data[0]
  76. tmp = {}
  77. print(conf)
  78. for k, v in data.items():
  79. tp = conf.get(k)
  80. if tp:
  81. tp_m = tp.split("#")
  82. tmp[k] = v.split("#")[0] + tp_m[-1]
  83. sa_nd_qxz_status = db.sa_nd_qxz_status
  84. qxz_obj = sa_nd_qxz_status.find({"device_id": "860181067701570"}, {"_id": 0}).sort("upl_time", pymongo.DESCENDING).limit(1)
  85. qxz_obj = qxz_obj[0]
  86. if qxz_obj.get("depth"):
  87. depth = qxz_obj.get("depth")
  88. else:
  89. depth = ""
  90. len_ = len((qxz_obj.get("temp")).split(","))
  91. for i in range(len_):
  92. if i != 0:
  93. depth += ","
  94. depth += str((i+1)*10)
  95. data = {"depth":depth,"csq":qxz_obj.get("csq"),"temp":qxz_obj.get("temp"),"swc":qxz_obj.get("swc"),"atm":qxz_obj.get("atm"),"at":qxz_obj.get("at"),"ats":qxz_obj.get("ats"),"uptime":qxz_obj.get("upl_time")}
  96. myclient.close()
  97. return Response({
  98. "qxz":tmp,
  99. "qxz_conf": conf,
  100. "sqz": data,
  101. "mtg_name": True
  102. })
  103. class QxWarning(APIView):
  104. # 气象预警数据
  105. def post(self, request):
  106. myclient = get_table()
  107. db = myclient.smartfarming
  108. qxz_alarm = db.sa_qxz_alarm_log_new
  109. k = qxz_alarm.find_one(sort=[("id", -1)])["id"] + 1
  110. query = {'device_id': "864865060482180"}
  111. data = qxz_alarm.find(query, {"_id":0, "warning_content": 1, "upl_time": 1}).sort("upl_time", pymongo.DESCENDING).limit(5)
  112. myclient.close()
  113. return Response({"warning": list(data), "mtg_name": True})
  114. class CameraDetail(APIView):
  115. # 监控数据
  116. def post(self, request):
  117. myclient = get_table()
  118. db = myclient.smartfarming
  119. sa_device_camera = db.sa_device_camera
  120. sa_device_camera_account = db.sa_device_camera_account
  121. sa_device = db.sa_device
  122. result = []
  123. for device_id in ["L31671797-1", "L42185263-1"]:
  124. camera = sa_device_camera.find_one({"device_id": device_id}, {"device_info": 1, "account_id": 1, "_id": 0})
  125. addr = camera.get("device_info")
  126. account_id = camera.get("account_id")
  127. camera_account = sa_device_camera_account.find_one({"id": account_id}, {"account_type": 1, "_id": 0})
  128. if camera_account:
  129. account_type = camera_account.get("account_type")
  130. if account_type == 0:
  131. addr = addr.replace("http://", "https://")
  132. elif account_type == 1:
  133. pass
  134. else:
  135. addr = addr.replace("http://", "https://")
  136. device_info = sa_device.find_one({"device_id": device_id}, {"_id": 0, "device_name":1})
  137. device_name = device_info.get("device_name")
  138. result.append({
  139. "device_id": device_id,
  140. "device_name": device_name if device_name else device_id,
  141. "device_info": addr,
  142. "is_name": device_name,
  143. })
  144. myclient.close()
  145. return Response({"camera": result, "mtg_name": True})