| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- import pymongo
- from urllib import parse
- import datetime
- import re
- def get_table():
- passwd = parse.quote_plus("yfkj@6020")
- user = parse.quote_plus("root")
- myclient = pymongo.MongoClient("mongodb://{0}:{1}@8.136.98.49:57017/".format(user,passwd))
- db = myclient.smartfarming
- return db
- def get_device_info(device_id):
- # 获取设备的版本,经纬度
- db = get_table()
- sa_device = db.sa_device
- query = {'device_id': device_id}
- device = sa_device.find_one(query, {"dver_num": 1, "lng": 1, "lat": 1, "_id": 0})
- return device
- def get_sim_info(device_id, start, end):
- start = int((datetime.datetime.strptime(start, "%Y-%m-%d %H:%M:%S")).timestamp())
- end = int((datetime.datetime.strptime(end, "%Y-%m-%d %H:%M:%S")).timestamp())
- # sim卡信息
- db = get_table()
- qxz_base_info = db.sa_qxz_base_info
- query = {'device_id': device_id}
- base_info = qxz_base_info.find_one(query, {"_id": 0, "iccid": 1})
- return base_info.get("iccid")
- def get_sa_qxz_info_record(device_id, start, end):
- # 获取电压,信号强度 历史数据
- start = int((datetime.datetime.strptime(start, "%Y-%m-%d %H:%M:%S")).timestamp())
- end = int((datetime.datetime.strptime(end, "%Y-%m-%d %H:%M:%S")).timestamp())
- db = get_table()
- qxz_info_record = db.sa_qxz_info_record
- query = {'device_id': device_id, 'uptime': {"$lte": end, "$gte": start}}
- info_record = qxz_info_record.find(query, {"_id": 0, 'volt': 1, "rssi": 1})
- result = {"volt": {"data": [], "explan": "电压"}, "rssi": {"data": [], "explan": "信号强度"}}
- for k in info_record:
- result["volt"]["data"].append(float(k.get("volt")))
- result["rssi"]["data"].append(float(k.get("rssi")))
- return result
- def get_sa_qxz_data(device_id, start, end, conf):
- # 获取具体数据
- start = int((datetime.datetime.strptime(start, "%Y-%m-%d %H:%M:%S")).timestamp())
- end = int((datetime.datetime.strptime(end, "%Y-%m-%d %H:%M:%S")).timestamp())
- db = get_table()
- qxz_data = db.sa_qxz_data
- query = {'device_id': device_id, 'uptime': {"$lte": end, "$gte": start}}
- field = conf.keys()
- fields = {i: 1 for i in field}
- fields.update({"_id": 0, "uptime": 1})
- data = qxz_data.find(query, fields).sort("uptime", pymongo.DESCENDING)
- return list(data)
- def get_qxz_conf(device_id):
- print(device_id)
- # 获取配置文件
- db = get_table()
- qxz_conf = db.sa_qxz_conf
- query = {'device_id': device_id}
- conf = qxz_conf.find_one(query, {"_id": 0, "uptime": 0, "device_id": 0, "id": 0, "manual_change": 0})
- conf = {k: v for k, v in conf.items() if v is not None and v != ""}
- conf.update({"uptime": "上传数据条数#999#个"})
- return conf
- def get_conf_data(conf, data):
- conf_keys = conf.keys()
- results = {}
- for i in conf_keys:
- temp = []
- for detail in data:
- value = str(detail.get(i))
- print(value, "+++++++++++")
- if value:
- va_sp = value.split("#")
- print(va_sp, "--------------")
- vk = float(va_sp[0])
- if vk > 9999999999:
- temp.append(9999999999)
- elif vk < -9999999999:
- temp.append(-9999999999)
- else:
- temp.append(vk)
- results[i] = {
- "data": temp,
- "explan": (conf.get(i)).split("#")[0]
- }
- return results
- def device_detail_deivce_id(short_id):
- """确定设备所在平台以及完整设备号"""
- db = get_table()
- regex = re.compile('.*{}$'.format(short_id))
- bd_device_dict = db.sa_device.find_one(
- filter = {"device_id":regex,"device_type_id":5},
- projection = {'_id': 0},
- sort = [('uptime', pymongo.DESCENDING)]
- )
- if bd_device_dict:
- device_id = bd_device_dict.get("device_id")
- position = bd_device_dict.get("province") + bd_device_dict.get("city")+ bd_device_dict.get("district")
- lng, lat = "", ""
- try:
- lng = float(bd_device_dict.get("lng"))
- lat = float(bd_device_dict.get("lat"))
- except Exception as e:
- print(e)
- return device_id, bd_device_dict.get("dver_num"), lng, lat, position
- else:
- return None, "", "", "", ""
- if __name__ == "__main__":
- device_id = '864865060471399'
- get_device_info(device_id)
- start = "2023-08-28 10:00:00"
- end = "2023-08-31 20:00:00"
- base_info = get_sim_info(device_id, start, end)
- conf = get_qxz_conf(device_id)
- data = get_sa_qxz_data(device_id, start, end, conf)
- print(data)
- # result = get_conf_data(conf, data)
- # for k, v in result.items():
- # print(k, v)
|