#!/usr/bin/perl

# ABSTRACT: simple example script to read from a Current Cost monitor
# PODNAME: current-cost-reader


use warnings;
use strict;
use Device::CurrentCost::Constants;
use Device::CurrentCost;
use Getopt::Long;
use Pod::Usage;
use List::Util qw/max/;

my $help;
my $man;
my $verbose = 0;
my $history = 0;
my %args;
GetOptions('help|?' => \$help,
           'man' => \$man,
           'verbose+' => \$verbose,
           'history+' => \$history,
           'classic' => sub { $args{type} = CURRENT_COST_CLASSIC },
          ) or pod2usage(2);
pod2usage(1) if ($help);
pod2usage(-exitstatus => 0, -verbose => 2) if $man;
my $device = shift || pod2usage({ -message => 'A device parameter is required',
                                  -exitval => 2 });

$args{history_callback} = \&history_callback if ($history);
my $cc = Device::CurrentCost->new(device => $device, %args);

$|=1; # don't buffer output

while (1) {
  my $msg = $cc->read() or next;
  if ($verbose == 0) {
    $msg->has_readings or next;
    print $msg->value, ' ', $msg->units, "\n";
  } elsif ($verbose == 1) {
    $msg->has_readings or next;
    print 'Total: ', $msg->value, ' ', $msg->units, "\n";
    foreach my $phase (1..3) {
      print 'Phase ', $phase, ': ', $msg->value($phase), " ", $msg->units, "\n";
    }
  } else {
    print $msg->summary,"\n";
  }
}

sub history_callback {
  my ($sensor, $interval, $data) = @_;
  my $max = 0;
  my @rows = ();
  foreach my $ago (sort { $a <=> $b } keys %{$data}) {
    $max = max($max, $data->{$ago});
    push @rows, [ $ago, $data->{$ago} ];
  }
  return if ($max == 0);
  print "History: $sensor $interval ", scalar @rows, "\n";
  foreach my $row (@rows) {
    printf "  %3d %12.5f %-50s\n", @$row, bar($row->[1], $max, 50);
  }
}

sub bar {
  my ($val, $max, $chars) = @_;
  return '#' x int(.5 + (($val/$max) * $chars));
}

__END__

=pod

=encoding UTF-8

=head1 NAME

current-cost-reader - simple example script to read from a Current Cost monitor

=head1 VERSION

version 1.142220

=head1 SYNOPSIS

  current-cost-reader [options] device

  # read from the USB tty device of Current Cost monitor
  current-cost-reader --classic /dev/ttyUSB0

  # read from the USB tty device of an older Classic Current Cost monitor
  current-cost-reader --classic /dev/ttyUSB0

  # read from saved log file
  current-cost-reader cc128.log

  # read from saved log file of an older Classic Current Cost monitor
  current-cost-reader --classic classic.log

=head1 DESCRIPTION

This script is an example of the usage of the L<Device::CurrentCost>
API.  It simply writes a summary of the received data to stdout.

=head1 OPTIONS

=over

=item B<-help>

Print a brief help message.

=item B<-man>

Print the manual page.

=item B<-verbose>

Make output more verbose.

=item B<-history>

Display completed history records when available.

=back

=head1 SEE ALSO

L<Device::CurrentCost>

Current Cost website: http://www.currentcost.com/

=head1 AUTHOR

Mark Hindess <soft-cpan@temporalanomaly.com>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2014 by Mark Hindess.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut
