<?php
/*********************************************************      
 文件说明:         
    功能说明:显示所有BTS的基本状态
		1.显示状态内容：显示一个bsc和连接到这个bsc上的Abis状态
		2.取状态的方法：从BssOmcDb的各个表取出各种参数
		3.定时发送get命令，从而更新各个对象的状态信息

    调用关系:调用:header.inc  
             被调用:     
    变量说明:

 作者:    
      
修改注释:      

NO.1      
姓名:    
时间:    
修改说明:
*********************************************************/
require("../../inc/header.inc");
$helpId = "1.2.6.2"; 
$DEBUG=0;

//==========================================================
//变量定义
//==========================================================
$bssDb	= "BssOmcDb";
$pubDb	="OMC_PUB";
$bssSysTypeNo	='500';
$bscSysTypeNo	='510';
$btsSysTypeNo	='520';//BTS

$redColor = "#FF0000";
$blueColor = "#0000FF";
$yellowColor = "#FFFF00";

if($default_language == 'chn'){
	$ClockOperState = array('初始化','自行运行','获取同步信号','延时','同步信号有效','获取成功','时钟已同步');
	$ClockReference = array('第一时钟参考','第二时钟参考','连接','无参考');
	$AvailState = array('测试中','故障','电源中断','离线','在线','相关模块未激活','不完全工作','未安装');
	$AvailStateColor = array($yellowColor,$redColor,$redColor,$redColor,$blueColor,$yellowColor,$yellowColor,$redColor);
	$UsageState = array('空闲','运行','忙');
	$AlarmLevel= array('无告警','一级告警','二级告警','三级告警','低级警告','系统信息');
	$AdminState=array('无效值','加锁','解锁');
	$OperState=array('无效值','不可用','可用');
}else{
	$ClockOperState = array('Init','Free run','Acquire SYNC','Holdover','Acquire qual','Acquire','Track');
	$ClockReference = array('Primary_ref','Second_ref','Connector','None_ref');
	$AvailState = array('In test','Failed','Power off','Offline','Online','Dependency','Degraded','Not installed');
	$AvailStateColor = array($yellowColor,$redColor,$redColor,$redColor,$blueColor,$yellowColor,$yellowColor,$redColor);
	$UsageState = array('Idle','Active','Busy');
	$AlarmLevel= array('no alarm1','Critical','Major','Minor','Warning','Information');
	$AdminState=array('unknown','Locked','Unlocked');
	$OperState=array('unknown','Disabled','Enabled');
}
/*
$btsInfo[$i]=array(
		'bscId'=>'',
		'btsId'=>'',
		'name'=>'',
		'status'=>'',
		'AbisStatus'=>'',
		'AbisSlot'=>'',
		'AbisPort'=>'',
		'AbisChannel'=>'',
		'Trx2_stat'=>'',
		'Trx4_stat'=>'',
		'Trx6_stat'=>'',
		"ClockOperStat"=>'',
		"ClockReference"=>'',
		"refreshTime"=>''
);
*/
//==========================================================
//输入变量
//==========================================================
//清除一个BTS的状态时，需传入的变量
if($DEBUG) echo "<br>actMode=$actMode";
if($DEBUG) echo "<br>sysTypeNo=$sysTypeNo";
if($DEBUG) echo "<br>sysNo=$sysNo";
if($DEBUG) echo "<br>subSysNo=$subSysNo";

//==========================================================
//数据处理
//==========================================================
//清除一个BTS的状态
if($actMode == 'clear'){
	$clrSql="DELETE FROM sysStat 
			 WHERE  sysTypeNo='$sysTypeNo' AND sysNo='$sysNo' AND subSysNo='($subSysNo-1)'
			";
	if($DEBUG) echo "<br>clrSql=$clrSql";
	mysqli_query($pubConn,$clrSql);
	echo mysqli_error($pubConn);
}

$listSql = "SELECT a.bssid,a.ip,b.Label as name,a.connected FROM BssOmcDb.OMCR_BSSTABLE as a, BssOmcDb.OMCR_BssFunc as b" .
           " WHERE a.ip!='' AND a.bssid=b.bssid ORDER BY bssid";
if($DEBUG) echo "<br>listSql = $listSql<br>";
$listResult = mysqli_query($pubConn,$listSql);
$i=0;
while($listRows=@mysqli_fetch_array($listResult)){
	//get installed bsc id, alias,updateTime form sysInfo table
	$updateTimeSql = "SELECT updateTime	FROM sysStat
						WHERE sysTypeNo='$bscSysTypeNo'
						AND	sysNo='$listRows[bssid]'
					";
	if($DEBUG) 
	echo "<br>updateTimeSql = $updateTimeSql";
	$result = mysqli_query($pubConn,$updateTimeSql);
	echo mysqli_error($pubConn);
	//if(mysqli_num_rows($result) <= 0){
	//	echo "<br>$strNotFoundAnyBSC";
	//	exit();
	//}
	
	$rows = mysqli_fetch_array($result);
	$bscInfo[$i][bscId]=$listRows[bssid];
	$bscInfo[$i][refreshTime]=$rows[updateTime];
	if($DEBUG) echo "<br>===================";
	if($DEBUG) echo "<br>bscInfo[$i][bscId]={$bscInfo[$i][bscId]}";
	if($DEBUG) echo "<br>bscInfo[$i][refreshTime]={$bscInfo[$i][refreshTime]}";
	$i++;
}
$bscNum = $i;
if($DEBUG) 
echo "<br>bscNum=$bscNum";

$j = 0;

/*
//get installed BTS's
for($i=0;$i<$bscNum;$i++){
	
	$bscId=$bscInfo[$i][bscId];
	
	//Bts refreshTime
	$Bts_sql="SELECT subSysNo,updateTime
				FROM sysStat
				WHERE sysTypeNo='$btsSysTypeNo'	AND sysNo='$bscId'
			";
	if($DEBUG) echo "<br><br>Bts_sql = $Bts_sql";
	$result = mysqli_query($pubConn,$Bts_sql);
	echo mysqli_error($pubConn);
	$k=0;
	while($rows = mysqli_fetch_array($result)){
		$refreshTimeArr[$k][btsId]=$rows[subSysNo] + 1;
		$refreshTimeArr[$k][refreshTime]=$rows[updateTime];
		$k++;
	}

	//Bts status   --> Bts.$btsId.0
	$ObjectId = "Bts.".$btsId.".0";
	//echo "<br>ObjectId=$ObjectId";
	$Bts_sql="SELECT ObjectId
				FROM OMCR_BSSTREE 
				WHERE bssid = '$bscId' AND ObjectId like 'Bts.%.0' AND Presence > '0'
			";
	//if($DEBUG) 
	echo "<br><br>Bts_sql = $Bts_sql";
	$result = mysqli_query($pubConn,$Bts_sql);
	echo mysqli_error($pubConn);
	while($rows = mysqli_fetch_array($result)){
		$btsInfo[$j][bscId]=$bscId;
		$tmpObjArr=explode('.',$rows[ObjectId]);
		$tmpBtsId=$tmpObjArr[1];
		$btsInfo[$j][btsId]=$tmpBtsId;
		for($k=0;$k<sizeof($refreshTimeArr);$k++){
			if($refreshTimeArr[$k][btsId] == $tmpBtsId){
				$btsInfo[$j][refreshTime]=$refreshTimeArr[$k][refreshTime];
				break;
			}
			if($k == (sizeof($refreshTimeArr) - 1)){
				$btsInfo[$j][refreshTime]=$bscInfo[$i][refreshTime];
			}
		}
		if($DEBUG) echo "<br>===================";
		if($DEBUG) echo "<br>btsInfo[$j][bscId]={$btsInfo[$j][bscId]}";
		if($DEBUG) echo "<br>btsInfo[$j][btsId]={$btsInfo[$j][btsId]}";
		if($DEBUG) echo "<br>btsInfo[$j][refreshTime]={$btsInfo[$j][refreshTime]}";
		$j++;
	}
}
*/

//get installed BTS's
$j=0;
for($i=0;$i<$bscNum;$i++){	
	$bscId=$bscInfo[$i][bscId];
	
	$Bts_sql="SELECT subSysNo,updateTime
				FROM sysStat
				WHERE sysTypeNo='$btsSysTypeNo'	AND sysNo='$bscId'
			";
	if($DEBUG) echo "<br><br>Bts_sql = $Bts_sql";
	$result = mysqli_query($pubConn,$Bts_sql);
	echo mysqli_error($pubConn);
	while($rows = mysqli_fetch_array($result)){
		$btsInfo[$j][bscId]=$bscId;
		$btsInfo[$j][btsId]=$rows[subSysNo];
		$btsInfo[$j][refreshTime]=$rows[updateTime];
		if($DEBUG) echo "<br>===================";
		if($DEBUG) echo "<br>btsInfo[$j][bscId]={$btsInfo[$j][bscId]}";
		if($DEBUG) echo "<br>btsInfo[$j][btsId]={$btsInfo[$j][btsId]}";
		if($DEBUG) echo "<br>btsInfo[$j][refreshTime]={$btsInfo[$j][refreshTime]}";
		$j++;
	}
}


$btsNum=$j;
if($DEBUG) echo "<br>btsNum=$btsNum";
for($j=0;$j<$btsNum;$j++){
	$bscId=$btsInfo[$j][bscId];
	$btsId=$btsInfo[$j][btsId];

	//Bts status   --> Bts.$btsId.0
	$ObjectId = "Bts.".$btsId.".0";
	$Bts_sql="SELECT  t1.AdminState,t1.OperState,t1.AvailStatus,t1.UsageState,t2.Presence,t1.Label 
				FROM OMCR_Bts as t1,OMCR_BSSTREE as t2
				WHERE t1.bssid = '$bscId' AND t2.bssid = '$bscId' 
				AND t1.ObjectId = '$ObjectId' AND t2.ObjectId = '$ObjectId'
			";
	if($DEBUG) 
	echo "<br><br>Bts_sql = $Bts_sql";
	$result = mysqli_query($pubConn,$Bts_sql);
	echo mysqli_error($pubConn);
	$rows = mysqli_fetch_array($result);
	$btsInfo[$j][status]	= $AvailState[$rows[AvailStatus]];
	$btsInfo[$j][statusColor] = $AvailStateColor[$rows[AvailStatus]];
	$btsInfo[$j][name]		= $rows[Label];

	//clock module->BtsMgr_Shelf_Card.$btsId.0.10
	$ObjectId = "BtsMgr_Shelf_Card.".$btsId.".0.17";
	$clock_module_sql =  "SELECT t1.CurrentOperationalState,t1.CurrentClockReference,t2.Presence
				FROM OMCR_ClockMod as t1,OMCR_BSSTREE as t2
				WHERE t1.bssid = '$bscId' AND t2.bssid = '$bscId' 
					AND t1.ObjectId = '$ObjectId' AND t1.ObjectId = t2.ObjectId
				 ";
	if($DEBUG) echo "<br><br>clock_module_sql = $clock_module_sql";
	$result = mysqli_query($pubConn,$clock_module_sql);
	echo mysqli_error($pubConn);
	if(mysqli_num_rows($result) > 0){
	$rows = mysqli_fetch_array($result);
		$btsInfo[$j][ClockOperStat]	= $ClockOperState[$rows[CurrentOperationalState]];
		$btsInfo[$j][ClockReference]= $ClockReference[$rows[CurrentClockReference]];
	}else{
		$btsInfo[$j][ClockOperStat]	= '--';
		$btsInfo[$j][ClockReference]= '--';
	}
	//get Abis status -> AbisMgr.$btsId.0
	$ObjectId = "AbisMgr.".$btsId.".0"; 
	$Abis_sql="SELECT t1.AvailStatus,t1.UsageState,t1.e1Slot,t1.e1Port,t1.e1Chan,t2.Presence
			FROM OMCR_AbisMgr as t1,OMCR_BSSTREE as t2
			WHERE t1.bssid = '$bscId' AND t2.bssid = '$bscId' 
				AND t1.ObjectId = '$ObjectId' AND t1.ObjectId = t2.ObjectId  
		  ";
	if($DEBUG) echo "<br><br>Abis_sql = $Abis_sql";
	$result = mysqli_query($pubConn,$Abis_sql);
	echo mysqli_error($pubConn);
	$rows = mysqli_fetch_array($result);
	if($rows[Presence] > 0){
		$btsInfo[$j][AbisStatus]	= $AvailState[$rows[AvailStatus]];
		$btsInfo[$j][AbisStatusColor] = $AvailStateColor[$rows[AvailStatus]];
		$btsInfo[$j][AbisSlot]		= $rows[e1Slot];
		$btsInfo[$j][AbisPort]		= $rows[e1Port];
		$btsInfo[$j][AbisChannel]	= $rows[e1Chan];
	}else{
		$btsInfo[$j][AbisStatus]	= '--';
		$btsInfo[$j][AbisSlot]		= '--';
		$btsInfo[$j][AbisPort]		= '--';
		$btsInfo[$j][AbisChannel]	= '--';
	}
	//TRX status   --> Trx.bts_id.0.n
	$ObjectId = "Trx.".$btsId.".0.%"; 
	$Trx_sql="SELECT t1.ObjectId,t1.AvailStatus,t1.UsageState,t2.Presence
			FROM OMCR_Trx as t1,OMCR_BSSTREE as t2
			WHERE t1.bssid = '$bscId' AND t2.bssid = '$bscId' 
				AND t1.ObjectId like '$ObjectId' AND t1.ObjectId = t2.ObjectId
		  ";
	if($DEBUG) echo "<br><br>Trx_sql = $Trx_sql";
	$result = mysqli_query($pubConn,$Trx_sql);
	echo mysqli_error($pubConn);
	while($rows = mysqli_fetch_array($result)){
		$tmpStr=explode('.',$rows[ObjectId]);
		$p=$tmpStr[3] - 0;
		if($rows[Presence] > 0){
			$btsInfo[$j]["Trx".$p."_stat"]=$AvailState[$rows[AvailStatus]];
		}else{
			$btsInfo[$j]["Trx".$p."_stat"]='--';
		}
	}

	if($DEBUG) echo "<br>==============";
	if($DEBUG) echo "<br>btsInfo[$j][bscId]		={$btsInfo[$j][bscId]}";
	if($DEBUG) echo "<br>btsInfo[$j][btsId]		={$btsInfo[$j][btsId]}";
	if($DEBUG) echo "<br>btsInfo[$j][name]		={$btsInfo[$j][name]}";
	if($DEBUG) echo "<br>btsInfo[$j][status]	={$btsInfo[$j][status]}";
	if($DEBUG) echo "<br>btsInfo[$j][AbisStatus]={$btsInfo[$j][AbisStatus]}";
	if($DEBUG) echo "<br>btsInfo[$j][AbisSlot]	={$btsInfo[$j][AbisSlot]}";
	if($DEBUG) echo "<br>btsInfo[$j][AbisPort]	={$btsInfo[$j][AbisPort]}";
	if($DEBUG) echo "<br>btsInfo[$j][AbisChannel]={$btsInfo[$j][AbisChannel]}";
	if($DEBUG) echo "<br>btsInfo[$j][Trx2_stat]	={$btsInfo[$j][Trx2_stat]}";
	if($DEBUG) echo "<br>btsInfo[$j][Trx4_stat]	={$btsInfo[$j][Trx4_stat]}";
	if($DEBUG) echo "<br>btsInfo[$j][Trx6_stat]	={$btsInfo[$j][Trx6_stat]}";
	if($DEBUG) echo "<br>btsInfo[$j][ClockOperStat]	={$btsInfo[$j][ClockOperStat]}";
	if($DEBUG) echo "<br>btsInfo[$j][ClockReference]={$btsInfo[$j][ClockReference]}";
	if($DEBUG) echo "<br>btsInfo[$j][refreshTime]	={$btsInfo[$j][refreshTime]}";
}



//==========================================================
//输出并显示数据
//==========================================================

?>
<BODY topmargin="5" leftmargin="12">
<?php
echo "<table boder=\"0\" width=\"100%\">";
echo "<tr>";
echo "<td width=\"50%\">";
echo "$strSysStatus > BTS";
echo "</td>";
echo "<td width=\"50%\" align=\"right\" title=\"Help\">";
showHelp($helpId);
echo "</td>";
echo "</tr>";
echo "</table>";
?>
<table border="0" width="100%">        
<tr><td align="center">
<font size="2" color="#0000ff"><?php echo "BTS $strStatusInformation"; ?></font>
</td></tr>
</table>        
        
<table border="1" width="100%" cellpadding="1" cellspacing="0" bordercolor="#666666" bordercolordark="#FFFFFF" bgcolor="#FFFFFF">            
<tr bgcolor="#E6E6E6">
<td rowspan="2"><?php echo "$strName"; ?></td>
<td align="center" colspan="2">ID</td>
<td align="center" colspan="4">ABIS</td>
<td align="center" colspan="1">BTS</td>
<td align="center" colspan="2"><?php echo "$strClock"; ?></td>
<td align="center" rowspan="2"><?php echo "$strRefreshTime"; ?></td>
</tr>              
        
<tr bgcolor="#E6E6E6"> 
<td>BSC</td>
<td>BTS</td>          
<td><?php echo "$strStatus"; ?></td>
<td><?php echo "$strSlot"; ?></td>
<td><?php echo "$strPort"; ?></td>
<td><?php echo "$strChannel"; ?></td>
<td><?php echo "$strStatus"; ?></td>
<!--<td>TRX2</td>
<td>TRX4</td>
<td>TRX6</td>-->
<td><?php echo "$strOperationMode"; ?></td>
<td><?php echo "$strReference"; ?></td> 
</tr>            
<?php
for($j=0;$j<$btsNum;$j++){
	echo "<tr>";
	$tmpBtsName=urldecode($btsInfo[$j][name]);
	echo "<td>&nbsp;$tmpBtsName</td>";
	echo "<td>{$btsInfo[$j][bscId]}</td>";
	echo "<td>{$btsInfo[$j][btsId]}</td>";
	echo "<td><font color=\"{$btsInfo[$j][AbisStatusColor]}\">&nbsp;{$btsInfo[$j][AbisStatus]}</font></td>";
	echo "<td><font color=\"#0000FF\">&nbsp;{$btsInfo[$j][AbisSlot]}</font></td>";
	echo "<td><font color=\"#0000FF\">&nbsp;{$btsInfo[$j][AbisPort]}</font></td>";
	echo "<td><font color=\"#0000FF\">&nbsp;{$btsInfo[$j][AbisChannel]}</font></td>";
	echo "<td><font color=\"{$btsInfo[$j][statusColor]}\">&nbsp;{$btsInfo[$j][status]}</font></td>";
	/*
	echo "<td><font color=\"#0000FF\">&nbsp;{$btsInfo[$j][Trx2_stat]}</font></td>";
	echo "<td><font color=\"#0000FF\">&nbsp;{$btsInfo[$j][Trx4_stat]}</font></td>";
	echo "<td><font color=\"#0000FF\">&nbsp;{$btsInfo[$j][Trx6_stat]}</font></td>";
	*/
	echo "<td><font color=\"#0000FF\">&nbsp;{$btsInfo[$j][ClockOperStat]}</font></td>";
	echo "<td><font color=\"#0000FF\">&nbsp;{$btsInfo[$j][ClockReference]}</font></td>";
	echo "<td><font color=\"#0000FF\">&nbsp;{$btsInfo[$j][refreshTime]}</font></td>";
	echo "</tr>";
}
?> 
<!--
<tr>          
<td>00_00</td>              
<td>NY BTS1</td>
<td><font color="#0000FF">Online</font></td>
<td><font color="#0000FF">1</font></td>
<td><font color="#0000FF">1</font></td>
<td><font color="#0000FF">16</font></td>
<td><font color="#FF0000">Fail</font></td>
<td><font color="#0000FF">--</font></td>
<td><font color="#0000FF">--</font></td>
<td><font color="#0000FF">OK</font></td>
<td><font color="#0000FF">Free Run</font></td>
<td><font color="#0000FF">--</font></td>
<td><font color="#FF0000">2003-01-23 16:39:04</font></td>
</tr>           
-->
</table>

<table border="0" width="100%" cellpadding="0" cellspacing="1">
 <tr>
   <td>
		<?php echo $strRemark; ?>: &nbsp; &nbsp;
		<font color=#0000FF>Y</font> = <?php echo $strNormal; ?>, &nbsp; &nbsp; 
		<font color=#FF0000>N</font> = <?php echo $strAbnormal; ?>,&nbsp; &nbsp;
		-- = <?php echo $strUninstalled; ?>.
   </td>
   <td align="right">
    <?php
		echo "<a href=\"../networkStatus/mapStatus.php?showingPageType=$src_page_type&pageNum=$src_page_num\" target=\"contents\">";
		
		echo "<img align=absBottom border=0 src=\"../../images/left.gif\" width=\"14\" height=\"14\">";      
		echo "$strBack</a>"; 
	?>
   </td>
 </tr>
</table>

<?php
//==========================================================
//发送NMI命令，读出数据，更新数据库
//==========================================================
for($j=0;$j<$btsNum;$j++){
	
	$btsId=$btsInfo[$j][btsId];

	//nmi命令定义数组，在显示信息之后，需要发送命令来更新数据库
	$BtsOid = 'Bts.'.$btsId.'.0';
	$ClockModuleOid = 'BtsMgr_Shelf_Card.'.$btsId.'.0.10';
	$AbisOid = 'AbisMgr.'.$btsId.'.0'; 
	$Trx2Oid = 'Trx.'.$btsId.'.0.2'; 
	$Trx4Oid = 'Trx.'.$btsId.'.0.4'; 
	$Trx6Oid = 'Trx.'.$btsId.'.0.6'; 

	/*
	$nmiCommArr = array(
			array('oid '.$BtsOid,'get','0'),
			array('oid '.$ClockModuleOid,'get','0'),
			array('oid '.$AbisOid,'get','0'),
			array('oid '.$Trx2Oid,'get','0'),
			array('oid '.$Trx4Oid,'get','0'),
			array('oid '.$Trx6Oid,'get','0')
			);
	*/
	
	$nmiCommArr = array(
			array('oid '.$BtsOid,'get','0'),
			array('oid '.$ClockModuleOid,'get','0'),
			array('oid '.$AbisOid,'get','0')
			);
	for($i=0;$i<sizeof($nmiCommArr);$i++){
		$nmi_command = $nmiCommArr[$i][0];
		$Oid_sql = "REPLACE INTO OMCR_COMMAND
				VALUES('$bgn_row_num','{$btsInfo[$j][bscId]}','$nmi_command',null,'255','-1',null,null,null)";
		if($DEBUG) 
		echo "<br>set Oid_sql = $Oid_sql";
		$result = mysqli_query($pubConn,$Oid_sql);
		echo mysqli_error($pubConn);
		$bgn_row_num++;
		$nmi_command = $nmiCommArr[$i][1];
		$Command_sql = "REPLACE INTO OMCR_COMMAND
				VALUES('$bgn_row_num','{$btsInfo[$j][bscId]}','$nmi_command',null,'255','-1',null,null,null)";
		if($DEBUG) 
		echo "<br>Commnad_sql = $Command_sql";
		$result = mysqli_query($pubConn,$Command_sql);
		echo mysqli_error($pubConn);
		$bgn_row_num++;
	}
}
?>
</body>
</html>   
  