<?php
require("../../inc/lib.inc");
?>

<?php
$maxRowPerPage=16;
$mtp3_systypeno=222;
$plat_systypeno=200;
$mss_platno=9;
$mtp3_modid=1;
$linkset_index_name="lsIndex";
$linkset_title_name="lsTitle";
$linkset_status_name="lsStatus";
$link_index_name="lkIndex";
$link_title_name="lkTitle";
$link_status_name="lkLinkStatus";
$link_lsID_name="lkLinkSetID";

$field_sep=",";
$line_sep=";";

function GetOIDByName($sysTypeNo,$name)
{
	$database="OBJ_".$sysTypeNo.".paramConf";
	$sql="select * from $database where name_2 like '%$name%' ";
	$res=@mysqli_query($pubConn,$sql);
	$row=@mysqli_fetch_array($res);
	$oids="";
	for($i=1;$i<=10;$i++)
	{
		$tmpN="level_".$i;
		$tmpV=$row[$tmpN]+0;
		if($tmpV == 0)
			break;
			
		$oids.=$tmpV.".";
	}
	if($i>1)
		$oids=substr($oids,0,strlen($oids)-1);
	
	return $oids;
}
function getLevel($objIdStr)
{
	$levelArr=explode('.',$objIdStr);
	for($j=0;$j<sizeof($levelArr);$j++)
	{
		$levelNo=$j+1;
		${"level_$levelNo"}=$levelArr[$j];
		if($Debug) echo "<BR>levelArr[$j]={$levelArr[$j]}";
		if($j == 0){
			$levelWhere ="level_$levelNo='${"level_$levelNo"}'";
		}else{
			$levelWhere .=" AND level_$levelNo='${"level_$levelNo"}'";
		}
	}
	$levelNo+=1;
	if($levelNo<=10)
		$levelWhere.=" AND level_$levelNo=0 ";
	return($levelWhere);
}
function getLinkInfo($table,$titleoid,$statusoid,$instance)
{
	$instanceNo=omc_keep_length($instance,"0",4);
	
	$tmpwhere=getLevel($titleoid);
	$sql="select * from $table where $tmpwhere and instanceNo = '$instanceNo' ";
	$res=@mysqli_query($pubConn,$sql);
	$num=mysqli_num_rows($res);
	if($num == 0)
		$linkInfo[Title]="";
	else
	{
		$row=@mysqli_fetch_array($res);
		$linkInfo[Title]=$row[initValue];
	}
	
	$tmpwhere=getLevel($statusoid);
	$sql="select * from $table where $tmpwhere and instanceNo = '$instanceNo' ";
	$res=@mysqli_query($pubConn,$sql);
	$num=mysqli_num_rows($res);
	if($num == 0)
		$linkInfo[Status]=0;
	else
	{
		$row=@mysqli_fetch_array($res);
		$linkInfo[Status]=$row[initValue]+0;
	}
	return $linkInfo;
}

function getStatusByHeartbeat()
{
	global $mss_platno,$plat_systypeno;
	$sql="select * from OMC_PUB.sysStat where sysTypeNo={$plat_systypeno} and sysNo={$mss_platno} order by updateTime desc ";
	$res=@mysqli_query($pubConn,$sql);
	$num=mysqli_num_rows($res);
	
	if($num == 0)
		return $StatusSet;
	
	$row=@mysqli_fetch_array($res);
	$baseStat=$row[detailStat];
	$compStat=substr($baseStat,38);
	$compStatLen=strlen($compStat)-1;
	//echo "$compStat<br>";
	$index=0;
	$i=0;
	while($i<$compStatLen)
	{
		$tmp=substr($compStat,$i,4);
		$component[$index][ID]=$tmp+0;
		$i+=4;
		$tmp=substr($compStat,$i,4);
		$component[$index][Length]=hexdec($tmp);
		$i+=4;
		$component[$index][Content]=substr($compStat,$i,$component[$index][Length]*2);
		$i+=$component[$index][Length]*2;
		$index++;
	}
	
	
	
	if($component[1][Length] == 0)
		return $StatusSet;
		
	
	$mtp3_hb=$component[1][Content];
	$pos=0;
	$link_hb=substr($mtp3_hb,8,256*2);
	$linkset_hb=substr($mtp3_hb,520,128*2);
	
	for($i=0;$i<256;$i++)
	{
		$tmpi=hexdec(substr($link_hb,$i*2,2));
		$tmpb=omc_keep_length(decbin($tmpi),"0",8);
		$tmpb=strrev($tmpb);
		for($j=0;$j<4;$j++)
		{
			$tmp2bit=strrev(substr($tmpb,$j*2,2));
			$StatusSet[LinkStatus][$i*4+$j]=bindec($tmp2bit);
		}
	}
	
	for($i=0;$i<128;$i++)
	{
		$tmpi=hexdec(substr($linkset_hb,$i*2,2));
		$tmpb=omc_keep_length(decbin($tmpi),"0",8);
		$tmpb=strrev($tmpb);
		for($j=0;$j<4;$j++)
		{
			$tmp2bit=strrev(substr($tmpb,$j*2,2));
			$tt=$i*4+$j;
			$yy=bindec($tmp2bit);
			$StatusSet[LinkSetStatus][$i*4+$j]=bindec($tmp2bit);
		}
	}
	
	return $StatusSet;
}

?>
<?php


$mtp3_param_table="OBJ_".$mtp3_systypeno.".param_".$mss_platno;
$linkset_index_oid=GetOIDByName($mtp3_systypeno,$linkset_index_name);
$linkset_title_oid=GetOIDByName($mtp3_systypeno,$linkset_title_name);
$linkset_status_oid=GetOIDByName($mtp3_systypeno,$linkset_status_name);
$link_index_oid=GetOIDByName($mtp3_systypeno,$link_index_name);
$link_title_oid=GetOIDByName($mtp3_systypeno,$link_title_name);
$link_status_oid=GetOIDByName($mtp3_systypeno,$link_status_name);
$link_lsID_oid=GetOIDByName($mtp3_systypeno,$link_lsID_name);

$response="";
//get the page list
$tmpwhere=getLevel($linkset_index_oid);
$sql="select * from $mtp3_param_table where $tmpwhere ";
$res=@mysqli_query($pubConn,$sql);
$num=mysqli_num_rows($res);
$page_count=ceil($num/$maxRowPerPage);
$response.="page_count".$field_sep.$page_count.$line_sep;	

//get the linkset index
$tmpwhere=getLevel($linkset_index_oid);
$from=$current_page_no * $maxRowPerPage;
$sql="select * from $mtp3_param_table where $tmpwhere order by instanceNo LIMIT $from, $maxRowPerPage ";
$res=@mysqli_query($pubConn,$sql);
$num=mysqli_num_rows($res);
$linksetCount=$num;

//system("echo '$sql' >> /tmp/test.txt");


if($num > 0)
	$response.="layer".$field_sep."blank_row_id".$field_sep."hide".$line_sep;
else
	$response.="layer".$field_sep."blank_row_id".$field_sep."show".$line_sep;

for($index=0;$index<$maxRowPerPage;$index++)
{
	//system("echo 'linksetCount=$linksetCount' >> /tmp/test.txt");
	if($index < $linksetCount)
		$response.="layer".$field_sep."row_id_{$index}".$field_sep."show".$line_sep;
	else
		$response.="layer".$field_sep."row_id_{$index}".$field_sep."hide".$line_sep;
}

$linkset_id_cond=" and (";	
for($index=0;$index < $num;$index++)
{
	$row=@mysqli_fetch_array($res);
	$linkset[$index][Instance]=$row[instanceNo]+0;
	$linkset_id_cond.=" initValue={$linkset[$index][Instance]} ";
	if($index != ($num-1))
		$linkset_id_cond.=" or ";
	$linkset[$index][LinkIndex]=0;
	$index_mirror[$linkset[$index][Instance]][Index]=$index;
}
$linkset_id_cond.=") ";
if($num == 0)
	$linkset_id_cond="";

$statusSet=getStatusByHeartbeat();

//get linkset status and title
$tmpwhere=getLevel($linkset_title_oid);
$from=$current_page_no * $maxRowPerPage;
$sql="select * from $mtp3_param_table where $tmpwhere order by instanceNo LIMIT $from, $maxRowPerPage ";
$res=@mysqli_query($pubConn,$sql);
$num=mysqli_num_rows($res);

for($index=0;$index < $num;$index++)
{
	$row=@mysqli_fetch_array($res);
	$instance=$row[instanceNo]+0;
	$pos=$index_mirror[$instance][Index];
	$linkset[$index][Title]=$row[initValue];
}

$tmpwhere=getLevel($linkset_status_oid);
$from=$current_page_no * $maxRowPerPage;
$sql="select * from $mtp3_param_table where $tmpwhere order by instanceNo LIMIT $from, $maxRowPerPage ";
$res=@mysqli_query($pubConn,$sql);
$num=mysqli_num_rows($res);

for($index=0;$index < $num;$index++)
{
	$row=@mysqli_fetch_array($res);
	$instance=$row[instanceNo]+0;
	$pos=$index_mirror[$instance][Index];
	//$linkset[$index][Status]=$row[initValue]+0;
	$linkset[$index][Status]=$statusSet[LinkSetStatus][$instance]+0;
}

$status_value=array(0=>"N",1=>"Y",2=>"I",3=>"--");
$status_color=array(0=>"#ff0000",1=>"#0000ff",2=>"#cecece",3=>"#000000");
for($index=0;$index<$linksetCount;$index++)
{
	if(strlen($linkset[$index][Title])>0)
		$value=$linkset[$index][Instance]."[".$linkset[$index][Title]."]";
	else
		$value=$linkset[$index][Instance];
	$response.="obj".$field_sep."ls_title_id_{$index}".$field_sep.$value.$field_sep."#000000".$line_sep;
	
	$value=$linkset[$index][Status]+0;
	$response.="obj".$field_sep."ls_status_id_{$index}".$field_sep.$status_value[$value].$field_sep.$status_color[$value].$line_sep;
}

//get link status
$tmpwhere=getLevel($link_lsID_oid);
$from=$current_page_no * $maxRowPerPage;
$sql="select * from $mtp3_param_table where $tmpwhere $linkset_id_cond order by instanceNo asc ";
$res=@mysqli_query($pubConn,$sql);
$num=mysqli_num_rows($res);

for($index=0;$index<$num;$index++)
{
	$row=@mysqli_fetch_array($res);
	$ls_instance=$row[initValue]+0;
	$lk_instance=$row[instanceNo]+0;
	$ls_index=$index_mirror[$ls_instance][Index];
	$lk_index=$linkset[$ls_index][LinkIndex];

	$lkInfo=getLinkInfo($mtp3_param_table,$link_title_oid,$link_status_oid,$lk_instance);
	
	$value=$lk_instance;
	//$color=$status_color[$lkInfo[Status]];
	$color=$status_color[$statusSet[LinkStatus][$lk_instance]];
	$response.="obj".$field_sep."lk_status_id_{$ls_index}_{$lk_index}".$field_sep."{$value}".$field_sep."{$color}".$line_sep;
	
	//$response.="title".$field_sep."lk_title_id_{$ls_index}_{$lk_index}".$field_sep."{$lkInfo[Title]}".$line_sep;
	
	
	$linkset[$ls_index][LinkIndex]++;
}

for($index=0;$index<$num;$index++)
{
	for($j=$linkset[$index][LinkIndex];$j<$maxRowPerPage;$j++)
	{
		$response.="obj".$field_sep."lk_status_id_{$index}_{$j}".$field_sep."--".$field_sep."#000000".$line_sep;
		//$response.="obj".$field_sep."lk_status_id_{$index}_{$j}".$field_sep."--".$line_sep;
	}
}

$response=substr($response,0,strlen($response)-1);
//system("date >> /tmp/test.txt");
echo $response;
	

  
?>
		