#!/usr/bin/perl

# Takes one reading every 6 minutes (10 per hour)
# provides new graph after every reading
# rotate graph each hour (with current hour showing live readings)
# append records to text log file with date as name, i.e. 20090121.log

use IO::Socket;

if ($#ARGV == -1 ) {
  print STDERR "Usage: $0 <-iSQM_LE_ADDR> [options]\n";
  print STDERR "  Options: [-pSQM_LE_PORT] default 10001\n";
  print STDERR "           [-nN_READINGS]  default 0=endless\n";
  print STDERR "           [-sSLEEPTIME]   \n";
  print STDERR "           [-vLEVEL] level of verbosity in log\n\n";
  print STDERR "                     default = Time mpsas temperature\n\n";
  print STDERR "                     2 = Time period temperature\n\n";
  print STDERR "  Example:\n";
  print STDERR "       $0 -i192.168.1.101 10001 10 \n";
  exit;
} else {

  foreach (@ARGV) {
     if(substr($_, 0, 2) eq "-i") {
       $sqm_le_addr=substr($_, 2)
     }

     if(substr($_, 0, 2) eq "-p") {
       $sqm_le_port=substr($_, 2)
     }

     if(substr($_, 0, 2) eq "-n") {
       $n_readings=substr($_, 2)
     }

     if(substr($_, 0, 2) eq "-s") {
       $sleep_sec=substr($_, 2);
       if ($sleep_sec<=1) {
         print STDERR "The sleep time must be greater than 1 second\n";
         exit;
       }
     }

     if(substr($_, 0, 2) eq "-l") {
       $logfile=substr($_, 2);
     }

     if(substr($_, 0, 2) eq "-v") {
       $verbosity=substr($_, 2)
     }

  }
}

if (! defined($sqm_le_addr)) {
  print STDERR "The address or IP of the SQM-LE is required\n";
  exit;
}

if (! defined($sqm_le_port)) {
  print STDERR "Port number not provided so 10001 assumed\n";
  $sqm_le_port = 10001;
}

if (! defined($n_readings)) {
  print STDERR "Number of readings not provided, so endless assumed\n";
  $n_readings = 0;
}

if (! defined($sleep_sec)) {
  $sleep_sec = 360;
  print STDERR "Sleep time not provided, so $sleep_sec seconds assumed\n";
}

$count=0;
while ($count<=$n_readings) {

  if ($n_readings>0){
    $count++
  }

  #-- Initialize response string
  $str="";

  # open socket
  $remote = IO::Socket::INET->new(PeerAddr => $sqm_le_addr,
 	                        PeerPort => $sqm_le_port,
                                Proto    => 'tcp') 
    || die("Cannot connect to port $sqm_le_port on $sqm_le_addr:$!");

  $remote->autoflush(1);
#   print STDERR "[Connected to $sqm_le_addr:$sqm_le_port]\n";

  #-- Send request to remote SQM
  print $remote "rx";


  #-- Add response to string
  $str .= <$remote>;

  #-- If newline, print string and start next count
  if ($str =~ /\n/) {
    print $str;
    #The string looks something like this, second and third lines are just a ruler:
    #  r,-02.21m,0000000007Hz,0000059399c,0000000.129s, 037.0C
    #            1         2         3         4         5
    #  0123456789 123456789 123456789 123456789 123456789 1234

    $mpsas=substr($str,2,6);
    $frequency=substr($str,10,10);
    $counts=substr($str,23,10);
    $period=substr($str,35,11);
    $temperature=substr($str,48,6);

    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
    
    $logfile =sprintf("%04d%02d%02d.log",$year+1900,$mon+1,$mday);
    open  LOGFILE , ">>./".$logfile;

    if ($verbosity==2){
      printf LOGFILE  "%02d:%02d:%02d %s %s\n",$hour,$min,$sec,$period,$temperature;
    }else{
      printf LOGFILE  "%02d:%02d:%02d %sm %sC\n",$hour,$min,$sec,$mpsas,$temperature;
    }
    close LOGFILE;

    #-- Close remote socket
    $remote->close;

    #-- Delay between readings
    sleep($sleep_sec);
  }
}



