

document.write("<script type='text/javascript' src='../static/bootstrap/bootstrap/js/bootstrap.js'></script>");
document.write("<link rel='stylesheet' href='../static/bootstrap/bootstrap/css/bootstrap.css'  media='all'>");
document.write("<script type='text/javascript' src='../static/echarts/echarts.min.js'></script>");
document.write("<script type='text/javascript' src='../public/interface.js'></script>");
document.write("<script type='text/javascript' src='../public/public.js'></script>");

document.write("<script type='text/javascript' src='../static/layui/layui.all.js'></script>");
document.write("<link rel='stylesheet' href='../static/layui/css/layui.css'  media='all'>");


$(function(){




    var interface = new Interface(16);


    var obj = {cnt:new Array(16), delay:0, set_delay:1, delay1:500, delay2:500, overDelay:1200, get_delay:100, TaktTimeDelay:50, OverTimeDelay:200};


    //定时刷新
    {
        function get_idn(i,dat){
            if(dat.length > 0){
                $("#Text_Model_" + (i+1) + "_text").text(dat.substring(0,7));
                $("#Text_PortState_" + (i+1) + "_text").text("已连接");
                $("#Text_PortState_" + (i+1) + "_text").css('color','#00ff00');
            }else{
                $("#Text_Model_" + (i+1) + "_text").text("未应答");
                $("#Text_PortState_" + (i+1) + "_text").text("未应答");
                $("#Text_PortState_" + (i+1) + "_text").css('color','#ff0000');
            }
            obj.cnt[i]++;
        }
        window.get_idn_0 = function(i, dat){get_idn(i,dat);}
        window.get_idn_1 = function(i, dat){get_idn(i,dat);}
        window.get_idn_2 = function(i, dat){get_idn(i,dat);}
        window.get_idn_3 = function(i, dat){get_idn(i,dat);}
        window.get_idn_4 = function(i, dat){get_idn(i,dat);}
        window.get_idn_5 = function(i, dat){get_idn(i,dat);}
        window.get_idn_6 = function(i, dat){get_idn(i,dat);}
        window.get_idn_7 = function(i, dat){get_idn(i,dat);}
        window.get_idn_8 = function(i, dat){get_idn(i,dat);}
        window.get_idn_9 = function(i, dat){get_idn(i,dat);}
        window.get_idn_10 = function(i, dat){get_idn(i,dat);}
        window.get_idn_11 = function(i, dat){get_idn(i,dat);}
        window.get_idn_12 = function(i, dat){get_idn(i,dat);}
        window.get_idn_13 = function(i, dat){get_idn(i,dat);}
        window.get_idn_14 = function(i, dat){get_idn(i,dat);}
        window.get_idn_15 = function(i, dat){get_idn(i,dat);}

        function get_vcm(i,dat){
            if(dat.length > 0){
                $("#Text_Volt_" + (i+1) + "_text").text(dat.substring(0, dat.indexOf(",")));
                $("#Text_Curr_" + (i+1) + "_text").text(dat.substring(dat.indexOf(",")+1, dat.length-1));
            }
            obj.cnt[i] = 0;
        }
        window.get_vcm_0 = function(i, dat){get_vcm(i,dat);}
        window.get_vcm_1 = function(i, dat){get_vcm(i,dat);}
        window.get_vcm_2 = function(i, dat){get_vcm(i,dat);}
        window.get_vcm_3 = function(i, dat){get_vcm(i,dat);}
        window.get_vcm_4 = function(i, dat){get_vcm(i,dat);}
        window.get_vcm_5 = function(i, dat){get_vcm(i,dat);}
        window.get_vcm_6 = function(i, dat){get_vcm(i,dat);}
        window.get_vcm_7 = function(i, dat){get_vcm(i,dat);}
        window.get_vcm_8 = function(i, dat){get_vcm(i,dat);}
        window.get_vcm_9 = function(i, dat){get_vcm(i,dat);}
        window.get_vcm_10 = function(i, dat){get_vcm(i,dat);}
        window.get_vcm_11 = function(i, dat){get_vcm(i,dat);}
        window.get_vcm_12 = function(i, dat){get_vcm(i,dat);}
        window.get_vcm_13 = function(i, dat){get_vcm(i,dat);}
        window.get_vcm_14 = function(i, dat){get_vcm(i,dat);}
        window.get_vcm_15 = function(i, dat){get_vcm(i,dat);}

        window.t1 = function() {
            for(let a=0; a<16; a++){
                if(interface.getSerialIsOpen(a) != "True"){
                    $("#Text_Volt_" + (a+1) + "_text").text("");
                    $("#Text_Curr_" + (a+1) + "_text").text("");
                    $("#Text_Port_" + (a+1) + "_text").text("");
                    $("#Text_Model_" + (a+1) + "_text").text("未连接");
                    $("#Text_PortState_" + (a+1) + "_text").text("未连接");
                    $("#Text_PortState_" + (i+1) + "_text").css('color','#ff0000');
                }
                else if(interface.getFreeTime(a) < 100){
                    //if(a == 9) layer.msg(a + " " + interface.getFreeTime(a) + ",  " + interface.getSerialIsOpen(a))
                }
                else if(obj.cnt[a] == 0){
                    var port = $("#Select_PortSet_" + (a+1).toString() + "_input").val();
                    $("#Text_Port_" + (a+1) + "_text").text(port);
                    interface.getSerialComm(a, "*idn?", "get_idn_" + a, obj.TaktTimeDelay, obj.OverTimeDelay);
                }
                else if(obj.cnt[a] == 1){
                    interface.getSerialComm(a, "MEAS:VCM?", "get_vcm_" + a, obj.TaktTimeDelay, obj.OverTimeDelay);
                }
                else{
                    obj.cnt[a] = 0;
                }
            }
        }
    }



    // for(var num = 1; num <= 16; num++) {
    //     $("#Select_PortSet_" + num + "_input").bind("change", {
    //         index: num
    //     }, clickHandler);
    // }
    // function clickHandler(event) {
    //     button_SetingPort(event.data.index-1);
    // }

    function button_SetingPort(i){
        let PortState = JSON.parse(interface.getSerialInfo());
        var port = $("#Select_PortSet_" + (i+1).toString() + "_input").val();
        var baud = $("#Select_BaudRateSet_" + (i+1).toString() + "_input").val();

        if(port.substring(0,3) == "COM" && interface.getSerialIsOpen(i) != "True"){
            let state = interface.setSerialOpen(i, port, parseInt(baud), "None", 1, 8);
        }else if(port.substring(0,3) == "不使用"){
            interface.setSerialClose(i);
            $("#Text_PortState_" + (a+1) + "_text").text("未连接");
            $("#Text_PortState_" + (i+1) + "_text").css('color','#ff0000');
        }
        if(port != PortState.data[i]["PortName"]){
            interface.setSerialClose(i);
        }
        return true;
    }
    function button_SetingCurrVolt(i){
        var curr = $("#Input_Curr_" + (i+1).toString() + "_input").val();
        var volt = $("#Input_Volt_" + (i+1).toString() + "_input").val();
        interface.setSerialSend(i, "CURR " + curr, "set_curr", obj.delay1, obj.delay2, obj.overDelay);
        window.set_curr = function (i) {
            interface.setSerialSend(i, "VOLT " + volt, "set_volt", obj.delay1, obj.delay2, obj.overDelay);
            window.set_volt = function (i) {
                layer.msg("<h1>完成设置</h1>");
            }
        }
        return true;
    }

    $("#Button_PortSeting_1").click(function () { button_SetingCurrVolt(0);});
    $("#Button_PortSeting_2").click(function () {button_SetingCurrVolt(1);});
    $("#Button_PortSeting_3").click(function () {button_SetingCurrVolt(2);});
    $("#Button_PortSeting_4").click(function () {button_SetingCurrVolt(3);});
    $("#Button_PortSeting_5").click(function () {button_SetingCurrVolt(4);});
    $("#Button_PortSeting_6").click(function () {button_SetingCurrVolt(5);});
    $("#Button_PortSeting_7").click(function () {button_SetingCurrVolt(6);});
    $("#Button_PortSeting_8").click(function () {button_SetingCurrVolt(7);});
    $("#Button_PortSeting_9").click(function () {button_SetingCurrVolt(8);});
    $("#Button_PortSeting_10").click(function () {button_SetingCurrVolt(9);});
    $("#Button_PortSeting_11").click(function () {button_SetingCurrVolt(10);});
    $("#Button_PortSeting_12").click(function () {button_SetingCurrVolt(11);});
    $("#Button_PortSeting_13").click(function () {button_SetingCurrVolt(12);});
    $("#Button_PortSeting_14").click(function () {button_SetingCurrVolt(13);});
    $("#Button_PortSeting_15").click(function () {button_SetingCurrVolt(14);});
    $("#Button_PortSeting_16").click(function () {button_SetingCurrVolt(15);});


    $("#button_exit").click(function () {
        interface.exit_windows();
    });


    //input值恢复与定时备份
    {
        var userAgent = myBrowser().indexOf("WOW64");
        function stringToBool(str) {
            if(str == "True" || str == "true" || str == "TRUE"){
                return true;
            }
            return false;
        }

        var id_list = [
            "Text_PortState_1_text",                    //
            "Text_PortState_2_text",                    //
            "Text_PortState_3_text",                    //
            "Text_PortState_4_text",                    //
            "Text_PortState_5_text",                    //
            "Text_PortState_6_text",                    //
            "Text_PortState_7_text",                    //
            "Text_PortState_8_text",                    //
            "Text_PortState_9_text",                    //
            "Text_PortState_10_text",                   //
            "Text_PortState_11_text",                   //
            "Text_PortState_12_text",                   //
            "Text_PortState_13_text",                   //
            "Text_PortState_14_text",                   //
            "Text_PortState_15_text",                   //
            "Text_PortState_16_text",                   //

            "Select_PortSet_1_input",                   //
            "Select_PortSet_2_input",                   //
            "Select_PortSet_3_input",                   //
            "Select_PortSet_4_input",                   //
            "Select_PortSet_5_input",                   //
            "Select_PortSet_6_input",                   //
            "Select_PortSet_7_input",                   //
            "Select_PortSet_8_input",                   //
            "Select_PortSet_9_input",                   //
            "Select_PortSet_10_input",                  //
            "Select_PortSet_11_input",                  //
            "Select_PortSet_12_input",                  //
            "Select_PortSet_13_input",                  //
            "Select_PortSet_14_input",                  //
            "Select_PortSet_15_input",                  //
            "Select_PortSet_16_input",                  //

            "Select_BaudRateSet_1_input",               //
            "Select_BaudRateSet_2_input",               //
            "Select_BaudRateSet_3_input",               //
            "Select_BaudRateSet_4_input",               //
            "Select_BaudRateSet_5_input",               //
            "Select_BaudRateSet_6_input",               //
            "Select_BaudRateSet_7_input",               //
            "Select_BaudRateSet_8_input",               //
            "Select_BaudRateSet_9_input",               //
            "Select_BaudRateSet_10_input",              //
            "Select_BaudRateSet_11_input",              //
            "Select_BaudRateSet_12_input",              //
            "Select_BaudRateSet_13_input",              //
            "Select_BaudRateSet_14_input",              //
            "Select_BaudRateSet_15_input",              //
            "Select_BaudRateSet_16_input",              //

            "Input_Addr_1_input",                        //
            "Input_Addr_2_input",                        //
            "Input_Addr_3_input",                        //
            "Input_Addr_4_input",                        //
            "Input_Addr_5_input",                        //
            "Input_Addr_6_input",                        //
            "Input_Addr_7_input",                        //
            "Input_Addr_8_input",                        //
            "Input_Addr_9_input",                        //
            "Input_Addr_10_input",                       //
            "Input_Addr_11_input",                       //
            "Input_Addr_12_input",                       //
            "Input_Addr_13_input",                       //
            "Input_Addr_14_input",                       //
            "Input_Addr_15_input",                       //
            "Input_Addr_16_input",                       //

            "Input_PortStopBit_1_input",                //
            "Input_PortStopBit_2_input",                //
            "Input_PortStopBit_3_input",                //
            "Input_PortStopBit_4_input",                //
            "Input_PortStopBit_5_input",                //
            "Input_PortStopBit_6_input",                //
            "Input_PortStopBit_7_input",                //
            "Input_PortStopBit_8_input",                //
            "Input_PortStopBit_9_input",                //
            "Input_PortStopBit_10_input",               //
            "Input_PortStopBit_11_input",               //
            "Input_PortStopBit_12_input",               //
            "Input_PortStopBit_13_input",               //
            "Input_PortStopBit_14_input",               //
            "Input_PortStopBit_15_input",               //
            "Input_PortStopBit_16_input",               //

            "Input_Curr_1_input",                     //
            "Input_Curr_2_input",                     //
            "Input_Curr_3_input",                     //
            "Input_Curr_4_input",                     //
            "Input_Curr_5_input",                     //
            "Input_Curr_6_input",                     //
            "Input_Curr_7_input",                     //
            "Input_Curr_8_input",                     //
            "Input_Curr_9_input",                     //
            "Input_Curr_10_input",                    //
            "Input_Curr_11_input",                    //
            "Input_Curr_12_input",                    //
            "Input_Curr_13_input",                    //
            "Input_Curr_14_input",                    //
            "Input_Curr_15_input",                    //
            "Input_Curr_16_input",                    //

            "Input_Volt_1_input",                     //
            "Input_Volt_2_input",                     //
            "Input_Volt_3_input",                     //
            "Input_Volt_4_input",                     //
            "Input_Volt_5_input",                     //
            "Input_Volt_6_input",                     //
            "Input_Volt_7_input",                     //
            "Input_Volt_8_input",                     //
            "Input_Volt_9_input",                     //
            "Input_Volt_10_input",                    //
            "Input_Volt_11_input",                    //
            "Input_Volt_12_input",                    //
            "Input_Volt_13_input",                    //
            "Input_Volt_14_input",                    //
            "Input_Volt_15_input",                    //
            "Input_Volt_16_input",                    //
        ];

        //查找到所有串口
        function getAllPortHtml() {
            var html = "";
            var allCom = interface.getSerialFind().split(",");
            for(var i=0;i<allCom.length;i++){
                if(allCom[i] != ""){html += "<option value ='" + allCom[i] + "'>" + allCom[i] + "</option>";}
            }
            html += "<option value = '不使用'>不使用</option>";
            return html;
        }

        // $("#" + id_list[0x10]).click( function () {if($("#" + id_list[0x10]).val() == "Refresh"){$("#" + id_list[0x10]).html(getAllPortHtml());}});
        // $("#" + id_list[0x11]).click( function () {if($("#" + id_list[0x11]).val() == "Refresh"){$("#" + id_list[0x11]).html(getAllPortHtml());}});
        // $("#" + id_list[0x12]).click( function () {if($("#" + id_list[0x12]).val() == "Refresh"){$("#" + id_list[0x12]).html(getAllPortHtml());}});
        // $("#" + id_list[0x13]).click( function () {if($("#" + id_list[0x13]).val() == "Refresh"){$("#" + id_list[0x13]).html(getAllPortHtml());}});
        // $("#" + id_list[0x14]).click( function () {if($("#" + id_list[0x14]).val() == "Refresh"){$("#" + id_list[0x14]).html(getAllPortHtml());}});
        // $("#" + id_list[0x15]).click( function () {if($("#" + id_list[0x15]).val() == "Refresh"){$("#" + id_list[0x15]).html(getAllPortHtml());}});
        // $("#" + id_list[0x16]).click( function () {if($("#" + id_list[0x16]).val() == "Refresh"){$("#" + id_list[0x16]).html(getAllPortHtml());}});
        // $("#" + id_list[0x17]).click( function () {if($("#" + id_list[0x17]).val() == "Refresh"){$("#" + id_list[0x17]).html(getAllPortHtml());}});
        // $("#" + id_list[0x18]).click( function () {if($("#" + id_list[0x18]).val() == "Refresh"){$("#" + id_list[0x18]).html(getAllPortHtml());}});
        // $("#" + id_list[0x19]).click( function () {if($("#" + id_list[0x19]).val() == "Refresh"){$("#" + id_list[0x19]).html(getAllPortHtml());}});
        // $("#" + id_list[0x1a]).click( function () {if($("#" + id_list[0x1a]).val() == "Refresh"){$("#" + id_list[0x1a]).html(getAllPortHtml());}});
        // $("#" + id_list[0x1b]).click( function () {if($("#" + id_list[0x1b]).val() == "Refresh"){$("#" + id_list[0x1b]).html(getAllPortHtml());}});
        // $("#" + id_list[0x1c]).click( function () {if($("#" + id_list[0x1c]).val() == "Refresh"){$("#" + id_list[0x1c]).html(getAllPortHtml());}});
        // $("#" + id_list[0x1d]).click( function () {if($("#" + id_list[0x1d]).val() == "Refresh"){$("#" + id_list[0x1d]).html(getAllPortHtml());}});
        // $("#" + id_list[0x1e]).click( function () {if($("#" + id_list[0x1e]).val() == "Refresh"){$("#" + id_list[0x1e]).html(getAllPortHtml());}});
        // $("#" + id_list[0x1f]).click( function () {if($("#" + id_list[0x1f]).val() == "Refresh"){$("#" + id_list[0x1f]).html(getAllPortHtml());}});


        if(userAgent >= 0){
            for(var i=0; i<16; i++){
                $("#" + id_list[i + 0x00]).text("未能连通"); $("#" + id_list[i + 0x00]).css("color", "#FF0000");
                $("#" + id_list[i + 0x10]).html(getAllPortHtml()); var port = interface.getIni("Dc_PowerPortSeting", id_list[i + 0x10],  "不使用"); $("#" + id_list[i + 0x10]).val(port);
                $("#" + id_list[i + 0x20]).val(interface.getIni("Dc_PowerPortSeting", id_list[i + 0x20],  "9600"));
                $("#" + id_list[i + 0x30]).val(interface.getIni("Dc_PowerPortSeting", id_list[i + 0x30],  "1"));
                $("#" + id_list[i + 0x40]).val("8");
                $("#" + id_list[i + 0x50]).val(interface.getIni("Dc_PowerPortSeting", id_list[i + 0x50],  "0.500"));
                $("#" + id_list[i + 0x60]).val(interface.getIni("Dc_PowerPortSeting", id_list[i + 0x60],  "2.000"));
            }
            window.t_save = function() {
                for(var i=0x10; i<id_list.length; i++){
                    interface.setIni("Dc_PowerPortSeting", id_list[i],     $("#" + id_list[i]).val());
                }
            }
        }
    }



    {//串口初始化
        let t_setPort_cnt = 0;
        let t_setPort = window.setInterval(function () {
            if(t_setPort_cnt < 16){
                button_SetingPort(t_setPort_cnt++);
            }else{
                t_setPort_cnt = 0;
            }
        }, 100);
    }
    interface.setTime([["t1", 200], ["t_save", 10000]]);


});





