<?php
/********************************************************************
 程序说明：   
    功能说明：发送完参数命令后，通过定时扫描数据库，显示命令的操作的状态过程，每隔两秒自动刷新
    
    输入变量：(由发送命令页面传来)$commIdStr,$okURL,$failURL
	
	输出并输入变量：$usedTime,$waitTime,$isFinish,$okURL,$failURL,$commIdStr

 作者：
********************************************************************/
require("../../inc/header.inc");
$DEBUG = 0;

$failURL	= rawurldecode($failURL);
$okURL		= rawurldecode($okURL);
$pubDb		= 'OMC_PUB';
$commTable	= 'parameterComm';
$sysConfTable ='sysConf';
if($commNum <= 0){
	$commArr = explode('-',$commIdStr);
	$commNum = sizeof($commArr);
}
$maxOverTime = $commNum * 60 + 10; //根据子系统个数和对每个子系统操作的最大延时（60秒）来决定
$maxWaitTime = $commNum * 60;
$maxStopTime = $commNum * 60 + 10;
$maxOfCommId = 50000; //设定命令表的最大值

if($DEBUG) echo "<br>if(isFinish != 0 || (waitTime >= maxWaitTime) ||(stopTime > maxStopTime))<br>";
if($DEBUG) echo "<br>if($isFinish != 0 || ($waitTime >= $maxWaitTime) ||($stopTime > $maxStopTime))<br>";

//命令处理完毕
//---------------- 
if(($isFinish != 0) || ($waitTime >= $maxWaitTime) || ($stopTime > $maxStopTime)){ 
	if($commId >= $maxOfCommId){ //初始化命令表
		//先删除表
		$dropSql = "DROP TABLE $commTable ";
		mysqli_query($pubConn,$dropSql);
		//再建一个结构相同的新表
		$createSql="CREATE TABLE parameterComm ( 
				 commId int(6) NOT NULL auto_increment, 
				 commType tinyint(1) NOT NULL default '0', 
				 sysTypeNo smallint(3) NOT NULL default '0', 
				 sysNo smallint(3) NOT NULL default '0', 
				 subSysNo smallint(3) NOT NULL default '0', 
				 instanceNo int(6) unsigned NOT NULL default '0', 
				 paramType tinyint(1) NOT NULL default '0', 
				 objId varchar(30) NOT NULL default '', 
				 commState tinyint(1) NOT NULL default '0', 
				 totalNum int(6) NOT NULL default '0', 
				 finishNum int(6) NOT NULL default '0', 
				 sendTime datetime NOT NULL default '0000-00-00 00:00:00', 
				 bgnTime datetime default '0000-00-00 00:00:00', 
				 endTime datetime default '0000-00-00 00:00:00', 
				 remark varchar(200) binary default NULL, 
				 PRIMARY KEY ( commId ), 
				 KEY commType ( commType ), 
				 KEY sysTypeNo ( sysTypeNo ), 
				 KEY sysNo ( sysNo ), 
				 KEY subSysNo ( subSysNo ), 
				 KEY paramType ( paramType ), 
				 KEY objId ( objId ), 
				 KEY commState ( commState ) 
			) ";
		mysqli_query($pubConn,$createSql);
	}else{
		//删除命令表的相关命令
		$commArr = explode('-',$commIdStr);
		for($i=0;$i<sizeof($commArr);$i++){
			if(strlen($commArr[$i]) <=0){ 
				continue;
			}
			$deleteSql = "DELETE FROM $commTable WHERE commId='$commArr[$i]'";
			if($DEBUG) echo "<br>deleteSql = $deleteSql";
			mysqli_query($pubConn,$deleteSql);
			echo mysqli_error($pubConn);
		}
	}
	
	// 重定向返回指定页
	if($isFinish != 0){
		if($isFinish > 0){
			echo "<b><font size=3 color=blue>Finished!!</font></b>";
			if($DEBUG) exit();
			echo " <meta http-equiv=\"refresh\" content=\"2; url=$okURL\"> ";
		}elseif($isFinish == -1){
			echo "<b><font size=3 color=black>Failed in some system!!</font></b>";
			if($DEBUG) exit();
			echo " <meta http-equiv=\"refresh\" content=\"2; url=$okURL\"> ";
		}else{
			echo "<b><font size=3 color=red>Failed!!</font></b>";
			if($DEBUG) exit();
			echo " <meta http-equiv=\"refresh\" content=\"2; url=$failURL\"> ";
		}
	}elseif($usedTime >= $maxOverTime && $stopTime == 1){
		echo "<b><font size=3 color=red>Stoped!!</font></b>";
		if($DEBUG) exit();
		echo " <meta http-equiv=\"refresh\" content=\"2; url=$failURL\"> ";
	}else{ //$usedTime >= $maxOverTime
	  	echo "<b><font size=3 color=red>Timeout!!</font></b>";
		if($DEBUG) exit();
		echo " <meta http-equiv=\"refresh\" content=\"2; url=$failURL\"> ";
	}
	exit();	
}//end of if($isFinish....)


//显示命令执行状态
//----------------------------- 
$showTime = $usedTime*2;
echo "<font size=2>Time:$showTime s</font>";
//$stopTime = 0;
$okNum	 = 0;
$failNum = 0;
$commNum = 0;
$commArr = explode('-',$commIdStr);
$thisTImeFinishNumstr='';
$lastTimeFinishNumArr = explode('-',$lastTimeFinishNumStr);
for($i=0;$i<sizeof($commArr);$i++){
	if(strlen($commArr[$i]) <=0){ continue;}
	$commNum++;
	$commSql = "SELECT * FROM $commTable WHERE commId= $commArr[$i]";
	if($DEBUG) echo "<br>get command = $commSql";
	$commResult = mysqli_query($pubConn,$commSql);
	if(mysqli_num_rows($commResult) <= 0){
		echo "<font size=2 color=red><b>Cannot find this command's info</b></font>";
		exit();
	}
	$commRows	= @mysqli_fetch_array($commResult);
	$sysTypeNo	= $commRows[sysTypeNo];
	$sysNo		= $commRows[sysNo];
	$subSysNo	= $commRows[subSysNo];
	$commType	= $commRows[commType];
	$paramType	= $commRows[paramType];
	$objId		= $commRows[objId];
	$commState	= $commRows[commState];

	switch($commType){
		case 0: echo "<font size=2> GET </font>";
			break;
		case 3: echo "<font size=2> SET </font>";
			break;
		default:
			echo "<font size=3>Unknown Command Type</font>";
			exit();
	}
	
	//获取该系统类型的snmp头和类型名称
	if(!isset($sysTypeName)){
		$sysConfSql = "SELECT snmpHead,sysTypeName FROM $sysConfTable WHERE sysTypeNo='$sysTypeNo'";
		if($DEBUG) echo "<br>get conf = $sysConfSql";
		$sysConfResult = mysqli_query($pubConn,$sysConfSql);
		echo mysqli_error($pubConn);
		$sysConfRows   = @mysqli_fetch_array($sysConfResult);
		$sysTypeName = $sysConfRows[sysTypeName];
		$snmpHead	 = $sysConfRows[snmpHead];
	}
	$sysName = $sysTypeName.'_'.$sysNo.'_'.$subSysNo;
	echo "<br><font size=2> $sysName 's</font>";
	$objIdStr=$snmpHead.'.'.$objId;
	echo "<font size=2 color=blue>$objIdStr</font>";
	
	switch($paramType){
		case 0: echo "<font size=2> [sub-system parameter] </font>";
			break;
		case 1: echo "<font size=2> [public sub-system parameter]</font>";
			break;
		case 2: echo "<font size=2> [public system parameter]</font>";
			break;
		default:
			echo "<font size=3>Unknown Parameter Type</font>";
			exit();
	}
	
	switch($commState){
		case 0: //process failed
			$failNum++;
			echo "<br><b><font size=2 color=red> $commRows[remark]</font></b>";
			break;
		case 1: //wait process
			$waitTime++;
			break;
		case 2: //in process
			$waitTime = 0;
			//只要有一个命令的完成数在增长，就认为该组命令在运行，否则认为命令处理超时
			if($DEBUG) echo "====> {$commRows[finishNum]} > {$lastTimeFinishNumArr[$i]}";
			if($commRows[finishNum] > $lastTimeFinishNumArr[$i]){
				$stopTime = 0;
			}else{
				$stopTime++;
			}
			omcProgressBar($commRows[totalNum],$commRows[finishNum]);
			break;
		case 3: //process finished
			$okNum++;
			omcProgressBar(100,100);
			break;
		default:
			echo "<font size=3>Unknown Command State</font>";
			exit();
	}
	$thisTimeFinishNumStr .=$commRows[finishNum].'-';
	echo "<br><br>";
}//end of for($i=0;$i<sizeof($commArr);$i++)
if($DEBUG) echo"<br>$commNum =?= $finishNum";
if($commNum == $okNum){
	$isFinish = 1;
}elseif($failNum > 0 && $okNum > 0){
	$isFinish = -1;
}elseif($commNum == $failNum){
	$isFinish = -2;
}else{
	$isFinish = 0;
}
if($DEBUG) echo"<br>lastTimeFinishNumStr=$lastTimeFinishNumStr";
if($DEBUG) echo"<br>thisTimeFinishNumStr=$thisTimeFinishNumStr";
$lastTimeFinishNumStr = $thisTimeFinishNumStr;
//刷新返回
//--------------
$usedTime++; //总时间
$failURL= rawurlencode($failURL);
$okURL  = rawurlencode($okURL);
//保留变量值,传到下次刷新
$keepStr= "usedTime=$usedTime&waitTime=$waitTime&isFinish=$isFinish&stopTime=$stopTime&failURL=$failURL&okURL=$okURL&commIdStr=$commIdStr&commNum=$commNum&lastTimeFinishNumStr=$lastTimeFinishNumStr&selectedDPC=$selectedDPC";
if($DEBUG) echo "<br>keepStr=$keepStr";
?>
<meta http-equiv="refresh" content="2 url=<?php echo "$PHP_SELF?$keepStr"; ?>">
<meta http-equiv="pragma" content="no-cache">