#!/usr/bin/perl -wU

use strict;
use IO::Handle;
use Switch;
use bytes;


my $log = "ups_log";
my $serial_line = "/dev/ttyUSB0";
my $verbose_option = "V";
my $tty_cmd = "stty -F " . $serial_line . " 9600 cs8 ignpar -echo -echok -icrnl -ixon -ixoff ixany -icanon -opost -onlcr time 12 min 0 ";
my @battery_status = ("Good","Weak","REPLACE");

open( UPSLOG, ">>",$log) || die( "open error $log $?");
open( TTY,"+<",$serial_line ) || die( "open serial line error: $serial_line $?");

autoflush TTY;
autoflush UPSLOG;

my @args = ( "$tty_cmd"  );
#system(@args) || die ("stty cmd failed for $serial_line $?");

logmsg ("$0 started.");

my $response = sendsw("^P003MAN");
print "UPS Manufacturer:$response\n";
$response = sendsw("^P003MOD");
print "UPS Model:$response\n";
$response = sendsw("^P003NOM");
my @voltage = split(/,/,$response);
print "Input Voltage:$voltage[0]\n";
print "Input Freq:$voltage[1]\n";
print "Output Voltage:$voltage[2]\n";
print "Output Freq:$voltage[3]\n";
print "VA Rating:$voltage[4]\n";
print "Output Pwr:$voltage[5]\n";
print "Battery Time:$voltage[6]\n";
print "Audible Alarm:$voltage[7]\n";
print "Low Voltage xfer:$voltage[8]\n";
print "High Voltage xfer:$voltage[9]\n";
print "Battery Installed:$voltage[10]\n";
print "Battery Life:$voltage[11]\n";
print "------------\n";
print "UPS BATTERY STATUS:\n";
$response = sendsw("^P003ST1");
my @batterystatus = split(/,/,$response);
print "Battery Condition:$batterystatus[0] $battery_status[$batterystatus[0]]\n";
print "Battery Status:$batterystatus[1] $battery_status[$batterystatus[1]]\n";
print "Charge:$batterystatus[2]\n";
print "Seconds on Battery:$batterystatus[3]\n";
print "Estimated Minutes:$batterystatus[4]\n";
print "Estimated Charge:$batterystatus[5]\n";
print "Voltage:$batterystatus[6]\n";
print "Current:$batterystatus[7]\n" if ( defined $batterystatus[7]);
print "Temperature:$batterystatus[8]\n"if ( defined$batterystatus[8]);


print "UPS INPUT STATUS:\n";
$response = sendsw("^P003ST2");
my @inputstatus = split(/,/,$response);
print "Line Bads:$inputstatus[0]\n";
print "Num Lines:$inputstatus[1]\n";
print "Freq:$inputstatus[2]\n";
print "Voltage:$inputstatus[3]\n";

print "UPS ALARMS:\n";
$response = sendsw("^P003ST2");
my @alarmstatus = split(/,/,$response);

logmsg ("$0 ended.");

#----------------------------------------------------------


sub logmsg {
    my ( $logmessage ) = @_;
    my $timestamp = localtime;
    print UPSLOG "$timestamp\t$logmessage\n";
}

    
sub sendsw {
    my ( $sendmsg ) = @_;
    my $timeout = 0;
    my $result = "";
    print TTY "$sendmsg"; # do not send c/r, each command has perscribed length
    logmsg( "send:$sendmsg");

WAITLOOP:
    while ( $timeout < 5 ) {
        $timeout++;
	sleep(1);
        while (  ) {
            $result = $_;
            logmsg( "recv: $_");
            last;
        }
	last if ( length( $result ) > 0 );
    }
    return "TIMEOUT" if $timeout >= 5;
    if ( $result =~ /^\^D/ ) {
       $result = substr( $result,5);
    }
    return $result;
}