#!/usr/bin/perl -T
#$Id: plot_values.cgi,v 1.11 2023/02/21 08:46:29 sqko Exp $
use strict;
use warnings;
use CGI qw(:standard :netscape);
use RFC::Main;
use RFC::Rig;
use RFC::RFCCGI qw(:cgi :html);
use RFC::Header;

my $maxrow = 50;
my $r = param('rig');
unless ($r)
{
    print header();
    print "No rig specified!\n";
    exit;
}
my $rig = RFC::Rig->new($r);
my @devicelist = $rig->list_types();
unshift @devicelist,'';
my %h = ();
foreach (@devicelist)
{
    $h{$_} = ucfirst($rig->get_uiname($_));
}

&print_header('Active data plot for rig '.$rig->name,-onload=>'load_start()');
print center(h3("Data plot for rig ".$rig->name));
print '<table cellspacing=5>';
print '<tr height=1><td width=10%>';
print '<td width=20%>';
print '<td width=20%>';
print '<td width=20%>';
print '<td width=20%>';
print '<td width=10%>';
print '</tr>';
print start_form(-name=>'setup');
print_hidden_rig();
for (my $x = 1; $x < 5; $x++)
{
    print "<tr><td></td><th>Type $x:</th><td>";
    print popup_menu(-name=>'type'.$x,-id=>'type'.$x,-values=>[(sort {$a cmp $b} @devicelist)],-labels=>\%h,-class=>'textfield',-onchange=>"change_list(this.value,$x)");
    print "</td><th>Device $x:</th><td>";
    my $type = param('type'.$x);
    my @list = $rig->avaliable_devices($type);
    unshift @list,'';
    print popup_menu(-name=>'dev'.$x,-id=>'dev'.$x,-values=>[(sort {$a cmp $b} @list)],-class=>'textfield',-onchange=>'unset_data()');
    print '</td></tr>';
}
print end_form();
print '</tr><tr><td height=20>';
print '</tr><tr><td colspan=6 align=center id="plot" height=350>';
print '</table>';
print start_form(-name=>'data');
print hidden(-name=>'data',-value=>'',-force=>1);
print hidden(-name=>'pid',-value=>$$,-force=>1);
print end_form();


print <<END;

<style>
body {
   background: #f4f5f6;
}
</style>


<script language="javascript">

var arr = new Array();
var row_max = $maxrow;
var id = 0;
var errstr;

function unset_data()
{
    arr.length = 0;
    document.data.data.value = "";
}

function change_list(type,nr)
{
    id = nr;
    run_ajax('change_config.cgi','&action=list_devices&type='+type,set_list);
}

function set_list(res)
{
    var larr = res.split("\\n");
    if (larr[0] != "OK")
    {
	alert(res);
	return false;
    }
    var element = document.getElementById('dev'+id);
    if (element == null)
    {
	alert("No element found with name 'dev"+id+"'");
	return false;
    }
    if (element.length > 0)
    {
	element.length = 0;
	document.data.data.value = "";
	arr.length = 0;
    }
    var optn = document.createElement("OPTION");
    optn.value = '';
    optn.text = '';
    element.options.add(optn);
    for (x = 1; x < larr.length; x++)
    {
        var optn = document.createElement("OPTION");
        optn.value = larr[x];
        optn.text = larr[x];
        element.options.add(optn);
    }
}



function load_start()
{
    get_data();
}

function set_data(r)
{
    arr.push(r);
    while (arr.length > row_max)
    {
        arr.shift();
    }
    document.data.data.value = arr.join("\\n");
    if (arr.length > 1)
    {
	plot_data();
    }
    setTimeout("get_data()",1000);
}

function get_data()
{
    var devarr = new Array;
    for (x = 1; x < 10; x++)
    {
	var type = document.getElementById('type'+x);
	var name = document.getElementById('dev'+x);
	if (type != null && type != "" && name != null && name != "")
	{
	    var t = type.value;
	    var n = name.value;
	    if (t != "" && n != "")
	    {
		devarr.push(t + '-' + n);
	    }
	}
    }
    if (devarr.length == 0)
    {
	setTimeout("get_data()",1000);
	return;
    }
    var devlst = devarr.join(',');
    run_ajax('change_config.cgi','&action=read_multi&devlist='+devlst,set_data);
}


function set_plot(res)
{
    var out = res.split("\\n");
    if (out[0] != "OK")
    {
	if (errstr != res)
	{
	    alert(res);
	    errstr = res;
	}
        return false;
    }
    var element = document.getElementById('plot');
    element.innerHTML = "<img src='/celltest/png/"+out[1]+"?"+Math.random()+"'>";
}


function plot_data()
{
    var data = document.data.data.value;
    data = data.replace(/\\n/g,"%0A");
    data = data.replace(/\\+/g,"%2B");
    var devarr = new Array;
    var keyarr = new Array;
    var nr = 2;
    for (x = 1; x < 10; x++)
    {
	var type = document.getElementById('type'+x);
	var name = document.getElementById('dev'+x);
	if (type != null && type != "" && name != null && name != "")
	{
	    var t = type.value;
	    var n = name.value;
	    if (t != "" && n != "")
	    {
		devarr.push(nr);
		keyarr.push(n);
		nr++;
	    }
	}
    }
    var rows = devarr.join(',');
    var keys = keyarr.join(',');
    var str = '&action=plot2&data='+data+'&rows='+rows+'&size=1,0.7'+'&keys='+keys+'&pid='+document.data.pid.value;
    run_ajax('ajax_plot.cgi',str,set_plot);
}



</script>


END

    print_end();
