<!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: 5px;'>
    <a class='you_p' href='#' style='color: #fff'>基本信息</a>&nbsp;&nbsp;<b><span id="anote">SID: xx</span></b>
  </div>
  <div class='setbase' style='border: #B0B7BF 1px solid'>
    <table  width='100%'>
    <tr><th><span>设备命名:</span></th><th style='text-align: left' id='tname'></th></tr>
    <tr><th><span>通讯设置:</span></th><th style='text-align: left' id='tserdef'></th></tr>
    <tr><th><span>其  他:</span></th><th style='text-align: left' id='text1'></th></tr>
    </table>
  </div>

  <div class='list2' style='margin: 5px;'>
    <a class='you_p' href='#' style='color: #fff'>参数设置</a>
  </div>
  <div id='setparam' style='border: #B0B7BF 1px solid'>
  </div>

  <div class='list2' style='margin: 5px;'>
    <a class='you_p' href='#' style='color: #fff'>告警设置</a>&nbsp;&nbsp;&nbsp;<span id="aalm">无(不告警)</span>
  </div>

</div>
<div>
  <table width='100%'><tr>
    <th><a href='javascript:history.back()' class='back' style='color: #fff'>返回</a></th>
  </tr></table>
</div>

<script>

var itype=<% write(itype);%>;
var isid=<% write(sid);%>;
var aidType=0;
var aidSIDx=0;

<% db_Open("/NandFlash/dbCfg.db3"); %>
var jdef=<% db_QryAdv("select s.*,v.vmode as Dvmode,v.vref as Dvref,v.vref2 as Dvref2,v.ANote as VNote from tbSMDef s left join tbSMDefVal v on (v.Type=s.Type and v.Fld=s.Fld and v.SID="+sid+") where s.Type="+itype+" and s.vmode>=0 order by FldDept,Fld",0); %>;
var idefv=<% write(db_QryInt("select count(*) from tbSMDefVal where Type="+itype+" and SID="+sid,1)); %>;
var jsid=<% db_QryAdv("select s.*,k.Name as KName,t.Name as TName,t.ExtDi,t.ExtSer from tbSM s left join tbSMKind k on k.Kind=s.Kind left join tbSMType t on t.Type=s.Type and t.Tag1=s.Tag1 where SID="+sid,0); %>;
var jalm=<% db_QryAdv("select * from tbAlm where AID<16 and AID=(select AID from tbSM where SID="+sid+")",0); %>;
var jport =<% db_QryAdv("select * from tbPort where Type=1",0);%>;
var jdefval= <%db_QryAdv("select * from tbSMDefVal order by Fld",0); %>;
var jfx =<%db_QryAdv("select * from tbAICfg where Type ="+itype+" order by Xval",0);%>
var jfy =<%db_QryAdv("select * from tbAICfg where Type ="+itype+" order by Yval",0);%>
<% db_Close(); %>
var iport = 0;
var Asid = 0;
var chanel =0;
listSID();
function listSID(){
  if(jsid.reccnt==0) return;
  $("#anote").html("SID: "+isid+" ("+jsid.recs[0].TName+")");
  $("#tname").html(jsid.recs[0].Name);
  $("#tserdef").html(jsid.recs[0].SerDef);
  iport = jsid.recs[0].SerType;
  Asid = iport>>8;
  chanel = iport%256;
  //$("#text1").val(jsid.recs[0].Ext1);
  if(jalm.reccnt==0)
    $("#aalm").html("自定义告警");
  else
    $("#aalm").html("AID:"+jalm.recs[0].AID+" "+jalm.recs[0].Note);
}

$(function(){
    listParam();
});

function isNumber(obj) {
    return typeof obj === 'number' && !isNaN(obj)
}
function calfxy(data,val,xy)//xy=0返回x值，=1返回y值
{  
	var ydx=0;var xdx = 0;var k = 0;var b =0 ; var fy =0;
	var dat = val;var xyfirst =0,xylast=0;

	if(data.reccnt>=3)
	{
		if(xy == 0) 
		{
		xyfirst= data.recs[0].Yval;
		xylast=data.recs[data.reccnt-1].Yval;
		}
		else
		{
		xyfirst= data.recs[0].Xval;
		xylast=data.recs[data.reccnt-1].Xval;
		} 
		for(var i=0;i < data.reccnt;i++)
		{
			if( dat< xyfirst)
			{
				xdx = data.recs[1].Xval-data.recs[0].Xval;
				ydx =data.recs[1].Yval-data.recs[0].Yval;
				k =ydx/xdx;
				b =data.recs[0].Yval-k*data.recs[0].Xval;//b=y-k*x////y=k*x+b
			}
			else if ( dat > xylast)
			{
				xdx = data.recs[data.reccnt-1].Xval-data.recs[data.reccnt-2].Xval;
				ydx =data.recs[data.reccnt-1].Yval-data.recs[data.reccnt-2].Yval;
				k =ydx/xdx;
				b =data.recs[data.reccnt-1].Yval-k*data.recs[data.reccnt-1].Xval;//b=y-k*x////y=k*x+b	   
			}
			else
			{
				if(data.recs[i].Yval<=dat && data.recs[i+1].Yval>=dat)
				{
					xdx = data.recs[i+1].Xval-data.recs[i].Xval;
					ydx =data.recs[i+1].Yval-data.recs[i].Yval;
					k =ydx/xdx;
					b =data.recs[i].Yval-k*data.recs[i].Xval;//b=y-k*x////y=k*x+b				  
				}
			}     
		}
	}
	else if(data.reccnt==2)
	{
		xdx = data.recs[1].Xval-data.recs[0].Xval;
		ydx =data.recs[1].Yval-data.recs[0].Yval;
		k =ydx/xdx;
		b =data.recs[0].Yval-k*data.recs[0].Xval;//b=y-k*x////y=k*x+b
	}
	else 
	{
		;
	}	  	 	  
	if(xy==0)
	{
		fxy =(val-b)/k;  	    
	}
	else
	{
		fxy =k*val+b;  	    
	}   	
	return fxy;
}
function listParam() 
{
	if(jdef.reccnt==0 && jsid.reccnt ==0) 
	{
		$("#setparam").html("&nbsp;");
		//$("#setdetail").hide();
		return;
	}
	var stbl="<table width='100%'>";
	var ichks = 0;
	if( itype > 3590)//模拟量
	{
		for(var i =0 ; i< jdefval.reccnt;i++)
		{
			if(jdefval.recs[i].SID == Asid && chanel ==jdefval.recs[i].Fld)
			{
				var set =jdefval.recs[i].vmode;
				stbl+="<tr><th>设置值:&nbsp;</th> ";
				stbl+="<th style='text-align: left'>";
				if(set==10) stbl+="不检测";
				else if(set==11) stbl+="范围内";
				else if(set==12) stbl+="范围外";
				stbl+="&nbsp;";
				stbl+=(calfxy(jfx,jdefval.recs[i].vref/100,1)).toFixed(2);
				stbl+="&nbsp;~&nbsp;";
				stbl+=(calfxy(jfx,jdefval.recs[i].vref2/100,1)).toFixed(2);
				stbl+="&nbsp;最小值|低于此值告警</th><th style='text-align: left'>最大值|高于此值告警</th></tr>";
				stbl+="</tabel>";
				break;
			}
		}
		}
	else
	{
		//$.each(jdef.recs,function(i,n){
		for(var i=0; i<jdef.reccnt; i++){
			var n=jdef.recs[i];
			if (isNumber(n.Dvmode))
			n.vmode=n.Dvmode;
			if (isNumber(n.Dvref))
			n.vref=n.Dvref;
			if (isNumber(n.Dvref2))
			n.vref2=n.Dvref2;
			var ivmode=n.vmode;
			if(n.Fld==0){
				if(ichks==1) stbl+="</th></tr>";
				stbl+="<tr><th style='padding: 10px;background:#E5E9EC'>&nbsp</th><th style='padding: 10px;background:#E5E9EC'>"+n.Note+"</th><th style='padding: 10px;background:#E5E9EC'>&nbsp;</th></tr>";
				ichks = n.vkind;
				if(ichks==1)
				stbl+="<tr><th>"+n.Note+"</th><th>";
			}
			else if(n.Note!='' && ivmode>=0){
				if(ichks==1) {
					if(ivmode) stbl+="■"; else stbl+="□";
					stbl+=n.Note+"&nbsp;&nbsp;&nbsp";
				}
				else{
					stbl+="<tr><th>"+n.Note+"</th><th style='text-align: left'>";
					if(ivmode==0 || ivmode==10) stbl+="不检测";
					else if(ivmode==1) stbl+="等于";
					else if(ivmode==2) stbl+="不等于";
					else if(ivmode==3) stbl+="大于";
					else if(ivmode==4) stbl+="小于";
					else if(ivmode==11) stbl+="范围内";
					else if(ivmode==12) stbl+="范围外";
					stbl+="&nbsp;&nbsp;&nbsp;";
					if(ivmode<10){
						stbl+="("+(n.vref/n.vfactor)+")&nbsp;"+n.vtip;
					}
					else {
						stbl+="&nbsp;"+(n.vref/n.vfactor)+" &nbsp;~&nbsp;";
						stbl+="&nbsp;"+(n.vref2/n.vfactor)+"&nbsp;&nbsp;&nbsp;&nbsp;"+n.vtip;
					}
					if(itype==512)
						stbl+="</th><th>"+n.VNote+"</th></tr>";
					else
						stbl+="</th><th>"+n.vtip2+"</th></tr>";
				}
			}
		}
		if (ichks==1) stbl+="</th><th></th></tr>";
		stbl += "</table>";
	}
	$("#setparam").html(stbl);
}

</script>
</body>
</html>