<?php

require("../../inc/lib.inc");

$DEBUG = 0;
$pubDb		 = 'OMC_PUB';
$sysInfoTable='OMC_PUB.sysInfo';
$sysStatTable='OMC_PUB.sysStat';
$initValue	 ='initValue';
$readValue	 ='readValue';

{//get all exist elements from elementExistConf
	$elementConfTb = 'elementExistConf';
	$selSql = " select elementTypeNo from $elementConfTb where existFlag='1' and elementTypeNo!='0' order by elementNo";
	if($DEBUG) echo "<br>selSql=$selSql";
	$res = @mysqli_query($pubConn,$selSql);
	$i=0;
	while($rows = mysqli_fetch_array($res))
	{
		$existElementArr[$i++] = $rows[elementTypeNo];
	}
	if(0)print_r($existElementArr);
}//end get all exisit elements

$localElementArr=array(
	array('sysTypeName'=>'MSC',	'sysTypeNo'=>'320','sysTypeSSN'=>'8','GT'=>'--','existFlag'=>'0','license'=>'--','licenseColor'=>'','users'=>'--','usersColor'=>'','state'=>'1','stateColor'=>'','confLink'=>"../mss/msc.php?load_flag=$getflag"),
	array('sysTypeName'=>'VLR',	'sysTypeNo'=>'325','sysTypeSSN'=>'7','GT'=>'--','existFlag'=>'0','license'=>'--','licenseColor'=>'','users'=>'--','usersColor'=>'','state'=>'1','stateColor'=>'','confLink'=>"../mss/vlr.php?load_flag=$getflag"),
	array('sysTypeName'=>'HLR',	'sysTypeNo'=>'330','sysTypeSSN'=>'6','GT'=>'--','existFlag'=>'0','license'=>'--','licenseColor'=>'','users'=>'--','usersColor'=>'','state'=>'1','stateColor'=>'','confLink'=>"../mss/hlr.php?load_flag=$getflag"),
	array('sysTypeName'=>'AUC',	'sysTypeNo'=>'340','sysTypeSSN'=>'10','GT'=>'--','existFlag'=>'0','license'=>'--','licenseColor'=>'','users'=>'--','usersColor'=>'','state'=>'1','stateColor'=>'','confLink'=>"../mss/auc.php?load_flag=$getflag"),
	array('sysTypeName'=>'SMSC','sysTypeNo'=>'350','sysTypeSSN'=>'11','GT'=>'--','existFlag'=>'0','license'=>'--','licenseColor'=>'','users'=>'--','usersColor'=>'','state'=>'1','stateColor'=>'','confLink'=>"../mss/smsc.php?load_flag=$getflag"),
	array('sysTypeName'=>'PPS',	'sysTypeNo'=>'360','sysTypeSSN'=>'147','GT'=>'--','existFlag'=>'0','license'=>'--','licenseColor'=>'','users'=>'--','usersColor'=>'','state'=>'1','stateColor'=>'','confLink'=>"../mss/pps.php?load_flag=$getflag"),
	array('sysTypeName'=>'EIR',	'sysTypeNo'=>'380','sysTypeSSN'=>'9','GT'=>'--','existFlag'=>'0','license'=>'--','licenseColor'=>'','users'=>'--','usersColor'=>'','state'=>'1','stateColor'=>'','confLink'=>"../mss/eir.php?load_flag=$getflag"),
	array('sysTypeName'=>'MNP',	'sysTypeNo'=>'370','sysTypeSSN'=>'32','GT'=>'--','existFlag'=>'0','license'=>'--','licenseColor'=>'','users'=>'--','usersColor'=>'','state'=>'1','stateColor'=>'','confLink'=>"../mss/mnp.php?load_flag=$getflag"));


if($DEBUG) echo "<br>正在调试中！！";
//==========================================================
//变量定义
//==========================================================
?>

<?php
////////////////
$locEleParTab='OBJ_223.param_99';
for($i=0;$i< sizeof($localElementArr);$i++)
{
	$canAdd = array_search($localElementArr[$i][sysTypeNo], $existElementArr);
	for($k=0; $k<sizeof($existElementArr);$k++)
	{
		if($localElementArr[$i][sysTypeNo] == $existElementArr[$k])
		{
			$localElementArr[$i][existFlag]=1;
			break;
		}
	}
}
if($localElementArr[0][existFlag]==1)
{
	$localElementArr[1][existFlag]=1;
}
////////////////

for($i=0;$i<sizeof($localElementArr);$i++)
{
	if($localElementArr[$i][existFlag]==1)
	{
		$getSql="SELECT baseStat,updateTime FROM $sysInfoTable	WHERE sysTypeNo='{$localElementArr[$i][sysTypeNo]}' 
		ORDER BY sysNo,subSysNo";
		if($DEBUG) echo "<BR>getSql=$getSql";
		$result	=mysqli_query($pubConn,$getSql);
		if(mysqli_num_rows($result)==0)
		{
			$localElementArr[$i][state]=0;
			$localElementArr[$i][stateColor]='#FF0000';
		}
		else
		{
			while($rows=mysqli_fetch_array($result))
			{
				$out_time=date("Y-m-d H:i:s",mktime(date("H"),date("i")-10,0,date("m"),date("d"),date("Y")));				
				//if(1&&$localElementArr[$i][sysTypeNo]==360) echo "<br>" . "{$rows[1]}< $out_time";
				if($rows[0]==0||$rows[1]< $out_time)
				{
					$localElementArr[$i][state]=0;
					$localElementArr[$i][stateColor]='#FF0000';
				}
			}
		}
	}
}
?>

<?php
function getLevelWhere($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"}'";
		}
	}
	return($levelWhere);
}

function getSubSyss($num)
{
	$i=0;
	do
	{
		$arr[$i++] = $num % 2;		
	}while($num = floor($num/2));
	for($j=$i; $j<4; $j++)
		$arr[$j] = 0;
	
	return $arr;
}
function toAscii($byteValue){	
	$charValue = "";
	$tmpLen=strlen($byteValue);
	for($charCount=0; $charCount < $tmpLen; $charCount++){
		$unitByte = substr($byteValue,$charCount,2);
		$charCount++;
		if($unitByte == 0) break;
		$unitChar = chr(HexDec($unitByte)); // hex -> ascii
		$charValue .= $unitChar;
	}
	return $charValue;	
}
?>
<?php
///////////////////get e1 status
$e1selSql = "SELECT sysTypeNo,sysNo,subSysNo,detailStat,updateTime 
			FROM sysStat WHERE sysTypeNo='111' order by sysNo,subSysNo";
$e1Result = @mysqli_query($pubConn,$e1selSql);
while($e1Row = @mysqli_fetch_array($e1Result))
{
	$hex_mes = $e1Row[detailStat];
	$sysNo = $e1Row[sysNo];
	$subSysNo = $e1Row[subSysNo];
	
	$bin_mes='';
	for($i=0;$i<strlen($hex_mes);$i++)
	{
		$sig_hex_mes=substr($hex_mes,$i,1);
		$sig_dec_mes=HexDec($sig_hex_mes);
		$sig_bin_mes=DecBin($sig_dec_mes);
		$sig_bin_mes=omc_keep_length($sig_bin_mes,0,4);
		$bin_mes.=$sig_bin_mes;
	}
	
	$e1HeartBeatArr[$sysNo][$subSysNo] = $bin_mes;
}

$ins_bgn	=	22;	//e1 installed
$phy_bgn	=	24;	//e1 layer1(physical layer)
$el2_bgn	=   25; //e1 layer2
$E1state1=1;//OK
$E1state2=0;//DOWN	
for($i=0; $i< 256; $i++)
{
	$cardno = Floor($i/8);
	$sysNo = $cardno > 15 ? 1 : 0;
	$subSysNo = $cardno % 16;
	$port = $i - $cardno*8;
	//e1 install
	$bin_mes = $e1HeartBeatArr[$sysNo][$subSysNo];
	//if($DEBUG)echo "<br>$i: $bin_mes";
	$bin_ins=substr($bin_mes,$ins_bgn*8+7-$port,1);//0:'--'; 1:'Y'
	$tmp1 = bindec($bin_ins);
	
	//e1 layer1
	$bin_phy=substr($bin_mes,$phy_bgn*8+7-$port,1);//0:'Y'; 1:'N'; 2:'--'
	$tmp2 = bindec($bin_phy);
	$tmp2++;
	$tmp2 = ($tmp2>1) ? 0 : $tmp2;
	
	//e1 layer2 install
	$bin_inl=substr($bin_mes,$el2_bgn*8+7-$port,1);//0:'N'; 1:'Y';
	$tmp3 = bindec($bin_inl);
				
	$E1stateArr1[$i] = $E1state1 & $tmp2 & $tmp3;
	$E1stateArr2[$i] = $E1state2 | $tmp2 | $tmp3;
//	echo "<br>E1stateArr1[$i]:{$E1stateArr1[$i]},E1stateArr2[$i]:{$E1stateArr2[$i]}";
}

$tmpObjIdStr='2.4.2';
$levelWhere = getLevelWhere($tmpObjIdStr);
$tmpTgValue=strtoupper(omc_keep_length($tmpTgValue,0,2));
$getSql="SELECT initValue,instanceNo FROM OBJ_322.param_99 
			WHERE $levelWhere and initValue!='EE' order by instanceNo";				
$result	=mysqli_query($pubConn,$getSql);
while($Rows = mysqli_fetch_array($result))
{
	$tg = hexdec($Rows[initValue]);
	if(!isset($e1StatAtTgArr1[$tg])) $e1StatAtTgArr1[$tg]=1;
	if(!isset($e1StatAtTgArr2[$tg])) $e1StatAtTgArr2[$tg]=0;
	$e1StatAtTgArr1[$tg] &= $E1stateArr1[$Rows[instanceNo]];
	$e1StatAtTgArr2[$tg] |= $E1stateArr2[$Rows[instanceNo]];
//	echo "<br>$Rows[instanceNo]:{$E1stateArr1[$Rows[instanceNo]]};e1StatAtTgArr1[$tg]:{$e1StatAtTgArr1[$tg]},e1StatAtTgArr2[$tg]:{$e1StatAtTgArr2[$tg]}";
}

////////////////get lins status
$out_time=date("Y-m-d H:i:s",mktime(date("H"),date("i"),date("s")-30,date("m"),date("d"),date("Y")));
//E1 link status		
$HbparamSql = " SELECT detailStat FROM OMC_PUB.sysStat  
		WHERE sysTypeNo='200' AND sysNo='9' AND (subSysNo='0' or subSysNo='1') AND updateTime>'$out_time'
		";
$Hbresult = mysqli_query($pubConn,$HbparamSql);				
while($Hbvalue_mpt3Arr[]  = mysqli_fetch_array($Hbresult));//取mtp3 的heartbeat
if(sizeof($Hbvalue_mpt3Arr)==3)
	$isDualServ = 1;
//E1 link
$tmpObjIdStr='2.5.1';
$levelWhere = getLevelWhere($tmpObjIdStr);
$tmpValue=dechex($linkset);
$tmpValue=strtoupper(omc_keep_length($tmpValue,0,2));
$getSql="SELECT initValue,instanceNo FROM OBJ_222.param_99 
			WHERE $levelWhere order by instanceNo";				
$result	=mysqli_query($pubConn,$getSql);			
while($rows=mysqli_fetch_array($result))
{	
	$linkflag	= getBitValue($rows[initValue],'06.5-06.5');
	$ls	= hexdec(getBitValue($rows[initValue],'00.0-00.7'));
	if($linkflag)
	{
		if(!isset($e1StatAtLsArr1[$ls])) $e1StatAtLsArr1[$ls]=1;
		if(!isset($e1StatAtLsArr2[$ls])) $e1StatAtLsArr2[$ls]=0;	

		$Hboffset=39+Floor($rows[instanceNo]/4);
		$tmpValue=substr($Hbvalue_mpt3Arr[0][detailStat],$Hboffset*2,2);
		if($isDualServ && $rows[instanceNo]>=128)
			$tmpValue=substr($Hbvalue_mpt3Arr[1][detailStat],$Hboffset*2,2);
		$bitoff	=$rows[instanceNo]%4;
		$bitoff1=$bitoff*2;
		$bitoff2=$bitoff1+1;
		$bitoff='0.'.$bitoff1.'-0.'.$bitoff2;
//echo "<br>$bitoff";
		$tmp=bindec(getBitValue($tmpValue,$bitoff));		
		if($tmp>1)
			$tmp=0;	
		$e1StatAtLsArr1[$ls] &= $tmp;
		$e1StatAtLsArr2[$ls] |= $tmp;
//		echo "<br>$rows[instanceNo]: e1StatAtLsArr1[$ls]:{$e1StatAtLsArr1[$ls]},e1StatAtLsArr2[$ls]:{$e1StatAtLsArr2[$ls]}";
	}
}	

?>
<?php

{//element status
	$othersElementObjIdStr='2.11';
	$levelWhere = getLevelWhere($othersElementObjIdStr);
	if($DEBUG) echo "<BR>levelWhere=$levelWhere";
	$othersElementParamTable='OBJ_225.param_99';
	$getSql="SELECT $initValue,instanceNo FROM $othersElementParamTable 
				WHERE $levelWhere and initValue LIKE '01%'
			";
	if($DEBUG) echo "<BR>getSql=$getSql";
	$result	=mysqli_query($pubConn,$getSql);
	echo mysqli_error($pubConn);
	
	$typeOffset 	= '1.0-1.7';
	$sysNoOffset 	= '2.0-2.3';
	$subSysNoOffset = '2.4-2.7';
	$tgOffset 		= '3.0-3.7';
	$lsOffset 		= '4.0-4.7';
	$appTypeOffset 	= '5.0-5.7';
	$appGrpOffset 	= '6.0-6.7';
	$nameOffset 	= '14.0-23.7';
	$i=0;
	while($rows=mysqli_fetch_array($result))
	{
		if($DEBUG)echo "<BR>initValue=$rows[initValue]";
		$typeValue=getBitValue($rows[initValue],$typeOffset);
		if($typeValue==0)
			continue;
		$type = $elementsArr[$i][type]=hexdec($typeValue);
		$ls2tg = $elementsArr[$i][tg]=hexdec(getBitValue($rows[initValue],$tgOffset));
		$ls = $elementsArr[$i][ls]=hexdec(getBitValue($rows[initValue],$lsOffset));
		$elementsArr[$i][appType]=hexdec(getBitValue($rows[initValue],$appTypeOffset));
		$elementsArr[$i][name]=getBitValue($rows[initValue],$nameOffset);
		if($elementsArr[$i][name]==0)
			$elementsArr[$i][name]=$elementsArr[$i][tg];
		else
			$elementsArr[$i][name]=trim(toAscii($elementsArr[$i][name]));
		if($type==17)//isdn switch
		{
			$e1State1 = $e1StatAtTgArr1[$ls2tg];
			$e1State2 = $e1StatAtTgArr2[$ls2tg];
			if($e1State1==1&&$e1State2==1)
				$E1state='Available';
			elseif($e1State1==0&&$e1State2==1)
				$E1state='Alarm';
			else
				$E1state='Unavailable';
		}
		else if($type==18/* && $ls2tg==255*/)//only link, but no e1
		{
			$e1State1 = $e1StatAtLsArr1[$ls];
			$e1State2 = $e1StatAtLsArr2[$ls];
			if($e1State1==1&&$e1State2==1)
				$E1state='Available';
			elseif($e1State1==0&&$e1State2==1)
				$E1state='Alarm';
			else
				$E1state='Unavailable';
		}
		else
		{
			$e1State1 = $e1StatAtTgArr1[$ls2tg] & $e1StatAtLsArr1[$ls];
			$e1State2 = $e1StatAtTgArr2[$ls2tg] | $e1StatAtLsArr2[$ls];
			if($e1State1==1&&$e1State2==1)
				$E1state='Available';
			elseif($e1State1==0&&$e1State2==1)
				$E1state='Alarm';
			else
				$E1state='Unavailable';
		}
//		if($ls<=5)$E1state='Available';
		$elementsArr[$i][state] = $E1state;
		$i++;	
	}
}//end element status
//print_r($elementsArr);
for($i=0;$i<sizeof($elementsArr);$i++)
{
	switch($elementsArr[$i][type])
	{
		case '1'://BSS	
		{		
			$bssElementNo=sizeof($bssElementArr);
			$bssElementArr[$bssElementNo][state]=$elementsArr[$i][state];
			$bssElementArr[$bssElementNo][name]=$elementsArr[$i][name];
			break;
		}
		case '2'://AAS
		{ 	
			$aasElementNo=sizeof($aasElementArr);		
			$aasElementArr[$aasElementNo][state]=$elementsArr[$i][state];
			break;
		}
		case '3'://VMS 	
		{		
			$vmsElementNo=sizeof($vmsElementArr);	
			$vmsElementArr[$vmsElementNo][state]=$elementsArr[$i][state];
			break;
		}
		case '4'://PCR 	
		{	
			$pcrElementNo=sizeof($pcrElementArr);	
			$pcrElementArr[$pcrElementNo][state]=$elementsArr[$i][state];
			break;
		}
		case '5'://CNF 	多个cnf公用一个linkset，从SubsysNo中区分每个cnf 0001 0000 -> cnf-0-1; 1000 0000 -> cnf-0-4
		{		
			$cnfElementNo=sizeof($cnfElementArr);	
			$cnfElementArr[$cnfElementNo][state] = $elementsArr[$i][state];
			break;
		}
		case '16'://remote elements
		{
			$switchElementNo=sizeof($switchElementArr);
			$switchElementArr[$switchElementNo][state]=$elementsArr[$i][state];
			break;
		}
		case '17'://isdn switch
		{
			$switchElementNo=sizeof($switchElementArr);
			$switchElementArr[$switchElementNo][state]=$elementsArr[$i][state];
			break;
		}
		case '18'://application status
		{
			switch($elementsArr[$i][appType])
			{
				case 0 :
					$mscElementNo=sizeof($mscElementArr);		
					$mscElementArr[$mscElementNo][state]=$elementsArr[$i][state];
					break;
				case 1 :
					$hlrElementNo=sizeof($hlrElementArr);		
					$hlrElementArr[$hlrElementNo][state]=$elementsArr[$i][state];
					break;
				case 2 :
					$vlrElementNo=sizeof($vlrElementArr);		
					$vlrElementArr[$vlrElementNo][state]=$elementsArr[$i][state];
					break;
				case 3 :
					$mcElementNo=sizeof($mcElementArr);		
					$mcElementArr[$mcElementNo][state]=$elementsArr[$i][state];
					break;
				case 4 :
					$sgsnElementNo=sizeof($sgsnElementArr);		
					$sgsnElementArr[$sgsnElementNo][state]=$elementsArr[$i][state];
					break;
				case 5 :
					$newElementNo=sizeof($newElementArr);		
					$newElementArr[$newElementNo][state]=$elementsArr[$i][state];
					break;
			}
			break;
		}
		default:
			break;
	}
}

?>

<?php
{//output data
	for($i=0;$i<sizeof($localElementArr);$i++)
	{
		if($localElementArr[$i][existFlag]==1)
		{	
			echo "###";
			if($localElementArr[$i][state]>0)
				echo "Normal";
			else
				echo "Alarm";
		}	
	}	
	echo "end";

//To BSS
	if(sizeof($bssElementArr)>0)
	{
		foreach($bssElementArr as $key => $row)
		{
			$name[$key] = $row['name'];
		}
		array_multisort($name, SORT_ASC, $bssElementArr);
	}
	for($i=0;$i<sizeof($bssElementArr);$i++)
	{
		echo "###";
		echo $bssElementArr[$i][state];
	}	
	echo "end";

//To Periphreal
	for($i=0;$i<sizeof($aasElementArr);$i++)
	{	
		echo "###";	
		echo $aasElementArr[$i][state];
	}
	echo "end";
	
	for($i=0;$i<sizeof($vmsElementArr);$i++)
	{	
		echo "###";	
		echo $vmsElementArr[$i][state];
	}
	echo "end";
	
	for($i=0;$i<sizeof($pcrElementArr);$i++)
	{	
		echo "###";	
		echo $pcrElementArr[$i][state];
	}
	echo "end";
	
	for($i=0;$i<sizeof($cnfElementArr);$i++)
	{	
		echo "###";	
		echo $cnfElementArr[$i][state];
	}
	echo "end";
	
	//To Switch
	for($i=0;$i<sizeof($switchElementArr);$i++)
	{
		echo "###";
		echo $switchElementArr[$i][state];
	}
	echo "end";
	
	//To Application msc
	for($i=0;$i<sizeof($mscElementArr);$i++)
	{
		echo "###";
		echo $mscElementArr[$i][state];
	}
	echo "end";
	//To Application hlr
	for($i=0;$i<sizeof($hlrElementArr);$i++)
	{
		echo "###";
		echo $hlrElementArr[$i][state];
	}
	echo "end";
	//To Application vlr
	for($i=0;$i<sizeof($vlrElementArr);$i++)
	{
		echo "###";
		echo $vlrElementArr[$i][state];
	}
	echo "end";
	//To Application mc
	for($i=0;$i<sizeof($mcElementArr);$i++)
	{
		echo "###";
		echo $mcElementArr[$i][state];
	}
	echo "end";
	//To Application sgsn
	for($i=0;$i<sizeof($sgsnElementArr);$i++)
	{
		echo "###";
		echo $sgsnElementArr[$i][state];
	}
	echo "end";
	//To Application new
	for($i=0;$i<sizeof($newElementArr);$i++)
	{
		echo "###";
		echo $newElementArr[$i][state];
	}
	echo "end";
}//end output data
?>