#!/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;

my $MAX_ANS_LEN  = 16384;   # 16 Kb
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
    {
	print "WARNING: split didn't find '=' in line\n";
    }
}
close(CONF);

my $line;
my $NumOfBands = 0;

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 "MONITOR> Starting monitoring bands for SAID $SAID\n";

#--- Get all bands for current Spectrum Analyzer
my $query = "SELECT sc1.NodeID FROM SystemConf sc1 JOIN SystemConf sc2 WHERE sc1.NodeID=sc2.NodeID AND sc1.Name='said' AND sc1.Value=$SAID AND sc2.Name='Active' AND sc2.Value=0";
my $band_st = Query $dbh $query or die "$Mysql::db_errstr";
while ($line = $band_st->fetchrow_arrayref)
{
    my $BID = $$line[0];
    printf "MONITOR> Start band: BandID=$BID, SAID=$SAID\n";
    system "./scanBand.pl", 
		"$SAID", "$BID";
    printf "MONITOR> End band\n";
    $NumOfBands++;
}

if($NumOfBands==0)
{
    print "MONITOR> Number of bands is ZERO\n";
    sleep(10);
}

undef $band_st;
undef $dbh;
print "MONITOR> END\n";
