<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="GB2312">
    <title></title>
    <link href="css/style.css" title="style" rel="stylesheet" type="text/css" />
    <script src="js/jquery-1.6.3.min.js"></script>
</head>
<body style="background: #F3F3F3">
<div>
  <div class='list2' style='margin: 10px;'>
    <a class='you_p' href='#' style='color: #fff'>232设备</a>
  </div>
  <div style='border: #B0B7BF 1px solid'>
    <table id='tbList' width='100%'>
    <tr bgcolor='#E5E9EC'><th style='padding: 10px'><span>SID</span> </th><th style='padding: 10px'><span>监测项描述</span></th><th style='padding: 10px'><span>传感器类型</span></th><th style='padding: 10px'><span>基本信息</span></th><th style='padding: 10px'><span>操作</span></th></tr>
    </table>
  </div>
</div>
<div>
  <table width='100%'>
    <tr>
    <td width="50%" style="border:none">传感器类型: <select id="lsType" style="width:350px"></select></td>
    <td style="border:none">端 口: <select id="lsPort" style="width:100px"></select>&nbsp;&nbsp;自动默认参数</td>
    </tr>
    <tr>
    <td><a href='javascript:sidAdd()' class='back' style='color: #fff;background: #ffa225'>+添加监测项</a></td>
    <td><a href='javascript:history.back()' class='back' style='color: #fff'>返回</a></td>
    </tr>
  </table>
</div>
<div  style="color:red">
  <br>温馨提示：
  <br>添加传感器的方法：选择需要添加的“传感器”与相对应的“端口”后，
  <br>点击“+添加监测项”，即可完成对传感器的添加。
</div>
<script>

<% db_Open("/NandFlash/dbCfg.db3"); %>
var jtype=<% db_QryAdv("select Type,t.Kind,t.Name,t.Note,Tag1,Ext1,Port,PortDef,ASubSys,k.Name as KName from tbSMType t left join tbSMKind k on k.Kind=t.Kind where Port=3 order by Type,Tag1",0); %>;
var jport=<% db_QryAdv("select * from tbPort where Type=3 and Val not in(select SerType from tbSM where State=0 and SerAddr=0) order by Type,Name",0); %>;
var isidmax=<% write(db_QryInt("select max(SID) from tbSM",1));%>;
<% db_Close(); %>

listT();
function listT(){
  if(jtype.reccnt==0) return;
  var s="";
  $.each(jtype.recs, function(i,n){
    s+="<option value='"+((n.Type<<8)+n.Tag1)+"'>"+n.KName+" ("+n.Name+")&nbsp;&nbsp;&nbsp;"+n.Note;
  });
  $("#lsType").html(s);
}

listP();
function listP(){
  if(jport.reccnt==0) return;
  var s="";
  $.each(jport.recs, function(i,n){
    s+="<option value='"+n.Val+"'>"+SACtoCOM(n.Name,n.Val);
  });
  $("#lsPort").html(s);
}

$(function(){
    qryPort();
    qry232();
});

function qryPort(){
  $.ajax({
    type: "POST",
    url: "omdev/port.asp",
    data: "op=qryok&port="+3,
    contentType:"application/x-www-form-urlencoded; charset=gb2312",
    dataType:"json",
    success: function(msg){
      jport=msg;
      listP();
    }
  });
}
function qry232(){
  $.ajax({
    type: "POST",
    url: "omdev/smbyport.asp",
    data: "port="+3,
    contentType:"application/x-www-form-urlencoded; charset=gb2312",
    dataType:"json",
    success: function(msg){
      list232(msg)
    }
  });
}
function list232(data){
  var s='';
  s+="<tr bgcolor='#E5E9EC'><th style='padding: 10px'><span>SID</span> </th><th style='padding: 10px'><span>监测项描述</span></th><th style='padding: 10px'><span>传感器类型</span></th><th style='padding: 10px'><span>基本信息</span></th><th style='padding: 10px'><span>操作</span></th></tr>";
  $.each(data.recs,function(i,n){
    s+="<tr id='tr"+i+"'><th>"+n.SID+"</th><th style='text-align: left'>"+n.Name+"</th><th style='text-align: left'>"+n.KName+"("+n.TName+")</th><th style='text-align: left'>"+getNotes(n,i)+"</th><th>"+getOps(n,i)+"</th></tr>";
  })
  $("#tbList").html(s);
  $("a:contains('启用')").addClass("red");
}


//文本 "ttySAC1:2400,n,8,1" to COM 显示
function SACtoCOM(str , val)
{
	if(str.length<=0)
	{
		return "";
	}
	/*if((val & 0xf00000) ==0x100000)
	{
		var resultstr="COM";
		var s = str;
		resultstr =resultstr + (parseInt(s.substring(6,7))+1)+ s.substring(7);
	}
	else*/
	{
		resultstr = str;
	}
	return resultstr;
}

//文本 COM to "ttySAC1:2400,n,8,1" 取值
function COMtoSAC(str , val)
{
	if(str.length<=0)
	{
		return "";
	}
	/*if( (val & 0xf00000) ==0x100000 )
	{
		var resultstr="";
		var s = str;
		resultstr = "ttySAC" + (parseInt(s.substring(3,4))-1) + ":";
	}
	else*/
	{
		var resultstr=str;
	}
	
	
	return resultstr;
}


function getNotes(n, i){
  var s="";
  s+="端口:"+SACtoCOM(n.SerDef,n.SerKind);
  //s+="&nbsp;附注:"+n.Note;
  return s;
}
function getOps(n, i){
  var s="";
  s+="<a href='C0-3x.asp?itype="+n.Type+"&sid="+n.SID+"&port="+n.SerType+"'>编辑</a>";
  s+="&nbsp;&nbsp;<a id='sid_st"+i+"' href='javascript:sidSt("+n.SID+","+i+","+n.SerType+")'>";
  if(n.State==0) s+="停用"; else s+="启用";
  s+="</a>";
  s+="&nbsp;&nbsp;<a href='javascript:sidDel("+n.SID+","+i+")'>删除</a>";
  return s;
}
function sidSt(isid, itr, iser){
  var s=$("#sid_st"+itr).text()
  var ist=0;
  if(s=="停用"){
    s="启用"; ist=1;
  }
  else {
    s="停用"; ist=0;
  }
  $.ajax({
    type: "POST",
    url: "omdev/smx.asp",
    data: "op=stx&sid="+isid+"&ser="+iser+"&st="+ist,
    dataType:"json",
    success: function(msg){
      if(msg.op=="ok"){
        $("#sid_st"+itr).html(s);
        $("a:contains('启用')").addClass("red");
        $("a:contains('停用')").removeClass("red");
        qryPort();
      }
      else
        if (ist==0) alert("启用失败,端口已经使用!");
    }
  });
}

function sidDel(isid, itr){
  var s="确定要删除 "+$("#tr"+itr+" th:eq(1)").text()+" SID="+isid+" 吗?";
  if(!confirm(s)) return;
  $.ajax({
    type: "POST",
    url: "omdev/smx.asp",
    data: "op=del&sid="+isid,
    dataType:"json",
    success: function(msg){
      if(msg.op=="ok"){
        $("#tr"+itr).remove();
        qryPort();
      }
      else
        alert("删除 "+$("#tr"+itr+" th:eq(0)").text()+" 编号"+itype+"-"+itag+" 失败!");
    }
  });
}

function sidAdd(){
  if(jport.reccnt==0){
    alert("没有端口可用(已经全部使用),不能增加!");
    return;
  }
  var idx=$('#lsType').prop('selectedIndex');
  var smtype=jtype.recs[idx];
  var iser=$("#lsPort").val();
  var iserK=iser;
  var sser=COMtoSAC($("#lsPort").find("option:selected").text(),$("#lsPort").find("option:selected").val());
  if(iser & 0xff0000){
    sser+=smtype.PortDef;
    iserK=(iser & 0xff0000);
  }
  var spost="";
  //null,kind,type,'name','',0,tag1,0,1,'',serK,serT,serD,serA,0,0,0,ASubSys,0,0,ext1,0
  spost+="&insval=null,"+smtype.Kind;
  spost+=","+smtype.Type;
  spost+=",'"+smtype.KName+"-"+(isidmax+1);
  spost+="','-',0,"+smtype.Tag1;
  spost+=",0,1,'',"+iserK;
  spost+=","+iser;
  spost+=",'"+sser;
  spost+="',0,0,0,0,"+smtype.ASubSys+",0,0,"+smtype.Ext1+",0";
  $.ajax({
    type: "POST",
    url: "omdev/smx.asp",
    data: "op=ins&sid=0&addr=0&type="+smtype.Type+spost,
    dataType:"json",
    success: function(msg){
      if(msg.op=="ok")
        window.location.reload(); // 动态方式?
    }
  });
}
</script>
</body>
</html>