|
@@ -0,0 +1,178 @@
|
|
|
|
|
+from datetime import datetime
|
|
|
|
|
+import pymongo
|
|
|
|
|
+import pandas as pd
|
|
|
|
|
+from urllib import parse
|
|
|
|
|
+import requests
|
|
|
|
|
+import os
|
|
|
|
|
+import sys
|
|
|
|
|
+import django
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
|
+sys.path.append(BASE_DIR)
|
|
|
|
|
+os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'bigdataAPI.settings')
|
|
|
|
|
+django.setup()
|
|
|
|
|
+from apps.PestAnalysis.models import EnvTempHum, HeNanAddr
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def bigata_mongo():
|
|
|
|
|
+ user = parse.quote_plus("root")
|
|
|
|
|
+ passwd = parse.quote_plus("yfkj@6020")
|
|
|
|
|
+ myclient = pymongo.MongoClient("mongodb://{0}:{1}@8.136.98.49:57017/".format(user,passwd))
|
|
|
|
|
+ return myclient
|
|
|
|
|
+
|
|
|
|
|
+def device_aggrate():
|
|
|
|
|
+ myclient = bigata_mongo()
|
|
|
|
|
+ mydb = myclient.smartfarming
|
|
|
|
|
+ mycol = mydb.sa_device
|
|
|
|
|
+ query = [
|
|
|
|
|
+ {
|
|
|
|
|
+ "$match": {
|
|
|
|
|
+ "province": "河南省",
|
|
|
|
|
+ "device_type_id": 15
|
|
|
|
|
+ }
|
|
|
|
|
+ },
|
|
|
|
|
+ {
|
|
|
|
|
+ "$group": {
|
|
|
|
|
+ "_id": {
|
|
|
|
|
+ "province": "$province",
|
|
|
|
|
+ "city": "$city",
|
|
|
|
|
+ "district": "$district"
|
|
|
|
|
+ },
|
|
|
|
|
+ "device_ids": { "$addToSet": "$device_id" },
|
|
|
|
|
+ "unique_count": { "$sum": 1 }
|
|
|
|
|
+ }
|
|
|
|
|
+ },
|
|
|
|
|
+ {
|
|
|
|
|
+ "$project": {
|
|
|
|
|
+ "location": "$_id",
|
|
|
|
|
+ "device_ids": 1,
|
|
|
|
|
+ "unique_count": 1,
|
|
|
|
|
+ "_id": 0
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ ]
|
|
|
|
|
+ result = mycol.aggregate(query)
|
|
|
|
|
+
|
|
|
|
|
+ # for i in list(result):
|
|
|
|
|
+ # device_ids = i.get("device_ids")
|
|
|
|
|
+ # count = i.get("unique_count")
|
|
|
|
|
+ # location = i.get("location")
|
|
|
|
|
+ # print("地址:", location, "数量", count)
|
|
|
|
|
+ myclient.close()
|
|
|
|
|
+ return list(result)
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def nd_qxz_data(device_ids, start, end):
|
|
|
|
|
+ myclient = bigata_mongo()
|
|
|
|
|
+ mydb = myclient.smartfarming
|
|
|
|
|
+ mycol = mydb.sa_nd_qxz_data
|
|
|
|
|
+ query = {
|
|
|
|
|
+ "upl_time": {
|
|
|
|
|
+ "$gte": start,
|
|
|
|
|
+ "$lte": end
|
|
|
|
|
+ },
|
|
|
|
|
+ "device_id": {
|
|
|
|
|
+ "$in": device_ids
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ project = {
|
|
|
|
|
+ "_id": 0,
|
|
|
|
|
+ "temp": 1,
|
|
|
|
|
+ "swc": 1
|
|
|
|
|
+ }
|
|
|
|
|
+ nd_data = mycol.find(query,project)
|
|
|
|
|
+ myclient.close()
|
|
|
|
|
+ nd_data = list(nd_data)
|
|
|
|
|
+ tmp_list = {}
|
|
|
|
|
+ if nd_data:
|
|
|
|
|
+ for k in nd_data:
|
|
|
|
|
+ temp = k.get("temp")
|
|
|
|
|
+ swc = k.get("swc")
|
|
|
|
|
+ if temp:
|
|
|
|
|
+ temp = temp.split(",")
|
|
|
|
|
+ for index, ti in enumerate(temp):
|
|
|
|
|
+ try:
|
|
|
|
|
+ ti = float(ti)
|
|
|
|
|
+ except:
|
|
|
|
|
+ ti = 0
|
|
|
|
|
+ if "temp_" + str(index) in tmp_list:
|
|
|
|
|
+ tmp_list["temp_" + str(index)].append(ti)
|
|
|
|
|
+ else:
|
|
|
|
|
+ tmp_list["temp_" + str(index)] = [ti]
|
|
|
|
|
+ if swc:
|
|
|
|
|
+ swc = swc.split(",")
|
|
|
|
|
+ for index, si in enumerate(swc):
|
|
|
|
|
+ try:
|
|
|
|
|
+ si = float(si)
|
|
|
|
|
+ except:
|
|
|
|
|
+ si = 0
|
|
|
|
|
+ if "swc_" + str(index) in tmp_list:
|
|
|
|
|
+ tmp_list["swc_" + str(index)].append(si)
|
|
|
|
|
+ else:
|
|
|
|
|
+ tmp_list["swc_" + str(index)] = [si]
|
|
|
|
|
+ avg_data = {}
|
|
|
|
|
+ for k, v in tmp_list.items():
|
|
|
|
|
+ avg_data[k] = round(sum(v) / len(v), 2)
|
|
|
|
|
+ return avg_data
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def main():
|
|
|
|
|
+ start_year = datetime(2025, 1, 1, 0, 0, 0).timestamp()
|
|
|
|
|
+ end_year = datetime(2026, 1, 2, 0, 0, 0).timestamp()
|
|
|
|
|
+ device_addr = device_aggrate()
|
|
|
|
|
+ for i in device_addr:
|
|
|
|
|
+ device_ids = i.get("device_ids")
|
|
|
|
|
+ location = i.get("location")
|
|
|
|
|
+ province = location.get("province")
|
|
|
|
|
+ city = location.get("city")
|
|
|
|
|
+ district = location.get("district")
|
|
|
|
|
+ print(district)
|
|
|
|
|
+ henan = HeNanAddr.objects.filter(province=province, city=city, district=district)
|
|
|
|
|
+ if henan.exists():
|
|
|
|
|
+ henan = henan.first()
|
|
|
|
|
+ addr_code = henan.code
|
|
|
|
|
+ if addr_code:
|
|
|
|
|
+ # 时间
|
|
|
|
|
+ for r in range(int(start_year), int(end_year), 86400):
|
|
|
|
|
+ data = nd_qxz_data(device_ids, r, r + 86400)
|
|
|
|
|
+ format_date = datetime.fromtimestamp(r).strftime("%Y-%m-%d")
|
|
|
|
|
+ for k, v in data.items():
|
|
|
|
|
+ if k.startswith("temp"):
|
|
|
|
|
+ k_lst = k.split("_")
|
|
|
|
|
+ if len(k_lst) == 2:
|
|
|
|
|
+ title = f"{(int(k_lst[1]) + 1) * 10}cm土壤温度"
|
|
|
|
|
+ EnvTempHum.objects.create(
|
|
|
|
|
+ title = title,
|
|
|
|
|
+ value = v,
|
|
|
|
|
+ unit = "℃",
|
|
|
|
|
+ type = "1",
|
|
|
|
|
+ date = r,
|
|
|
|
|
+ format_date = format_date,
|
|
|
|
|
+ province = province,
|
|
|
|
|
+ city = city,
|
|
|
|
|
+ district = district,
|
|
|
|
|
+ addr_code = addr_code
|
|
|
|
|
+ )
|
|
|
|
|
+ elif k.startswith("swc"):
|
|
|
|
|
+ k_lst = k.split("_")
|
|
|
|
|
+ if len(k_lst) == 2:
|
|
|
|
|
+ title = f"{(int(k_lst[1]) + 1) * 10}cm土壤湿度"
|
|
|
|
|
+ EnvTempHum.objects.create(
|
|
|
|
|
+ title = title,
|
|
|
|
|
+ value = v,
|
|
|
|
|
+ unit = "%",
|
|
|
|
|
+ type = "2",
|
|
|
|
|
+ date = r,
|
|
|
|
|
+ format_date = format_date,
|
|
|
|
|
+ province = province,
|
|
|
|
|
+ city = city,
|
|
|
|
|
+ district = district,
|
|
|
|
|
+ addr_code = addr_code
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+if __name__ == '__main__':
|
|
|
|
|
+ main()
|