+++ /dev/null
-#!/usr/bin/perl -w
-# -*- CPerl -*-
-#
-# Weather/daemon
-#
-# Fetches data from the local owserver once per minute. Appends to
-# Log/YEAR/MONTH/DAY.txt.
-
-use strict;
-use IO::File;
-use Date::Format;
-
-sub mysystem (@)
-{
- if (system (@_) != 0) {
- my $status = $?;
- my $sig = $status & 127;
- my $CMD = join ", ", map { "\"$_\"" } @_;
- if ($status == -1) {
- die "Failed to execute: $CMD: $!\n";
- }
- elsif ($sig) {
- $! = 1;
- die "Died with signal $sig: $CMD\n";
- }
- else {
- return $status >> 8;
- }
- }
- return 0;
-}
-
-sub mybacktick ($)
-{
- my ($cmdline) = @_;
-
- my @lines = `$cmdline`;
- my $status = $?;
- my $sig = $status & 127;
- $status >>= 8;
- if ($status == 0) { return @lines; }
- elsif ($sig) {
- $! = 1;
- die "Died with signal $sig: $cmdline\n";
- }
- else {
- die "Failed to execute: $cmdline: $!\n";
- }
-}
-
-sub mymkdir ($)
-{
- my ($dirpath) = @_;
-
- my @path_names = split /\//, $dirpath;
- my $path;
- if (!$path_names[0]) {
- $path = "/";
- shift @path_names;
- } else {
- $path = ".";
- }
- my @created;
- while (@path_names) {
- $path .= "/" . shift @path_names;
- if (! -d $path) {
- if (-e $path) {
- die "mkdir $dirpath: already exists; not a directory!\n";
- }
- if (! mkdir $path) {
- die "mkdir $path: $!\n";
- } else {
- chmod 02775, $path;
- push @created, $path;
- }
- }
- }
- return @created;
-}
-
-my $LOG;
-my $ymd = "";
-sub reopen_log ($$$)
-{
- my ($year, $month, $day) = @_;
- my $new_ymd = "$year/$month/$day";
- return if $new_ymd eq $ymd;
- close $LOG if defined $LOG;
- umask 07;
- mymkdir "$year/$month";
- umask 027;
- my $filename = "$new_ymd.txt";
- $LOG = new IO::File;
- open $LOG, ">>$filename" or die "Could not open $filename: $!\n";
- $ymd = $new_ymd;
-}
-
-my $hostname;
-sub sensor_name ($)
-{
- my ($num) = @_;
- if ($hostname eq "carida") {
- my $name = ("pool")[$num];
- return $name;
- }
- elsif ($hostname eq "dathomir") {
- my $name = ("inside", "outside")[$num];
- warn "Bogus sensor number ($num) for host $hostname.\n" if !$name;
- return $name;
- }
- warn "Bogus hostname: $hostname\n";
- return undef;
-}
-
-sub owread ($$)
-{
- my ($name, $query) = @_;
-
- my $tries = 0;
- while ($tries < 3) {
- my $time = time;
- my $datime = time2str ("%Y-%m-%d %H:%M:%S", $time, "UTC");
- my ($y, $m, $d) = $datime =~ /^(\d{4})-(\d\d)-(\d\d) /;
- reopen_log $y, $m, $d;
- $tries += 1;
- my @lines = `/usr/bin/owread $query`;
- chomp @lines;
- my $status = $?;
- my $sig = $status & 127;
- $status >>= 8;
- if ($status != 0) {
- my $L = join "\\n", @lines;
- print $LOG "$datime\t$name\terror: status $status: $L\n";
- $LOG->flush;
- } elsif (@lines != 1) {
- my $L = join "\\n", @lines;
- print $LOG "$datime\t$name\terror: multiple lines: $L\n";
- $LOG->flush;
- } elsif ($lines[0] !~ /^ *(-?\d+(\.\d+)?)$/) {
- my $L = $lines[0];
- print $LOG "$datime\t$name\terror: bogus line: $L\n";
- $LOG->flush;
- } else {
- my $datum = $1;
- print $LOG "$datime\t$name\t$datum\n";
- $LOG->flush;
- return;
- }
- }
-}
-
-sub main () {
- $hostname = `hostname`;
- chomp $hostname;
- die "usage: $0\n" if @ARGV != 0;
- $0 = "weatherd";
- chdir "/home/monkey/Weather/Log" or die;
- umask 027;
- my $start = time;
- {
- my $secs = 60 - $start % 60;
- $start += $secs;
- sleep ($secs);
- }
- while (1) {
- owread "T", "{{ owread_path }}temperature";
- owread "H", "{{ owread_path }}HIH4000/humidity";
- $start += 60;
- my $now = time;
- while ($start < $now) { $start += 60; }
- my $secs = $start - $now;
- sleep ($secs);
- }
-}
-
-main;