# -*- coding:utf-8 -*-
# @Time    : 2020/5/22 2:22 下午
# @Author  : Creat by Han
import json
import ast
import copy
import datetime
import time
import logging
from django.forms.models import model_to_dict
from django.db.models import Q, Max, Min
from django.conf import settings
from kedong.decoration import kedong_deco, PortError
from smartfarming.models.device import MongoDevice, MongoCBDData
from smartfarming.models.weather import MongoQXZ_Base_Info, QXZdata_New, MongoQXZ_Conf, QXZstatus_New, MongoQXZ_Alarm
from smartfarming.api.views.weather.all_dict import qxz_dict
from smartfarming.models.weather import (
    MongoQXZ_Switch,
    MongoQXZ_Auto_Switch,
    MongoQXZ_Timing_Switch,
    QXZ_Switch_Status,
    QXZ_Default_Conf
)

config_dict = settings.CONFIG
logger = logging.getLogger("myapp")


@kedong_deco(login_required=True)
def qxz_page(request):
    """
    气象设备列表接口 :
    参数:
    page                    非必传(num)                     页码,默认为1
    device_id               非必传(str)                     设备号
    device_status           非必传(str)                     在线状态 1在线 0离线
    device_name             非必传(str)                     设备名称
    device_type             非必传(str)                      5气象站 8墒情站
    返回值:
    {
        "data": {                       
        "ids": [
            {
                "d_id":"",                              设备数据库ID
                "equip_name":"",                        设备名称
                "equip_id":12313456,                    设备ID
                "is_online":1,                          在线状态   1在线 0离线
                "rssi":10,                              信号强度
                "volt":22,                              电压
                "lng":113,                              经度
                "lat":113,                              纬度
                "off_time":""                           离线时间
                "address":""                            设备地址
                "device_expire_time": 1234444     设备到期时间
                "device_expire": # 设备状态 0未到期,1已到期,2即将到期
                "device_expire_days": 到期天数
                }
            ],                  
        "nums": 0                                       设备总数
        },
        "formError": {},
        "errorCode": 0,
        
        "message": "",
        "params": {}
    }

    """
    device_id = request.POST.get("device_id")
    device_status = request.POST.get("device_status")
    device_name = request.POST.get("device_name")
    page = request.POST.get("page","")
    page_size = request.POST.get("page_size","")
    device_type = request.POST.get("device_type","5")
    if not page and not page_size:
        page = 1 
        page_size = 8
    else:
        if not page.isdigit() and  not page_size.isdigit():
            raise PortError("","字段类型有误")
        else:
            page = int(page)
            page_size = int(page_size)
    
    if device_type not in ("5","8"):
        raise PortError("","参数超出范围")


    devices = MongoDevice.objects.filter(device_type_id=device_type).order_by('-device_status', '-uptime')
    #设备状态查询
    if device_status:
        devices = devices.filter(device_status=int(device_status))
    #设备号查询
    if device_id:
        devices = devices.filter(Q(device_id__icontains=device_id) | Q(device_name__icontains=device_id))
    #设备名称查询
    if device_name:
        devices = devices.filter(device_name__icontains=device_name)

    date = []
    nums = devices.count()

    result = devices[(page_size*(page-1)):(page*page_size)]
    result_device_id_list = [item.device_id for item in result]
    qxz_base_dict = {item.device_id: item for item in MongoQXZ_Base_Info.objects.filter(device_id__in=result_device_id_list)}
    for i in result:
        try:
            qxz_base = qxz_base_dict[i.device_id]
            rssi = qxz_base.rssi
            volt = qxz_base.volt
        except:
            rssi = ""
            volt = ""
            
        lng = i.lng
        lat = i.lat
        address = i.province+i.city+i.district
        is_online = i.device_status
        uptime = i.uptime

        date.append({
            "d_id":i.id,
            "equip_name":i.device_name,
            "equip_id":i.device_id,
            "volt":volt,
            "rssi":rssi,
            "lng":lng,
            "lat":lat,
            "address":address,
            "is_online":is_online,
            "uptime":uptime,
        })
    
    data = {'ids':date, 'nums': nums}
    return data


@kedong_deco(login_required=True)
def qxz_detail(request):
    """
    气象历史数据接口 ：
    参数：
    device_id               必传(string)
    page                    非必传(num)                     页码，默认为1
    start_time              非必传(string 时间戳)           开始时间    (用于时间搜索)
    end_time                非必传(string 时间戳)           结束时间    (用于时间搜索)

    返回值：
    {
        "data": {
            "conf": {
            "e1":x.e1,"e2":x.e2,"e3":x.e3,"e4":x.e4,"e5":x.e5,
            "e6":x.e6,"e7":x.e7,"e8":x.e8,"e9":x.e9,"e10":x.e10,
            "e11":x.e11,"e12":x.e12,"e13":x.e13,"e14":x.e14,
            "e15":x.e15,"e16":x.e16,"e17":x.e17,"e18":x.e18,
            "e19":x.e19,"e20":x.e20,"e21":x.e21,"e22":x.e22,
            "e23":x.e23,"e24":x.e24,"e25":x.e25,"e26":x.e26,
            "e27":x.e27,"e28":x.e28,"e29":x.e29,"e30":x.e30
            },
            "nums": 0,
            "rainFall": 1              气象设备是否具有日雨量累计因子,有返回1,无返回0
            "data": [
                {
                "e1":qxz_list.e1,"e2":qxz_list.e2,"e3":qxz_list.e3,"e4":qxz_list.e4,"e5":qxz_list.e5,
                "e6":qxz_list.e6,"e7":qxz_list.e7,"e8":qxz_list.e8,"e9":qxz_list.e9,"e10":qxz_list.e10,
                "e11":qxz_list.e11,"e12":qxz_list.e12,"e13":qxz_list.e13,"e14":qxz_list.e14,
                "e15":qxz_list.e15,"e16":qxz_list.e16,"e17":qxz_list.e17,"e18":qxz_list.e18,
                "e19":qxz_list.e19,"e20":qxz_list.e20,"e21":qxz_list.e21,"e22":qxz_list.e22,
                "e23":qxz_list.e23,"e24":qxz_list.e24,"e25":qxz_list.e25,"e26":qxz_list.e26,
                "e27":qxz_list.e27,"e28":qxz_list.e28,"e29":qxz_list.e29,"e30":qxz_list.e30, 
                "upl_time":qxz_list.upl_time.strftime('%Y-%m-%d %H:%M:%S')
                },
                {
                "e1":qxz_list.e1,"e2":qxz_list.e2,"e3":qxz_list.e3,"e4":qxz_list.e4,"e5":qxz_list.e5,
                "e6":qxz_list.e6,"e7":qxz_list.e7,"e8":qxz_list.e8,"e9":qxz_list.e9,"e10":qxz_list.e10,
                "e11":qxz_list.e11,"e12":qxz_list.e12,"e13":qxz_list.e13,"e14":qxz_list.e14,
                "e15":qxz_list.e15,"e16":qxz_list.e16,"e17":qxz_list.e17,"e18":qxz_list.e18,
                "e19":qxz_list.e19,"e20":qxz_list.e20,"e21":qxz_list.e21,"e22":qxz_list.e22,
                "e23":qxz_list.e23,"e24":qxz_list.e24,"e25":qxz_list.e25,"e26":qxz_list.e26,
                "e27":qxz_list.e27,"e28":qxz_list.e28,"e29":qxz_list.e29,"e30":qxz_list.e30, 
                "upl_time":qxz_list.upl_time.strftime('%Y-%m-%d %H:%M:%S')
                },...
            ]
            }
        "params": {},
        "formError": {},
        "errorCode": 0,
        "message": "",
    
    }

    """
    f_tbegin = request.POST.get('start_time')
    f_tend = request.POST.get('end_time')
    device_id = request.POST.get("device_id")
    try:

        device = MongoDevice.objects.get(device_id=device_id)
        device_expire = device.device_expire
        device_expire_time = device.device_expire_time
    except Exception as e:
        logger.error(f"气象站数据：{e} {device_id}")
        raise PortError('device_id',"暂无此设备")
    
    qxz_list = QXZdata_New.objects.filter(device_id=device_id)
    
    myuser_type = request.user_type
    if not myuser_type == 1 and not myuser_type == 5:
        if f_tbegin and int(f_tend) > int(device_expire_time):
            f_tend = device_expire_time
    if f_tbegin and f_tend:
        qxz_list =qxz_list.filter(uptime__range=(int(f_tbegin),int(f_tend))).order_by('-id')
    page = int(request.POST.get('page',1))
    data = []
    nums = qxz_list.count()
    conf = {}
    if nums:
        for i in qxz_list[(10*(page-1)):(page*10)]:
            dat = {}
            for e in range(1, 31):
                k = f'e{e}'
                v = getattr(i, k)
                dat[k] = v or ""
            data.append({"dat": dat, "time": i.uptime})

        try:
            x = MongoQXZ_Conf.objects.get(device_id=device_id)
            conf = {}
            for e in range(1, 31):
                k = f'e{e}'
                v = getattr(x, k)
                conf[k] = v or ""
        except Exception as e:
            conf = {}
    rainFall = 0
    for factor in conf.values():
        if factor == "降雨量累计#mm":
            rainFall = 1
            break
    data1 = {"data":data,"nums":nums,"conf":conf,"rainFall":rainFall}
    return data1


@kedong_deco(login_required=True)
def qxz_status(request):
    """
    气象详情接口 :
    参数:
    device_id               必传(string)
    返回值:
    {
    "data": {
        'dat':{
                "e1":qxz_list.e1,"e2":qxz_list.e2,"e3":qxz_list.e3,"e4":qxz_list.e4,"e5":qxz_list.e5,
                "e6":qxz_list.e6,"e7":qxz_list.e7,"e8":qxz_list.e8,"e9":qxz_list.e9,"e10":qxz_list.e10,
                "e11":qxz_list.e11,"e12":qxz_list.e12,"e13":qxz_list.e13,"e14":qxz_list.e14,
                "e15":qxz_list.e15,"e16":qxz_list.e16,"e17":qxz_list.e17,"e18":qxz_list.e18,
                "e19":qxz_list.e19,"e20":qxz_list.e20,"e21":qxz_list.e21,"e22":qxz_list.e22,
                "e23":qxz_list.e23,"e24":qxz_list.e24,"e25":qxz_list.e25,"e26":qxz_list.e26,
                "e27":qxz_list.e27,"e28":qxz_list.e28,"e29":qxz_list.e29,"e30":qxz_list.e30, 
                "upl_time":qxz_list.upl_time.strftime('%Y-%m-%d %H:%M:%S')
                },
        'conf':{
            "e1":x.e1,"e2":x.e2,"e3":x.e3,"e4":x.e4,"e5":x.e5,
            "e6":x.e6,"e7":x.e7,"e8":x.e8,"e9":x.e9,"e10":x.e10,
            "e11":x.e11,"e12":x.e12,"e13":x.e13,"e14":x.e14,
            "e15":x.e15,"e16":x.e16,"e17":x.e17,"e18":x.e18,
            "e19":x.e19,"e20":x.e20,"e21":x.e21,"e22":x.e22,
            "e23":x.e23,"e24":x.e24,"e25":x.e25,"e26":x.e26,
            "e27":x.e27,"e28":x.e28,"e29":x.e29,"e30":x.e30
            },
            "led":0,           0 无led点阵屏功能  1 有led点阵屏功能                                    
            "ledinfo":"点阵屏内容"
            },
    "errorCode": 0,
    "message": "",
    "formError": {}
    }

    """
    e_id = request.POST.get("device_id")
    if not e_id:
        raise PortError("","参数缺失")
    qxz_list = QXZdata_New.objects.filter(device_id=e_id).order_by("-uptime").first()
    if not qxz_list:
        raise PortError("","未找到此设备")
    data = []
    e12 = qxz_list.e12 if qxz_list.e12 else "0#110#e12"
    dat = {"e1":qxz_list.e1,"e2":qxz_list.e2,"e3":qxz_list.e3,"e4":qxz_list.e4,"e5":qxz_list.e5,
            "e6":qxz_list.e6,"e7":qxz_list.e7,"e8":qxz_list.e8,"e9":qxz_list.e9,"e10":qxz_list.e10,
            "e11":qxz_list.e11,"e12":e12,"e13":qxz_list.e13,"e14":qxz_list.e14,
            "e15":qxz_list.e15,"e16":qxz_list.e16,"e17":qxz_list.e17,"e18":qxz_list.e18,
            "e19":qxz_list.e19,"e20":qxz_list.e20,"e21":qxz_list.e21,"e22":qxz_list.e22,
            "e23":qxz_list.e23,"e24":qxz_list.e24,"e25":qxz_list.e25,"e26":qxz_list.e26,
            "e27":qxz_list.e27,"e28":qxz_list.e28,"e29":qxz_list.e29,"e30":qxz_list.e30, "uptime":qxz_list.uptime}
    try:
        x = MongoQXZ_Conf.objects.get(device_id=e_id)
        conf = {"e1":x.e1,"e2":x.e2,"e3":x.e3,"e4":x.e4,"e5":x.e5,
                "e6":x.e6,"e7":x.e7,"e8":x.e8,"e9":x.e9,"e10":x.e10,
                "e11":x.e11,"e12":x.e12,"e13":x.e13,"e14":x.e14,
                "e15":x.e15,"e16":x.e16,"e17":x.e17,"e18":x.e18,
                "e19":x.e19,"e20":x.e20,"e21":x.e21,"e22":x.e22,
                "e23":x.e23,"e24":x.e24,"e25":x.e25,"e26":x.e26,
                "e27":x.e27,"e28":x.e28,"e29":x.e29,"e30":x.e30}
    except:
        conf = {}
    try:
        led = MongoQXZ_Base_Info.objects.get(device_id=e_id).led
    except:
        led = "0"
    try:
        ledinfo = MongoQXZ_Base_Info.objects.get(device_id=e_id).ledinfo
    except:
        ledinfo = ""
    data = {'dat':dat,'conf':conf, "led":led,"ledinfo":ledinfo}
    return data


@kedong_deco(login_required=True)
def qxz_data_chart(request):
    """
    气象折线图接口 ：
    参数：
    device_id               必传(string)

    begin              必传(string 时间戳)             开始时间    (用于时间搜索)     默认近一个月
    end                非必传(string 时间戳)           结束时间    (用于时间搜索)
    返回值：
    {
        "data": {
            "conf": {
            "e1":x.e1,"e2":x.e2,"e3":x.e3,"e4":x.e4,"e5":x.e5,
            "e6":x.e6,"e7":x.e7,"e8":x.e8,"e9":x.e9,"e10":x.e10,
            "e11":x.e11,"e12":x.e12,"e13":x.e13,"e14":x.e14,
            "e15":x.e15,"e16":x.e16,"e17":x.e17,"e18":x.e18,
            "e19":x.e19,"e20":x.e20,"e21":x.e21,"e22":x.e22,
            "e23":x.e23,"e24":x.e24,"e25":x.e25,"e26":x.e26,
            "e27":x.e27,"e28":x.e28,"e29":x.e29,"e30":x.e30
            },
            "nums": 0,
            "data": [
                {
                "e1":qxz_list.e1,"e2":qxz_list.e2,"e3":qxz_list.e3,"e4":qxz_list.e4,"e5":qxz_list.e5,
                "e6":qxz_list.e6,"e7":qxz_list.e7,"e8":qxz_list.e8,"e9":qxz_list.e9,"e10":qxz_list.e10,
                "e11":qxz_list.e11,"e12":qxz_list.e12,"e13":qxz_list.e13,"e14":qxz_list.e14,
                "e15":qxz_list.e15,"e16":qxz_list.e16,"e17":qxz_list.e17,"e18":qxz_list.e18,
                "e19":qxz_list.e19,"e20":qxz_list.e20,"e21":qxz_list.e21,"e22":qxz_list.e22,
                "e23":qxz_list.e23,"e24":qxz_list.e24,"e25":qxz_list.e25,"e26":qxz_list.e26,
                "e27":qxz_list.e27,"e28":qxz_list.e28,"e29":qxz_list.e29,"e30":qxz_list.e30, 
                "upl_time":qxz_list.upl_time.strftime('%Y-%m-%d %H:%M:%S')
                },
                {
                "e1":qxz_list.e1,"e2":qxz_list.e2,"e3":qxz_list.e3,"e4":qxz_list.e4,"e5":qxz_list.e5,
                "e6":qxz_list.e6,"e7":qxz_list.e7,"e8":qxz_list.e8,"e9":qxz_list.e9,"e10":qxz_list.e10,
                "e11":qxz_list.e11,"e12":qxz_list.e12,"e13":qxz_list.e13,"e14":qxz_list.e14,
                "e15":qxz_list.e15,"e16":qxz_list.e16,"e17":qxz_list.e17,"e18":qxz_list.e18,
                "e19":qxz_list.e19,"e20":qxz_list.e20,"e21":qxz_list.e21,"e22":qxz_list.e22,
                "e23":qxz_list.e23,"e24":qxz_list.e24,"e25":qxz_list.e25,"e26":qxz_list.e26,
                "e27":qxz_list.e27,"e28":qxz_list.e28,"e29":qxz_list.e29,"e30":qxz_list.e30, 
                "upl_time":qxz_list.upl_time.strftime('%Y-%m-%d %H:%M:%S')
                },...
            ]
            }
        "params": {},
        "formError": {},
        "errorCode": 0,
        "message": "",
    
    }

    """
    
    device_id = request.POST.get("device_id")
    data = []
    f_tbegin = request.POST.get('begin')
    f_tend = request.POST.get('end')

    try:
        device = MongoDevice.objects.get(device_id=device_id)
        device_expire = device.device_expire
        device_expire_time = device.device_expire_time
    except:
        raise PortError('device_id',"暂无此设备")
    
    qxz_list = QXZdata_New.objects.filter(device_id=device_id)
    
    myuser_type = request.user_type
    if not myuser_type == 1 and not myuser_type == 5:
        if f_tbegin and int(f_tend) > int(device_expire_time):
            f_tend = device_expire_time
    if f_tbegin and f_tend:
        qxz_list =qxz_list.filter(uptime__range=(int(f_tbegin),int(f_tend))).order_by('-id')

    conf = {}
    for i in qxz_list:
        data.append({"dat":{"e1":i.e1 if i.e1 else "0#0#e1","e2":i.e2 if i.e2 else "0#0#e2","e3":i.e3 if i.e3 else "0#0#e3","e4":i.e4 if i.e4 else "0#0#e4","e5":i.e5 if i.e5 else "0#0#e5" ,
                "e6":i.e6 if i.e6 else "0#0#e6","e7":i.e7 if i.e7 else "0#0#e7","e8":i.e8 if i.e8 else "0#0#e8","e9":i.e9 if i.e9 else "0#0#e9","e10":i.e10 if i.e10 else "0#0#e10",
                "e11":i.e11 if i.e11 else "0#0#e11","e12":i.e12 if i.e2 else "0#0#e12","e13":i.e13,"e14":i.e14,
                "e15":i.e15,"e16":i.e16,"e17":i.e17,"e18":i.e18,
                "e19":i.e19,"e20":i.e20,"e21":i.e21,"e22":i.e22,
                "e23":i.e23,"e24":i.e24,"e25":i.e25,"e26":i.e26,
                "e27":i.e27,"e28":i.e28,"e29":i.e29,"e30":i.e30},
                "time":i.uptime})

    try:
        x = MongoQXZ_Conf.objects.get(device_id=device_id)
        conf = {"e1":x.e1,"e2":x.e2,"e3":x.e3,"e4":x.e4,"e5":x.e5,
                "e6":x.e6,"e7":x.e7,"e8":x.e8,"e9":x.e9,"e10":x.e10,
                "e11":x.e11,"e12":x.e12,"e13":x.e13,"e14":x.e14,
                "e15":x.e15,"e16":x.e16,"e17":x.e17,"e18":x.e18,
                "e19":x.e19,"e20":x.e20,"e21":x.e21,"e22":x.e22,
                "e23":x.e23,"e24":x.e24,"e25":x.e25,"e26":x.e26,
                "e27":x.e27,"e28":x.e28,"e29":x.e29,"e30":x.e30}
    except:
        conf = {}

    data1 = {"data":data,"conf":conf}
    return data1


@kedong_deco(login_required=True)
def qxz_early_warning(request):
    """
    气象站查看短信预警信息接口\配置预警信息接口
    参数：
    device_id          必传(str)                设备号
    status             必传(str)                当status等于see查看， 当status等于save保存
    alarm              非必传(json)             配置信息，例如：{"dat":{"e1":"1#12","e2":"0#","e3":"0#","e4":"0#","e5":"0#","e6":"0#","e7":"0#","e8":"0#","e9":"0#"},"tel":"18258845915","equip_id":"865553058440811"}
    
    返回值：
    {
        "data" 0  0 保存成功 1失败
    }
    """
    status = request.POST.get("status")
    device_id = request.POST.get("device_id")

    if status == "see":
        try:
            qxz_alarm = MongoQXZ_Alarm.objects.get(device_id=device_id)
            alarm = ast.literal_eval(qxz_alarm.conf)
            phone = qxz_alarm.phone 
        except Exception as e:
            print(e)
            alarm = ""
            phone = ""
        x = MongoQXZ_Conf.objects.get(device_id=device_id)
        conf = {"e1":x.e1,"e2":x.e2,"e3":x.e3,"e4":x.e4,"e5":x.e5,
                    "e6":x.e6,"e7":x.e7,"e8":x.e8,"e9":x.e9,"e10":x.e10,
                    "e11":x.e11,"e12":x.e12,"e13":x.e13,"e14":x.e14,
                    "e15":x.e15,"e16":x.e16,"e17":x.e17,"e18":x.e18,
                    "e19":x.e19,"e20":x.e20,"e21":x.e21,"e22":x.e22,
                    "e23":x.e23,"e24":x.e24,"e25":x.e25,"e26":x.e26,
                    "e27":x.e27,"e28":x.e28,"e29":x.e29,"e30":x.e30}
        for k in list(conf.keys()): 
            if not conf[k]:
                del conf[k]
        data = {"conf":conf,"alarm":alarm,"phone":phone}

    elif status == "save":
        alarm = ast.literal_eval(request.POST.get('alarm'))
        conf = alarm["dat"]
        try:
            MongoQXZ_Alarm.objects.update_or_create(
                device_id=device_id,
                defaults={
                    "conf": {"dat": conf},
                    "upl_time": int(time.time())
                }
            )
            return {"code":0}
        except Exception as e:
            raise PortError("","更新失败") 
    else:
        raise PortError("","参数超出范围")
    return data


def qxz_configs(conf,e_data,es):
    """气象站要素配置"""
    e3s = conf.get(es,"")
    code = e_data.split('#')[1]
    if e3s:
        config = e3s.split("#")
        config.insert(0,code)
    else:
        config = qxz_dict_1[e_data.split('#')[1]]
    return config


@kedong_deco(login_required=True)
def qxz_day_data(request):
    """
    气象24H数据接口 :
    参数:
    device_id               必传(string)

    """
    device_id = request.POST.get("device_id")
    # 获取最近24小时的开始时间与结束时间
    end = int(time.time())
    start = end - 24 * 3600
    # 查询数据
    qxz_data = QXZdata_New.objects.filter(device_id=device_id).filter(uptime__range=(start, end))
    e1, e2 ,e3,e4,e5,e6,e7,e8,e9,e10 = [999999, -99999, 0, 0],[999999, -99999, 0, 0],[999999, -99999, 0, 0],[999999, -99999, 0, 0],[999999, -99999, 0, 0],[999999, -99999, 0, 0],[999999, -99999, 0, 0],[999999, -99999, 0, 0],[999999, -99999, 0, 0],[999999, -99999, 0, 0]
    e11,e12,e13,e14,e15,e16,e17,e18,e19,e20 = [999999, -99999, 0, 0],[999999, -99999, 0, 0],[999999, -99999, 0, 0],[999999, -99999, 0, 0],[999999, -99999, 0, 0],[999999, -99999, 0, 0],[999999, -99999, 0, 0],[999999, -99999, 0, 0],[999999, -99999, 0, 0],[999999, -99999, 0, 0]
    e21,e22,e23,e24,e25,e26,e27,e28,e29,e30 = [999999, -99999, 0, 0],[999999, -99999, 0, 0],[999999, -99999, 0, 0],[999999, -99999, 0, 0],[999999, -99999, 0, 0],[999999, -99999, 0, 0],[999999, -99999, 0, 0],[999999, -99999, 0, 0],[999999, -99999, 0, 0],[999999, -99999, 0, 0]
    for m in qxz_data:
        uptime = m.uptime
        k1 = (m.e1)
        if k1:
            k1 = float(k1.split("#")[0])
            if e1[0] >= k1:
                e1[0] = k1
                e1[2] = uptime
            if e1[1] <= k1:
                e1[1] = k1
                e1[3] = uptime
        k2 = (m.e2)
        if k2:
            k2 = float(k2.split("#")[0])
            if e2[0] >= k2:
                e2[0] = k2
                e2[2] = uptime
            if e2[1] <= k2:
                e2[1] = k2
                e2[3] = uptime
        k3 = (m.e3)
        if k3:
            k3 = float(k3.split("#")[0])
            if e3[0] >= k3:
                e3[0] = k3
                e3[2] = uptime
            if e3[1] <= k3:
                e3[1] = k3
                e3[3] = uptime
        k4 = (m.e4)
        if k4:
            k4 = float(k4.split("#")[0])
            if e4[0] >= k4:
                e4[0] = k4
                e4[2] = uptime
            if e4[1] <= k4:
                e4[1] = k4
                e4[3] = uptime
        k5 = m.e5
        if k5:
            k5 = float(k5.split("#")[0])
            if e5[0] >= k5:
                e5[0] = k5
                e5[2] = uptime
            if e5[1] <= k5:
                e5[1] = k5
                e5[3] = uptime
        k6 = (m.e6)
        if k6:
            k6 = float(k6.split("#")[0])
            if e6[0] >= k6:
                e6[0] = k6
                e6[2] = uptime
            if e6[1] <= k6:
                e6[1] = k6
                e6[3] = uptime
        k7 = (m.e7)
        if k7:
            k7 = float(k7.split("#")[0])
            if e7[0] >= k7:
                e7[0] = k7
                e7[2] = uptime
            if e7[1] <= k7:
                e7[1] = k7
                e7[3] = uptime
        k8 = (m.e8)
        if k8:
            k8 = float(k8.split("#")[0])
            if e8[0] >= k8:
                e8[0] = k8
                e8[2] = uptime
            if e8[1] <= k8:
                e8[1] = k8
                e8[3] = uptime
        k9 = (m.e9)
        if k9:
            k9 = float(k9.split("#")[0])
            if e9[0] >= k9:
                e9[0] = k9
                e9[2] = uptime
            if e9[1] <= k9:
                e9[1] = k9
                e9[3] = uptime
        k10 = (m.e10)
        if k10:
            k10 = float(k10.split("#")[0])
            if e10[0] >= k10:
                e10[0] = k10
                e10[2] = uptime
            if e10[1] <= k10:
                e10[1] = k10
                e10[3] = uptime
        k11 = (m.e11)
        if k11:
            k11 = float(k11.split("#")[0])
            if e11[0] >= k11:
                e11[0] = k11
                e11[2] = uptime
            if e11[1] <= k11:
                e11[1] = k11
                e11[3] = uptime
        k12 = (m.e12)
        if k12:
            k12 = float(k12.split("#")[0])
            if e12[0] >= k12:
                e12[0] = k12
                e12[2] = uptime
            if e12[1] <= k12:
                e12[1] = k12
                e12[3] = uptime
        k13 = (m.e13)
        if k13:
            k13 = float(k13.split("#")[0])
            if e13[0] >= k13:
                e13[0] = k13
                e13[2] = uptime
            if e13[1] <= k13:
                e13[1] = k13
                e13[3] = uptime
        k14 = (m.e14)
        if k14:
            k14 = float(k14.split("#")[0])
            if e14[0] >= k14:
                e14[0] = k14
                e14[2] = uptime
            if e14[1] <= k14:
                e14[1] = k14
                e14[3] = uptime
        k15 = (m.e15)
        if k15:
            k15 = float(k15.split("#")[0])
            if e15[0] >= k15:
                e15[0] = k15
                e15[2] = uptime
            if e15[1] <= k15:
                e15[1] = k15
                e15[3] = uptime
        k16 = (m.e16)
        if k16:
            k16 = float(k16.split("#")[0])
            if e16[0] >= k16:
                e16[0] = k16
                e16[2] = uptime
            if e16[1] <= k16:
                e16[1] = k16
                e16[3] = uptime
        k17 = (m.e17)
        if k17:
            k17 = float(k17.split("#")[0])
            if e17[0] >= k17:
                e17[0] = k17
                e17[2] = uptime
            if e17[1] <= k17:
                e17[1] = k17
                e17[3] = uptime
        k18 = (m.e18)
        if k18:
            k18 = float(k18.split("#")[0])
            if e18[0] >= k18:
                e18[0] = k18
                e18[2] = uptime
            if e18[1] <= k18:
                e18[1] = k18
                e18[3] = uptime
        k19 = (m.e19)
        if k19:
            k19 = float(k19.split("#")[0])
            if e19[0] >= k19:
                e19[0] = k19
                e19[2] = uptime
            if e19[1] <= k19:
                e19[1] = k19
                e19[3] = uptime
        k20 = (m.e20)
        if k20:
            k20 = float(k20.split("#")[0])
            if e20[0] >= k20:
                e20[0] = k20
                e20[2] = uptime
            if e20[1] <= k20:
                e20[1] = k20
                e20[3] = uptime
        k21 = (m.e21)
        if k21:
            k21 = float(k21.split("#")[0])
            if e21[0] >= k21:
                e21[0] = k21
                e21[2] = uptime
            if e21[1] <= k21:
                e21[1] = k21
                e21[3] = uptime
        k22 = (m.e22)
        if k22:
            k22 = float(k22.split("#")[0])
            if e22[0] >= k22:
                e22[0] = k22
                e22[2] = uptime
            if e22[1] <= k22:
                e22[1] = k22
                e22[3] = uptime
        k23 = (m.e23)
        if k23:
            k23 = float(k23.split("#")[0])
            if e23[0] >= k23:
                e23[0] = k23
                e23[2] = uptime
            if e23[1] <= k23:
                e23[1] = k23
                e23[3] = uptime
        k24 = (m.e24)
        if k24:
            k24 = float(k24.split("#")[0])
            if e24[0] >= k24:
                e24[0] = k24
                e24[2] = uptime
            if e24[1] <= k24:
                e24[1] = k24
                e24[3] = uptime
        k25 = (m.e25)
        if k25:
            k25 = float(k25.split("#")[0])
            if e25[0] >= k25:
                e25[0] = k25
                e25[2] = uptime
            if e25[1] <= k25:
                e25[1] = k25
                e25[3] = uptime
        k26 = (m.e26)
        if k26:
            k26 = float(k26.split("#")[0])
            if e26[0] >= k26:
                e26[0] = k26
                e26[2] = uptime
            if e26[1] <= k26:
                e26[1] = k26
                e26[3] = uptime
        k27 = (m.e27)
        if k27:
            k27 = float(k27.split("#")[0])
            if e27[0] >= k27:
                e27[0] = k27
                e27[2] = uptime
            if e27[1] <= k27:
                e27[1] = k27
                e27[3] = uptime
        k28 = (m.e28)
        if k28:
            k28 = float(k28.split("#")[0])
            if e28[0] >= k28:
                e28[0] = k28
                e28[2] = uptime
            if e28[1] <= k28:
                e28[1] = k28
                e28[3] = uptime
        k29 = (m.e29)
        if k29:
            k29 = float(k29.split("#")[0])
            if e29[0] >= k29:
                e29[0] = k29
                e29[2] = uptime
            if e29[1] <= k29:
                e29[1] = k29
                e29[3] = uptime
        k30 = (m.e30)
        if k30:
            k30 = float(k30.split("#")[0])
            if e30[0] >= k30:
                e30[0] = k30
                e30[2] = uptime
            if e30[1] <= k30:
                e30[1] = k30
                e30[3] = uptime


    conf = {}
    x = MongoQXZ_Conf.objects.filter(device_id=device_id).first()
    if x:
        conf = {"e1":x.e1,"e2":x.e2,"e3":x.e3,"e4":x.e4,"e5":x.e5,
                "e6":x.e6,"e7":x.e7,"e8":x.e8,"e9":x.e9,"e10":x.e10,
                "e11":x.e11,"e12":x.e12,"e13":x.e13,"e14":x.e14,
                "e15":x.e15,"e16":x.e16,"e17":x.e17,"e18":x.e18,
                "e19":x.e19,"e20":x.e20,"e21":x.e21,"e22":x.e22,
                "e23":x.e23,"e24":x.e24,"e25":x.e25,"e26":x.e26,
                "e27":x.e27,"e28":x.e28,"e29":x.e29,"e30":x.e30}
    try:
        data = []
        for k, v in conf.items():
            if v:
                if k == "e1" and e1[-1] != 0:
                    data.append({
                        "max": e1[1],
                        "maxtime": e1[3],
                        "min": e1[0],
                        "mintime": e1[2],
                        "enum": [
                            "",
                            (v.split("#"))[0],
                            (v.split("#"))[1]
                        ],
                        "ekey": "e1"
                    })
                if k == "e2" and e2[-1] != 0:
                    data.append({
                        "max": e2[1],
                        "maxtime": e2[3],
                        "min": e2[0],
                        "mintime": e2[2],
                        "enum": [
                            "",
                            (v.split("#"))[0],
                            (v.split("#"))[1]
                        ],
                        "ekey": "e2"
                    })
                if k == "e3" and e3[-1] != 0:
                    data.append({
                        "max": e3[1],
                        "maxtime": e3[3],
                        "min": e3[0],
                        "mintime": e3[2],
                        "enum": [
                            "",
                            (v.split("#"))[0],
                            (v.split("#"))[1]
                        ],
                        "ekey": "e3"
                    })
                if k == "e4" and e4[-1] != 0:
                    data.append({
                        "max": e4[1],
                        "maxtime": e4[3],
                        "min": e4[0],
                        "mintime": e4[2],
                        "enum": [
                            "",
                            (v.split("#"))[0],
                            (v.split("#"))[1]
                        ],
                        "ekey": "e4"
                    })
                if k == "e5" and e5[-1] != 0:
                    data.append({
                        "max": e5[1],
                        "maxtime": e5[3],
                        "min": e5[0],
                        "mintime": e5[2],
                        "enum": [
                            "",
                            (v.split("#"))[0],
                            (v.split("#"))[1]
                        ],
                        "ekey": "e5"
                    })
                if k == "e6" and e6[-1] != 0:
                    data.append({
                        "max": e6[1],
                        "maxtime": e6[3],
                        "min": e6[0],
                        "mintime": e6[2],
                        "enum": [
                            "",
                            (v.split("#"))[0],
                            (v.split("#"))[1]
                        ],
                        "ekey": "e6"
                    })
                if k == "e7" and e7[-1] != 0:
                    data.append({
                        "max": e7[1],
                        "maxtime": e7[3],
                        "min": e7[0],
                        "mintime": e7[2],
                        "enum": [
                            "",
                            (v.split("#"))[0],
                            (v.split("#"))[1]
                        ],
                        "ekey": "e7"
                    })
                if k == "e8" and e8[-1] != 0:
                    data.append({
                        "max": e8[1],
                        "maxtime": e8[3],
                        "min": e8[0],
                        "mintime": e8[2],
                        "enum": [
                            "",
                            (v.split("#"))[0],
                            (v.split("#"))[1]
                        ],
                        "ekey": "e8"
                    })
                if k == "e9" and e9[-1] != 0:
                    data.append({
                        "max": e9[1],
                        "maxtime": e9[3],
                        "min": e9[0],
                        "mintime": e9[2],
                        "enum": [
                            "",
                            (v.split("#"))[0],
                            (v.split("#"))[1]
                        ],
                        "ekey": "e9"
                    })
                if k == "e10" and e10[-1] != 0:
                    data.append({
                        "max": e10[1],
                        "maxtime": e10[3],
                        "min": e10[0],
                        "mintime": e10[2],
                        "enum": [
                            "",
                            (v.split("#"))[0],
                            (v.split("#"))[1]
                        ],
                        "ekey": "e10"
                    })
                if k == "e11" and e11[-1] != 0:
                    data.append({
                        "max": e11[1],
                        "maxtime": e11[3],
                        "min": e11[0],
                        "mintime": e11[2],
                        "enum": [
                            "",
                            (v.split("#"))[0],
                            (v.split("#"))[1]
                        ],
                        "ekey": "e11"
                    })
                if k == "e12" and e12[-1] != 0:
                    data.append({
                        "max": e12[1],
                        "maxtime": e12[3],
                        "min": e12[0],
                        "mintime": e12[2],
                        "enum": [
                            "",
                            (v.split("#"))[0],
                            (v.split("#"))[1]
                        ],
                        "ekey": "e12"
                    })
                if k == "e13" and e13[-1] != 0:
                    data.append({
                        "max": e13[1],
                        "maxtime": e13[3],
                        "min": e13[0],
                        "mintime": e13[2],
                        "enum": [
                            "",
                            (v.split("#"))[0],
                            (v.split("#"))[1]
                        ],
                        "ekey": "e13"
                    })
                if k == "e14" and e14[-1] != 0:
                    data.append({
                        "max": e14[1],
                        "maxtime": e14[3],
                        "min": e14[0],
                        "mintime": e14[2],
                        "enum": [
                            "",
                            (v.split("#"))[0],
                            (v.split("#"))[1]
                        ],
                        "ekey": "e14"
                    })
                if k == "e15" and e15[-1] != 0:
                    data.append({
                        "max": e15[1],
                        "maxtime": e15[3],
                        "min": e15[0],
                        "mintime": e15[2],
                        "enum": [
                            "",
                            (v.split("#"))[0],
                            (v.split("#"))[1]
                        ],
                        "ekey": "e15"
                    })
                if k == "e16" and e16[-1] != 0:
                    data.append({
                        "max": e16[1],
                        "maxtime": e16[3],
                        "min": e16[0],
                        "mintime": e16[2],
                        "enum": [
                            "",
                            (v.split("#"))[0],
                            (v.split("#"))[1]
                        ],
                        "ekey": "e16"
                    })
                if k == "e17" and e17[-1] != 0:
                    data.append({
                        "max": e17[1],
                        "maxtime": e17[3],
                        "min": e17[0],
                        "mintime": e17[2],
                        "enum": [
                            "",
                            (v.split("#"))[0],
                            (v.split("#"))[1]
                        ],
                        "ekey": "e17"
                    })
                if k == "e18" and e18[-1] != 0:
                    data.append({
                        "max": e18[1],
                        "maxtime": e18[3],
                        "min": e18[0],
                        "mintime": e18[2],
                        "enum": [
                            "",
                            (v.split("#"))[0],
                            (v.split("#"))[1]
                        ],
                        "ekey": "e18"
                    })
                if k == "e19" and e19[-1] != 0:
                    data.append({
                        "max": e19[1],
                        "maxtime": e19[3],
                        "min": e19[0],
                        "mintime": e19[2],
                        "enum": [
                            "",
                            (v.split("#"))[0],
                            (v.split("#"))[1]
                        ],
                        "ekey": "e19"
                    })
                if k == "e20" and e20[-1] != 0:
                    data.append({
                        "max": e20[1],
                        "maxtime": e20[3],
                        "min": e20[0],
                        "mintime": e20[2],
                        "enum": [
                            "",
                            (v.split("#"))[0],
                            (v.split("#"))[1]
                        ],
                        "ekey": "e20"
                    })

    except Exception as e:
        logger.error(f"气象站24h数据：{e}")
        raise PortError(" ", "计算数据失败,稍后重试")
    dat = {"data":data,"conf":conf}
    return dat


@kedong_deco(login_required=False)
def device_status_data(request):
    """
    虫情测报灯/数据详情
    设备实时状态接口
        参数:
        device_id               必传                            设备id
        返回值:
        "data": {
        "upds": 1,              上仓门状态 1打开,0关闭
        "tpl": 5,               低温保护阈值温度
        "dver": "1.0.0",        设备固件版本
        "gs": 0,                通道状态 1落虫,0排水
        "hs": 0,                加热状态 1加热,0正常
        "dnds": 0,              下仓门状态 1打开,0关闭
        "is_online": 0,         在线状态
        "csq": 29,              信号强度
        "tph": 70,              高温保护阈值温度
        "ts": 0                 定时模式 0光控,1时控
        "tps": 1                温控状态 1 温控,0 正常
        "lps": 0                光控状态 1 光控,0 正常
        "hrt": 22               加热仓实时温度
        "device_name":"这里是设备名称",
        "address": "这里是设备位置",
        "iccid": 1234444  SIM卡号
    }
    """
    post_info = request.POST
    _device_id = post_info.get("device_id")
    if not _device_id:
        raise PortError('device_id', "该设备不存在")
    try:
        device = MongoDevice.objects.get(device_id=_device_id)
    except:
        raise PortError('device_id','暂无此设备')
    d_id = device.id
    device_name = device.device_name
    province = device.province
    city = device.city
    district = device.district
    device_status = device.device_status  # 在线状态
    myuser_type = request.user_type
    device_datas = MongoCBDData.objects.filter(device_id=d_id).order_by('-id')
    try:
        device_data = json.loads(device_datas[0].device_data)
    except json.JSONDecodeError as e:
        device_data = eval(device_datas[0].device_data)
    except:
        raise PortError("device_id","暂无数据")
    
    ds = device_data.get("ds",1 )
    ts = device_data.get("ts",1)
    up_st = {
        "is_online": device_status,
        "ts":ts,
        "hs":device_data["hs"],
        "upds":device_data["upds"],
        "dnds":device_data["dnds"],
        "csq":device_data["csq"],
        "dver":device_data["dver"],
        "tph":device_data["tph"],
        "tpl":device_data["tpl"],
        "tps":device_data["tps"],
        "lps":device_data["lps"],
        "hrt":device_data["hrt"],
        "iccid":device_data["iccid"],
        "rps":device_data["rps"],
        "ah":device_data["ah"],
        "at":device_data["at"],
        "ds":ds,
        "device_name":device_name,
        "address": province + city + district,
    }
    return up_st

@kedong_deco(login_required=True)
def qxz_switch_status(request):
    """
    气象站开关、阈值状态接口
    参数:
    device_id            必传(str)              设备号
    
    返回值:
    {
    qxz_switch: "[{'status': 0, 'JK': 0}, {'status': 0, 'JK': 1}]"                        开关状态
    switch_name: "[{'name': '开关0', 'JK': 0}, {'name': '', 'JK': 1}]"                     开关名称
    timing_list: "[{'JK': 0, 'Duration': 4, 'StartTime': 3}, {'JK': 1, 'Duration': 8, 'StartTime': 7}]"     定时控制
    auto_list: [{'lower': 32767, 'JK': 0, 'method': 0, 'upper': 32767, 'eKey': 0}]"    阈值状态
    }
    """
    device_id = request.POST.get("device_id")
    data = []
    try:
        switch_obj = QXZ_Switch_Status.objects.get(device_id=device_id)
        qxz_switch = switch_obj.qxz_switch
    except:
        qxz_switch = ""
    try:
        switch_name_obj = MongoQXZ_Switch.objects.get(device_id=device_id)
        switch_name = switch_name_obj.qxz_switch_data
    except:
        switch_name = ""
    try:
        timing_list = MongoQXZ_Timing_Switch.objects.get(device_id=device_id).qxz_timing
    except:
        timing_list = ""
    
    try:
        auto_list = MongoQXZ_Auto_Switch.objects.get(device_id=device_id).qxz_auto
    except:
        auto_list = ""
    data = {'qxz_switch':qxz_switch, 'switch_name':switch_name, 'timing_list':timing_list,"auto_list":auto_list}
    return data



@kedong_deco(login_required=False)
def qxz_element_conf(request):
    """
    气象站查看通道要素接口\保存通道要素接口:
    参数:
    device_id          必传(str)                设备号
    status             必传(str)                当status等于see查看, 当status等于save保存
    ekey               非必传(str)              通道号 如e1
    conf               非必传(str)              通道标题,如大气温度#℃
    
    说明:当status等于see查看,只需要传device_id,当status等于save保存 需传device_id、conf、ekey
    返回值:
    当status等于see时:
        "data": {
        "default": {                                   原来值
            "e1": "大气温度#℃",
            "e2": "大气湿度#%RH",
            "e3": "风速#m/s",
            "e4": "风向#°",
            "e5": "模拟气压#hpa",
            "e6": "简易总辐射#w/㎡"
        },
        "conf": {                                      修改值
            "e1": "大气温度#℃",
            "e2": "大气湿度#%RH",
            "e3": "风速#m/s",
            "e4": "风向#°",
            "e5": "模拟气压#hpa",
            "e6": "简易总辐射#w/㎡"
        }
    },


    当status等于save时:
    {
        "data" 0                  0 保存成功 1失败
    }

    """
    device_id = request.POST.get("device_id")
    status = request.POST.get("status")
    ekey = request.POST.get("ekey")
    conf = request.POST.get("conf")

    if status == "see":
        x = MongoQXZ_Conf.objects.get(device_id=device_id)
        i = QXZ_Default_Conf.objects.get(device_id=device_id)
        conf = {"e1":x.e1,"e2":x.e2,"e3":x.e3,"e4":x.e4,"e5":x.e5,
                "e6":x.e6,"e7":x.e7,"e8":x.e8,"e9":x.e9,"e10":x.e10,
                "e11":x.e11,"e12":x.e12,"e13":x.e13,"e14":x.e14,
                "e15":x.e15,"e16":x.e16,"e17":x.e17,"e18":x.e18,
                "e19":x.e19,"e20":x.e20,"e21":x.e21,"e22":x.e22,
                "e23":x.e23,"e24":x.e24,"e25":x.e25,"e26":x.e26,
                "e27":x.e27,"e28":x.e28,"e29":x.e29,"e30":x.e30}
        default = {"e1":i.e1,"e2":i.e2,"e3":i.e3,"e4":i.e4,"e5":i.e5,
                "e6":i.e6,"e7":i.e7,"e8":i.e8,"e9":i.e9,"e10":i.e10,
                "e11":i.e11,"e12":i.e12,"e13":i.e13,"e14":i.e14,
                "e15":i.e15,"e16":i.e16,"e17":i.e17,"e18":i.e18,
                "e19":i.e19,"e20":i.e20,"e21":i.e21,"e22":i.e22,
                "e23":i.e23,"e24":i.e24,"e25":i.e25,"e26":i.e26,
                "e27":i.e27,"e28":i.e28,"e29":i.e29,"e30":i.e30}

        for k in list(conf.keys()): 
            if not conf[k]:
                del conf[k]

        for y in list(default.keys()): 
            if not default[y]:
                del default[y]
        data = {"default":default,"conf":conf}
    elif status == "save":
        times = int(time.time())
        conf_obj = MongoQXZ_Conf.objects.get(device_id=device_id)
        if ekey == "e1":
            con = (conf_obj.e1).split("#")
            conf_obj.e1 = conf + "#"+ con[1]
            conf_obj.update = times
            conf_obj.save()
            data = {"code": 0}
        elif ekey == "e2":
            con = (conf_obj.e2).split("#")
            conf = conf + "#"+ con[1]
            conf_obj.e2 = conf
            conf_obj.update = times
            conf_obj.save()
            data = {"code": 0}
        elif ekey == "e3":
            con = (conf_obj.e3).split("#")
            conf_obj.e3 = conf + "#"+ con[1]
            conf_obj.update = times
            conf_obj.save()
            data = {"code": 0}
        elif ekey == "e4":
            con = (conf_obj.e4).split("#")
            conf_obj.e4 = conf + "#"+ con[1]
            conf_obj.update = times
            conf_obj.save()
            data = {"code": 0}
        elif ekey == "e5":
            con = (conf_obj.e5).split("#")
            conf_obj.e5 = conf + "#"+ con[1]
            conf_obj.update = times
            conf_obj.save()
            data = {"code": 0}
        elif ekey == "e6":
            con = (conf_obj.e6).split("#")
            conf_obj.e6 = conf + "#"+ con[1]
            conf_obj.update = times
            conf_obj.save()
            data = {"code": 0}
        elif ekey == "e7":
            con = (conf_obj.e7).split("#")
            conf_obj.e7 = conf + "#"+ con[1]
            conf_obj.update = times
            conf_obj.save()
            data = {"code": 0}
        elif ekey == "e8":
            con = (conf_obj.e8).split("#")
            conf_obj.e8 = conf + "#"+ con[1]
            conf_obj.update = times
            conf_obj.save()
            data = {"code": 0}
        elif ekey == "e9":
            con = (conf_obj.e9).split("#")
            conf_obj.e9 = conf + "#"+ con[1]
            conf_obj.update = times
            conf_obj.save()
            data = {"code": 0}
        elif ekey == "e10":
            con = (conf_obj.e10).split("#")
            conf_obj.e10 = conf + "#"+ con[1]
            conf_obj.update = times
            conf_obj.save()
            data = {"code": 0}
        elif ekey == "e11":
            con = (conf_obj.e11).split("#")
            conf_obj.e11 = conf + "#"+ con[1]
            conf_obj.update = times
            conf_obj.save()
            data = {"code": 0}
        elif ekey == "e12":
            con = (conf_obj.e12).split("#")
            conf_obj.e12 = conf + "#"+ con[1]
            conf_obj.update = times
            conf_obj.save()
            data = {"code": 0}
        elif ekey == "e13":
            con = (conf_obj.e13).split("#")
            conf_obj.e13 = conf + "#"+ con[1]
            conf_obj.update = times
            conf_obj.save()
            data = {"code": 0}
        elif ekey == "e14":
            con = (conf_obj.e14).split("#")
            conf_obj.e14 = conf + "#"+ con[1]
            conf_obj.update = times
            conf_obj.save()
            data = {"code": 0}
        elif ekey == "e15":
            con = (conf_obj.e15).split("#")
            conf_obj.e15 = conf + "#"+ con[1]
            conf_obj.update = times
            conf_obj.save()
            data = {"code": 0}
        elif ekey == "e16":
            con = (conf_obj.e16).split("#")
            conf_obj.e16 = conf + "#"+ con[1]
            conf_obj.update = times
            conf_obj.save()
            data = {"code": 0}
        elif ekey == "e17":
            con = (conf_obj.e17).split("#")
            conf_obj.e17 = conf + "#"+ con[1]
            conf_obj.update = times
            conf_obj.save()
            data = {"code": 0}
        elif ekey == "e18":
            con = (conf_obj.e18).split("#")
            conf_obj.e18 = conf + "#"+ con[1]
            conf_obj.update = times
            conf_obj.save()
            data = {"code": 0}
        elif ekey == "e19":
            con = (conf_obj.e19).split("#")
            conf_obj.e19 = conf + "#"+ con[1]
            conf_obj.update = times
            conf_obj.save()
            data = {"code": 0}
        elif ekey == "e20":
            con = (conf_obj.e20).split("#")
            conf_obj.e20 = conf + "#"+ con[1]
            conf_obj.update = times
            conf_obj.save()
            data = {"code": 0}
        elif ekey == "e21":
            con = (conf_obj.e21).split("#")
            conf_obj.e21 = conf + "#"+ con[1]
            conf_obj.update = times
            conf_obj.save()
            data = {"code": 0}
        elif ekey == "e22":
            con = (conf_obj.e22).split("#")
            conf_obj.e22 = conf + "#"+ con[1]
            conf_obj.update = times
            conf_obj.save()
            data = {"code": 0}
        elif ekey == "e23":
            con = (conf_obj.e23).split("#")
            conf_obj.e23 = conf + "#"+ con[1]
            conf_obj.update = times
            conf_obj.save()
            data = {"code": 0}
        elif ekey == "e24":
            con = (conf_obj.e24).split("#")
            conf_obj.e24 = conf + "#"+ con[1]
            conf_obj.update = times
            conf_obj.save()
            data = {"code": 0}
        elif ekey == "e25":
            con = (conf_obj.e25).split("#")
            conf_obj.e25 = conf + "#"+ con[1]
            conf_obj.update = times
            conf_obj.save()
            data = {"code": 0}
        elif ekey == "e26":
            con = (conf_obj.e26).split("#")
            conf_obj.e26 = conf + "#"+ con[1]
            conf_obj.update = times
            conf_obj.save()
            data = {"code": 0}
        elif ekey == "e27":
            con = (conf_obj.e27).split("#")
            conf_obj.e27 = conf + "#"+ con[1]
            conf_obj.update = times
            conf_obj.save()
            data = {"code": 0}
        elif ekey == "e28":
            con = (conf_obj.e28).split("#")
            conf_obj.e28 = conf + "#"+ con[1]
            conf_obj.update = times
            conf_obj.save()
            data = {"code": 0}
        elif ekey == "e29":
            con = (conf_obj.e29).split("#")
            conf_obj.e29 = conf + "#"+ con[1]
            conf_obj.update = times
            conf_obj.save()
            data = {"code": 0}
        elif ekey == "e30":
            con = (conf_obj.e30).split("#")
            conf_obj.e30 = conf + "#"+ con[1]
            conf_obj.update = times
            conf_obj.save()
            data = {"code": 0}
        else:
            data = {"code": 1}
    else:
        raise PortError("","参数超出范围")
    return data