wip
authorDirk Koopman <djk@tobit.co.uk>
Thu, 10 May 2012 15:18:21 +0000 (16:18 +0100)
committerDirk Koopman <djk@tobit.co.uk>
Thu, 10 May 2012 15:18:21 +0000 (16:18 +0100)
.gitignore
DWeather/dweather [new file with mode: 0644]
DWeather/lib/DWeather.pm
DWeather/lib/DWeather/Debug.pm
DWeather/lib/DWeather/Log.pm [deleted file]
DWeather/lib/DWeather/Logger.pm [new file with mode: 0644]
DWeather/lib/DWeather/Serial.pm
DWeather/lib/DWeather/Station.pm [new file with mode: 0644]
DWeather/lib/DWeather/Station/Vantage.pm [new file with mode: 0644]
DWeather/lib/DWeather/Vantage.pm [new file with mode: 0644]

index b25c15b81fae06e1c55946ac6270bfdb293870e8..f020ecd8476f0639c0ee09e839822c58f1aeceee 100644 (file)
@@ -1 +1,6 @@
 *~
+blib
+MYMETA*
+Makefile
+logs
+pm_to_blib
diff --git a/DWeather/dweather b/DWeather/dweather
new file mode 100644 (file)
index 0000000..3844fcc
--- /dev/null
@@ -0,0 +1,36 @@
+#!/usr/bin/perl
+#
+# dweather - a distributed weather station
+#
+# copyright (c) 2012 Dirk Koopman G1TLH
+#
+#
+
+use strict;
+use warnings;
+
+use lib qw(. ./blib ./lib ./DWeather/lib);
+
+use DWeather;
+use DWeather::Logger;
+use DWeather::Debug;
+use AnyEvent;
+
+my $sigint = AnyEvent->signal (signal => "INT", cb => sub { my $sig = shift; terminate("on signal $sig")});
+my $sigterm = AnyEvent->signal (signal => "TERM", cb => sub { my $sig = shift; terminate("on signal $sig")});
+
+dbginit();
+dbg("*** dweather started");
+
+my $cv = AnyEvent->condvar;
+my @res = $cv->recv;
+
+exit 0;
+
+
+sub terminate
+{
+       my $m = shift;
+       dbg("*** dweather ended" . ($m ? " $m" : ' normally'));
+       exit(0);
+}
index cbe5b97edcf731bb864d128ec653615c4ae19238..5c1048153e99f2f9e0319d6443c14eae03de0c60 100644 (file)
@@ -11,6 +11,9 @@ our @ISA = qw(Exporter);
 
 our $VERSION = '0.01';
 
+use DWeather::Logger;
+use DWeather::Debug;
+use DWeather::Serial;
 
 # Preloaded methods go here.
 
index bf815202b79d42cd0067f2c84883ea8197eb2140..3535184b3461918e1d853ea9a946f04de2417d1e 100644 (file)
@@ -10,7 +10,7 @@
 # modify it under the same terms as Perl itself.
 #
 
-package Debug;
+package DWeather::Debug;
 
 require Exporter;
 @ISA = qw(Exporter);
@@ -20,7 +20,7 @@ $VERSION = sprintf( "%d.%02d", q$Revision: 1.1 $ =~ /(\d+)\.(\d+)/ );
 use strict;
 use vars qw(%dbglevel $fp);
 
-use SMGLog ();
+use DWeather::Logger;
 use Carp qw(cluck);
 
 %dbglevel = ();
@@ -52,8 +52,6 @@ if (!defined $DB::VERSION) {
    );
 } 
 
-dbginit();
-
 sub dbg
 {
        my $t = time; 
@@ -78,7 +76,7 @@ sub dbginit
                $SIG{__DIE__} = sub { dbg($@, Carp::longmess(@_)); };
        }
 
-       $fp = SMGLog->new('debug', 'dat', 'd');
+       $fp = DWeather::Logger->new('debug', 'log', 'd') unless $fp;
 }
 
 sub dbgclose
diff --git a/DWeather/lib/DWeather/Log.pm b/DWeather/lib/DWeather/Log.pm
deleted file mode 100644 (file)
index 4d801be..0000000
+++ /dev/null
@@ -1,178 +0,0 @@
-#
-# the general purpose logging machine
-#
-# This module is designed to allow you to log stuff in SMG format
-#
-# The idea is that you give it a prefix which is a directory and then 
-# the system will log stuff to a directory structure which looks like:-
-#
-# ./logs/<prefix>/yyyy/mmdd.[log|<optional suffix]
-#   
-# Routines are provided to read these files in and to append to them
-# 
-# Copyright (c) - 1998-2007 Dirk Koopman G1TLH
-#
-# This library is free software; you can redistribute it and/or
-# modify it under the same terms as Perl itself.
-#
-
-package SMGLog;
-
-use IO::File;
-use Exporter;
-use Carp;
-use File::Path;
-
-@ISA = qw(Exporter);
-@EXPORT = qw(Log LogDbg);
-
-use strict;
-
-use vars qw($log $path);
-$log = undef;
-$path = './logs';
-
-init();
-
-# make the Log() export use this default file
-sub init
-{
-       $log = SMGLog->new("sys_log");
-}
-
-# create a log object that contains all the useful info needed
-# prefix is the main directory off of the data directory
-# suffix is the suffix after the month/day
-sub new
-{
-       my ($pkg, $prefix, $suffix) = @_;
-       my $ref = {};
-       my $dir = "$path/$prefix";
-       $ref->{prefix} = $dir;
-       $ref->{suffix} = $suffix || 'log';
-               
-       # make sure the directory exists
-       mkpath($dir, 0, 0777) unless -d $dir;
-       die "cannot create or access $dir $!" unless -d $dir;
-       
-       return bless $ref, $pkg;
-}
-
-# open the appropriate data file
-sub open
-{
-       my ($self, $dayno, $mode) = @_;
-       
-       my ($year, $month, $day) = (gmtime($dayno * 86400))[5,4,3];
-       $year += 1900;
-       $month += 1;
-       
-       # if we are writing, check that the directory exists
-       if (defined $mode) {
-               my $dir = "$self->{prefix}/$year";
-               mkdir($dir, 0777) if ! -e $dir;
-       }
-       
-       $self->{fn} = sprintf "$self->{prefix}/$year/%02d%02d", $month, $day;
-       $self->{fn} .= ".$self->{suffix}" if $self->{suffix};
-       
-       $self->{mode} = $mode || 'r';
-       
-       my $fh = new IO::File $self->{fn}, $mode, 0666;
-       return unless $fh;
-       
-       $fh->autoflush(1) if $mode ne 'r'; # make it autoflushing if writable
-       $self->{fh} = $fh;
-
-       $self->{year} = $year;
-       $self->{month} = $month;
-       $self->{day} = $day;
-       $self->{dayno} = $dayno;
-               
-#      DXDebug::dbg("dxlog", "opening $self->{fn}\n");
-       
-       return $self->{fh};
-}
-
-# open the previous log file in sequence
-sub openprev
-{
-       my $self = shift;
-       return $self->open($self->{dayno} - 1, @_);
-}
-
-# open the next log file in sequence
-sub opennext
-{
-       my $self = shift;
-       return $self->open($self->{dayno} + 1, @_);
-}
-
-# write (actually append) to a file, opening new files as required
-sub write
-{
-       my ($self, $dayno, $line) = @_;
-       if (!$self->{fh} || 
-               $self->{mode} ne ">>" || 
-               $dayno != $self->{dayno}) {
-               $self->open($dayno, ">>") or confess "can't open $self->{fn} $!";
-       }
-
-       return $self->{fh}->print("$line\n");
-}
-
-# read a line from an opened file
-sub read
-{
-       my $self = shift;
-       confess "can't read $self->{fh} $!" unless $self->{fh};
-       return $self->{fh}->getline;
-}
-
-# write (actually append) using the current date to a file, opening new files as required
-sub writenow
-{
-       my ($self, $line) = @_;
-       my $dayno = int (time / 86400);
-       return $self->write($dayno, $line);
-}
-
-# write (actually append) using a unix time to a file, opening new files as required
-sub writeunix
-{
-       my ($self, $t, $line) = @_;
-       my $dayno = int ($t / 86400);
-       return $self->write($dayno, $line);
-}
-
-# close the log file handle
-sub close
-{
-       my $self = shift;
-       undef $self->{fh};                      # close the filehandle
-       delete $self->{fh};     
-}
-
-sub DESTROY
-{
-       my $self = shift;
-       undef $self->{fh};                      # close the filehandle
-       delete $self->{fh} if $self->{fh};
-}
-
-sub Log
-{
-       my $l = ref $_[0] ? shift : $log;
-       return unless $l;
-       my $t = time;
-       my $ts = sprintf("%02d:%02d:%02d", (gmtime($t))[2,1,0]);
-       $l->writeunix($t, "$ts $_") for @_;
-}
-
-sub LogDbg
-{
-    Log(@_);
-    Debug::dbg(@_) if Debug::isdbg('chan');
-}
-
-1;
diff --git a/DWeather/lib/DWeather/Logger.pm b/DWeather/lib/DWeather/Logger.pm
new file mode 100644 (file)
index 0000000..b8233bf
--- /dev/null
@@ -0,0 +1,179 @@
+#
+# the general purpose logging machine
+#
+# This module is designed to allow you to log stuff in SMG format
+#
+# The idea is that you give it a prefix which is a directory and then 
+# the system will log stuff to a directory structure which looks like:-
+#
+# ./logs/<prefix>/yyyy/mmdd.[log|<optional suffix]
+#   
+# Routines are provided to read these files in and to append to them
+# 
+# Copyright (c) - 1998-2007 Dirk Koopman G1TLH
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the same terms as Perl itself.
+#
+
+package DWeather::Logger;
+
+use IO::File;
+use Exporter;
+use Carp;
+use File::Path;
+
+@ISA = qw(Exporter);
+@EXPORT = qw(Log LogDbg);
+
+use strict;
+
+use vars qw($log $path);
+$log = undef;
+$path = './logs';
+
+# make the Log() export use this default file
+sub init
+{
+       my $default_dir = shift || 'sys_log';
+       $log = __PACKAGE__->new($default_dir) unless $log;
+}
+
+# create a log object that contains all the useful info needed
+# prefix is the main directory off of the data directory
+# suffix is the suffix after the month/day
+sub new
+{
+       my ($pkg, $prefix, $suffix) = @_;
+       my $ref = {};
+       my $dir = "$path/$prefix";
+       $ref->{prefix} = $dir;
+       $ref->{suffix} = $suffix || 'log';
+               
+       # make sure the directory exists
+       mkpath($dir, 0, 0777) unless -d $dir;
+       die "cannot create or access $dir $!" unless -d $dir;
+       
+       return bless $ref, $pkg;
+}
+
+# open the appropriate data file
+sub open
+{
+       my ($self, $dayno, $mode) = @_;
+       
+       my ($year, $month, $day) = (gmtime($dayno * 86400))[5,4,3];
+       $year += 1900;
+       $month += 1;
+       
+       # if we are writing, check that the directory exists
+       if (defined $mode) {
+               my $dir = "$self->{prefix}/$year";
+               mkdir($dir, 0777) if ! -e $dir;
+       }
+       
+       $self->{fn} = sprintf "$self->{prefix}/$year/%02d%02d", $month, $day;
+       $self->{fn} .= ".$self->{suffix}" if $self->{suffix};
+       
+       $self->{mode} = $mode || 'r';
+       
+       my $fh = new IO::File $self->{fn}, $mode, 0666;
+       return unless $fh;
+       
+       $fh->autoflush(1) if $mode ne 'r'; # make it autoflushing if writable
+       $self->{fh} = $fh;
+
+       $self->{year} = $year;
+       $self->{month} = $month;
+       $self->{day} = $day;
+       $self->{dayno} = $dayno;
+               
+#      DXDebug::dbg("dxlog", "opening $self->{fn}\n");
+       
+       return $self->{fh};
+}
+
+# open the previous log file in sequence
+sub openprev
+{
+       my $self = shift;
+       return $self->open($self->{dayno} - 1, @_);
+}
+
+# open the next log file in sequence
+sub opennext
+{
+       my $self = shift;
+       return $self->open($self->{dayno} + 1, @_);
+}
+
+# write (actually append) to a file, opening new files as required
+sub write
+{
+       my ($self, $dayno, $line) = @_;
+       if (!$self->{fh} || 
+               $self->{mode} ne ">>" || 
+               $dayno != $self->{dayno}) {
+               $self->open($dayno, ">>") or confess "can't open $self->{fn} $!";
+       }
+
+       return $self->{fh}->print("$line\n");
+}
+
+# read a line from an opened file
+sub read
+{
+       my $self = shift;
+       confess "can't read $self->{fh} $!" unless $self->{fh};
+       return $self->{fh}->getline;
+}
+
+# write (actually append) using the current date to a file, opening new files as required
+sub writenow
+{
+       my ($self, $line) = @_;
+       my $dayno = int (time / 86400);
+       return $self->write($dayno, $line);
+}
+
+# write (actually append) using a unix time to a file, opening new files as required
+sub writeunix
+{
+       my ($self, $t, $line) = @_;
+       my $dayno = int ($t / 86400);
+       return $self->write($dayno, $line);
+}
+
+# close the log file handle
+sub close
+{
+       my $self = shift;
+       undef $self->{fh};                      # close the filehandle
+       delete $self->{fh};     
+}
+
+sub DESTROY
+{
+       my $self = shift;
+       undef $self->{fh};                      # close the filehandle
+       delete $self->{fh} if $self->{fh};
+}
+
+sub Log
+{
+       my $l = ref $_[0] ? shift : $log;
+       return unless $l;
+       my $t = time;
+       my $ts = sprintf("%02d:%02d:%02d", (gmtime($t))[2,1,0]);
+       $l->writeunix($t, "$ts $_") for @_;
+}
+
+sub LogDbg
+{
+    Log(@_);
+    DWeather::Debug::dbg(@_) if DWeather::Debug::isdbg('chan');
+}
+
+init();
+
+1;
index 44da24f7cbe7a2fde0db1f00822deb8d852d22c7..40ac1607c343efc8b0c42c0e8c549a069942ae7a 100644 (file)
@@ -4,7 +4,7 @@
 
 use strict;
 
-package Serial;
+package DWeather::Serial;
 
 use POSIX qw(:termios_h);
 use Fcntl;
@@ -87,4 +87,10 @@ sub close
        $self->SUPER::close;
 }
 
+sub DESTROY
+{
+       my $self = shift;
+       $self->close;
+}
+
 1;
diff --git a/DWeather/lib/DWeather/Station.pm b/DWeather/lib/DWeather/Station.pm
new file mode 100644 (file)
index 0000000..f9c45fe
--- /dev/null
@@ -0,0 +1,26 @@
+#
+# General device abtraction for DWeather interfaces
+#
+#
+
+package DWeather::Station;
+
+use strict;
+use warnings;
+
+use base qw(DWeather::Serial);
+use DWeather::Debug;
+
+# return a File::IO handle set up for serial operations
+
+sub new
+{
+       my $pkg = shift;
+       my $class = ref $pkg || $pkg;
+       my $device = shift || "/dev/ttyS0";
+       
+       my $self = $class->SUPER::new($device, @_);
+       return $self;
+}
+
+1;
diff --git a/DWeather/lib/DWeather/Station/Vantage.pm b/DWeather/lib/DWeather/Station/Vantage.pm
new file mode 100644 (file)
index 0000000..5f3749e
--- /dev/null
@@ -0,0 +1,35 @@
+#
+# Vantage Pro 2 interface for DWeather
+#
+#
+
+package DWeather::Station::Vantage;
+
+use strict;
+use warnings;
+
+use base qw(DWeather::Station);
+use DWeather::Debug;
+use AnyEvent;
+
+sub new
+{
+       my $pkg = shift;
+       my $class = ref $pkg || $pkg;
+       my $device = shift;
+       
+       my $d = $class->SUPER::new($device, 19200);
+       return $d;
+}
+
+sub reset
+{
+
+}
+
+sub poll
+{
+
+}
+
+1;
diff --git a/DWeather/lib/DWeather/Vantage.pm b/DWeather/lib/DWeather/Vantage.pm
new file mode 100644 (file)
index 0000000..54cbb4a
--- /dev/null
@@ -0,0 +1,30 @@
+#
+# Vantage Pro 2 interface for DWeather
+#
+#
+
+use strict;
+use warnings;
+
+use base qw(DWeather::Serial);
+use AnyEvent;
+
+sub new
+{
+       my $pkg = shift;
+       my $class = ref $pkg || $pkg;
+       my $device = shift || '/dev/ttyS0';
+       
+       my $self = $class->SUPER::new($device, 19200);
+       return $self;
+}
+
+sub send
+{
+       
+}
+
+sub run
+{
+
+}