#!/usr/bin/perl -w
use strict;
use LWP::UserAgent;
use Net::HTTP;
use HTTP::Request::Common qw(POST);
use Mysql;
use Socket;

my $SAID 	 = shift || 0;
my $BID		 = shift || 0;
my $N		 = shift || 100;

my $opt_user 	 = "satis";
my $opt_password = "satis";
my $database 	 = "satis";

my $conf="/usr/local/controller/scripts/scripts.conf";
open(CONF,$conf) or die "Can't open config file: $!";
my $confparam="";
my $confvalue;

while(<CONF>)
{
    chomp();
    ($confparam,$confvalue)=split(/=/);
    if(defined($confvalue))
    {
        if($confparam eq "dbuser") 
	{
	    $opt_user=$confvalue;
	}
        elsif($confparam eq "dbpass") 
	{
	    $opt_password=$confvalue;
	}
        elsif($confparam eq "db") 
	{
	    $database=$confvalue;
	}
	else {}
    }
    else 
    {
        print "BAND> WARNING: split didn't find '=' in line\n";
    }
}
close(CONF);

my $line;
my $st;
my $sth;
my $query;

my $dbh = Mysql->connect("", "$database", "$opt_user", "$opt_password") ||
          die "Can't connect\n";

my $ua= LWP::UserAgent->new;
$ua->agent("Spectrum");

#--- Run BAND scanning ---
print "BAND> Starting scanning band: ID = $BID\n";

my $RFportID = 0;
my $request = "";
my $realMinFr = 0;
my $realMaxFr = 0;
my $stepfr = 0;
my $rf = 0;
my $cf = 0;
my $isInverted = 0;
my $aqFreq = 0;
my $measureType = 0; # monitoring

#-- Get band parameters
$query = "SELECT Name, Value FROM SystemConf WHERE NodeID=$BID;";
$st = Query $dbh $query or die "$Mysql::db_errstr";
while($line = $st->fetchrow_arrayref)
{	
    my $Name  = "$$line[0]";	
    my $Value = "$$line[1]";
    my $isAdd = 1;
    
    if($Name eq "minfr")
    {
	$isAdd = 0;
        $realMinFr = $Value;
    }	
    if($Name eq "maxfr")
    {
	$isAdd = 0;
	$realMaxFr = $Value;
    }
    if($Name eq "stepfr")
    {
        $isAdd = 0;
        $stepfr = $Value;
    }
    if($Name eq "cmid")
    {
        $isAdd = 0;
        if($Value > 0) {$RFportID = $Value;}
    }
    if($Name eq "rf")
    {
        $isAdd = 0;
        $rf = $Value;
    }
    if($Name eq "cf")
    {
        $isAdd = 0;
        $cf = $Value;
    }
    if($Name eq "isinverted")
    {
        $isAdd = 1;
        $isInverted = $Value; # 0=>TRUE
    }
    if($Name eq "aqfrequency")
    {
        $isAdd = 0;
        $aqFreq = $Value;
    }
	    
    if($isAdd==1)
    {
        $request = "$request" . "$Name=$Value&";
    }
}

print "BAND> Start scan: ID = $BID\n";
system "./scanDiapason.pl", 
	    "$SAID", "$RFportID", "$BID", 
	    "$realMinFr", "$realMaxFr", "$stepfr", "$N", 
	    "$cf", "$rf", "$isInverted", "$aqFreq", 
	    "$measureType", "$request";
print "BAND> End scan: ID = $BID\n";

undef $st;
undef $sth;
undef $dbh;