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, MongoDeviceConfig
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']

qx_device = {
"861551055315402",
"861551056086671",
"861551056088693",
"861551056088479",
"861551056088719",
"861551055313365",
"861551056086614",
"861551056093800",
"861551056095367",
"861551056102502",
"861551056096621",
"861551056101785",
"861551056086549",
"861551055354120",
"861551055353536",
"861551056092018",
"861551055319586",
"861551056088875",
"861551055324651",
"861551055313423"
}


def dsj_qxz_conf_info():
    # 初始化气象站与墒情站时间间隔配置
    cmd = '{"cmd": "config", "ext": {"interval": 1}}'
    for k in qx_device:
        in_d_id = MongoDevice.objects.get(device_id=k).id
        config, is_created = MongoDeviceConfig.objects.get_or_create(
                d_id=in_d_id,
                defaults={
                    "device_config": cmd,
                    "cmd": "config",
                    "addtime": int(time.time()),
                    "uptime": int(time.time())
                }
            )
        print(f"设备: {k} 是否新建  {is_created} ")
    return True


if __name__ == "__main__":
    dsj_qxz_conf_info()