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

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

 作者:      
      
修改注释:      

NO.1      
姓名:    
时间:    
修改说明:
*********************************************************/
require("../../inc/header.inc");
$DEBUG=0;
global $pubConn,$OMC_server;
//==========================================================
//变量定义
//==========================================================
$bssDb = "BssOmcDb";
$bssConn = mysqli_connect($OMC_server[0]['host'],$OMC_server[0]['user'],$OMC_server[0]['password'], "BssOmcDb");

$pubDb = "OMC_PUB";
//$bscSysTypeNo=omc_sys_type2code(BSC);
$bscSysTypeNo='510';
$bssSysTypeNo='500';

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

//确定显示语言种类
if($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');
}

//nmi命令定义数组，在显示信息之后，需要发送命令来更新数据库
$nmiCommArr = array(
		array('oid Bsc.0','get','0'),
		array('oid Card.0.17','get','0'),
		array('oid ALink.0.0.0','get','0'),
		array('oid ALink.0.0.1','get','0'),
		array('oid ALink.0.0.2','get','0'),
		array('oid ALink.0.0.3','get','0')
		);

//根据需要显示的信息定义结构体，方便信息的存放和显示
/*
$bscInfo[i]=array(
		"bssId"=>'',
		"alias"=>'',
		"status"=>'',
		"ALink_0_stat"=>'',
		"ALink_0_slot"=>'',
		"ALink_0_port"=>'',
		"ALink_1_stat"=>'',
		"ALink_1_slot"=>'',
		"ALink_1_port"=>'',
		"ALink_2_stat"=>'',
		"ALink_2_slot"=>'',
		"ALink_2_port"=>'',
		"ALink_3_stat"=>'',
		"ALink_3_slot"=>'',
		"ALink_3_port"=>'',
		"ClockOperStat"=>'',
		"ClockReference"=>'',
		"refreshTime"=>''
	);
*/
//==========================================================
//输入变量
//==========================================================
//清除一个BSC的状态时，需传入的变量
if($DEBUG) echo "<br>actMode=$actMode";
if($DEBUG) echo "<br>sysTypeNo=$bscSysTypeNo";
if($DEBUG) echo "<br>sysNo=$sysNo";
if($DEBUG) echo "<br>subSysNo=$subSysNo";

//==========================================================
//数据处理
//==========================================================
//清除一个BSC的状态
if($actMode == 'clear'){
	$clrSql="DELETE FROM sysStat 
			 WHERE  sysTypeNo='$bscSysTypeNo' AND sysNo='$sysNo' AND subSysNo='$subSysNo'
			";
	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($bssConn,$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][alias]=$listRows[name];
	$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][alias]={$bscInfo[$i][alias]}";
	if($DEBUG) echo "<br>bscInfo[$i][refreshTime]={$bscInfo[$i][refreshTime]}";
	$i++;
}
$bscNum = $i;
//echo "<br>bscNum=$bscNum";
//get system alias
//$sysAlias=

for($i=0; $i<$bscNum; $i++){
	//Bsc status   --> Bsc.bsc_id
	$ObjectId = 'Bsc.0';
	$Bsc_sql="SELECT t1.AdminState,t1.OperState,t1.AvailStatus,t1.UsageState,t2.Presence
			FROM OMCR_Bsc as t1,OMCR_BSSTREE as t2
			WHERE t1.bssid = '{$bscInfo[$i][bscId]}' 
				AND t2.bssid = '{$bscInfo[$i][bscId]}' 
				AND t1.ObjectId = '$ObjectId' 
				AND t1.ObjectId = t2.ObjectId
		  ";
	if($DEBUG) echo "<br><br>Bsc_sql = $Bsc_sql";
	$result = mysqli_query($bssConn,$Bsc_sql);
	echo mysqli_error($bssConn);
	$rows = mysqli_fetch_array($result);
	$bscInfo[$i][bscStatus]= $AvailState[$rows[AvailStatus]];
	$bscInfo[$i][bscStatusColor]= $AvailStateColor[$rows[AvailStatus]];

	//clock module->Card.0.10
	$ObjectId = 'Card.0.17';
	$clock_module_sql =  "SELECT t1.CurrentOperationalState,t1.CurrentClockReference,t2.Presence
				FROM OMCR_ClockMod as t1,OMCR_BSSTREE as t2
				WHERE t1.bssid = '{$bscInfo[$i][bscId]}' 
					AND t2.bssid = '{$bscInfo[$i][bscId]}' 
					AND t1.ObjectId = '$ObjectId' 
					AND t1.ObjectId = t2.ObjectId  
				ORDER BY t1.ObjectId
				 ";
	if($DEBUG) echo "<br><br>clock_module_sql = $clock_module_sql";
	$result = mysqli_query($bssConn,$clock_module_sql);
	echo mysqli_error($bssConn);
	$rows = mysqli_fetch_array($result);
	$bscInfo[$i][ClockOperStat]=$ClockOperState[$rows[CurrentOperationalState]];
	$bscInfo[$i][ClockReference]=$ClockReference[$rows[CurrentClockReference]];
	
	//get ALink status -> ALink.Bsc_id.0.n 
	$ObjectId = 'ALink.0.0.%';
	$ALink_sql = " SELECT t1.AvailStatus,t1.UsageState,t1.e1Slots,t1.e1Framer,t1.e1Channel,t2.Presence
			FROM OMCR_ALink as t1,OMCR_BSSTREE as t2
			WHERE t1.bssid = '{$bscInfo[$i][bscId]}' 
				AND t2.bssid = '{$bscInfo[$i][bscId]}' 
				AND t1.ObjectId = t2.ObjectId
			 ";
	if($DEBUG) echo "<br>ALink_sql = $ALink_sql";
	$result = mysqli_query($bssConn,$ALink_sql);
	echo mysqli_error($bssConn);
	$AlinkNum=mysqli_num_rows($result);
	for($k=0;$k<$AlinkNum;$k++){
		$rows = mysqli_fetch_array($result);
		if($rows[Presence] > 0){
			$bscInfo[$i]["ALink_".$k."_stat"]=$AvailState[$rows[AvailStatus]];
			$bscInfo[$i]["ALink_".$k."_statColor"]=$AvailStateColor[$rows[AvailStatus]];
			$bscInfo[$i]["ALink_".$k."_slot"]=$rows[e1Slots];
			$bscInfo[$i]["ALink_".$k."_port"]=$rows[e1Framer];
		}else{
			$bscInfo[$i]["ALink_".$k."_stat"]='--';
			$bscInfo[$i]["ALink_".$k."_slot"]='--';
			$bscInfo[$i]["ALink_".$k."_port"]='--';		
		}
	}

	if($DEBUG) echo "<br>===================";
	if($DEBUG) echo "<br>bscInfo[$i][bscId]={$bscInfo[$i][bscId]}";
	if($DEBUG) echo "<br>bscInfo[$i][alias]={$bscInfo[$i][alias]}";
	if($DEBUG) echo "<br>bscInfo[$i][refreshTime]={$bscInfo[$i][refreshTime]}";
	if($DEBUG) echo "<br>bscInfo[$i][bscStatus]= {$bscInfo[$i][bscStatus]}";
	if($DEBUG) echo "<br>bscInfo[$i][ClockOperStat]={$bscInfo[$i][ClockOperStat]}";
	if($DEBUG) echo "<br>bscInfo[$i][ClockReference]={$bscInfo[$i][ClockReference]}";
	if($DEBUG) echo "<br>bscInfo[$i][ALink_0_stat]={$bscInfo[$i][ALink_0_stat]}";
	if($DEBUG) echo "<br>bscInfo[$i][ALink_0_slot]={$bscInfo[$i][ALink_0_slot]}";
	if($DEBUG) echo "<br>bscInfo[$i][ALink_0_port]={$bscInfo[$i][ALink_0_port]}";
	
	if($DEBUG) echo "<br>bscInfo[$i][ALink_1_stat]={$bscInfo[$i][ALink_1_stat]}";
	if($DEBUG) echo "<br>bscInfo[$i][ALink_1_slot]={$bscInfo[$i][ALink_1_slot]}";
	if($DEBUG) echo "<br>bscInfo[$i][ALink_1_port]={$bscInfo[$i][ALink_1_port]}";
	
	if($DEBUG) echo "<br>bscInfo[$i][ALink_2_stat]={$bscInfo[$i][ALink_2_stat]}";
	if($DEBUG) echo "<br>bscInfo[$i][ALink_2_slot]={$bscInfo[$i][ALink_2_slot]}";
	if($DEBUG) echo "<br>bscInfo[$i][ALink_2_port]={$bscInfo[$i][ALink_2_port]}";
	
	if($DEBUG) echo "<br>bscInfo[$i][ALink_3_stat]={$bscInfo[$i][ALink_3_stat]}";
	if($DEBUG) echo "<br>bscInfo[$i][ALink_3_slot]={$bscInfo[$i][ALink_3_slot]}";
	if($DEBUG) echo "<br>bscInfo[$i][ALink_3_port]={$bscInfo[$i][ALink_3_port]}";
}


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

?>
<meta http-equiv="refresh" content="20 url=<?php echo "$PHP_SELF?sysId=$sysId";?>">
<BODY topmargin="5" leftmargin="12">
<?php
echo "<table boder=\"0\" width=\"100%\">";
echo "<tr>";
echo "<td width=\"50%\">";
echo "$strSysStatus > $strCentralOffice > BSC";
echo "</td>";
echo "<td width=\"50%\" align=\"right\" title=\"Help\">";
$helpId="1.2.6.1";
showHelp($helpId);
echo "</td>";
echo "</tr>";
echo "</table>";
?>
<form method="POST" name=status action="<?php echo "$PHP_SELF";?>" >   
<table border="0" width="100%">        
<tr><td align="center">
<font size="2" color="#0000ff"><?php echo "BSC $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  rowspan="2" >ID</td>             
<td  colspan="3" >ALINK 0</td>
<td  colspan="3" >ALINK 1</td>
<td  colspan="3" >ALINK 2</td>
<td  colspan="3" >ALINK 3</td>
<td  rowspan="2"><?php $tempStatus=strtolower($strStatus); echo "BSC $tempStatus"; ?></td>
<td  colspan="2" ><?php echo "$strClock"; ?></td>
<td  rowspan="2" ><?php echo "$strRefreshTime"; ?></td>  
</tr>                
          
<tr bgcolor="#E6E6E6">                        
<td><?php echo "$strStatus"; ?></td>
<td><?php echo "$strSlot"; ?></td>
<td><?php echo "$strPort"; ?></td>
<td><?php echo "$strStatus"; ?></td>
<td><?php echo "$strSlot"; ?></td>
<td><?php echo "$strPort"; ?></td>
<td><?php echo "$strStatus"; ?></td>
<td><?php echo "$strSlot"; ?></td>
<td><?php echo "$strPort"; ?></td>
<td><?php echo "$strStatus"; ?></td>
<td><?php echo "$strSlot"; ?></td>
<td><?php echo "$strPort"; ?></td>
<td><?php echo "$strOperationMode"; ?></td>
<td><?php echo "$strReference"; ?></td> 
</tr>               
<?php
for($i=0; $i<$bscNum; $i++){       
	echo "<tr>";
	$tmpBscName=urldecode($bscInfo[$i][alias]);
	echo "<td>&nbsp;$tmpBscName</td>";
	echo "<td>{$bscInfo[$i][bscId]}</td>";
	echo "<td><font color=\"{$bscInfo[$i][ALink_0_statColor]}\">{$bscInfo[$i][ALink_0_stat]}</font></td>";
	echo "<td><font color=\"#0000FF\">{$bscInfo[$i][ALink_0_slot]}</font></td>";
	echo "<td><font color=\"#0000FF\">{$bscInfo[$i][ALink_0_port]}</font></td>";
	echo "<td><font color=\"{$bscInfo[$i][ALink_1_statColor]}\">{$bscInfo[$i][ALink_1_stat]}</font></td>";
	echo "<td><font color=\"#0000FF\">{$bscInfo[$i][ALink_1_slot]}</font></td>";
	echo "<td><font color=\"#0000FF\">{$bscInfo[$i][ALink_1_port]}</font></td>";
	echo "<td><font color=\"{$bscInfo[$i][ALink_2_statColor]}\">{$bscInfo[$i][ALink_2_stat]}</font></td>";
	echo "<td><font color=\"#0000FF\">{$bscInfo[$i][ALink_2_slot]}</font></td>";
	echo "<td><font color=\"#0000FF\">{$bscInfo[$i][ALink_2_port]}</font></td>";
	echo "<td><font color=\"{$bscInfo[$i][ALink_3_statColor]}\">{$bscInfo[$i][ALink_3_stat]}</font></td>";
	echo "<td><font color=\"#0000FF\">{$bscInfo[$i][ALink_3_slot]}</font></td>";
	echo "<td><font color=\"#0000FF\">{$bscInfo[$i][ALink_3_port]}</font></td>";
	echo "<td><font color=\"{$bscInfo[$i][bscStatusColor]}\">{$bscInfo[$i][bscStatus]}</font></td>";
	echo "<td><font color=\"#0000FF\">{$bscInfo[$i][ClockOperStat]}</font></td>";
	echo "<td><font color=\"#0000FF\">{$bscInfo[$i][ClockReference]}</font></td>";
	echo "<td><font color=\"#0000FF\">{$bscInfo[$i][refreshTime]}</font></td>";
	echo "</tr>";
}
?>
</table>
</form>
<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

//----------------------------------Alarm list-----------------------------------------
//get alarm from OMCR_BSSTREE
$Alarm_sql = "SELECT  bssid,ObjectId,Severity,EventInfo,EventTime 
		FROM OMCR_ALARMEVENT 
		WHERE EventTime > 0 AND Severity > 0 
	     ";
if($DEBUG) echo "<br>Alarm_sql = $Alarm_sql";
$result = mysqli_query($bssConn,$Alarm_sql);
echo mysqli_error($bssConn);
if(mysqli_num_rows($result)){
	echo "<br><font color=\"blue\">BSS Alarm List</font>";
	echo "<table border='1' width='100%' cellpadding='1' cellspacing='0' bordercolor='#666666' bordercolordark='#FFFFFF' bgcolor='#FFFFFF'>";     
	echo " <tr bgcolor='E6E6E6' align='center'>";  
	echo "<td>BSS ID</td>"; 
	echo "<td>Object name</td>";   
	echo "<td>Alarm level</td>";       
	echo "<td>Alarm info</td>"; 
	echo "<td>Alarm Time</td>";
	echo "</tr> ";

	while($rows = mysqli_fetch_array($result)){
		echo "<tr align=\"center\">";
		echo "<td align=\"left\">$rows[bssid]</td>";
		echo "<td align=\"left\">$rows[ObjectId]</font></td>";
		$tmpLevel=$rows[Severity];
		echo "<td>$AlarmLevel[$tmpLevel]</td>";
		echo "<td align=\"left\">$rows[EventInfo]</td>";
		echo "<td>$rows[EventTime]</td>";
		echo "</tr>";
	}
	echo "</table>";
}

//==========================================================
//发送NMI命令，读出数据，更新数据库
//==========================================================
$getCommNoSql = "SELECT row_no FROM OMCR_COMMAND WHERE status=0 ORDER BY seqNum ASC LIMIT 0,1";
if($DEBUG) 
echo "<br>getCommNoSql=$getCommNoSql";
$result = mysqli_query($bssConn,$getCommNoSql);
$rows= mysqli_fetch_array($result);
$bgn_row_num = $rows['row_no'];

for($j=0;$j<$bscNum;$j++){
	for($i=0;$i<sizeof($nmiCommArr);$i++){
		$nmi_command = $nmiCommArr[$i][0];
		$Oid_sql = "REPLACE INTO OMCR_COMMAND
				VALUES('$bgn_row_num','{$bscInfo[$j][bscId]}','$nmi_command',null,'255','-1',null,null,null)";
		if($DEBUG) 
		echo "<br>set Oid_sql = $Oid_sql";
		$result = mysqli_query($bssConn,$Oid_sql);
		echo mysqli_error($bssConn);
		$bgn_row_num++;
		$nmi_command = $nmiCommArr[$i][1];
		$Command_sql = "REPLACE INTO OMCR_COMMAND
				VALUES('$bgn_row_num','{$bscInfo[$j][bscId]}','$nmi_command',null,'255','-1',null,null,null)";
		if($DEBUG) 
		echo "<br>Commnad_sql = $Command_sql";
		$result = mysqli_query($bssConn,$Command_sql);
		echo mysqli_error($bssConn);
		$bgn_row_num++;
	}
}
?>
</body></html>
