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.pest_count import MongoCBDPestWarning
import json
import requests
import pymongo
from urllib import parse



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))
    # 指定数据库
    db = myclient.smartfarming
    # 指定集合
    collection = db.sa_device_cbd_pest_warning
    pests = collection.find({})
    for i in pests:
        bank = MongoCBDPestWarning.objects.create(
            device_id = i["device_id"],
            user_id = i["user_id"],
            warning_name = i["warning_name"],
            warning_content = i["warning_content"],
            warning_types = i["warning_types"],
            upltime = i["upltime"],
            send_user = i["send_user"],
            status = i["status"]
        )
    print(f"数量:  {pests.count()}")
    return True


if __name__ == "__main__":
    dsj_qxz_conf_info()