vp2 

#!/usr/bin/perl -I lib

use strict;
use warnings;

use Logging qw( logf $debug );
use DBI;
use Getopt::Long qw(:config permute);  # allow mixed args.
use Sys::Syslog;
use Davis::VantagePro;

# Options variables
my $syslog      = 0;
my $logmin      = "notice";
my $dsn         = "dbi:Pg:dbname=weather";
my $dbuser      = "";
my $dbpass      = "";
my $device      = "/dev/ttyUSB0";
my $get_model   = 0;
my $get_version = 0;
my $imperial    = 0;
my $set_time    = 0;
my $get_time    = 0;
my $dump        = undef;
my $backlight   = undef;
my $loopfor     = undef;
my $template    = undef;
my $stream      = undef;
my $interval    = undef;
my $command     = undef;
my $helpmeplease = 0;

GetOptions ('debug!'      => \$debug,
            'syslog!'     => \$syslog,
            'model!'      => \$get_model,
            'version!'    => \$get_version,
            'logmin=s'    => \$logmin,
            'dsn=s'       => \$dsn,
            'dbuser=s'    => \$dbuser,
            'dbpass=s'    => \$dbpass,
            'device=s'    => \$device,
            'imperial'    => \$imperial,
            'gettime!'    => \$get_time,
            'settime'     => \$set_time,
            'backlight=s' => \$backlight,
            'template=s'  => \$template,
            'command=s'   => \$command,
            'loopfor=i'   => \$loopfor,
            'stream=i'    => \$stream,
            'interval=i'  => \$interval,
            'dump:s'      => \$dump,
            'help'        => \$helpmeplease  );

usage() if ( $helpmeplease );

Logging::setup_logging( $syslog, 'vp2', $logmin );

my $vpro = Davis::VantagePro->new($device);

# $vpro->Send("STRMOFF");
# $vpro->WakeUp();

printf( STDERR "Station model: %s\n", $vpro->Model()) if ( $get_model );
printf( STDERR "Firmware version: %s\n", $vpro->Version()) if ( $get_version );
printf( STDERR "Station time:  %s\n", $vpro->GetTime()) if ( $get_time );


# Setup and control things
$vpro->Units('imperial') if ( $imperial );
$vpro->SetTime() if ( $set_time );
$vpro->Backlight($backlight) if ( defined($backlight) );
$vpro->Template($template) if ( defined($template) );
$vpro->SetInterval($interval) if ( defined($interval) );

if ( defined($command ) ) {
  $vpro->Send($command);
  my $response = $vpro->Receive();
  Davis::hd($response);
}

# Output things
$vpro->Loop($loopfor) if ( defined($loopfor) );

$vpro->Dump($dump) if ( defined($dump) );

$vpro->Stream($stream) if ( defined($stream) );


exit(0);



sub usage {
  print <<EOUSAGE ;

Read and program a Davis VantagePro2 weather station console.

Usage:

    $0 [options]

Where options may include:

  --model               Display the console model.

  --version             Display the console version number.

  --imperial            Units produced for this request will be in imperial
                        rather than metric format.  This is not persistent.

  --gettime             Get the time from the console and display it.

  --settime             Set the console time to match the current localtime.

  --backlight=<on|off>  Turn the console backlight on or off.

  --interval=<n>        Set the archive period to <n> minutes. where <n> is
                        one of 1, 5, 10, 15, 20, 30, 60 or 120.


Data Output Options:

  --template=<template> Set a template for records displayed as a result of
                        a 'dump' or 'loopfor' call.  The template may be in
                        line, or it may be a filename preceded by an '\@'
                        sign.  Within the template field names surrounded
                        by [[field_name]] are replaced with their respective
                        values.  It is possible to add rudimentary 'printf'
                        formatting by following the field name with a ','
                        and the format, e.g.: [[barometric,%4.1f]].  To get
                        a list of field names, leave out the --template
                        option altogether.

  --loopfor=<count>     Use the LOOP command to retrieve current data every
                        two seconds for <count> times.

  --dump[=<timestamp>]  Dump the console archive of data.  If <timestamp> is
                        supplied then dump only the stored records since that
                        time.  Otherwise all records are retrieved.


Debugging & Logging Options:

  --help                Display this erudite and wonderful text.

  --debug               Enable (many) verbose debugging messages.

  --command=<string>    Send the command <string> to the console and display
                        a hex dump of the response.

  --stream=<count>      Stream data from the sensors, approximately every
                        second.  This data is not interpreted and is largely
                        useless without reading other reference data from 
                        the EEPROM in order to know what the sensor values
                        mean.

  --syslog              Log errors and things to syslog rather than to stderr

  --logmin              The minimum log level that should be sent to syslog.


Database Connection Options:

  --dsn=<DBI DSN>       These are used to configure a database connection. Or
  --dbuser=<user>       will be, when I write code to do that kind of thing.
  --dbpass=<pass>


Examples:

  Retrieve the mode, version and current time from the console and display them.

      $0 --model --version --gettime


  Dump the whole archive and display it according to the format in the file
  'postgresql-dump.tpl', using imperial measurements.

      $0 --imperial --template \@postgresql-dump.tpl --dump

  Tun on the console backlight:

      $0 --backlight on

  Loop 30 times displaying the current data:

      $0 --loopfor 30


Known Bugs:

  - The output of the stream command is not well understood.
  - Very little of the code uses the syslog functionality, as yet.

  
EOUSAGE

}