men_tou_gou.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. result = {}
  58. device_id = {
  59. "气象站": "864865060482180",
  60. "墒情站": "860181067701570"
  61. }
  62. myclient = get_table()
  63. db = myclient.smartfarming
  64. qxz_conf = db.sa_qxz_conf
  65. query = {'device_id': "864865060482180"}
  66. conf = qxz_conf.find_one(query, {"_id": 0, "uptime": 0, "device_id": 0, "id": 0})
  67. conf = {k: v for k, v in conf.items() if v is not None and v != ""}
  68. qxz_data = db.sa_qxz_data
  69. query = {'device_id': "864865060482180"}
  70. field = conf.keys()
  71. fields = {i: 1 for i in field}
  72. fields.update({"_id": 0, "uptime": 1})
  73. data = qxz_data.find(query, fields).sort("uptime", pymongo.DESCENDING).limit(1)
  74. data = data[0]
  75. tmp = {}
  76. print(conf)
  77. for k, v in data.items():
  78. tp = conf.get(k)
  79. if tp:
  80. tp_m = tp.split("#")
  81. tmp[k] = v.split("#")[0] + tp_m[-1]
  82. sa_nd_qxz_status = db.sa_nd_qxz_status
  83. qxz_obj = sa_nd_qxz_status.find({"device_id": "860181067701570"}, {"_id": 0}).sort("upl_time", pymongo.DESCENDING).limit(1)
  84. qxz_obj = qxz_obj[0]
  85. if qxz_obj.get("depth"):
  86. depth = qxz_obj.get("depth")
  87. else:
  88. depth = ""
  89. len_ = len((qxz_obj.get("temp")).split(","))
  90. for i in range(len_):
  91. if i != 0:
  92. depth += ","
  93. depth += str((i+1)*10)
  94. 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")}
  95. myclient.close()
  96. return Response({
  97. "qxz":tmp,
  98. "qxz_conf": conf,
  99. "sqz": data,
  100. "mtg_name": True
  101. })
  102. class QxWarning(APIView):
  103. # 气象预警数据
  104. def post(self, request):
  105. myclient = get_table()
  106. db = myclient.smartfarming
  107. qxz_alarm = db.sa_qxz_alarm_log_new
  108. k = qxz_alarm.find_one(sort=[("id", -1)])["id"] + 1
  109. query = {'device_id': "864865060482180"}
  110. data = qxz_alarm.find(query, {"_id":0, "warning_content": 1, "upl_time": 1}).sort("upl_time", pymongo.DESCENDING).limit(5)
  111. myclient.close()
  112. return Response({"warning": list(data), "mtg_name": True})
  113. class CameraDetail(APIView):
  114. # 监控数据
  115. def post(self, request):
  116. myclient = get_table()
  117. db = myclient.smartfarming
  118. sa_device_camera = db.sa_device_camera
  119. sa_device_camera_account = db.sa_device_camera_account
  120. sa_device = db.sa_device
  121. result = []
  122. for device_id in ["L31671797-1", "L42185263-1"]:
  123. camera = sa_device_camera.find_one({"device_id": device_id}, {"device_info": 1, "account_id": 1, "_id": 0})
  124. addr = camera.get("device_info")
  125. account_id = camera.get("account_id")
  126. camera_account = sa_device_camera_account.find_one({"id": account_id}, {"account_type": 1, "_id": 0})
  127. if camera_account:
  128. account_type = camera_account.get("account_type")
  129. if account_type == 0:
  130. addr = addr.replace("http://", "https://")
  131. elif account_type == 1:
  132. pass
  133. else:
  134. addr = addr.replace("http://", "https://")
  135. device_info = sa_device.find_one({"device_id": device_id}, {"_id": 0, "device_name":1})
  136. device_name = device_info.get("device_name")
  137. result.append({
  138. "device_id": device_id,
  139. "device_name": device_name if device_name else device_id,
  140. "device_info": addr,
  141. "is_name": device_name,
  142. })
  143. myclient.close()
  144. return Response({"camera": result, "mtg_name": True})