<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport"
          content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
    <title>通讯测试</title>
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="stylesheet" href="${ctx}/css/basic.css">
    <link rel="stylesheet"
          href="${ctx}/css/plugin/bootstrap/bootstrap.min.css">
    <link rel="stylesheet" href="${ctx}/js/plugin/layui/css/layui.css">
    <link rel="stylesheet" href="${ctx}/css/plugin/jquery/jquery-ui.css">
    <link rel="stylesheet"
          href="${ctx}/css/plugin/metroStyle/metroStyle.css">
</head>
<style>
    #ping {
        width: 100%;
        height: 95%;
        font-size: 12pt;
        color: white;
        background-color: black;
        margin-top: 10px;
    }

    a:link {
        text-decoration: none;
    }

    a:visited {
        text-decoration: none;
    }

    a:hover {
        text-decoration: none;
    }

    a:active {
        text-decoration: none;
    }

    .btn {
        padding: 0px 6px;
        font-size: 12px;
    }
</style>
<body class="pd15 f12">
<button type="button" class="btn btn-danger fr  ml10" id="clear"><i class="glyphicon glyphicon-trash"></i>清空</button>
<button type="button" class="btn themeBGColor fr  ml10" id="pingTest"><i class="glyphicon glyphicon-asterisk"></i>Ping
</button>
<button type="button" class="btn themeBGColor fr " id="telnetTest"><i class="glyphicon glyphicon-leaf"></i>Telnet
</button>
<div class="form-inline ">
    <div class="form-group ml15">所选的终端：</div>
    <a href="javascript:void(0)" id="name" style="font-size: 12px;"></a>
    <div class="form-group ml15">前置机IP：</div>
    <div class="form-group">
        <input type="text" id="Source_Address" onkeyup="if(isNaN(value))execCommand('undo')" name="Source_Address"
               readonly="readonly">
    </div>
    <div class="form-group ml15">终端IP：</div>
    <div class="form-group">
        <input type="text" id="Destination_Address" onkeyup="if(isNaN(value))execCommand('undo')"
               name="Destination_Address">
    </div>
    <div class="form-group ml15">端口号：</div>
    <div class="form-group">
        <input type="text" id="Port" onkeyup="this.value=this.value.replace(/\D/g,'')" name="Port" style="width: 44%">
    </div>
</div>
<textarea id="ping" readonly="readonly"></textarea>
<%--Common Modal --%>
<script src="${ctx}/js/plugin/jquery/jquery.min.js"></script>
<script src="${ctx}/js/plugin/highchart/highstock.js"></script>
<script src="${ctx}/js/color.js"></script>
<script src="${ctx}/js/plugin/highchart/export.js"></script>
<script src="${ctx}/js/plugin/highchart/highchart-ZH.js"></script>
<script src="${ctx}/js/plugin/layui/layer.js"></script>
<script src="${ctx}/js/plugin/layui/layui.js"></script>
<script src="${ctx}/js/plugin/jquery/jquery.contextMenu.js"></script>
<script src="${ctx}/js/plugin/jquery/jquery.cleverTabs.js"></script>
<script src="${ctx}/js/plugin/ztree/jquery.ztree.core.js"></script>
<script src="${ctx}/js/plugin/bootstrap/bootstrap-datetimepicker.js"></script>
<script src="${ctx}/js/plugin/bootstrap/bootstrap-datetimepicker.zh-CN.js"></script>
<script src="${ctx}/js/plugin/jquery/jquery.dataTables.js"></script>
<script src="${ctx}/jspJS/utils/timeControl.js"></script>
<script src="${ctx}/jspJS/utils/loading.js"></script>
<script src="${ctx}/jspJS/utils/judgeAjaxStatus.js"></script>
<script type="text/javascript">
    var webSocket = window.parent.parent.webSocket;
    var lineIndex;
    var devtype = '';
    devtype = window.parent.devtype;//调取bb变量
    lineIndex = window.parent.lineIndex;//调取bb变量
    var sourIp = "";
    var ip = '';
    var port = "";
    $(function () {
        var deviceName = parent.deviceNa();
        $.ajax({
            type: "POST",
            url: "/pqs9900/device/getSelName",
            data: {deviceName: deviceName},
            success: function (data) {
                var nodeIndex = data[0].nodeIndex;
                $.ajax({
                    type: "post",
                    url: "/pqs9900/device/setIp",
                    data: {
                        nodeIndex: nodeIndex
                    },
                    success: function (info) {
                        $("#Source_Address").val(info.nodeIP);
                    }
                })
                $("#name").text(deviceName);
                $("#Destination_Address").val(data[0].ip);
                $("#Port").val(data[0].portid);
            }
        })
        //根据获取的前置进行操作
        ip = $("#Destination_Address").val();
        sourIp = $("#Source_Address").val();
        port = $("#Port").val();
    })
    //ping命令的请求
    $("#pingTest").click(function () {
        startWebSocket(60, ip,sourIp,port);
    })
    //telnet命名的请求
    $("#telnetTest").click(function () {
        startWebSocket(70, ip,sourIp,port);
    })

    //将值发送到socket中
    function startWebSocket(type, ip, sourIp,port) {
        var jsonstr = '"data":{"type":"' + type + '","ip":"' + ip + '","sourIp":"' + sourIp + '","port":"' + port + '"}';
        jsonstr = 'len:' + (Array(4).join(0) + jsonstr.length).slice(-4) + '' + jsonstr + '';
        var ajaxTimeoutTest = $.ajax({
            type: "POST",
            timeout: 10000,
            url: "/pqs9900/device/socketon",
            async: true,
            data: {
                message: jsonstr
            },
            success: function (data) {
                //data数据体后的长度
                var socketLength = parseInt(data.substring(4, 8));
                var info = $.parseJSON(data.substring(15, socketLength + 8));
                if (info.errcode != 10000) {
                    layer.msg(info.errmsg, {
                        icon: 6,
                        time: 3000
                    });
                }
            },
            complete: function (XMLHttpRequest, status) { //请求完成后最终执行参数
                if (status == 'timeout') {//超时,status还有success,error等值的情况
                    ajaxTimeoutTest.abort();
                    layer.msg('请求超时', {
                        icon: 5,
                        time: 3000
                    });
                }
            }
        })
    }
</script>
</body>
</html>