#!/usr/bin/perl -T
# $Id: iv_setup.cgi,v 1.33 2023/10/09 07:50:05 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;
use SemaforeFile;


my $ref = &RFC::RFCCGI::get_user_data_ref();
my $uid = $$ref{'uid'};
my $r = param('rig');
unless ($r)
{
    print header();
    print "No rig specified!\n";
    exit;
}
my $rig = RFC::Rig->new($r);


if (!defined(param))
{
    print process('to_login.tt');
}
else
{
    if ($uid)
    {
        print process('main_nav.tt');
        $_ = param('action');
      CASE:
        {
            ### Insert case statements ###
	    /^delete$/ and do {&delete(); &calibration; last CASE;};
	    /^save$/ and do {&save(); &calibration; last CASE;};
	    /^calibration/ and do {&calibration; last CASE;};
	    /^Set values$/ and do {&save; last CASE;};
	    ### Default response ####
    	    &insert_data_form;
        }
        print process('end_html.tt');
    }
    else 
    {
        print process('not_auth.tt');
    }
}
exit;


sub delete()
{
    return unless (is_admin());
    my $filename = get_CGI_value('filelist');
    if ($filename =~ /^([\w\.\-]+)$/)
    {
	my $fn = $1;
	eval {
	    my $f1 = $homedir.'convert-tables/userfiles/'.$fn;
	    unlink($f1) if (-e $f1);
	    my $f2 = $homedir.'convert-tables/mv_to_temp/'.$fn;
	    unlink($f1) if (-e $f1);
	    my $f3 = $homedir.'convert-tables/temp_to_mv/'.$fn;
	    unlink($f1) if (-e $f1);	
	};
	if ($@)
	{
	    print h1({-class=>'Alert'},$@);
	}
    }
}


sub save()
{
    my $filename = get_CGI_value('filename');
    if ($filename =~ /^([\w\.\-]+)$/)
    {
	my $fn = $homedir.'convert-tables/userfiles/'.$1;
	my $file = SemaforeFile->new($fn);
	$file->writeline(get_CGI_value('new_file'));
    }
}

sub calibration()
{
    my @list = RFC::Main::spline_user_files();
    my %data = (
	'files' => [@list],
	);
    print process('thermo.tt',&get_template_data_ref(\%data));
}



sub insert_data_form
{
    my $clabel = $rig->get_cv('IV_control','current_label');
    my $vlabel = $rig->get_cv('IV_control','voltage_label');
    my $tlabel = $rig->get_cv('IV_control','temp_label');
    my $psulabel = $rig->get_cv('IV_control','iv_control_names');
    $psulabel = '' unless ($psulabel);
    my %plabels = ();
    foreach (split('\s*\,\s*',$psulabel))
    {
	$plabels{$_} = 1;
    }
    my @psulabels = $rig->list_devices('PSU');
    my @clabels = $rig->list_devices('simplechannel');
    my @vlabels = $rig->list_devices('simplechannel');
    my @tlabels = $rig->list_devices('templog');
    my $step1 = $rig->get_cv('IV_control','currentstep1');
    my $step2 = $rig->get_cv('IV_control','currentstep2');
    my $step3 = $rig->get_cv('IV_control','currentstep3');
    my $climit = $rig->get_cv('IV_control','currentlimit');
    my $vlimit = $rig->get_cv('IV_control','voltagelimit_iv');
    my $elimit = $rig->get_cv('IV_control','electrolysis_limit_voltage');
    my $pause = $rig->get_cv('IV_control','ivpause');
    my $pvhOCV = $rig->get_cv('IV_control','OCV_measure_number');
    my $errorlimit = $rig->get_cv('IV_control','diff_current_limit');
    my $mincurrent = get_cv('iVcurves','minimum_current_step');
    my $cache = $rig->get_cv('IV_control','allow_caching');
    $cache = 'No' unless ($cache);
    my @psus = $rig->list_devices_control('PSU');
    $mincurrent = 0.05 unless ($mincurrent);
    my $miniv = get_cv('iVcurves','minimum_cell_voltage_for_iv');
    my $maxiv = get_cv('iVcurves','maximum_cell_voltage_for_iv');
    $maxiv = 2000 unless ($maxiv);
    $miniv = 0 unless ($miniv);
    $maxiv -= 25;
    $miniv += 25;
    my $maxi = get_cv('iVcurves','current_limit');
    $maxi = 40 unless (defined($maxi));
    $maxi = 0.05 unless ($maxi > 0.05);
    my $mini = 0.05;
    my @list = $rig->list_devices_control('PSU');
    foreach (@list)
    {
	eval {
	    my $d = $rig->get_device('PSU',$_);
	    $maxi = $d->maxcurrent() if ($d->maxcurrent < $maxi);
	    $miniv = $d->minvoltage()*1000 if ($d->minvoltage() * 1000 > $miniv);
	    $maxiv = $d->maxvoltage()*1000 if ($d->maxvoltage() * 1000 < $maxiv);
	};
	if ($@)
	{
	    &errorlog($@);
	}
    }
    my %data = (
	'current_label' => $clabel,
	'voltage_label' => $vlabel,
	'temp_label' => $tlabel,
	'iv_control_names' => \%plabels,
	'psulabels' => [@psulabels],
	'clabels' => [@clabels],
	'vlabels' => [@vlabels],
	'tlabels' => [@tlabels],
	'currentstep1' => $step1,
	'currentstep2' => $step2,
	'currentstep3' => $step3,
	'currentlmit' => $climit,
	'voltage_limit_iv' => $vlimit,
	'electrolysis_lmit_voltage' => $elimit,
	'ivpause' => $pause,
	'OCV_measure_number' => $pvhOCV,
	'diff_current_limit' => $errorlimit,
	'minimum_current_step' => $mincurrent,
	'allow_caching' => $cache,
	'psus' => [@psus],
	'maxi' => $maxi,
	'miniv' => $miniv,
	'maxiv' => $maxiv,
	);

    print process('iv_setup.tt',&get_template_data_ref(\%data));
}    

