#!/usr/bin/perl -T
# $Id: show_rig_values.cgi,v 1.3 2014/06/25 08:39:27 sqko Exp $
############################################################
############################################################
###                                                      ###
###   Originally designed by Soeren Koch                 ###
###                                                      ###
###   Impelemented by Soeren Koch                        ###
###                                                      ###
###   Copyright by Soeren Koch January 2011              ###
###                                                      ###
############################################################
############################################################

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 $acmode = get_cv('global','access_mode');
$acmode = 'celltest' unless ($acmode);
my $r = param('rig');
unless ($r)
{
    print header();
    print "No rig specified!\n";
    exit;
}
my $rig = RFC::Rig->new($r);

if (!defined(param))
{
    &print_header("Please log in to the $name");
    print '<center>';
    RFC::RFCCGI::login();
    print '</center>';
    &print_end;
}
else
{
    #### husk at aendre accessparameter til det relevante for scriptet!! ####
    if (&login_ok())
    { 
        &print_header("Current values for rig ".$rig->name);
        print RFC_start_html(-title=>"Current values for rig ".$rig->name, -style=>{src=>'/celltest/RFCcontrol.css'});
	top_nav_bar_start;
	top_no_button;
	top_no_button;
	top_no_button;
	tab_newrow;
	&js_back();
	top_js_return('#006600','white');
	top_no_button;
	top_nav_bar_button('show_rig_values.cgi','','','Show current values','s2','rig,'.$rig->name.',1');
        top_nav_bar_button('rig_main.cgi','','','To rig'.$rig->name.' main page','s3','rig,'.$rig->name.',1');
	top_nav_bar_button('main.cgi','','','To menu','s4');
	end_top_bar;
	&insert_data_form;
	print_end;
    }
    else 
    {
	&not_auth;
    }
}
    
exit;



sub insert_data_form
{
    print center(h1("Current values for rig ".$rig->name));
    my $res = CGI_client($rig->name,'measure');
    &print_error() unless (defined($res));
    chomp $res;
    chomp $res;
    my %h = &split_data($res);
    print '<center><table border=0 cellspacing=10 cellpadding=5>';
    my $count = 1;
    print '<tr>';
    for my $key (sort keys %h)
    {

	print '<th>';
	print xss($key);
	print '</th><th>';
	print xss($h{$key});
	print '</th>';
	print '</tr><tr>' unless ($count++%3);
    }
    print '</tr>';
    print '</table></center>';
}    



sub split_data($)
{
    my $data = shift;
    return undef unless ($data);
    my @l = split(/\s+/,$data);
    my %hash = ();
    for (my $x = 0; $x < @l; $x+=4)
    {
	$hash{$l[$x]} = $l[$x+3] if defined($l[$x+3]);
    }
    return %hash;
}

