<?php
$pubDb	= 'OMC_PUB';
?>

<?php
//network inicator
$selSql = "SELECT initValue FROM OBJ_222.param_99 
			WHERE level_1='2' AND level_2='2'";

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

switch($ni){
	case 0:
		$tmpValue=setBitValue($selRows[initValue],'0.0',0);
		break;
	case 1:
		$tmpValue=setBitValue($selRows[initValue],'0.2',0);
		break;
	case 2:
		$tmpValue=setBitValue($selRows[initValue],'0.0',1);
		break;
	case 3:
		$tmpValue=setBitValue($selRows[initValue],'0.2',1);
		break;
	default:
		break;
}

$updateSql="UPDATE OBJ_222.param_99 SET initValue='$tmpValue' WHERE level_1='2' AND level_2='2'";
if($DEBUG)echo "<br>updateSql=$updateSql";
mysqli_query($pubConn,$updateSql);

$levelWhere = getLevelWhere($NetOPCArr[$ni][0]);
$opc=omc_keep_length($opc,'0',6);

$updateSql="UPDATE OBJ_222.param_99 SET initValue='$opc',readValue=''	WHERE $levelWhere";
if($DEBUG)echo "<br>updateSql=$updateSql";
mysqli_query($pubConn,$updateSql);

//Routing
$dpc=omc_keep_length($dpc,'0',6);
$tmpRouting=$dpc.'00'.'80';

switch($ni){
	case 0:
	case 2:
		$updateSql="UPDATE OBJ_222.param_99 SET initValue='$tmpRouting',readValue=''
					WHERE level_1='2' AND level_2='6' AND level_3='1' AND instanceNo='0'";
		break;
	case 1:
	case 3:
		$updateSql="UPDATE OBJ_222.param_99 SET initValue='$tmpRouting',readValue=''
					WHERE level_1='2' AND level_2='6' AND level_3='3' AND instanceNo='0'";
		break;
	default:
		break;
}
if($DEBUG)
echo "<br>updateSql=$updateSql";
mysqli_query($pubConn,$updateSql);

//clear PSTN loopback
$updateSql="UPDATE OBJ_222.param_99 SET initValue='0000008080',readValue=''
			WHERE level_1='2' AND level_2='6' AND level_3='3' AND instanceNo='7'";
mysqli_query($pubConn,$updateSql);

//linkset
$dpc=omc_keep_length($dpc,'0',6);
$tmpLinkset=$dpc.'00';
switch($ni){
	case 0:
	case 2:
		$tmpLinkset=setBitValue($tmpLinkset,'3.0-3.1','00');
		break;
	case 1:
	case 3:
		$tmpLinkset=setBitValue($tmpLinkset,'3.0-3.1','10');
		break;
	default:
		break;
}

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

//clear PSTN loopback
$updateSql="UPDATE OBJ_222.param_99 SET initValue='00000000',readValue=''
			WHERE level_1='2' AND level_2='4' AND level_3='1' AND instanceNo='7'";
mysqli_query($pubConn,$updateSql);

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

//link
$updateSql="UPDATE OBJ_222.param_99 SET initValue='000062',readValue=''
			WHERE level_1='2' AND level_2='5' AND level_3='1' AND instanceNo='0'";
mysqli_query($pubConn,$updateSql);

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

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



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

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

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

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

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



//Trunk Group
$tmpTgAtt='00000008070200FF0000000000';
$tmpTgAtt=setBitValue($tmpTgAtt,'1.0-3.7',$dpc);
switch($ni){
	case 0:
	case 2:
		$tmpTgAtt=setBitValue($tmpTgAtt,'5.0-5.7','00');
		break;
	case 1:
	case 3:
		$tmpTgAtt=setBitValue($tmpTgAtt,'5.0-5.7','02');
		break;
	default:
		break;
}

switch($ni){
	case 0:
	case 1:
		$tmpTgAtt=setBitValue($tmpTgAtt,'12.0-12.7','00');
		break;
	case 2:
	case 3:
		$tmpTgAtt=setBitValue($tmpTgAtt,'12.0-12.7','01');
		break;
	default:
		break;
}

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

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

$updateSql="UPDATE OBJ_322.param_99 SET initValue='000000000A0302FF0000000000',readValue=''
		WHERE level_1='2' AND level_2='1' AND level_3='4' AND instanceNo='8'";
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='255'";
mysqli_query($pubConn,$updateSql);


//PSTN to mobile prefix
$selSql = "SELECT initValue FROM OBJ_322.param_99 
			WHERE level_1='2' AND level_2='1' AND level_3='1' AND instanceNo='1'";

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

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

//PSTN to mobile prefix
$selSql = "SELECT initValue FROM OBJ_322.param_99 
			WHERE level_1='2' AND level_2='1' AND level_3='2' AND instanceNo='1'";

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

$tmpPrefix=setBitValue($selRows[initValue],'00.-0.7','43');

$updateSql="UPDATE OBJ_322.param_99 SET initValue='$tmpPrefix',readValue='2200EEEEEEEEEEEEEEEE010800EEEEEEEE030000'
		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='FF'
			WHERE level_1='2' AND level_2='1' AND level_3='3' AND instanceNo='30'";
mysqli_query($pubConn,$updateSql);

/**************Mobile to PSTN*********************************/
/*
$selSql = "SELECT initValue FROM OBJ_225.param_99 
			WHERE level_1='2' AND level_2='3'";

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

$tmpArr=explode("E",$selRows[initValue]);
$tmpCc=$tmpArr[0];

$tmpPrefixAtt='32';
$tmpInsertCount=dechex(strlen($tmpCc));
$tmpPrefixAtt=$tmpPrefixAtt.$tmpInsertCount.'0'.omc_keep_length($tmpCc,'E',16,0).'8100'.'0'.'0'.'EEEEEEEE'.'030100';
*/

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

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

$updateSql="UPDATE OBJ_322.param_99 SET initValue='00',readValue='FF'
			WHERE level_1='2' AND level_2='1' AND level_3='3' AND instanceNo='11'";
mysqli_query($pubConn,$updateSql);
*/
/*****************************************/
/*
$updateSql="UPDATE OBJ_322.param_99 SET initValue='$pstnPrefix_1',readValue='EEEEEEEEEEEEEEEE'
		WHERE level_1='2' AND level_2='1' AND level_3='1' AND instanceNo='12'";
mysqli_query($pubConn,$updateSql);

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

$updateSql="UPDATE OBJ_322.param_99 SET initValue='00',readValue='FF'
			WHERE level_1='2' AND level_2='1' AND level_3='3' AND instanceNo='12'";
mysqli_query($pubConn,$updateSql);
/***********************************************/
/*
$updateSql="UPDATE OBJ_322.param_99 SET initValue='$pstnPrefix_2',readValue='EEEEEEEEEEEEEEEE'
		WHERE level_1='2' AND level_2='1' AND level_3='1' AND instanceNo='13'";
mysqli_query($pubConn,$updateSql);

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

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

<?php
	$setForPstnArr=array(
		array('remark' => 'MTP3 parameters','sysTypeNo' => '222','sysId' => '222_99_99', 'oid' => '2'),
		array('remark' => 'MSC parameters','sysTypeNo' => '322','sysId' => '322_99_99', 'oid' => '2'),
	);

	//send set command
	echo "<br>Setting parameters to MSS!<br>";
	$commStr='';
	for($i=0;$i<sizeof($setForPstnArr);$i++){
		$tmpCommStr=sysCommSend($setForPstnArr[$i][sysId],$nSetComm,$setForPstnArr[$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($setForPstnArr);$j++){
				if($setForPstnArr[$j][sysTypeNo]==$tmpSysTypeNo)
					echo "<font color=\"#FF0000\">Error: $tmpRemark.({$setForPstnArr[$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);
		break;		
	}
	
	//send reset command
	$commStr='';
	for($i=0;$i<sizeof($resetCmdArr);$i++){
		if($DEBUG)echo "<br>sysId={$resetCmdArr[$i][sysId]},oid={$resetCmdArr[$i][oid]},value={$resetCmdArr[$i][value]}";
		setSysParamValue($resetCmdArr[$i][sysId],$resetCmdArr[$i][oid],'0',$resetCmdArr[$i][value]);
		$tmpCommStr=sysCommSend($resetCmdArr[$i][sysId],$nSetComm,$resetCmdArr[$i][oid]);
		if($DEBUG)echo "<br>tmpCommStr=$tmpCommStr";
		$commStr.=$tmpCommStr;
		if($DEBUG)echo "<br>commStr=$commStr";
	}
	
	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 "<br><font size=\"2\" color=\"#0000FF\">Process finished</font><br>";
		break;		
	}
	
?>
