#!/usr/bin/perl
#$Id: fastlog.pl,v 1.1 2023/11/29 07:05:27 sqko Exp $
###########################################################################################
#
# Usage: ./fastlog.pl $rig --time=$minutes --sleep=$seconds [device_type device_name]...
#
# Ex: ./fastlog.pl 71 --time=1 simplechannel Sample_1 --sleep=5 simplechannel Sample_2
#
###########################################################################################
use strict;
use warnings;
use RFC::Rig;
use RFC::Cache;
my $rig = RFC::Rig->new(shift);
$rig->check_run();
my $date = localtime();
$date =~ s/\s/_/g;
my $fn = 'Fastlog_'.$date.'.txt';
my $file = $rig->webdir().$fn;
my @devlist = ();
my $end_time = 0;
my $sleep = 1;
for (my $x = 0; $x < @ARGV; $x++)
{
    my $str = $ARGV[$x];
    if ($str =~ /--time=(\d+)/)
    {
	$end_time = $1;
    }
    if ($str =~ /--sleep=(\d+)/)
    {
	$sleep = $1;
    }
    next if ($str =~ /--\w+/);
    my $type = $ARGV[$x];
    $x++;
    my $name = $ARGV[$x];
    next unless ($type && $name);
    my $d = $rig->get_device($type,$name);
    push @devlist,$d if ($d);
}
exit unless (@devlist);
$sleep = 1 if ($sleep < 1);
my $end = undef;
$end = time + $end_time * 60 if ($end_time);
open MYOUT,'>',$file or die "Could not open file $file for writing: $!\n";
while (!$end || time < $end)
{
    my $data = RFC::Cache->new();
    my @data = ();
    foreach my $d (@devlist)
    {
	push @data,$d->readstring($data);
    }
    print MYOUT join(' ',@data),"\n";
    sleep($sleep);
}
close MYOUT;
