
/*该js文件用于处理"页面通讯"业务逻辑处理*/


/*********************************建立Socket连接、接收Socket消息、发送Socket消息**********************************/
var socket;
var reconnectAttempts = 1; //连接次数
var IsAllowConnect = false; //是否允许再次连接
var timeout; //计时器
var machinePlanID = 0; //设备保养流水号
//连接socket
//控制运动的标志位
//控制柜号标志
var clamp = 0;
var flag = 0;
function connect() {
    var ip = "123.56.95.229"; //ip��ַ192.168.1.240  
    //var ip = "127.0.0.1"; //ip��ַ
    var port = "61101"; //�˿ں�
    var opname = "OP2010";
    var readyStatus = new Array("正在连接", "已建立连接", "正在关闭连接", "已关闭连接");
    var host = "ws://" + ip + ":" + port + "//WebModule//websocket";

    //继续接受客户端 
    //    var host = "ws://172.16.62.251:8050";

    var address = document.location.host;

    if (opname.length < 2) { $.messager.confirm('提示', '请输入您的名字'); return; }

    //尝试连接至服务器
    try {

        if ("WebSocket" in window) {
            socket = new WebSocket(host);
        }
        else if ("MozWebSocket" in window) {
            socket = new MozWebSocket(host);
        } else {
            //document.getElementById("message_output").innerHTML = "浏览器不支持WebSocket";
        }
        //socket = new WebSocket(host);

    } catch (exception) {
        alert('与MES服务器断开连接');
        return;
    }


    //连接成功
    socket.onopen = function () {

        IsAllowConnect = false; //是否允许重复连接
        //关闭timer
        try {
            clearInterval(timeout);
        }
        catch (exception) {
        }
        var name = opname;

        //$.messager.show({
        //    title: '提示',
        //    msg: 'MES信息系统登录成功!',
        //    timeout: 3000,
        //    showType: 'slide'
        //});
        //alert("连接成功");
        //登陆成功后,发送reset信号,清除信号
        //var sendmessage = "MSG,IniSingalToWeb|" + name + "|1";
        //sendMessage(sendmessage);
        socket.send("LIN," + name);
        //socket.send("{<" + name + ">}");
        //socket.send("MSG,IniSingalToWeb|OP1040|1|");

    }

    //收到消息
    socket.onmessage = function (msg) {
        try {
            //返回的数据msg.data，包含了协议中的4部分9
            var arrays_signal = msg.data.split('|');
            var m_Source = arrays_signal[0];
            var m_Command = arrays_signal[1];
            var m_Opname = arrays_signal[2];
            var m_Value = arrays_signal[3];
            //var opName = "OP1010";
            switch (m_Source) {
                case "MES"://MES|TOWEB|Opname|cmd&value    or    MES|TOWEB|Opname|value
                    switch (m_Command) {
                        //XYZ轴坐标
                        case "CNC_Info":
                            var CNC_LocationPosition = m_Value.split("&");
                            document.getElementById("text1").innerHTML = CNC_LocationPosition[0];
                            document.getElementById("text2").innerHTML = CNC_LocationPosition[1];
                            document.getElementById("text3").innerHTML = CNC_LocationPosition[2];
                            document.getElementById("text4").innerHTML = CNC_LocationPosition[3];
                            document.getElementById("text5").innerHTML = CNC_LocationPosition[4];
                            document.getElementById("text6").innerHTML = CNC_LocationPosition[5];
                            document.getElementById("text7").innerHTML = CNC_LocationPosition[6];
                            document.getElementById("text8").innerHTML = CNC_LocationPosition[7];
                            document.getElementById("text9").innerHTML = CNC_LocationPosition[8];
                            break;
                        // //X轴坐标
                        // case "X_Location":
                        //     document.getElementById("text1").innerHTML = m_Value;
                        //     break;
                        // //Y轴坐标
                        // case "Y_Location":
                        //     document.getElementById("text2").innerHTML = m_Value;
                        //     break;
                        // //Z轴坐标
                        // case "Z_Location":
                        //     document.getElementById("text3").innerHTML = m_Value;
                        //     break;
                        // //每分钟速度
                        // case "Feed_Time":
                        //     document.getElementById("text5").innerHTML = m_Value;
                        //     break;
                        // //主轴速度 
                        // case "main_speed":
                        //     document.getElementById("text9").innerHTML = m_Value;
                        //     break;
                        // //进给修调
                        // case"Feed_Fix":
                        //     document.getElementById("text4").innerHTML = m_Value;
                        //     break;
                        // //进给速度
                        // case"FeedTrimming":
                        //     document.getElementById("text8").innerHTML = m_Value;
                        //     break;
                        // //进给时间
                        // case"SRO":
                        //     document.getElementById("text7").innerHTML = m_Value;
                        //     break;
                        // //单位/转
                        // case"Feed_Rate":
                        //     document.getElementById("text6").innerHTML = m_Value;
                        //     break;
                        default:
                            break;
                    }
                    break;
            }
        }
        catch (exception) {
            var error = exception.toString();
            // alert('系统出现错误，接受onmessage：' + error);
            return;
        }
    }
    //连接断开
    socket.onclose = function (event) {
        try {
            IsAllowConnect = true;
            //alert('与MES服务器连接已经断开，请重新登录')
            socket.close();

            if (IsAllowConnect) {
                //显示一下
                $.messager.show({
                    title: '提示',
                    msg: '连接次数为：' + reconnectAttempts,
                    timeout: 2000,
                    showType: 'slide'
                });
                //重新连接,打开计时器
                timeout = setTimeout(function () {
                    reconnectAttempts++;
                    connect();
                }, 1000);
            }
        }
        catch (exception) {
            var error = exception.toString();
            alert('系统出现错误,关闭onclose:' + error);
            return;
        }
    }

    //出现错误
    socket.onerror = function (event) {
        try {
            $.messager.show({
                title: '提示',
                msg: 'Web Socket error',
                timeout: 10000,
                showType: 'slide'
            });
        }
        catch (exception) {
            $.messager.show({
                title: '提示',
                msg: exception.toString(),
                timeout: 10000,
                showType: 'slide'
            });
        }
    }
}

//发送Socket
function sendMessage(message) {
    try {
        socket.send(message.toString());

    }
    catch (exception) {
        var error = exception.toString();
        alert('系统出现错误,发送Socket：' + error);
        return;
    }
}


