<?php
$bssDb = "BssOmcDb";
$dbname="BssOmcDb";
$hostname="localhost";
$username="root";
$password="rootaa";

?>
<script LANGUAGE="JavaScript">
function AssignTrx(bssid,objectid,btsid,cellid,trxnum,deltype,delcheckbox)
{
	var delValue,x,y;
	var baseHref;
	x = document.body.scrollLeft;
	y = document.body.scrollTop;
    baseHref = '<?php echo $PHP_SELF."?bssid=".$bssid."&objectid=".$objectid;?>';
	if(deltype == 'AssignTrx')
	{
		if(confirm('Do you want to Assign  Trx '+trxnum+' to Bts '+cellid+' ?')!=true)
			delcheckbox.checked=false;
		else
			window.location.href = baseHref+'&deltype='+deltype+'&btsid='+btsid+'&cellid='+cellid+'&trxnum='+trxnum;
	}
}

function delTheObj(bssid,objectid,btsid,adjhand,linkno,deltype,delcheckbox)
{
	var delValue,x,y;
	var baseHref;
	x = document.body.scrollLeft;
	y = document.body.scrollTop;
    baseHref = '<?php echo $PHP_SELF."?bssid=".$bssid."&objectid=".$objectid;?>';
	if(deltype == 'BtsMgr')
	{
		if(confirm('Do you want to delete BtsMgr '+btsid+' ?')!=true)
			delcheckbox.checked=false;
		else
			window.location.href = baseHref+'&deltype='+deltype+'&btsid='+btsid;
	}
	if(deltype == 'ALink')
	{
		if(confirm('Do you want to delete ALink '+linkno+' ?')!=true)
			delcheckbox.checked=false;
		else
			window.location.href = baseHref+'&deltype='+deltype+'&linkno='+linkno;
	}
	
}
function delAdj(bssid,objectid,btsid,cellid,adjhand,deltype,delcheckbox)
{
	var delValue,x,y;
	var baseHref;
	x = document.body.scrollLeft;
	y = document.body.scrollTop;
    baseHref = '<?php echo $PHP_SELF."?bssid=".$bssid."&objectid=".$objectid;?>';
	if(deltype == 'AdjHand')
	{
		if(confirm('Do you want to delete AdjHand '+adjhand+' ?')!=true)
			delcheckbox.checked=false;
		else
			window.location.href = baseHref+'&deltype='+deltype+'&cellid='+cellid+'&btsid='+btsid+'&adjhand='+adjhand;
	}
}

function delMsg(bssid,objectid,msgnum,deltp,delcheckbox)
{
	var delValue,x,y;
	var baseHref;
	x = document.body.scrollLeft;
	y = document.body.scrollTop;
    baseHref = '<?php echo $PHP_SELF."?bssid=".$bssid."&objectid=".$objectid;?>';
	if(deltp == 'SmMsg')
	{
	if(confirm('Do you want to delete SmMsg '+msgnum+' ?')!=true)
		delcheckbox.checked=false;
	else
		window.location.href = baseHref+'&msgnum='+msgnum+'&deltp='+deltp;
	}
}
function delCell(bssid,objectid,btsid,cellid,deltype,delcheckbox)
{
	var delValue,x,y;
	var baseHref;
	x = document.body.scrollLeft;
	y = document.body.scrollTop;
    baseHref = '<?php echo $PHP_SELF."?bssid=".$bssid."&objectid=".$objectid;?>';
	if(deltype == 'Bts')
	{
	if(confirm('Do you want to delete Bts '+cellid+' ?')!=true)
		delcheckbox.checked=false;
	else
		window.location.href = baseHref+'&btsid='+btsid+'&cellid='+cellid+'&deltype='+deltype;
	}
}

function AddCell(bssid,objectid,btsid,cellid,addtype)
{
	var delValue,x,y;
	var baseHref;
	x = document.body.scrollLeft;
	y = document.body.scrollTop;

    baseHref = '<?php echo $PHP_SELF ?>'+'?bssid='+bssid+'&objectid='+objectid;
	
	if(addtype == 'Bts')
	{
		if(confirm('Do you want to add Bts '+cellid+' ?')==true)
		{
			window.location.href = baseHref+'&addtype='+addtype+'&btsid='+btsid+'&cellid='+cellid;
		}
	}
}

function addAdj(bssid,objectid,btsid,cellid,adjhand,addtype)
{
	var delValue,x,y;
	var baseHref;
	x = document.body.scrollLeft;
	y = document.body.scrollTop;

    baseHref = '<?php echo $PHP_SELF ?>'+'?bssid='+bssid+'&objectid='+objectid;
	if(addtype == 'AdjHand')
	{
		if(confirm('Do you want to add AdjHand '+adjhand+' ?')==true)
		{
			window.location.href = baseHref+'&addtype='+addtype+'&btsid='+btsid+'&cellid='+cellid+'&adjhand='+adjhand;
		}
	}
}


function addTheObj(bssid,objectid,btsid,adjhand,linkno,addtype)
{
	var delValue,x,y;
	var baseHref;
	x = document.body.scrollLeft;
	y = document.body.scrollTop;

    baseHref = '<?php echo $PHP_SELF ?>'+'?bssid='+bssid+'&objectid='+objectid;
	if(addtype == 'BtsMgr')
	{
		if(confirm('Do you want to add BtsMgr '+btsid+' ?')==true)
		{
			window.location.href = baseHref+'&addtype='+addtype+'&btsid='+btsid;
		}
	}
	if(addtype == 'ALink')
	{
		if(confirm('Do you want to add ALink '+linkno+' ?')==true)
		{
			window.location.href = baseHref+'&addtype='+addtype+'&linkno='+linkno;
		}
	}
	
}
function addMsg(bssid,objectid,msgnum,addtp)
{
	var delValue,x,y;
	var baseHref;
	x = document.body.scrollLeft;
	y = document.body.scrollTop;

    baseHref = '<?php echo $PHP_SELF ?>'+'?bssid='+bssid+'&objectid='+objectid;
	if(addtp == 'SmMsg')
	{
	if(confirm('Do you want to add SmMsg '+msgnum+' ?')==true)
		{
			window.location.href = baseHref+'&msgnum='+msgnum+'&addtp='+addtp;
		}
	}
}

function upload_data(upload,bssid)
{
	window.location.href = '<?php echo $PHP_SELF ?>'+'?bssid='+bssid+'&upload='+upload;
}
function upload_data1(upload,bssid,btsid)
{
	window.location.href = '<?php echo $PHP_SELF ?>'+'?bssid='+bssid+'&btsid='+btsid+'&upload='+upload;
}
function backpage(flag,backurl)
{
	if(flag)
			window.location.href=backurl;
	else
			history.go(-1);
}
</script>

<?php

function putcommand($bssid,$objectid,$nmicmd)
{
	//Lock Tables
	//echo "objectid=$objectid<br>";
	$bssDb = "BssOmcDb";
	$sqlstring = "LOCK TABLES OMCR_COMMAND WRITE";
	$res = mysqli_query($pubConn,$sqlstring) or
          die("Invalid query:$sqlstring\n" . mysqli_error($pubConn));
	$sqlstring = "SELECT row_no FROM OMCR_COMMAND WHERE status=0 " .
             "ORDER BY seqNum ASC LIMIT 10";
			//echo "$sqlstring<BR>";
	$res = mysqli_query($pubConn,$sqlstring) or
          die("Invalid query:$sqlstring\n" . mysqli_error($pubConn));
	while (list($row_no[]) = mysqli_fetch_row($res));
	$no_of_rows = mysqli_num_rows($res);
	mysql_free_result($res);

	if ( $no_of_rows <= 0 || ($no_of_rows > 10) )
	{
    // No available row in OMCR_COMMAND
    // Send error to MySQL()
		header("Location: " .
        "$url?language=$language&bssid=$bssid&objectid=$objectid" .
        "&tablename=$tablename" .
        "&status='{${$parameter}[cmdtablefull]}'" .
        (isset($x)?"&x=$x":"") .
        (isset($y)?"&y=$y":""));
		return;
	}
	// Insert 2 commands in OMCR_COMMAND out of 10 possible available rows
	$i=0;
	do//OidCommand
	{
		if ($row_no[$i] != '')
		{
			$sqlstring = "REPLACE INTO OMCR_COMMAND VALUES(" . $row_no[$i] .
                     ",$bssid,'oid $objectid',null,255,-1,null,null,null)";
			//echo "sql>>>$sqlstring<br>";
			$res       = mysqli_query($pubConn,$sqlstring) or
                     die("Invalid query:$sqlstring\n" . mysqli_error($pubConn));
		}
		$i++;
	} while ($i<10 && mysql_affected_rows() != 2);

	$cmd1_row_no = $row_no[$i-1];

	do//nmicommand
	{
		if ($row_no[$i] != '')
		{
			$sqlstring = "REPLACE INTO OMCR_COMMAND VALUES(" .
                     $row_no[$i] . ",$bssid,'$nmicmd',null,255,-1,null,null,null)";
			//echo "sql>>>$sqlstring<br>";
			$res       = mysqli_query($pubConn,$sqlstring) or
                     die("Invalid query:$sqlstring\n" . mysqli_error($pubConn));
		}
		$i++;
	} while (mysql_affected_rows() != 2 && $i<10);
	$cmd2_row_no = $row_no[$i-1];
	//Unlock Tables
	$sqlstring = "UNLOCK TABLES";
	$res = mysqli_query($pubConn,$sqlstring) or
          die("Invalid query:$sqlstring\n" . mysqli_error($pubConn));
	
	//Waiting for Response
	$timewait=0;
	$timeout =24;
	$interval=500000;   // in micro-seconds
	while ($timewait < 1000000 * $timeout)
	{
		if($nmicmd!="get")
		{
			$cmd = explode(" ",$nmicmd);
		if($timewait==0){
			echo "<br>";
			if($nmicmd=="upload 12"){
				echo "<b>upload data</b>";
			}else if($cmd[0]=="action")
			{
				echo "<b>Doing this action</b>";
			}else{
			echo "<b>$nmicmd</b>";
			}
			echo "<br>";
		}
		echo "<img border=\"0\" src=\"../../images/arrow.gif\" width=\"10\" height=\"10\">";
	    flush();
		}
		$resultcode=$responsecode=$result=$response=null;
		usleep($interval);
		$timewait += $interval;

		$sqlstring = "SELECT t1.resultcode,t1.responsecode," .
                 "t2.engStatInfo,t3.engStatInfo " .
                 "FROM OMCR_COMMAND AS t1,OMCR_STATCODE AS t2,OMCR_VARSTATCODE AS t3 " .
                 "WHERE t2.statcode=t1.resultcode AND t3.statcode=t1.responsecode " .
                 "AND t1.status=0 " .
                 "AND (t1.row_no=$cmd1_row_no OR t1.row_no=$cmd2_row_no) " .
                 "ORDER BY seqNum ASC";

		//echo "$sqlstring<BR>";
		$res       = mysqli_query($pubConn,$sqlstring) or
                 die("Invalid query:$sqlstring\n" . mysqli_error($pubConn));
		while ( $mydata = mysqli_fetch_row($res) )
		{
			list($resultcode[],$responsecode[],$result[],$response[]) = $mydata;
		}
		$no_of_rows = mysqli_num_rows($res);
		//echo "$resultcode[0],$responsecode[0],$result[0],$response[0]<br>";
		//echo "$resultcode[1],$responsecode[1],$result[1],$response[1]<br>";
		//echo "no_of_rows = $no_of_rows<br>";
		mysql_free_result($res);
		if ($no_of_rows == 2)
		{
			
			if ($resultcode[0] != 0)  // 0='ok'
			{
				/*
				echo "<br>";
				echo "<font color=black size=-1><b>BSS and OMC connection broken!</b></font>";
				echo "<br>";
				
				
				echo "<table width=\"100%\"border=\"0\" width=\"100%\" cellpadding=\"1\" cellspacing=\"0\" bordercolor=\"#666666\" bordercolordark=\"#FFFFFF\">";
				echo "<tr>";
					echo "<td width=\"10%\"><a href=\"./bssSpecific.php?bssid=$bssid\"><img align=absBottom border=0 src=\"../../images/left.gif\" width=\"14\" height=\"14\">Back</a></td>";
				echo "</tr>";
				echo "</table>";
				*/
				
				$alarm_info="BSS and OMC connection broken!";
				$back_url="./bssSpecific.php?bssid=$bssid";
				show_alarm($alarm_info,$back_url);
				
				
				
				exit();
			}
			elseif ($resultcode[1] != 0)
			{
				/*
				echo "<br>";
				echo "<font color=black size=-1><b>Imp Command fail!!!</b></font>";
				echo "<br>";	
				
				echo "<table width=\"100%\"border=\"0\" width=\"100%\" cellpadding=\"1\" cellspacing=\"0\" bordercolor=\"#666666\" bordercolordark=\"#FFFFFF\">";
				echo "<tr>";
					echo "<td width=\"10%\"><a href=\"./bssSpecific.php?bssid=$bssid\"><img align=absBottom border=0 src=\"../../images/left.gif\" width=\"14\" height=\"14\">Back</a></td>";
				echo "</tr>";
				echo "</table>";
				*/
				
				$alarm_info="Imp command fail!";
				$back_url="./bssSpecific.php?bssid=$bssid";
				show_alarm($alarm_info,$back_url);
				
				exit();
			}
			return;
		}
		
	}//end while

	// Server or BSC has no response
    // OMCR_COMMAND.status did not change to zero before timeout
    //
    $sqlstring = "SELECT * FROM OMCR_COMMAND " .
                 "WHERE status=254 AND (row_no=$cmd1_row_no OR row_no=$cmd2_row_no) ";
    //echo "$sqlstring<BR>";
    $res = mysqli_query($pubConn,$sqlstring) or
           die("Invalid query:$sqlstring\n" . mysqli_error($pubConn));
	$test = mysqli_fetch_row($res);
	//echo "You are >>> $test";
    if (mysqli_fetch_row($res))
    {
    	/*
		echo "<br>";
        echo "<font color=black size=-1><b>No response from Bsc!!!</b></font>";
		echo "<br>";
		
		echo "<table width=\"100%\"border=\"0\" width=\"100%\" cellpadding=\"1\" cellspacing=\"0\" bordercolor=\"#666666\" bordercolordark=\"#FFFFFF\">";
		echo "<tr>";
			echo "<td width=\"10%\"><a href=\"./bssSpecific.php?bssid=$bssid\"><img align=absBottom border=0 src=\"../../images/left.gif\" width=\"14\" height=\"14\">Back</a></td>";
		echo "</tr>";
		echo "</table>";
		*/
		
		$alarm_info="No response from Bsc!";
		$back_url="./bssSpecific.php?bssid=$bssid";
		show_alarm($alarm_info,$back_url);
		
		exit();
	}
	else
	{
		/*
		echo "<br>";
        echo "<font color=black size=-1><b>No response from BssComm!!!</b></font>";
		echo "<br>";
		
		echo "<table width=\"100%\"border=\"0\" width=\"100%\" cellpadding=\"1\" cellspacing=\"0\" bordercolor=\"#666666\" bordercolordark=\"#FFFFFF\">";
		echo "<tr>";
			echo "<td width=\"10%\"><a href=\"./bssSpecific.php?bssid=$bssid\"><img align=absBottom border=0 src=\"../../images/left.gif\" width=\"14\" height=\"14\">Back</a></td>";
		echo "</tr>";
		echo "</table>";
		*/
		
		$alarm_info="No response from BssComm!";
		$back_url="./bssSpecific.php?bssid=$bssid";
		show_alarm($alarm_info,$back_url);
		
		exit();
		//echo date('h:i:s') . "\n";
		
	}
	$sqlstring = "UPDATE OMCR_COMMAND SET status=0 WHERE row_no=$cmd1_row_no OR row_no=$cmd2_row_no";
    //echo "$sqlstring<BR>";
    $res = mysqli_query($pubConn,$sqlstring) or
           die("Invalid query:$sqlstring\n" . mysqli_error($pubConn));
	
	
}

//==================show the alarm info==========================
function show_alarm($info,$backurl)
{
	echo "\n<script language=\"JavaScript\">\n";
	echo "window.location.href=\"./alarm_show.php?info=$info&backurl=$backurl\";\n";
	echo "</script>\n";
		
}
?>