<?php
$pubDb	= 'OMC_PUB';
$imsiStart=$mcc.$mnc.$msinStart;
$imsiEnd=$mcc.$mnc.$msinEnd;
$msisdnStart=$cc.$ndc.$snStart;
$msisdnEnd=$cc.$ndc.$snEnd;
?>

<?php
//configure local node
$selSql = "SELECT * FROM OBJ_223.param_99 
			WHERE level_1='2' AND level_2='1' 
			ORDER BY level_3";

if($DEBUG) 
echo "<br>selSql=$selSql";
$selResult=mysqli_query($pubConn,$selSql);
echo mysqli_error($pubConn);

$tmpSn=$snStart;
while($selRows=mysqli_fetch_array($selResult)){
	$localNodeMsisdn=$cc.$ndc.$tmpSn;
	$localNodeMsisdn=omc_keep_length($localNodeMsisdn,'F',20,0);
	if($selRows[level_3]==4||$selRows[level_3]==21)
		$localNode='02'.$localNodeMsisdn.'AC128001'.'FFFFFFFF';
	else
		$localNode='02'.$localNodeMsisdn.'AC126201'.'FFFFFFFF';

	$updateSql="UPDATE OBJ_223.param_99 SET initValue='$localNode',readValue=''
				WHERE level_1='2' AND level_2='1' AND level_3='$selRows[level_3]'";
	if($DEBUG)
	echo "<br>updateSql=$updateSql";
	mysqli_query($pubConn,$updateSql);
	$tmpSn++;
}

//hlr gtt
//imsi
//01064600200000000000FFFF4600299999999990FFFF03000064060107000000FFFFFFFFFFFFFFFFFFFF
$gtt='0106';
$tmpImsiStart=omc_keep_length($imsiStart,'F',20,0);
$gtt=$gtt.$tmpImsiStart;
$tmpImsiEnd=omc_keep_length($imsiEnd,'F',20,0);
$gtt=$gtt.$tmpImsiEnd;
$gtt=$gtt.'03000064060007000000FFFFFFFFFFFFFFFFFFFF';

$updateSql="UPDATE OBJ_223.param_99 SET initValue='$gtt',readValue=''
			WHERE level_1='2' AND level_2='2' AND instanceNo='0'";
mysqli_query($pubConn,$updateSql);

//hlr gtt
//msisdn
//0101867559000000FFFFFFFF86755999999FFFFFFFFF03000064060107000000FFFFFFFFFFFFFFFFFFFF
if($msrnStart>$msisdnStart+20){
	$gtt='0101';
	$tmpMsisdnStart=$msisdnStart+20;
	$tmpMsisdnStart=omc_keep_length($tmpMsisdnStart,'F',20,0);
	$gtt=$gtt.$tmpMsisdnStart;
	$tmpMsisdnEnd=$msrnStart-1;
	$tmpMsisdnEnd=omc_keep_length($tmpMsisdnEnd,'F',20,0);
	$gtt=$gtt.$tmpMsisdnEnd;
	$gtt=$gtt.'03000064060007000000FFFFFFFFFFFFFFFFFFFF';

	$updateSql="UPDATE OBJ_223.param_99 SET initValue='$gtt',readValue=''
				WHERE level_1='2' AND level_2='2' AND instanceNo='1'";
	if($DEBUG)
	echo "<br>updateSql=$updateSql";
	mysqli_query($pubConn,$updateSql);
}else{
	$updateSql="UPDATE OBJ_223.param_99 SET initValue='0000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000000000FFFFFFFFFFFFFFFFFFFF',readValue=''
				WHERE level_1='2' AND level_2='2' AND instanceNo='1'";
	if($DEBUG)
	echo "<br>updateSql=$updateSql";
	mysqli_query($pubConn,$updateSql);
}

if($msrnEnd<$msisdnEnd){

	$gtt='0101';
	$tmpMsisdnStart=$msrnEnd+1;
	$tmpMsisdnStart=omc_keep_length($tmpMsisdnStart,'F',20,0);
	$gtt=$gtt.$tmpMsisdnStart;
	$tmpMsisdnEnd=omc_keep_length($msisdnEnd,'F',20,0);
	$gtt=$gtt.$tmpMsisdnEnd;
	$gtt=$gtt.'03000064060007000000FFFFFFFFFFFFFFFFFFFF';

	$updateSql="UPDATE OBJ_223.param_99 SET initValue='$gtt',readValue=''
				WHERE level_1='2' AND level_2='2' AND instanceNo='2'";
	if($DEBUG)
		echo "<br>updateSql=$updateSql";
	mysqli_query($pubConn,$updateSql);
}else{
	$updateSql="UPDATE OBJ_223.param_99 SET initValue='0000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000000000FFFFFFFFFFFFFFFFFFFF',readValue=''
				WHERE level_1='2' AND level_2='2' AND instanceNo='2'";
	if($DEBUG)
	echo "<br>updateSql=$updateSql";
	mysqli_query($pubConn,$updateSql);
}


//xapp parameters
//mcc
$tmpMcc=omc_keep_length($mcc,'E',4,0);
$updateSql="UPDATE OBJ_225.param_99 SET initValue='$tmpMcc',readValue=''
			WHERE level_1='2' AND level_2='1' AND instanceNo='0'";
mysqli_query($pubConn,$updateSql);

//mnc
$tmpMnc=omc_keep_length($mnc,'E',4,0);
$updateSql="UPDATE OBJ_225.param_99 SET initValue='$tmpMnc',readValue=''
			WHERE level_1='2' AND level_2='2' AND instanceNo='0'";
mysqli_query($pubConn,$updateSql);

//cc
$tmpCc=omc_keep_length($cc,'E',4,0);
$updateSql="UPDATE OBJ_225.param_99 SET initValue='$tmpCc',readValue=''
			WHERE level_1='2' AND level_2='3' AND instanceNo='0'";
mysqli_query($pubConn,$updateSql);

//ndc
$tmpNdc=omc_keep_length($ndc,'E',6,0);
$updateSql="UPDATE OBJ_225.param_99 SET initValue='$tmpNdc',readValue=''
			WHERE level_1='2' AND level_2='4' AND instanceNo='0'";
mysqli_query($pubConn,$updateSql);

//international prefix
$tmpInternationalPrefix=omc_keep_length($internationalPrefix,'E',4,0);
$updateSql="UPDATE OBJ_225.param_99 SET initValue='$tmpInternationalPrefix',readValue=''
			WHERE level_1='2' AND level_2='5' AND instanceNo='0'";
mysqli_query($pubConn,$updateSql);

//national prefix
$tmpNationalPrefix=omc_keep_length($nationalPrefix,'E',4,0);
$updateSql="UPDATE OBJ_225.param_99 SET initValue='$tmpNationalPrefix',readValue=''
			WHERE level_1='2' AND level_2='6' AND instanceNo='0'";
mysqli_query($pubConn,$updateSql);

//VLR parameters
//MSRN
$msrn=omc_keep_length($msrnStart,'F',16,0).omc_keep_length($msrnEnd,'F',16,0);
$updateSql="UPDATE OBJ_325.param_99 SET initValue='$msrn',readValue=''
			WHERE level_1='2' AND level_2='1' AND level_3='13' AND instanceNo='0'";
if($DEBUG)
	echo "<br>updateSql=$updateSql";
mysqli_query($pubConn,$updateSql);


//SMSC parameters
//MSISDN
$tmpMsisdnStart=$msisdnStart+20;
$tmpMsisdnEnd=$msrnStart-1;
$tmpMsisdn=omc_keep_length($tmpMsisdnStart,'E',18,0).omc_keep_length($tmpMsisdnEnd,'E',18,0);
$updateSql="UPDATE OBJ_350.param_99 SET initValue='$tmpMsisdn',readValue=''
			WHERE level_1='2' AND level_2='1' AND level_3='4' AND instanceNo='0'";
mysqli_query($pubConn,$updateSql);

$tmpMsisdnStart=$msrnEnd+1;

$tmpMsisdn=omc_keep_length($tmpMsisdnStart,'E',18,0).omc_keep_length($msisdnEnd,'E',18,0);
$updateSql="UPDATE OBJ_350.param_99 SET initValue='$tmpMsisdn',readValue=''
			WHERE level_1='2' AND level_2='1' AND level_3='4' AND instanceNo='1'";
mysqli_query($pubConn,$updateSql);

/**************MSC Prefix*********************************/
//MSC-MSRN-Prefix
$tmpMsrn='';
for($i=0;$i<strlen($msrnStart);$i++){
	if($msrnStart[$i]!=$msrnEnd[$i])
		break;
	$tmpMsrn=$tmpMsrn.$msrnStart[$i];
}

$tmpMsrn=omc_keep_length($tmpMsrn,'E',16,0);
$updateSql="UPDATE OBJ_322.param_99 SET initValue='$tmpMsrn',readValue=''
			WHERE level_1='2' AND level_2='1' AND level_3='1' AND instanceNo='0'";
mysqli_query($pubConn,$updateSql);

$updateSql="UPDATE OBJ_322.param_99 SET initValue='4400EEEEEEEEEEEEEEEE890000EEEEEEEE030100',readValue=''
			WHERE level_1='2' AND level_2='1' AND level_3='2' AND instanceNo='0'";
mysqli_query($pubConn,$updateSql);


//MSC-SN-Prefix
$sn='';
for($i=0;$i<strlen($snStart);$i++){
	if($snStart[$i]!=$snEnd[$i])
		break;
	$sn=$sn.$snStart[$i];
}

$tmpPrefix=omc_keep_length($sn,'E',16,0);
$updateSql="UPDATE OBJ_322.param_99 SET initValue='$tmpPrefix',readValue=''
			WHERE level_1='2' AND level_2='1' AND level_3='1' AND instanceNo='1'";
mysqli_query($pubConn,$updateSql);

$tmpPrefixAtt='42';
$tmpCcNdc=$cc.$ndc;
$tmpInsertCount=dechex(strlen($tmpCcNdc));
$tmpPrefixAtt=$tmpPrefixAtt.$tmpInsertCount.'0'.omc_keep_length($tmpCcNdc,'E',16,0).'8500'.$tmpInsertCount.'0'.omc_keep_length($tmpCcNdc,'E',8,0).'030100';

$updateSql="UPDATE OBJ_322.param_99 SET initValue='$tmpPrefixAtt',readValue=''
			WHERE level_1='2' AND level_2='1' AND level_3='2' AND instanceNo='1'";
mysqli_query($pubConn,$updateSql);


//MSC-(National Prefix+NDC+SN)-Prefix
$tmpPrefix=$nationalPrefix.$ndc.$sn;
$tmpPrefix=omc_keep_length($tmpPrefix,'E',16,0);
$updateSql="UPDATE OBJ_322.param_99 SET initValue='$tmpPrefix',readValue=''
			WHERE level_1='2' AND level_2='1' AND level_3='1' AND instanceNo='2'";
mysqli_query($pubConn,$updateSql);

$tmpPrefixAtt='42';
$tmpInsertCount=dechex(strlen($cc));
$tmpDeleteCount=dechex(strlen($nationalPrefix));
$tmpPrefixAtt=$tmpPrefixAtt.$tmpInsertCount.$tmpDeleteCount.omc_keep_length($cc,'E',16,0).'8500'.$tmpInsertCount.$tmpDeleteCount.omc_keep_length($cc,'E',8,0).'030100';

$updateSql="UPDATE OBJ_322.param_99 SET initValue='$tmpPrefixAtt',readValue=''
			WHERE level_1='2' AND level_2='1' AND level_3='2' AND instanceNo='2'";
mysqli_query($pubConn,$updateSql);

//MSC-(Internatioanal prefix+CC+NDC+SN)-Prefix
$tmpPrefix=$internationalPrefix.$cc.$ndc.$sn;
$tmpPrefix=omc_keep_length($tmpPrefix,'E',16,0);
$updateSql="UPDATE OBJ_322.param_99 SET initValue='$tmpPrefix',readValue=''
			WHERE level_1='2' AND level_2='1' AND level_3='1' AND instanceNo='3'";
mysqli_query($pubConn,$updateSql);

$tmpDeleteCount=dechex(strlen($internationalPrefix));
$tmpPrefixAtt='42'.'0'.$tmpDeleteCount.'EEEEEEEEEEEEEEEE'.'8500'.'0'.$tmpDeleteCount.'EEEEEEEE'.'030100';
$updateSql="UPDATE OBJ_322.param_99 SET initValue='$tmpPrefixAtt',readValue=''
			WHERE level_1='2' AND level_2='1' AND level_3='2' AND instanceNo='3'";
mysqli_query($pubConn,$updateSql);

//MSC-(NDC+SN)-Prefix
$tmpPrefix=$ndc.$sn;
$tmpPrefix=omc_keep_length($tmpPrefix,'E',16,0);
$updateSql="UPDATE OBJ_322.param_99 SET initValue='$tmpPrefix',readValue=''
			WHERE level_1='2' AND level_2='1' AND level_3='1' AND instanceNo='4'";
mysqli_query($pubConn,$updateSql);

$tmpPrefixAtt='42';
$tmpInsertCount=dechex(strlen($cc));
$tmpPrefixAtt=$tmpPrefixAtt.$tmpInsertCount.'0'.omc_keep_length($cc,'E',16,0).'8500'.$tmpInsertCount.'0'.omc_keep_length($cc,'E',8,0).'030100';

$updateSql="UPDATE OBJ_322.param_99 SET initValue='$tmpPrefixAtt',readValue=''
			WHERE level_1='2' AND level_2='1' AND level_3='2' AND instanceNo='4'";
mysqli_query($pubConn,$updateSql);

//MSC-(CC+NDC+SN)-Prefix
$tmpPrefix=$cc.$ndc.$sn;
$tmpPrefix=omc_keep_length($tmpPrefix,'E',16,0);
$updateSql="UPDATE OBJ_322.param_99 SET initValue='$tmpPrefix',readValue=''
			WHERE level_1='2' AND level_2='1' AND level_3='1' AND instanceNo='5'";
mysqli_query($pubConn,$updateSql);

$updateSql="UPDATE OBJ_322.param_99 SET initValue='4400EEEEEEEEEEEEEEEE850000EEEEEEEE020100',readValue=''
			WHERE level_1='2' AND level_2='1' AND level_3='2' AND instanceNo='5'";
mysqli_query($pubConn,$updateSql);
/**************MSC Prefix END*********************************/


/**************XAPP-Conv Prefix *********************************/
//XAPP-Conv-SN-Prefix
$tmpPrefix='';
for($i=0;$i<strlen($sn);$i++)
	$tmpPrefix=$tmpPrefix.omc_keep_length(dechex(ord($sn[i])),'0',2);
$tmpPrefix=omc_keep_length($tmpPrefix,'0',32,0);

$tmpInsertCount=omc_keep_length(dechex(strlen($tmpCcNdc)),'0',2);

$tmpPrefixAtt=$tmpPrefix.'00'.$tmpInsertCount;

$tmpPrefix='';
$tmpCcNdc=$cc.$ndc;
for($i=0;$i<strlen($tmpCcNdc);$i++)
	$tmpPrefix=$tmpPrefix.omc_keep_length(dechex(ord($tmpCcNdc[$i])),'0',2);
$tmpPrefix=omc_keep_length($tmpPrefix,'0',32,0);
$tmpPrefixAtt=$tmpPrefixAtt.$tmpPrefix.'00';

$updateSql="UPDATE OBJ_225.param_99 SET initValue='$tmpPrefixAtt',readValue=''
			WHERE level_1='2' AND level_2='8' AND instanceNo='0'";
mysqli_query($pubConn,$updateSql);

//XAPP-Conv-(National Prefix+NDC+SN)-Prefix
$tmpValue=$nationalPrefix.$ndc.$sn;
$tmpPrefix='';
for($i=0;$i<strlen($tmpValue);$i++)
	$tmpPrefix=$tmpPrefix.omc_keep_length(dechex(ord($tmpValue[$i])),'0',2);
$tmpPrefix=omc_keep_length($tmpPrefix,'0',32,0);

$tmpDeleteCount=omc_keep_length(dechex(strlen($nationalPrefix)),'0',2);
$tmpInsertCount=omc_keep_length(dechex(strlen($cc)),'0',2);

$tmpPrefixAtt=$tmpPrefix.$tmpDeleteCount.$tmpInsertCount;

$tmpPrefix='';
for($i=0;$i<strlen($cc);$i++)
	$tmpPrefix=$tmpPrefix.omc_keep_length(dechex(ord($cc[$i])),'0',2);
$tmpPrefix=omc_keep_length($tmpPrefix,'0',32,0);

$tmpPrefixAtt=$tmpPrefixAtt.$tmpPrefix.'00';

$updateSql="UPDATE OBJ_225.param_99 SET initValue='$tmpPrefixAtt',readValue=''
			WHERE level_1='2' AND level_2='8' AND instanceNo='1'";
mysqli_query($pubConn,$updateSql);

//XAPP-Conv-(Internatioanal prefix+CC+NDC+SN)-Prefix
$tmpValue=$internationalPrefix.$cc.$ndc.$sn;
$tmpPrefix='';
for($i=0;$i<strlen($tmpValue);$i++)
	$tmpPrefix=$tmpPrefix.omc_keep_length(dechex(ord($tmpValue[$i])),'0',2);
$tmpPrefix=omc_keep_length($tmpPrefix,'0',32,0);

$tmpDeleteCount=omc_keep_length(dechex(strlen($internationalPrefix)),'0',2);

$tmpPrefixAtt=$tmpPrefix.$tmpDeleteCount.'00'.'00000000000000000000000000000000'.'00';

$updateSql="UPDATE OBJ_225.param_99 SET initValue='$tmpPrefixAtt',readValue=''
			WHERE level_1='2' AND level_2='8' AND instanceNo='2'";
mysqli_query($pubConn,$updateSql);

//XAPP-Conv-(NDC+SN)-Prefix
$tmpValue=$ndc.$sn;
$tmpPrefix='';
for($i=0;$i<strlen($tmpValue);$i++)
	$tmpPrefix=$tmpPrefix.omc_keep_length(dechex(ord($tmpValue[$i])),'0',2);
$tmpPrefix=omc_keep_length($tmpPrefix,'0',32,0);

$tmpInsertCount=omc_keep_length(dechex(strlen($cc)),'0',2);

$tmpPrefixAtt=$tmpPrefix.'00'.$tmpInsertCount;

$tmpPrefix='';

for($i=0;$i<strlen($cc);$i++)
	$tmpPrefix=$tmpPrefix.omc_keep_length(dechex(ord($cc[$i])),'0',2);
$tmpPrefix=omc_keep_length($tmpPrefix,'0',32,0);

$tmpPrefixAtt=$tmpPrefixAtt.$tmpPrefix.'00';

$updateSql="UPDATE OBJ_225.param_99 SET initValue='$tmpPrefixAtt',readValue=''
			WHERE level_1='2' AND level_2='8' AND instanceNo='3'";
mysqli_query($pubConn,$updateSql);

//XAPP-Conv-(CC+NDC+SN)-Prefix
$tmpValue=$cc.$ndc.$sn;
$tmpPrefix='';
for($i=0;$i<strlen($tmpValue);$i++)
	$tmpPrefix=$tmpPrefix.omc_keep_length(dechex(ord($tmpValue[$i])),'0',2);
$tmpPrefix=omc_keep_length($tmpPrefix,'0',32,0);

$tmpPrefixAtt=omc_keep_length($tmpPrefix,'0',70,0);
$updateSql="UPDATE OBJ_225.param_99 SET initValue='$tmpPrefixAtt',readValue=''
			WHERE level_1='2' AND level_2='8' AND instanceNo='4'";
mysqli_query($pubConn,$updateSql);
/**************XAPP-Conv Prefix END*********************************/

/**************XAPP-CLI-Conv Prefix *********************************/
//XAPP-CLI-Conv-(CC+NDC+SN)-Prefix
$tmpValue=$cc.$ndc.$sn;
$tmpPrefix='';
for($i=0;$i<strlen($tmpValue);$i++)
	$tmpPrefix=$tmpPrefix.omc_keep_length(dechex(ord($tmpValue[$i])),'0',2);
$tmpPrefix=omc_keep_length($tmpPrefix,'0',32,0);

$tmpValue=$cc.$ndc;
$tmpDeleteCount=omc_keep_length(dechex(strlen($tmpValue)),'0',2);

$tmpPrefixAtt='01'.$tmpPrefix.$tmpDeleteCount.'03';
$tmpPrefixAtt=omc_keep_length($tmpPrefixAtt,'0',74,0);
$updateSql="UPDATE OBJ_225.param_99 SET initValue='$tmpPrefixAtt',readValue=''
			WHERE level_1='2' AND level_2='9' AND instanceNo='0'";
mysqli_query($pubConn,$updateSql);

//XAPP-CLI-Conv-(International Prefix+CC+NDC+SN)-Prefix
$tmpValue=$internationalPrefix.$cc.$ndc.$sn;
$tmpPrefix='';
for($i=0;$i<strlen($tmpValue);$i++)
	$tmpPrefix=$tmpPrefix.omc_keep_length(dechex(ord($tmpValue[$i])),'0',2);
$tmpPrefix=omc_keep_length($tmpPrefix,'0',32,0);

$tmpValue=$internationalPrefix.$cc.$ndc;
$tmpDeleteCount=omc_keep_length(dechex(strlen($tmpValue)),'0',2);

$tmpPrefixAtt='01'.$tmpPrefix.$tmpDeleteCount.'03';
$tmpPrefixAtt=omc_keep_length($tmpPrefixAtt,'0',74,0);
$updateSql="UPDATE OBJ_225.param_99 SET initValue='$tmpPrefixAtt',readValue=''
			WHERE level_1='2' AND level_2='9' AND instanceNo='1'";
mysqli_query($pubConn,$updateSql);
/**************XAPP-CLI-Conv Prefix END*********************************/

/*************************************clear PSTN loopback******************/
$updateSql="UPDATE OBJ_322.param_99 SET initValue='EEEEEEEEEEEEEEEE',readValue=''
		WHERE level_1='2' AND level_2='1' AND level_3='1' AND instanceNo='30'";
mysqli_query($pubConn,$updateSql);

$updateSql="UPDATE OBJ_322.param_99 SET initValue='2200EEEEEEEEEEEEEEEE010800EEEEEEEE030000',readValue=''
			WHERE level_1='2' AND level_2='1' AND level_3='2' AND instanceNo='30'";
mysqli_query($pubConn,$updateSql);

$updateSql="UPDATE OBJ_322.param_99 SET initValue='00',readValue=''
			WHERE level_1='2' AND level_2='1' AND level_3='3' AND instanceNo='30'";
mysqli_query($pubConn,$updateSql);

?>

<?php
	$setForBssArr=array(
		array('remark' => 'SCCP parameters','sysTypeNo' => '223','sysId' => '223_99_99', 'oid' => '2'),
		array('remark' => 'XAPP parameters','sysTypeNo' => '225','sysId' => '225_99_99', 'oid' => '2'),
		array('remark' => 'MSC parameters','sysTypeNo' => '322','sysId' => '322_99_99', 'oid' => '2.1'),
		array('remark' => 'VLR parameters','sysTypeNo' => '325','sysId' => '325_99_99', 'oid' => '2.1.13'),
		array('remark' => 'SMSC parameters','sysTypeNo' => '350','sysId' => '350_99_99', 'oid' => '2.1.4')
	);

	//send set command
	echo "<br>Setting parameters to MSS!<br>";
	$commStr='';
	for($i=0;$i<sizeof($setForBssArr);$i++){
		if($DEBUG)
		echo "<br>sysId={$setForBssArr[$i][sysId]},oid={$setForBssArr[$i][oid]},value={$setForBssArr[$i][value]}";
		$tmpCommStr=sysCommSend($setForBssArr[$i][sysId],$nSetComm,$setForBssArr[$i][oid]);
		if($DEBUG)echo "<br>tmpCommStr=$tmpCommStr";
		$commStr.=$tmpCommStr;
		if($DEBUG)echo "<br>commStr=$commStr";
	}
	
	//check the command status
	if($commStr==""){
		echo "<br><font color=\"#FF0000\">Send the command error, Please check the device state!</font><br>";
		require("./display.php");
		exit();
	}
		
	$cmmIdStrArr=explode("-",$commStr);
	
	for($i=0;$i<sizeof($cmmIdStrArr)-1;$i++){
		if($i==0)
			$tmpWhere="(commId='$cmmIdStrArr[$i]'";
		else
			$tmpWhere=$tmpWhere." OR commId='$cmmIdStrArr[$i]'";
	}
	$tmpWhere=$tmpWhere.")";	
	
				
	$i = 0;
	while(1){
		if($i > $maxNum){
			$tmpRemark='Timeout';			
			$delComm = " DELETE FROM $sysCommTable WHERE $tmpWhere ";
			mysqli_query($pubConn,$delComm);						
			
			echo "<font color=\"#FF0000\">Error: $tmpRemark.</font></b>";
			require("./display.php");
			exit();
		}
		
		echo "<img border=\"0\" src=\"../../images/arrow.gif\" width=\"10\" height=\"10\">";
		flush();	
		
		$selComm = "SELECT commState,remark,objId,sysTypeNo FROM $sysCommTable WHERE $tmpWhere AND commState='$nProcFail'";
		if($DEBUG) echo "<br>selComm=$selComm";
		
		$selRes  = mysqli_query($pubConn,$selComm);
		
		if(mysqli_num_rows($selRes)>0){
			$selRow  = mysqli_fetch_array($selRes);
			$tmpRemark=$selRow[remark];
			$tmpOid=$selRow[objId];
			$tmpSysTypeNo=$selRow[sysTypeNo];
			$delComm = " DELETE FROM $sysCommTable WHERE $tmpWhere ";	
			mysqli_query($pubConn,$delComm);
			
			for($j=0;$j<sizeof($setForBssArr);$j++){
				if($setForBssArr[$j][sysTypeNo]==$tmpSysTypeNo)
					echo "<font color=\"#FF0000\">Error: $tmpRemark.({$setForBssArr[$j][remark]})</font></b>";	
				
			}
			require("./display.php");
			exit();
		}
		
		$selComm = "SELECT commState,remark FROM $sysCommTable WHERE $tmpWhere AND (commState='$nWaitProc' OR commState='$nInProc')";
	
		if($DEBUG)
		echo "<br>selComm=$selComm";
		
		$selRes  = mysqli_query($pubConn,$selComm);
		if($DEBUG) $tmpNum=mysqli_num_rows($selRes);
		if($DEBUG) echo "<br>tmpNum=$tmpNum";
		
		if(mysqli_num_rows($selRes)>0){
			sleep(1);
			$i++;
			continue;
		}
		$delComm = " DELETE FROM $sysCommTable WHERE $tmpWhere ";	
		mysqli_query($pubConn,$delComm);
		break;		
	}

sleep(2);
	//send save command
	$commStr='';
	for($i=0;$i<sizeof($saveCmdArr);$i++){
		if($DEBUG)echo "<br>sysId={$saveCmdArr[$i][sysId]},oid={$saveCmdArr[$i][oid]},value={$saveCmdArr[$i][value]}";
		setSysParamValue($saveCmdArr[$i][sysId],$saveCmdArr[$i][oid],'0',$saveCmdArr[$i][value]);
		$tmpCommStr=sysCommSend($saveCmdArr[$i][sysId],$nSetComm,$saveCmdArr[$i][oid]);
		if($DEBUG)echo "<br>tmpCommStr=$tmpCommStr";
		$commStr.=$tmpCommStr;
		if($DEBUG)echo "<br>commStr=$commStr";
	}
	
	//check the command status
	if($commStr==""){
		echo "<br><font color=\"#FF0000\">Send the command error, Please check the device state!</font><br>";
		require("./display.php");
		exit();
	}
		
	$cmmIdStrArr=explode("-",$commStr);
	
	for($i=0;$i<sizeof($cmmIdStrArr)-1;$i++){
		if($i==0)
			$tmpWhere="(commId='$cmmIdStrArr[$i]'";
		else
			$tmpWhere=$tmpWhere." OR commId='$cmmIdStrArr[$i]'";
	}
	$tmpWhere=$tmpWhere.")";	
	
				
	$i = 0;
	while(1){
		if($i > $maxNum){
			$tmpRemark='Timeout';			
			$delComm = " DELETE FROM $sysCommTable WHERE $tmpWhere ";
			mysqli_query($pubConn,$delComm);						
			
			echo "<font color=\"#FF0000\">Error: $tmpRemark.</font></b>";
			require("./display.php");
			exit();
		}
		echo "<img border=\"0\" src=\"../../images/arrow.gif\" width=\"10\" height=\"10\">";
		flush();	
		
		$selComm = "SELECT commState,remark,objId,sysTypeNo FROM $sysCommTable WHERE $tmpWhere AND commState='$nProcFail'";
		if($DEBUG) echo "<br>selComm=$selComm";
		
		$selRes  = mysqli_query($pubConn,$selComm);
		
		if(mysqli_num_rows($selRes)>0){
			$selRow  = mysqli_fetch_array($selRes);
			$tmpRemark=$selRow[remark];
			$tmpOid=$selRow[objId];
			$tmpSysTypeNo=$selRow[sysTypeNo];
			$delComm = " DELETE FROM $sysCommTable WHERE $tmpWhere ";	
			mysqli_query($pubConn,$delComm);
			
			for($j=0;$j<sizeof($resetCommArr);$j++){
				if($resetCommArr[$j][sysTypeNo]==$tmpSysTypeNo)
					echo "<font color=\"#FF0000\">Error: $tmpRemark.({$resetCommArr[$j][remark]})</font></b>";		
			}
			require("./display.php");
			exit();
		}
		
		$selComm = "SELECT commState,remark FROM $sysCommTable WHERE $tmpWhere AND (commState='$nWaitProc' OR commState='$nInProc')";
	
		if($DEBUG)echo "<br>selComm=$selComm";
		
		$selRes  = mysqli_query($pubConn,$selComm);
		if($DEBUG) $tmpNum=mysqli_num_rows($selRes);
		if($DEBUG) echo "<br>tmpNum=$tmpNum";
		
		if(mysqli_num_rows($selRes)>0){
			sleep(1);
			$i++;
			continue;
		}
		$delComm = " DELETE FROM $sysCommTable WHERE $tmpWhere ";	
		mysqli_query($pubConn,$delComm);
		echo "<font size=\"2\" color=\"#0000FF\">Finished</font>";
		break;		
	}

	
	$getSysIdSql = "SELECT * FROM $sysInfoTable 
					WHERE sysTypeNo='320' 
					ORDER BY baseStat DESC,updateTime DESC";

	if($DEBUG) echo "<br>getSysIdSql=$getSysIdSql";
	$getSysIdResult=mysqli_query($pubConn,$getSysIdSql);
	echo mysqli_error($pubConn);

	while($getSysIdRows=mysqli_fetch_array($getSysIdResult)){
		$dstSeverIp=$getSysIdRows[ip];	
		echo "<br>Restarting MSS,please wait about 6 minutes!<br>";
		flush();
		$tmpScript="./beforeClearSubs".' '.$dstSeverIp;
		system($tmpScript);
		
		//clear heartbeat
		$delSql="DELETE FROM OMC_PUB.sysStat";
		mysqli_query($pubConn,$delSql);
		
		for($i=0;$i<30;$i++){
			echo "<img border=\"0\" src=\"../../images/arrow.gif\" width=\"10\" height=\"10\">";
			flush();
			sleep(4);
		}

		$tmpScript="./mssStart".' '.$dstSeverIp;
		system($tmpScript);
		
		for($i=0;$i<45;$i++){
			$tmpSql = "SELECT * FROM OMC_PUB.sysStat WHERE sysTypeNo='200' AND sysNo='9'";

			if($DEBUG) echo "<br>tmpSql=$tmpSql";
			$tmpResult=mysqli_query($pubConn,$tmpSql);
			echo mysqli_error($pubConn);
			if(mysqli_num_rows($tmpResult)>0)
				break;
			sleep(4);
			echo "<img border=\"0\" src=\"../../images/arrow.gif\" width=\"10\" height=\"10\">";
			flush();
		}
		if($i==30)
			echo "<font size=\"2\" color=\"#FF0000\">Starting MSS failed,please check it</font>";
		else
			echo "<font size=\"2\" color=\"#0000FF\">Finished</font>";
	}
	echo "<br><font size=\"2\" color=\"#0000FF\">Process finished</font><br>";
?>
