yzl 2 år sedan
förälder
incheckning
453df038a0

+ 91 - 0
bigdata_django/zhijian/qxz/mongoclient.py

@@ -0,0 +1,91 @@
+import pymongo
+from urllib import parse
+import datetime
+
+
+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_qxz_base_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, 'uptime': {"$lte": end, "$gte": start}}
+    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})
+    return info_record
+
+
+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})
+    data = qxz_data.find(query, fields)
+    return list(data)
+
+
+def get_qxz_conf(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})
+    conf = {k: v for k, v in conf.items() if v is not None and v != ""}
+    return conf
+
+
+def get_conf_data(conf, data):
+    conf_keys = conf.keys()
+    results = {}
+    for i in conf_keys:
+        results[i] = {
+            "data": [k.get(i) for k in data],
+            "explan": conf.get(i)
+        }
+    return results
+
+
+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_qxz_base_info(device_id, start, end)
+    conf = get_qxz_conf(device_id)
+    data = get_sa_qxz_data(device_id, start, end, conf)
+    result = get_conf_data(conf, data)
+    for k, v in result.items():
+        print(k, v)
+

+ 0 - 0
bigdata_django/zhijian/qxz/special.py


+ 53 - 0
bigdata_django/zhijian/qxz/utils.py

@@ -0,0 +1,53 @@
+import json
+import requests
+import datetime
+
+
+def time_dif(self,checkdatetime):
+    """计算时间差"""
+    nowdatetime = datetime.datetime.now()
+    checkdatetime = datetime.datetime.strptime(checkdatetime, "%Y-%m-%d %H:%M:%S")
+    timedif = checkdatetime - nowdatetime
+    return timedif.days
+
+
+def sim_info(iccid):
+    """查询卡信息"""
+    url = "http://8.136.98.49:10001/iotcard/platsimview/inquiries/"
+    try:
+        response = requests.request("POST", url, data={"iccid": iccid})
+    except:
+        return [0, "查询卡信息异常稍后重试"]
+    else:
+        res_data = json.loads(response.text)
+        if res_data["msg"] == "success" and res_data["data"]:
+            expiry_date = res_data["data"]["expiry_date"]
+            if expiry_date == "未知":
+                return [0, "未查询到卡信息"]
+            else:
+                time_difference = time_dif(expiry_date)
+                if time_difference >= 180:
+                    return [1, "有效期剩余{}天".format(time_difference)]
+                elif 30 <= time_difference < 180:
+                    return [1, "有效期剩余{}天".format(time_difference)]
+                else:
+                    return [1, "有效期剩余{}天".format(time_difference)]
+        else:
+            return [1, "查询无结果"]
+        
+
+def get_position(lng, lat):
+    """使用经纬度获取位置信息"""
+    if lng and lat:
+        try:
+            ret = requests.post("http://api.map.baidu.com/geocoder?location=%s,%s&coord_type=gcj02&output=json"%(lat,lng))
+            ret_json = json.loads(ret.text)
+            province, city, district = ret_json["result"]["addressComponent"]["province"], \
+                                    ret_json["result"]["addressComponent"]["city"], \
+                                    ret_json["result"]["addressComponent"]["district"]
+            
+            return province + city + district
+        except Exception as e:
+            return False
+    else:
+        return False

+ 1 - 20
bigdata_django/zhijian/qxz_zhijian_new.py

@@ -1401,26 +1401,7 @@ class SCDThread(QtCore.QThread):
                                 rt = {'status': 1, 'msg': f'最大值:{str(max(idevice_data))},最小值:{str(min(idevice_data))}'}
                             else:
                                 rt = {'status': 0, 'msg': f'最大值:{str(max(idevice_data))},最小值:{str(min(idevice_data))}'}
-                        # elif k == "热通量#":
-                        #     # 从数据中解析
-                        #     idevice_key_rain = ""
-                        #     device_config = dict(device_config)
-                        #     for iname, ivalue in device_config.items():
-                        #         if ivalue and isinstance(ivalue, str):
-                        #             if "热通量" in ivalue:
-                        #                 idevice_key_rain = iname
-                        #     print(idevice_key_rain)
-                        #     idevice_data = []
-                        #     for d in device_data:
-                        #         idevice_data.append(float((d.get(idevice_key_rain)).split("#")[0]))
-                        #     if idevice_data:
-                        #         if (0 not in idevice_data) and max(idevice_data) < 500 and min(idevice_data) > -500:
-                        #             rt = {'status': 1, 'msg': f'最大值:{str(max(idevice_data))},最小值:{str(min(idevice_data))}'}
-                        #         else:
-                        #             rt = {'status': 0, 'msg': f'最大值:{str(max(idevice_data))},最小值:{str(min(idevice_data))}'}
-                        #         status, msg = rt['status'], rt['msg']
-                        #     else:
-                        #         status, msg = 0, "不合格"
+
                         else:
                             rt = self.cond.cond_msg_dict[key](value)
                     status, msg = rt['status'], rt['msg']