mongoclient.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. from distutils.command.config import config
  2. import pymongo
  3. from urllib import parse
  4. import datetime
  5. import re
  6. def get_table():
  7. passwd = parse.quote_plus("yfkj@6020")
  8. user = parse.quote_plus("root")
  9. myclient = pymongo.MongoClient("mongodb://{0}:{1}@8.136.98.49:57017/".format(user,passwd))
  10. db = myclient.smartfarming
  11. return db
  12. def get_device_info(device_id):
  13. # 获取设备的版本,经纬度
  14. db = get_table()
  15. sa_device = db.sa_device
  16. query = {'device_id': device_id}
  17. device = sa_device.find_one(query, {"dver_num": 1, "lng": 1, "lat": 1, "_id": 0})
  18. return device
  19. def get_sim_info(device_id, start, end):
  20. start = int((datetime.datetime.strptime(start, "%Y-%m-%d %H:%M:%S")).timestamp())
  21. end = int((datetime.datetime.strptime(end, "%Y-%m-%d %H:%M:%S")).timestamp())
  22. # sim卡信息
  23. db = get_table()
  24. qxz_base_info = db.sa_qxz_base_info
  25. query = {'device_id': device_id, 'uptime': {"$lte": end, "$gte": start}}
  26. base_info = qxz_base_info.find_one(query, {"_id": 0, "iccid": 1})
  27. return base_info.get("iccid")
  28. def get_sa_qxz_info_record(device_id, start, end):
  29. # 获取电压,信号强度 历史数据
  30. start = int((datetime.datetime.strptime(start, "%Y-%m-%d %H:%M:%S")).timestamp())
  31. end = int((datetime.datetime.strptime(end, "%Y-%m-%d %H:%M:%S")).timestamp())
  32. db = get_table()
  33. qxz_info_record = db.sa_qxz_info_record
  34. query = {'device_id': device_id, 'uptime': {"$lte": end, "$gte": start}}
  35. info_record = qxz_info_record.find(query, {"_id": 0, 'volt': 1, "rssi": 1})
  36. result = {"volt": {"data": [], "explan": "电压"}, "rssi": {"data": [], "explan": "信号强度"}}
  37. for k in info_record:
  38. result["volt"]["data"].append(float(k.get("volt")))
  39. result["rssi"]["data"].append(float(k.get("rssi")))
  40. return result
  41. def get_sa_qxz_data(device_id, start, end, conf):
  42. # 获取具体数据
  43. start = int((datetime.datetime.strptime(start, "%Y-%m-%d %H:%M:%S")).timestamp())
  44. end = int((datetime.datetime.strptime(end, "%Y-%m-%d %H:%M:%S")).timestamp())
  45. db = get_table()
  46. qxz_data = db.sa_qxz_data
  47. query = {'device_id': device_id, 'uptime': {"$lte": end, "$gte": start}}
  48. field = conf.keys()
  49. fields = {i: 1 for i in field}
  50. fields.update({"_id": 0, "uptime": 1})
  51. data = qxz_data.find(query, fields).sort("uptime", pymongo.DESCENDING)
  52. return list(data)
  53. def get_qxz_conf(device_id):
  54. print(device_id)
  55. # 获取配置文件
  56. db = get_table()
  57. qxz_conf = db.sa_qxz_conf
  58. query = {'device_id': device_id}
  59. conf = qxz_conf.find_one(query, {"_id": 0, "uptime": 0, "device_id": 0, "id": 0})
  60. conf = {k: v for k, v in conf.items() if v is not None and v != ""}
  61. conf.update({"uptime": "上传数据条数#999#个"})
  62. return conf
  63. def get_conf_data(conf, data):
  64. conf_keys = conf.keys()
  65. results = {}
  66. for i in conf_keys:
  67. temp = []
  68. for detail in data:
  69. value = str(detail.get(i))
  70. va_sp = value.split("#")
  71. # if len(va_sp) == 3:
  72. vk = float(va_sp[0])
  73. if vk > 9999999999:
  74. temp.append(9999999999)
  75. elif vk < -9999999999:
  76. temp.append(-9999999999)
  77. else:
  78. temp.append(vk)
  79. results[i] = {
  80. "data": temp,
  81. "explan": (conf.get(i)).split("#")[0]
  82. }
  83. return results
  84. def device_detail_deivce_id(short_id):
  85. """确定设备所在平台以及完整设备号"""
  86. db = get_table()
  87. regex = re.compile('.*{}$'.format(short_id))
  88. bd_device_dict = db.sa_device.find_one(
  89. filter = {"device_id":regex,"device_type_id":5},
  90. projection = {'_id': 0},
  91. sort = [('uptime', pymongo.DESCENDING)]
  92. )
  93. if bd_device_dict:
  94. device_id = bd_device_dict.get("device_id")
  95. position = bd_device_dict.get("province") + bd_device_dict.get("city")+ bd_device_dict.get("district")
  96. lng, lat = "", ""
  97. try:
  98. lng = float(bd_device_dict.get("lng"))
  99. lat = float(bd_device_dict.get("lat"))
  100. except Exception as e:
  101. print(e)
  102. return device_id, bd_device_dict.get("dver_num"), lng, lat, position
  103. else:
  104. return None, "", "", "", ""
  105. if __name__ == "__main__":
  106. device_id = '864865060471399'
  107. get_device_info(device_id)
  108. start = "2023-08-28 10:00:00"
  109. end = "2023-08-31 20:00:00"
  110. base_info = get_sim_info(device_id, start, end)
  111. conf = get_qxz_conf(device_id)
  112. data = get_sa_qxz_data(device_id, start, end, conf)
  113. result = get_conf_data(conf, data)
  114. for k, v in result.items():
  115. print(k, v)