import os
import sys
import time
import django
local_path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
print(local_path)
if local_path not in sys.path:
    sys.path.append(local_path)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "kedong.settings")
django.setup()
from django.conf import settings
from smartfarming.models.device import MongoDevice, MongoSCDData
import json
import requests
import pymongo
from urllib import parse


my_client = pymongo.MongoClient(host="8.136.98.49", port=27017, username="root", password="yfkj@6020")
my_col = my_client['smartfarming']['sa_device']

scd_device = {
    "865328061465380",
    "865328061478136",
    "865328061471529",
    "865328061466933",
    "865328061428701",
    "865328061466404",
    "865328061449913",
    "865328061455027",
    "865328061271879",
    "865328061253190",
    "865328061460159",
    "865328061438890",
    "865328061429451",
    "869298052295540",
    "865328061465679",
    "865328061450044"
}


def dsj_qxz_conf_info():
    user = parse.quote_plus("root")
    passwd = parse.quote_plus("yfkj@6020")
    # 账号密码方式连接MongoDB | "mongodb://用户名:密码@公网ip:端口/"
    myclient = pymongo.MongoClient("mongodb://{0}:{1}@8.136.98.49:57017/".format(user,passwd))
    # myclient = pymongo.MongoClient("mongodb://127.0.0.1:12514/")
    # 指定数据库
    db = myclient.smartfarming
    # 指定集合
    collection = db.sa_device
    collection1 = db.sa_device_scd_data

    for i in scd_device:
        device_info = collection.find_one({'device_id':i})
        device_id = device_info['id']
        id = device_info["id"]
        device, is_created = MongoDevice.objects.get_or_create(
            device_id = device_info["device_id"],
            defaults={
                "dver_num": device_info["dver_num"],
                "device_model": device_info["device_model"],
                "device_type_id": device_info["device_type_id"],
                "device_name": device_info["device_name"],
                "owner_uid": device_info["owner_uid"],
                "user_dealer": device_info["user_dealer"],
                "province": device_info["province"],
                "city": device_info["city"],
                "district": device_info["district"],
                "device_code": device_info["device_code"],
                "device_status": device_info["device_status"],
                "off_time": device_info["off_time"],
                "ftp_push_addr": device_info["ftp_push_addr"],
                "serverconf": device_info["serverconf"],
                "lng": device_info["lng"],
                "lat": device_info["lat"],
                "gps": device_info["gps"],
                "addtime": device_info["addtime"],
                "uptime": device_info["uptime"],
                "glass_slide_time": device_info["glass_slide_time"],
                "cultivate_time": device_info["cultivate_time"],
                "add_position": device_info["add_position"],
                "simid": device_info["simid"],
                "disable": device_info["disable"],
                "decoy": device_info["decoy"],
                "sim_dealer": device_info["sim_dealer"],
                "xy_uptime": device_info["xy_uptime"],
                "networking": device_info["networking"],
                "equipment": device_info["equipment"],
                "sale_uid": device_info["sale_uid"],
                "salesman_task_number": device_info["salesman_task_number"],
                "whether_equipment_status": device_info["whether_equipment_status"],
                "device_expire_time": device_info["device_expire_time"],
                "device_activation": device_info["device_activation"],
                "device_expire": device_info["device_expire"],
                "device_expire_days": device_info["device_expire_days"],
                "water_pump_switch": device_info["water_pump_switch"],
                "device_notes": device_info["device_notes"],
                "water_pump_times": device_info["water_pump_times"],
                "water_pump_switch_times": device_info["water_pump_switch_times"],
                "xy_expire_time": device_info["xy_expire_time"],
                "device_tag": device_info["device_tag"]
            }
        )
        print(device.id, is_created)
        data = {'device_id': device_id,'addtime':{'$gt':1681808604,'$lt':1682413404}}
        cursor = collection1.find(data,{'_id':0,'id':0,'device_id':0})

        for i in cursor:
            scd_data = MongoSCDData.objects.create(
                device_id=device.id,
                device_data=i["device_data"],
                addtime=i['addtime'],
                device_status=i['device_status']
            )
            print(scd_data)
        # print(f"设备: {i} {device_id} 数据量  {cursor.count()}")
    return True


if __name__ == "__main__":
    dsj_qxz_conf_info()