#!/usr/bin/perl
#
# simfaults.pl - Steven Lerner 6/2005
#                Woods Hole Oceanographic Institution
#
#       Description: Provides an interactive menu for toggling on/off
#                    DCS components via mars power controller serial port (obc)
#
#       Assumptions: Reguires that the serial connection to power ctrl
#                    is already logged-in (ie; open minicom session,
#                    log-in, and then quit w/o reset via ctl-a q)
#                    
#       Usage: simfaults
#
#       Output: writes log to Data/date.pwron.dat
#
# History:
#       Date       Who    Description
#       ----       ---    ---------------------------------------------------
#      06/05/2005  SL     Create grom get_power_data.pl
#################################################################################
require "getopts.pl";
require "flush.pl";
require "ctime.pl";
require "timelocal.pl";

$VERSION = "v1.0";
$DEBUG = 0;

#parameters
$port = "/dev/ttyS0"; #Need to make sure comm params already setup
                      #and for now, pwr-ctl needs to be logged-in
#$port = "/dev/null";

#Set labels
$load_label[3]="Cisco MRA01-01";
$load_label[4]="Cisco MRA01-02";
$load_label[5]="OE West 75";
$load_label[6]="Cyclades";
$load_label[7]="OE East 75";
$load_label[8]="OE East 98";
$load_label[9]="OE West 98";
$load_label[10]="West NE Optical Switch";
$load_label[11]="West EDFA";
$load_label[13]="East NE Optical Switch";
$load_label[14]="East EDFA";

#Open simfault log
open(LOG,">>Data/simflt.log") || die "Unable to open log file: Data/simflt.log\n";
print LOG "\n\n$timestamp ============= Simulate MARS DCS Faults =============\n";

TOP_MENU:
print <<"EndMenu";


    0.   Text Entry
    1.   OE East 75
    2.   OE East 98
    3.   East EDFA
    4.   East NE Optical Switch
    5.   OE West 75
    6.   OE West 98
    7.   West EDFA
    8.   West NE Optical Switch
    9.   Cyclades
    10.  Cicso MRA01-01
    11.  Cicso MRA01-02
    12.  Cycle through ALL components
    99.  Exit

EndMenu
print "Enter Choice: ";$choice=<STDIN>; chomp($choice);
if ($choice == 99)
   {$timestamp = gmtimestamp(1);
    print "    $timestamp  ----- End of Tests -----\n";
    print LOG "$timestamp  ----- End of Tests -----\n";
    close(LOG);
    exit;
    }

if ($choice < 0 || $choice > 12) 
  {print "***Invalid choice \n"; goto TOP_MENU;}

if ($choice == 0)
   {print "Text: ";$comment=<STDIN>; chomp($comment);
    $timestamp = gmtimestamp(1); if ($comment eq "") {$comment = "<blank>";}
    print "Logged: $timestamp TXT $comment";
    print LOG "$timestamp TXT $comment\n";
    goto TOP_MENU;
    }
    
elsif ($choice <= 11)
   {#setup DCS load mappings
    if    ($choice == 1)  {$load = 7; }
    elsif ($choice == 2)  {$load = 8; }
    elsif ($choice == 3)  {$load = 14;}
    elsif ($choice == 4)  {$load = 13;}
    elsif ($choice == 5)  {$load = 5; }
    elsif ($choice == 6)  {$load = 9; }
    elsif ($choice == 7)  {$load = 11;}
    elsif ($choice == 8)  {$load = 10;}
    elsif ($choice == 9)  {$load = 6; }
    elsif ($choice == 10) {$load = 3; }
    elsif ($choice == 11) {$load = 4; }


    print "Turn OFF $load_label[$load] [load $load] - press <cr>: ";$ret=<STDIN>;
    #Stop get_power_data.pl script to avoid serial port conflict
    `killall get_power_data.pl`;
    #open com port
    open(COM,"+<$port") || die "Unable to open serial port: $port\n";
    #setup auto-flush
    select((select($port), $| = 1)[0]);
    #set Load off
    $timestamp = gmtimestamp(1);
    print     "$timestamp set $load_label[$load] load$load OFF";
    print LOG "$timestamp set $load_label[$load] load$load OFF\n";
    $cmd = "sets $load 0\n";
    print "Sending $cmd to $port\n" if $DEBUG;
    print COM "$cmd"; flush(COM);
    while ($line = <COM>) {last if ($line =~ /load/);}
    close(COM);
    #restart get_power_data script
    system("./get_power_data.pl&");


    print "\n\nTurn ON $load_label[$load] [load $load] - press <cr>: ";$ret=<STDIN>;
    #Stop get_power_data.pl script to avoid serial port conflict
    `killall get_power_data.pl`;
    #open com port
    open(COM,"+<$port") || die "Unable to open serial port: $port\n";
    #setup auto-flush
    select((select($port), $| = 1)[0]);
    #set Load on
    $timestamp = gmtimestamp(1);
    print     "$timestamp set $load_label[$load] load$load ON";
    print LOG "$timestamp set $load_label[$load] load$load ON\n";
    $cmd = "sets $load 1\n";
    print "Sending $cmd to $port\n" if $DEBUG;
    print COM "$cmd"; flush(COM);
    while ($line = <COM>) {last if ($line =~ /load/);}
    close(COM);
    #restart get_power_data script
    system("./get_power_data.pl&");
    goto TOP_MENU;
    }
    

elsif ($choice == 12)
   {###Hacked - duplicate code above and cycle through each choice as
    ###"entered" by user
   for ($choice=1;$choice<=11;$choice++) {
      #setup DCS load mappings
    if    ($choice == 1)  {$load = 7; }
    elsif ($choice == 2)  {$load = 8; }
    elsif ($choice == 3)  {$load = 14;}
    elsif ($choice == 4)  {$load = 13;}
    elsif ($choice == 5)  {$load = 5; }
    elsif ($choice == 6)  {$load = 9; }
    elsif ($choice == 7)  {$load = 11;}
    elsif ($choice == 8)  {$load = 10;}
    elsif ($choice == 9)  {$load = 6; }
    elsif ($choice == 10) {$load = 3; }
    elsif ($choice == 11) {$load = 4; }


    print "Turn OFF $load_label[$load] [load $load] - press <cr>: ";$ret=<STDIN>;
    #Stop get_power_data.pl script to avoid serial port conflict
    `killall get_power_data.pl`;
    #open com port
    open(COM,"+<$port") || die "Unable to open serial port: $port\n";
    #setup auto-flush
    select((select($port), $| = 1)[0]);
    #set Load off
    $timestamp = gmtimestamp(1);
    print     "$timestamp set $load_label[$load] load$load OFF";
    print LOG "$timestamp set $load_label[$load] load$load OFF\n";
    $cmd = "sets $load 0\n";
    print "Sending $cmd to $port\n" if $DEBUG;
    print COM "$cmd"; flush(COM);
    while ($line = <COM>) {last if ($line =~ /load/);}
    close(COM);
    #restart get_power_data script
    system("./get_power_data.pl&");


    print "\n\nTurn ON $load_label[$load] [load $load] - press <cr>: ";$ret=<STDIN>;
    #Stop get_power_data.pl script to avoid serial port conflict
    `killall get_power_data.pl`;
    #open com port
    open(COM,"+<$port") || die "Unable to open serial port: $port\n";
    #setup auto-flush
    select((select($port), $| = 1)[0]);
    #set Load on
    $timestamp = gmtimestamp(1);
    print     "$timestamp set $load_label[$load] load$load ON";
    print LOG "$timestamp set $load_label[$load] load$load ON\n";
    $cmd = "sets $load 1\n";
    print "Sending $cmd to $port\n" if $DEBUG;
    print COM "$cmd"; flush(COM);
    while ($line = <COM>) {last if ($line =~ /load/);}
    close(COM);
    #restart get_power_data script
    system("./get_power_data.pl&");
    
    print "\n\n";
    } #end foreach choice
} 


exit;

#
#Subroutines Follow
#
sub gmtimestamp {local($no_gmtlabel) = $_[0];
        local($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime;
        # zero fill hour,min,sec and month
        if ($sec < 10) {$sec = "0$sec";}
        if ($min < 10) {$min = "0$min";}
        if ($hour < 10) {$hour = "0$hour";}
        $mon += 1; #from gmtime defined 0-11, make it 1-12
        if ($mon < 10) {$mon = "0$mon";}
        if ($mday < 10) {$mday = "0$mday";}
        #make year 4-digits - Note: Year is years since 1900
        $year += 1900;
        if ($no_gmtlabel)
             {return "$year/$mon/$mday $hour:$min:$sec";}
        else {return "$year/$mon/$mday $hour:$min:$sec GMT";}
}




sub gmtimestamp_file {
        local($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime;
        # zero fill hour,min,sec and month
        if ($sec < 10) {$sec = "0$sec";}
        if ($min < 10) {$min = "0$min";}
        if ($hour < 10) {$hour = "0$hour";}
        $mon += 1; #from gmtime defined 0-11, make it 1-12
        if ($mon < 10) {$mon = "0$mon";}
        if ($mday < 10) {$mday = "0$mday";}
        #make year 4-digits - Note: Year is years since 1900
        $year += 1900;
        return "$year$mon$mday";
}

