#!/usr/bin/perl -w
#2345678911234567892123456789312345678941234567895123456789612345678971234567898

# 1cm of real rain is about 50mm recorded.

# License: GPL v3
# Author: Marc MERLIN <marc_soft at merlins.org> 2010/05/01
# $Id: parse_rfx-xpl 1289 2017-07-06 16:27:50Z svnuser $

use strict;
use Date::Manip;
use IO::Handle;

my $VERBOSE = 0;

# Find rfx-xpl.xml
chdir("/var/local/src/rfxcom_xpl");
my $LOGFILE = "/var/log/xpl.log";
$LOGFILE=$ENV{'TEMPLOGFILE'} if ($ENV{'TEMPLOGFILE'});

open(XPL, "LD_LIBRARY_PATH=/lib/i386-linux-gnu21 /var/local/src/rfxcom_xpl/rfx-xpl-static -interface eth0 -d 2>&1 |");
open(LOG, ">>$LOGFILE");
LOG->autoflush;

my $lastprocess = "";
while (<XPL>)
{
    next if (/process\[\d+\] return:/);
    my $date = UnixDate("now", "%Y/%m/%d %T");

    # remove garbage character from rfx-xpl:
    s/ �//g;
    if (/process\[\d+\]: .* cnt=/)
    {
	$lastprocess = $_ ;
	next;
    }
    elsif ($lastprocess)
    {
	$lastprocess =~ s/:.* cnt/ cnt/;
	print LOG "$date: $lastprocess";
	$lastprocess = "";
    }

    if (not /^X10: \S+ o(n|ff)$/ and 
	not /^X10 Sec: addr/ and 
	not / addr: / )
    {
	next;
    }

    s/(hum: \d+)/$1 %/;
    if ( /(\d+) hPa/ )
    {
	my $pres = $1;
	my $inhg = sprintf("%5.2f", $pres / 33.8639);

	s#(\d+) hPa#$pres hPa / $inhg inHg#;
    }
    elsif ( m#speed (\S+) m/s average (\S+) m/s# )
    {
	my ($speed1, $speed2) = ($1, $2);
	my $knots1 = int($speed1 * 1.94384449);
	my $knots2 = int($speed2 * 1.94384449);

	s#speed (\S+) m/s average (\S+) m/s#speed $speed1 m/s / $knots1 kts average $speed2 m/s / $knots2 kts#;
    }

    print LOG "$date: $_";
}

# vim:sts=4:sw=4
