#!/usr/bin/perl -T
use strict;
use warnings;
use RFC::Rig;
use RFC::RFCCGI qw(:cgi :html);
use CGI qw(:standard :netscape);
my $rig = RFC::Rig->new(param('rig'));
my $type = xss(param('type'));
my $device = xss(param('device'));
my $dev = $rig->init_device($type,$device);
&print_header($dev->name." on rig ".$rig->name,-onload=>'load_start()',-onresize=>"set_size()");
print RFC_start_html(-title=>" Device monitor ", -style=>{src=>'/celltest/RFCcontrol7.css'});
my $rows = $dev->monitor_rows();
my $maxrow = 18;

print '<center>',"\n";

print start_form(-name=>'test');
print popup_menu(-name=>'select',-values=>['data','plot'],-default=>'data',-force=>1,-onchange=>'set_mode(this.value)');
print '<br>';
print '<div id=plot style="display:none;margin:auto;"><img id=pngplot src=""></img></div>';
print textarea(-name=>'data',-id=>'data',-value=>'',-force=>1,-rows=>$maxrow,-style=>'width:400px;');
print end_form();
print "</center>\n";

my $user = get_user();
print <<END;

<script language="javascript">

var row_max = $maxrow;
var doplot = false;
var type = "$type";
var device = "$device";
var arr = new Array();
var size = '0.6,0.6';
var is_logged_in = true;
var username = '$user';
var w = 400;
var h = 350;

function set_size()
{
    w = window.innerWidth;
    h = window.innerHeight;
    size = (0.6 * w / 420) + ',' + (0.6 * h / 350);
    if (w > 400)
    {
	row_max = $maxrow * w / 400;
    }
    else
    {
	row_max = $maxrow;
    }
}

function check_status()
{
    raw_ajax('ajax.cgi','?action=is_logged_in&name='+username,set_status);
}

function set_status(res)
{
    var reg = /YES/;
    if (!reg.test(res))
    {   
        is_logged_in = false;
    }
}


function load_start()
{
    get_data();
}

function set_data(r)
{
    var reg = /Session\\w+timeout/;
    if (reg.test(r))
    {
	self.close();
    }
    arr.push(r);
    while (arr.length > row_max)
    {
	arr.shift();
    }
    document.test.data.value = arr.join("\\n");
    setTimeout("get_data()",500);
}

function get_data()
{
    var typename = type;
    var dev = device;
    if (is_logged_in)
    {
        check_status();
	run_ajax('change_config.cgi','&action=read_device&type='+typename+'&device='+dev,set_data);
    }
}

function set_mode(m)
{
    var d = document.getElementById('data');
    if (m == 'plot')
    {
        d.style.display = 'none';
	doplot = true;
        var element = document.getElementById('plot');
	element.style.display = 'block';
	plot_data();
    }
    else
    {
	var element = document.getElementById('plot');
        d.style.display = 'block';
	doplot = false;
	element.style.display = 'none';
    }
}

function set_plot(res)
{
    if (doplot == false)
    {
	return;
    }
    var out = res.split("\\n");
    if (out[0] != "OK")
    {
        alert(res);
        return false;
    }
    var element = document.getElementById('pngplot');
    element.src = "/celltest/png/"+out[1]+"?"+Math.random();
    if (doplot)
    {
	setTimeout("plot_data()",500);	
    }
}

function plot_data()
{
    if (is_logged_in)
    {
        check_status();
	var data = document.test.data.value;
	data = data.replace(/\\n/g,"%0A");
	data = data.replace(/\\+/g,"%2B");
	run_ajax('ajax_plot.cgi','&action=plot&data='+data+'&rows=$rows&size='+size,set_plot);
    }
}




</script>

END

print end_html;

