mongoclient.py 4.6 KB

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