<!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>
  </div>
  <div class='list2' style='margin: 5px;'>
    <a class='you_p' href='#' style='color: #fff'>基本设置</a>&nbsp;&nbsp;<b><span id="anote"></span></b>
  </div>
  <div class='setbase' style='border: #B0B7BF 1px solid'>
    <table width='100%'>
    <tr><th width="15%"><span>监测项描述:</span></th><th style='text-align: left'><input id="tname" value="" style="width:250px"></input></th><th style='text-align: left'>如: 红外xx</th></tr>
    <tr style="display:none"><th><span>说  明:</span></th><th style='text-align: left'><input id="tnote" value="" style="width:250px"></input></th><th style='text-align: left'>帮助了解位置等</th></tr>
    <tr><th><span>端  口:</span> </th><th style='text-align: left'><select id="tport" style="width:150px"></select></th><th style='text-align: left'>端口选择</th></tr>
    
	<tr  style="display:none"><th><span>型  号:</span></th><th style='text-align: left'><select id="lsType" style="width:350px"></select></th><th style='text-align: left'>选择设备型号&nbsp;</h></tr>
	
    </table>
  </div>
  <div class='list2' style='margin: 5px;'>
    <a class='you_p' href='#' style='color: #fff'>参数设置</a>
  </div>
  
  <div class='setparam' style='border: #B0B7BF 1px solid'>
    <table  width='100%'>
      <tr><th width="15%">告警参数:</th><th style='text-align: left'>
        <input id="aparam0" type="radio" name="aparam" value="0" checked="checked">禁用告警</input>&nbsp;&nbsp;&nbsp;
        <input id="aparam1" type="radio" name="aparam" value="1">闭合告警</input>&nbsp;&nbsp;&nbsp;
        <input id="aparam2" type="radio" name="aparam" value="2">断开告警</input></th><th style='text-align: left'>告警方式选择</th></tr>
    </table>
  </div>
  
  <div id = "selectai" class='setparamai' style='border: #B0B7BF 1px solid'>
  <table width='100%'>    	  
      <tr>
	    <td><span>设置值:</span></td>
	    <td style="width:110px"><select id="Almselet" style="width:80px"  maxlength="10" >
			  <option value="10">不检查
			  <option value="11">范围内
              <option value="12">范围外	
			  </select>           
        </td>
		<td>&nbsp; &nbsp;<input name="almlow" style="width:80px" type="text" id="almlow" value="">&nbsp; &nbsp;~&nbsp; &nbsp;<input name="almhight" style="width:80px" type="text" id="almhight" value=""></td> 
		<td><span>偏移量:</span>&nbsp;<input name="offset" style="width:80px" type="text" id="offset" value=""><span>单位:</span>&nbsp;<input name="unit" style="width:60px" type="text" id="unit"  value=""></td>
      </tr>
    </table>
  </div>
  
  <div class='list2' style='margin: 5px;'>
    <a class='you_p' href='#' style='color: #fff'>告警设置</a>
    <span>&nbsp;&nbsp;&nbsp;模板选择：
    <select id="lsAlm" style="width:150px" onchange="onAlmSel()">
    <option value="0">无(不告警)
    <option value="1">声光告警
    <option value="2">短信告警
    <option value="3">电话告警
    <option value="4">邮件告警
    <option value="5">声光+短信+电话告警
    <option value="6">自定义告警
    </select></span>
  </div>
  <div class='setalm' style='border: #B0B7BF 1px solid'>
    <table  width='100%'>
      <tr><th width="15%"><span>预警保持时间(s):</span></th><th style='text-align: left'><input id="atimeon" value="5" style="width:100px"></input></th><th style='text-align: left'>&nbsp;数值范围:0~9999</th></tr>
      <tr><th><span>重复告警间隔(s):</span></th><th style='text-align: left'><input id="atimeredo" value="900" style="width:100px"></input></th><th style='text-align: left'>&nbsp;数值范围:0~9999</th></tr>
      <tr><th><span>告警发送方式:</span></th><th style='text-align: left'>
        <input type="checkbox" name="almmask" value="1">声光</input>&nbsp;&nbsp;
        <input type="checkbox" name="almmask" value="2">语音</input>&nbsp;&nbsp;
        <input type="checkbox" name="almmask" value="4">短信</input>&nbsp;&nbsp;
        <input type="checkbox" name="almmask" value="8">彩信</input>&nbsp;&nbsp;
        <input type="checkbox" name="almmask" value="16">电话</input>&nbsp;&nbsp;
        <input type="checkbox" name="almmask" value="32">邮件</input>&nbsp;&nbsp;
        <input type="checkbox" name="almmask" value="256">联动1</input>&nbsp;&nbsp;
        <input type="checkbox" name="almmask" value="512">联动2</input>&nbsp;&nbsp;</th></tr>
      <tr><th><span>告警时段选择:</span></th><th style='text-align: left'>
        <select id="lsAlmTime" style="width:150px">
        <option value="0">全时段
        <option value="1">工作时段
        <option value="2">周六周日
        <option value="3">自定义时段
        </select></th><th>&nbsp;</th></tr>
      <tr><th><span>告警抓拍设置:</span></th><th style='text-align: left'>
        <select id="lsAlmCam" style="width:250px">
        <option value="0">无
        </select></th><th>&nbsp;</th></tr>
      <tr><th><span>告警短信内容:</span></th><th style='text-align: left'>
        <textarea id="almsms" style='width: 400px'>短信内容</textarea></th><th>&nbsp;</th></tr>
      <tr><th><span>告警电话内容:</span></th><th style='text-align: left'>
        <textarea id="almtel" style='width: 400px'>电话内容</textarea></th><th>&nbsp;</th></tr>
      <tr><th><span>告警解除方式:</span></th><th style='text-align: left'>
        <input type="checkbox" name="offmask" value="1">声光</input>&nbsp;&nbsp;
        <input type="checkbox" name="offmask" value="2">语音</input>&nbsp;&nbsp;
        <input type="checkbox" name="offmask" value="4">短信</input>&nbsp;&nbsp;
        <input type="checkbox" name="offmask" value="8">彩信</input>&nbsp;&nbsp;
        <input type="checkbox" name="offmask" value="16">电话</input>&nbsp;&nbsp;
        <input type="checkbox" name="offmask" value="32">邮件</input>&nbsp;&nbsp;
        <input type="checkbox" name="offmask" value="256">联动1</input>&nbsp;&nbsp;
        <input type="checkbox" name="offmask" value="512">联动2</input>&nbsp;&nbsp;</th></tr>
      <tr><th><span>告警解除内容:</span></th><th style='text-align: left'>
        <textarea id="almoff" style='width: 400px'>告警解除内容</textarea></th><th>&nbsp;</th></tr>
      <tr><th><span>信息接收人:</span></th><th id="lsRecv" style='text-align: left'>
        <input type="checkbox" name="arecv">管理员</input>&nbsp;&nbsp
        <input type="checkbox" name="arecv">用户1</input>&nbsp;&nbsp;
    </table>
  </div>

</div>
<div>
  <table width='100%'><tr>
    <th><a id='abtn' href='javascript:saveclick()' class='back' style='color: #fff;background: #ffa225'>保存</a></th>
    <th><a href='javascript:history.back()' class='back' style='color: #fff'>返回</a></th>
  </tr></table>
</div>

<script>
var isid=<% write(sid);%>;
var itype=<% write(type);%>;
var iport=<% write(port);%>;
var tag1=<% write(tag1);%>;
var aidType=0;
var aidSIDx=0;

<% db_Open("/NandFlash/dbCfg.db3"); %>
var jtype=<% db_QryAdv("select Type,t.Kind,t.Name,t.Note,Tag1,Port,ASubSys,k.Name as KName from tbSMType t left join tbSMKind k on k.Kind=t.Kind where Port=1 order by Type,Tag1",0); %>;
var jport=<% db_QryAdv("select * from tbPort where Type=1 and (Val not in (select SerType from tbSM where State=0) or Val="+port+") order by Type,Name",0); %>;
var jsid=<% db_QryAdv("select s.*,k.Name as KName,t.Note as TNote,t.Name as TName 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 jcam=<% db_QryAdv("select s.SID,s.Name,t.Name as TName from tbSM s left join tbSMType t on t.Type=s.Type and t.Tag1=s.Tag1 where s.Type=3072 or s.Type=3073",0); %>;
var jalm=<% db_QryAdv("select * from tbAlm where AID<16",0); %>;
var jalmType=<% db_QryAdv("select * from tbAlm where AID="+type,0); %>;
var jalmSid=<% db_QryAdv("select * from tbAlm where AID="+(sid+4096),0); %>;
var jrecv=<% db_QryAdv("select Name,Recv from tbUser where State=0 and Recv>0",0); %>;

var jsmai=<% db_QryAdv("select * from tbAIDef where Type="+type,0); %>;
var jfx =<%db_QryAdv("select * from tbAICfg where Type ="+type+" order by Xval",0);%>
var jfy =<%db_QryAdv("select * from tbAICfg where Type ="+type,0);%>
var jdefval= <%db_QryAdv("select * from tbSMDefVal where SID = "+(port>>8)+" order by Fld",0); %>;
var jalmTime=<% db_QryAdv("select TID, Note from tbAlmTime",0); %>;
<% db_Close(); %>

listAlmTime();
function listAlmTime(){
  if(jalmTime.reccnt==0) return;
  var s="";
  $.each(jalmTime.recs, function(i,n){
    s+="<option value='"+n.TID+"'>"+n.Note;
  });
  $("#lsAlmTime").html(s);
}

var lsid = 0;

function lisAialm(sid,port)
{
  if(jfx.reccnt<2)
  {
    $("#almlow").val(0);
	$("#almhight").val(0);
	$("#Almselet").val(10);
  }
  else
  {
    if(jdefval.reccnt>0) 
	  {  	       
		$("#almlow").val((calfxy(jfx,jdefval.recs[port%256-1].vref/100,1)).toFixed(2));
		$("#almhight").val((calfxy(jfx,jdefval.recs[port%256-1].vref2/100,1)).toFixed(2));
		$("#Almselet").val(jdefval.recs[port%256-1].vmode);		
	  }
  }
  
  if(jsmai.reccnt>0)
  {
    $("#unit").val(jsmai.recs[0].Unit);
    $("#offset").val(jsmai.recs[0].Offset);
  }
}
listT();
function listT(){
  if(jtype.reccnt==0) return;
  var s="";
  $.each(jtype.recs, function(i,n){
    s+="<option value='"+n.Type+"'>"+n.KName+" ("+n.Name+")&nbsp;&nbsp;&nbsp;"+n.Note;
  });
  $("#lsType").html(s);
  $("#lsType").val(itype);
}

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

listCam();
function listCam(){
  if(jcam.reccnt==0) return;
  var s="<option value='0'>无";  
  $.each(jcam.recs, function(i,n){
    s+="<option value='"+n.SID;
    s+="'>"+n.Name+" ("+n.TName+")</input>&nbsp;&nbsp;";
  });
  $("#lsAlmCam").html(s);
}

listRecv();
function listRecv(){
  if(jrecv.reccnt==0) return;
  var s="";
  $.each(jrecv.recs, function(i,n){
    s+="<input type='checkbox' name='arecv' value='"+n.Recv+"'>"+n.Name+"</input>&nbsp;&nbsp;";
  });
  $("#lsRecv").html(s);
}

listAlm();
function listAlm(){
  if(jalm.reccnt==0) return;
  var s="";
  $.each(jalm.recs, function(i,n){
    s+="<option value='"+n.AID+"'>"+n.Note;
    if(n.Note.indexOf("按类型")>=0) aidType=n.AID;
    if(n.Note.indexOf("自定义")>=0) aidSIDx=n.AID;
  });
  $("#lsAlm").html(s);
  if(jsid.reccnt>0){
    if (jsid.recs[0].AID<16)
      $("#lsAlm").val(jsid.recs[0].AID);
    else{
      if(jsid.recs[0].AID==jsid.recs[0].Type)
        $("#lsAlm").val(aidType);
      else
        $("#lsAlm").val(aidSIDx);
    }
    onAlmSel();
  }
}

$(function(){
  if(isid==0){
    $("#anote").html("开关量设备增加");
    $("#asid").hide();
    $("#abtn").html("增加");
  }
  else if (jsid.reccnt>0){
    $("#anote").html(jsid.recs[0].TNote+" ("+jsid.recs[0].TName+")");
    $("#tname").val(jsid.recs[0].Name);
    $("#tnote").val(jsid.recs[0].Note);
    $("#tport").val(jsid.recs[0].SerType);
    if (jsid.recs[0].Ext1==0)
      $("#aparam0").attr("checked",true);
    if (jsid.recs[0].Ext1==1)
      $("#aparam1").attr("checked",true);
    if (jsid.recs[0].Ext1==2)
      $("#aparam2").attr("checked",true);
  }
});

function onAlmSel(){
  var idx=$('#lsAlm').prop('selectedIndex');
  var alm=jalm.recs[idx];
  if(alm.AID==aidType){
    if(jalmType.reccnt>0) showAlm(jalmType.recs[0]);
  }
  else if(alm.AID==aidSIDx){
    if(jalmSid.reccnt>0) showAlm(jalmSid.recs[0]);
  }
  else
    showAlm(alm);
}

function showAlm(alm){
  $("#atimeon").val(alm.TimeOn);
  $("#atimeredo").val(alm.TimeRedo);
  showAlmMask(alm.AlmMask);
  $("#lsAlmTime").val(alm.AlmTid);
  $("#lsAlmCam").val(alm.AlmCam);
  $("#almsms").val(alm.AlmSms);
  $("#almtel").val(alm.AlmTel);
  showOffMask(alm.AlmOffMask);
  $("#almoff").val(alm.AlmOff);
  showRecv(alm.AlmRecv);
}
function showAlmMask(imask){
  $("input[name='almmask']").each(function(){
    var iv=parseInt(this.value);
    if(iv & imask)
      $(this).attr("checked",true);
    else
      $(this).attr("checked",false);
  });
}
function showOffMask(imask){
  $("input[name='offmask']").each(function(){
    var iv=parseInt(this.value);
    if(iv & imask)
      $(this).attr("checked",true);
    else
      $(this).attr("checked",false);
  });
}
function showRecv(irecv){
  $("input[name='arecv']").each(function(){
    var iv=parseInt(this.value);
    if(irecv & (1<<(iv-1)))
      $(this).attr("checked",true);
    else
      $(this).attr("checked",false);
  });
}

function saveAlm(){
  var spost="&setval=";
  spost+="TimeOn="+$("#atimeon").attr("value");
  spost+=",TimeRedo="+$("#atimeredo").attr("value");
  spost+=",AlmMask="+getAlmMask();
  spost+=",AlmTid="+$("#lsAlmTime").val();
  spost+=",AlmCam="+$("#lsAlmCam").val();
  spost+=",AlmSms='"+$("#almsms").attr("value");
  spost+="',AlmTel='"+$("#almtel").attr("value");
  spost+="',AlmOffMask="+getOffMask();
  spost+=",AlmOff='"+$("#almoff").attr("value");
  spost+="',AlmRecv="+getRecv();
  var idx=$('#lsAlm').prop('selectedIndex');
  var alm=jalm.recs[idx];
  var iaid=alm.AID;
  if(alm.AID==aidSIDx){
    iaid=isid+0x1000;
    spost+=",Note='"+$("#tname").attr("value")+" 告警'";
  }
  else if(alm.AID==aidType){
    iaid=itype;
    idx=$('#lsType').prop('selectedIndex');
    spost+=",Note='"+jtype.recs[idx].KName+" 告警'";
  }
  
  $.ajax({
    type: "POST",
    url: "omdev/almx.asp",
    data: "op=upd&aid="+iaid+"&sid="+isid+spost,
    dataType:"json",
    success: function(msg){
      if(msg.op=="ok")
        history.back();
    }
  });
}
function getAlmMask(){
  var iv=0;
  $("input[name='almmask']:checked").each(function(){
    iv+=parseInt(this.value);
  });
  return iv;
}
function getOffMask(){
  var iv=0;
  $("input[name='offmask']:checked").each(function(){
    iv+=parseInt(this.value);
  });
  return iv;
}
function getRecv(){
  var iv=0;
  $("input[name='arecv']:checked").each(function(){
    iv|=(1 << (parseInt(this.value)-1));
  });
  return iv;
}
function saveAiuint()
{
  var spost ="";
  spost+="&type="+itype;
  spost+="&tag="+"0";
  spost+="&unit="+"'"+$("#unit").attr("value")+"'";
  spost+="&offset="+$("#offset").attr("value");
  $.ajax({                   //post插入一个记录到tbSM表格，
		  type: "POST",
		  url: "omdev/smx.asp",
		  data: "op=updai"+spost,
		  dataType:"json",
		  success: function(msg){
			if(msg.op=="ok")
			 // history.back();
			 ;
		  }
		});
}
slport();
function slport()
{
  if (tag1 == 1)//查找字符串有AI
   {
     $(".setparamai").show();
	 $(".setparam").hide();
   }   
  else
  {
    $(".setparam").show();
    $(".setparamai").hide();
  }   
}
function saveparam()
{
  var iv=$("#tport").find("option:selected").text();
  var aport = $("#tport").val();
  var nport ="";
  var nsid = aport>>8;
  var nch ="";
  var idx=$('#lsType').prop('selectedIndex');
  var smtype=jtype.recs[idx];
  if(aport!=iport)
  {
	 nch = aport%256;
  }
  else
  {
     nch = iport%256;
  }  
  if (tag1 == 1)//查找字符串有AI
   { 
	var sop="updalm";
	var aipost ="";
	var imode=$("#Almselet").val();
	aipost+="&lsid="+lsid;
	aipost+="&nsid="+nsid;
	aipost+="&lch="+iport%256;
	aipost+="&nch="+nch;
	aipost+="&insval=vmode="+imode+", vref="+parseInt(calfxy(jfy,($("#almlow").attr("value")-$("#offset").attr("value")),0)*100)+", vref2="+parseInt(calfxy(jfy,($("#almhight").attr("value")-$("#offset").attr("value")),0)*100)+ ", ASubSys="+smtype.ASubSys+", ANote=''"			  		
		$.ajax({                 
		  type: "POST",
		  url: "omdev/smdef.asp",
		  data: "op="+sop+"&type="+itype+aipost,
		  async: false,
		  dataType:"json",
		  success: function(msg){
			if(msg.op!="ok")
			  alert("保存 "+n.Note+" 失败!");
		  }
		});
	} 
}
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 = parseInt(val);
	  var ymin =0,ymax=0;
	  
	  if(data.reccnt>=3)
	  {
	    if(xy == 0) 
		  {
			ymin= data.recs[0].Yval;
			ymax=data.recs[data.reccnt-1].Yval;
		  }
		  else
		  {
			ymin= data.recs[0].Xval;
			ymax=data.recs[data.reccnt-1].Xval;
		  } 
	   
		 if( dat<=ymin)//小于最小的
		 {
		   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 >=ymax)//大于最大
		 {
		   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
		 {
		   for(var i=0;i < data.reccnt;i++)
		   {
			   if(data.recs[i].Yval<dat && dat<data.recs[i+1].Yval)
			   {
				  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;   					  
			   }
			}
		 }     
	    
	  }
	  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 
	  {
	    alert("请输入至少2组模拟量传感器参数!");
	  }	  	 	  
	  if(xy==0)
	  {
	    fxy =(val-b)/k;  	    
	  }
	  else
	  {
	     fxy =k*val+b;  	    
	  }   	
	   return fxy;
	}
function saveclick(){
  /*var smask="";
  var imask=0;
  $("input[name='almmask']:checked").each(function(){
    smask+=" "+this.value;
    imask+=parseInt(this.value);
  });
  alert(smask+" iv="+imask+" atid="+$("#lsAlmTime").val());
  return;*/

  var sname="";
  sname=$("#tname").attr("value");
  if (sname=="" || $("#atimeon").attr("value")=="" || $("#atimeredo").attr("value")==""){
    alert("错误: 输入不完整,请重新输入!");
    return;
  }
  var iv=0;
  var spost="";
  var idx=$('#lsType').prop('selectedIndex');
  var smtype=jtype.recs[idx];
  if(isid==0){
    //null,kind,type,'name','',0,tag1,0,1,'',serK,serT,serD,serA,0,0,0,ASubSys,ID1,0,ext1,0
    iv=parseInt($("#lsType").val());
    spost+="&insval=null,"+(iv-(iv%256));
    spost+=","+iv;
    spost+=",'"+sname;
    spost+="','"+$("#tnote").attr("value");
    iv=parseInt($("#tport").val());
    spost+="',0,"+smtype.Tag1+",0,1,'',"+(iv & 0xffff00);
    spost+=","+iv;
    idx=$('#lsAlm').prop('selectedIndex');
    var alm=jalm.recs[idx];
	if(tag1 ==1) {
	 spost+=",'',0,0,0,0,"+smtype.ASubSys+",0,0,0,0";
	}
	else
	{
	 spost+=",'',0,0,0,"+alm.AID+","+smtype.ASubSys+",0,0,"+$("input:radio[name='aparam']:checked").val()+",0";
	}
	
    $.ajax({
      type: "POST",
      url: "omdev/smx.asp",
      data: "op=ins&sid=0&addr=0&type="+iv+"&name="+sname+spost,
      dataType:"json",
      success: function(msg){
        if(msg.op=="ok")
          history.back();
      }
    });
    return;
  }
  saveAiuint();
  saveparam();
  spost+="&sid="+isid;
  spost+="&name=";
  spost+="&setval=Name='"+sname;
  spost+="',Note='"+$("#tnote").attr("value");
  iv=parseInt($("#tport").val());
  spost+="',SerKind="+(iv & 0xffff00)+",SerType="+iv;
  spost+=",SerAddr=0";
  spost+=",Type="+$("#lsType").val();
  if(tag1 == 1)
  {
     spost+=",Ext1="+"0";
  }
  else
  {
   spost+=",Ext1="+$("input:radio[name='aparam']:checked").val();
  }
  spost+=",ASubSys="+smtype.ASubSys;
  $.ajax({
    type: "POST",
    url: "omdev/smx.asp",
    data: "op=upd"+spost,
    dataType:"json",
    success: function(msg){
      if(msg.op=="ok")
        saveAlm(); //history.back();
      else
        alert("保存数据 编号"+itype+"-"+itag+" 失败!");
    }
  });

  //var s="";
  //s=$("input:radio[name='aparam']:checked").val() ;
  //alert("alm="+s);
}
</script>
</body>
</html>