#!/usr/bin/perl
use strict;

use v5.10.1;

use Serial;
use Mojo::IOLoop;
use Mojo::IOLoop::Stream;
#use Mojo::JSON  qw(decode_json encode_json);
use JSON;
use Debug;
use SMGLog;

my $devname = "/dev/davis";
my $rain_mult = 0.2;    			# 0.1 or 0.2 mm or 0.01 inches
my $tid;
my $rid;
our $did;
my $nlcount;
my $state = "ready";
my $buf;
my $dbg;
my $last_reading;
my $ser;							# the serial port Mojo::IOLoop::Stream
our $ending = 0;

our $json = JSON->new->canonical(1);

our $last_min = int(time/60)*60;
our $last_hour = 0;
our $last_rain_hour;
our $last_rain_min;
our $last_rain;

our $loop_count;				# how many LOOPs we have done, used as start indicator

our	@crc_table = (
				  0x0, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
				  0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
				  0x1231, 0x210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,
				  0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de,
				  0x2462, 0x3443, 0x420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485,
				  0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d,
				  0x3653, 0x2672, 0x1611, 0x630, 0x76d7, 0x66f6, 0x5695, 0x46b4,
				  0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc,
				  0x48c4, 0x58e5, 0x6886, 0x78a7, 0x840, 0x1861, 0x2802, 0x3823,
				  0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b,
				  0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0xa50, 0x3a33, 0x2a12,
				  0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a,
				  0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0xc60, 0x1c41,
				  0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49,
				  0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0xe70,
				  0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78,
				  0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f,
				  0x1080, 0xa1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067,
				  0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e,
				  0x2b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256,
				  0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d,
				  0x34e2, 0x24c3, 0x14a0, 0x481, 0x7466, 0x6447, 0x5424, 0x4405,
				  0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c,
				  0x26d3, 0x36f2, 0x691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634,
				  0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab,
				  0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x8e1, 0x3882, 0x28a3,
				  0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a,
				  0x4a75, 0x5a54, 0x6a37, 0x7a16, 0xaf1, 0x1ad0, 0x2ab3, 0x3a92,
				  0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9,
				  0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0xcc1,
				  0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
				  0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0xed1, 0x1ef0
				 );

our %bar_trend;

$bar_trend{-60} = "Falling Rapidly";
$bar_trend{196} = "Falling Rapidly";
$bar_trend{-20} = "Falling Slowly";
$bar_trend{236} = "Falling Slowly";
$bar_trend{0} = "Steady";
$bar_trend{20} = "Rising Slowly";
$bar_trend{60} = "Rising Rapidly";

our $ending = 0;

$SIG{TERM} = $SIG{INT} = sub {++$ending; Mojo::IOLoop->stop;};
$SIG{HUP} = 'IGNORE';

dbginit();
if (@ARGV) {
	dbgadd(@ARGV);
} 
dbgadd('chan');

dbg '***';
dbg "*** starting $0";
dbg '***';

dbg scalar gmtime($last_min);
dbg scalar gmtime($last_hour);

my $dlog = SMGLog->new("day");
$did = Mojo::IOLoop->recurring(1 => sub {$dlog->flushall});

do_reopen($devname);

dbg '***';
dbg "*** ending $0";
dbg '***';

exit 0;

sub on_read
{
	my ($ser, $d) = @_;
	$buf .= $d;
	$d =~ s/([\%\x00-\x1f\x7f-\xff])/sprintf("%%%02X", ord($1))/eg; 
	dbg "read added '$d' buf lth=" . length $buf if isdbg 'raw';
	if ($state eq 'waitnl' && $buf =~ /[\cJ\cM]+/) {
		dbg "Got \\n" if isdbg 'state';
		Mojo::IOLoop->remove($tid) if $tid;
		undef $tid;
		undef $buf;
		$nlcount = 0;
		$ser->write("LPS 1 1\n");
		chgstate("waitloop");
	} elsif ($state eq "waitloop") {
		if ($buf =~ /\x06/) {
			dbg "Got ACK 0x06" if isdbg 'state';
			chgstate('waitlooprec');
			undef $buf;
		}
	} elsif ($state eq 'waitlooprec') {
		if (length $buf >= 99) {
			dbg "got loop record" if isdbg 'chan';
			chgstate('');
			process($buf);
			undef $buf;
		}
	}
}

sub start_loop
{
	$nlcount = 1;
	dbg "start_loop writing $nlcount \\n" if isdbg 'state';
	$ser->write("\n");
	Mojo::IOLoop->remove($tid) if $tid;
	undef $tid;
	$tid = Mojo::IOLoop->recurring(0.6 => sub {
									   if (++$nlcount > 10) {
										   dbg "\\n count > 10, closing connection" if isdbg 'chan';
										   do_reopen($devname);
										   return;
									   }
									   dbg "writing $nlcount \\n" if isdbg 'state'; 
									   $ser->write("\n");
								   });
	chgstate("waitnl");
}

sub chgstate
{
	dbg "state '$state' -> '$_[0]'" if isdbg 'state';
	$state = $_[0];
}

sub do_reopen
{
	my $name = shift;
	dbg "do reopen on '$name' ending $ending";
	unless ($ending) {
		$ser = do_open($name);
		start_loop();
		chgstate('');
		$nlcount = 0;
		Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
	}
}

sub do_open
{
	my $name = shift;

	$ser->close if $ser;
	undef $ser;

	my $ob = Serial->new($name, 19200) || die "$name $!\n";
	dbg "streaming $name fileno(" . fileno($ob) . ")" if isdbg 'chan';
	
	my $ser = Mojo::IOLoop::Stream->new($ob);
	$ser->on(error=>sub {dbg "serial $_[1]"; do_reopen($name) unless $ending});
	$ser->on(close=>sub {dbg "serial closing"; do_reopen($name) unless $ending});
	$ser->on(timeout=>sub {dbg "serial timeout";});
	$ser->on(read=>sub {on_read(@_)});
	$ser->start;

	Mojo::IOLoop->remove($tid) if $tid;
	undef $tid;
	Mojo::IOLoop->remove($rid) if $rid;
	undef $rid;
	$rid = Mojo::IOLoop->recurring(2.5 => sub {
									   start_loop() if !$state;
								   });
	chgstate('');
	$nlcount = 0;
	
	return $ser;
}

my @min;
my @hour;

sub process
{
	my $blk = shift;
	my $loo =  substr $blk,0,3;
	unless ( $loo eq 'LOO') {
		dbg "Block invalid loo -> $loo" if isdbg 'chan'; return;
	}

	my $tmp;
	my $rain;
	my %h;

	# Common ones
	$tmp = unpack("s", substr $blk,7,2) / 1000;
	$h{Pressure} = sprintf("%.0f",in2mb($tmp))+0;

	$tmp = unpack("s", substr $blk,9,2) / 10;
	$h{Temp_In} = sprintf("%.1f", f2c($tmp))+0;
	
	$tmp  = unpack("s", substr $blk,12,2) / 10;
	$h{Temp_Out}  = sprintf("%.1f", f2c($tmp))+0;

	$tmp = unpack("C", substr $blk,14,1);
	$h{Wind} = sprintf("%.1f",mph2mps($tmp))+0;
	$h{Dir}     = unpack("s", substr $blk,16,2)+0;

	my $wind = {w => $h{Wind}, d => $h{Dir}};
	push @min, $wind;

	$h{Humidity_Out} = unpack("C", substr $blk,33,1)+0;
	$h{Humidity_In}  = unpack("C", substr $blk,11,1)+0;

	$tmp = unpack("C", substr $blk,43,1)+0;
	$h{UV} = $tmp unless $tmp >= 255;
	$tmp = unpack("s", substr $blk,44,2)+0;	# watt/m**2
	$h{Solar} = $tmp unless $tmp >= 32767;

#	$h{Rain_Rate}  = sprintf("%0.1f",unpack("s", substr $blk,41,2) * $rain_mult)+0;
	$rain = $h{Rain_Day}   = sprintf("%0.1f", unpack("s", substr $blk,50,2) * $rain_mult)+0;
	$h{Rain} = ($rain >= $last_rain ? $rain - $last_rain : $rain) if $loop_count;
	$last_rain = $rain;

	# what sort of packet is it?

	my $sort =  unpack("C", substr $blk,4,1);
	if ($sort) {

		# Newer LOOP2 packet
		$tmp = unpack("C", substr $blk,18,2);
#		$h{Wind_Avg_10} = sprintf("%.1f",mph2mps($tmp/10))+0;
		$tmp = unpack("C", substr $blk,20,2);
#		$h{Wind_Avg_2} = sprintf("%.1f",mph2mps($tmp/10))+0;
		$tmp = unpack("C", substr $blk,22,2);
#		$h{Wind_Gust_10} = sprintf("%.1f",mph2mps($tmp/10))+0;

#		$h{Dir_Avg_10} = unpack("C", substr $blk,24,2)+0;
		$tmp = unpack("C", substr $blk,30,2);
		$h{Dew_Point} = sprintf("%0.1f", f2c($tmp))+0;

	} else {

		# Older LOOP packet
		$tmp = unpack("C", substr $blk,15,1);
#		$h{Wind_Avg_10} = sprintf("%.1f",mph2mps($tmp))+0;
		$h{Dew_Point}  = sprintf("%0.1f", dew_point($h{Temp_Out}, $h{Humidity_Out}))+0;
		$h{Rain_Month}  = sprintf("%0.1f", unpack("s", substr $blk,52,2) * $rain_mult)+0;
		$h{Rain_Year}  = sprintf("%0.1f", unpack("s", substr $blk,54,2) * $rain_mult)+0;

	}

	my $crc_calc = CRC_CCITT($blk);

	if ($crc_calc==0) {
		my $ts = time;
		my $j;
		my $s;
		if ($ts >= $last_hour + 3600) {
			$h{Pressure_Trend}    = unpack("C", substr $blk,3,1);
			$h{Pressure_Trend_txt} = $bar_trend{$h{Pressure_Trend}};
			$h{Batt_TX_OK}  = (unpack("C", substr $blk,86,1)+0) ^ 1;
			$h{Batt_Console}  = sprintf("%0.2f", unpack("s", substr $blk,87,2) * 0.005859375)+0;
			$h{Forecast_Icon}  = unpack("C", substr $blk,89,1);
			$h{Forecast_Rule}  = unpack("C", substr $blk,90,1);
			$h{Sunrise}  = sprintf( "%04d", unpack("S", substr $blk,91,2) );
			$h{Sunrise}  =~ s/(\d{2})(\d{2})/$1:$2/;
			$h{Sunset}   = sprintf( "%04d", unpack("S", substr $blk,93,2) );
			$h{Sunset}  =~ s/(\d{2})(\d{2})/$1:$2/;

			if ($loop_count) {	# i.e not the first
				my $a = average(scalar @hour ? @hour : {w => $h{Wind}, d => $h{Dir}});

				$h{Wind_1h} = sprintf("%0.1f", $a->{w})+0;
				$h{Dir_1h} = sprintf("%0.0f", $a->{d})+0;
				$h{Rain_1h} = $rain >= $last_rain_hour ? $rain - $last_rain_hour : $rain;

				$h{Wind_1m} = sprintf("%0.1f", $a->{w})+0;
				$h{Dir_1m} = sprintf("%0.0f", $a->{d})+0;
				$h{Rain_1m} = $rain >= $last_rain_min ? $rain - $last_rain_min : $rain;
			}
			$last_rain_min = $last_rain_hour = $rain;

			$j = $json->encode(\%h);
			$s = qq|{"t":$ts,"h":$j}|;
			$last_hour = int($ts/3600)*3600;
			$last_min = int($ts/60)*60;
			@hour = ();
			@min = ();
		} elsif ($ts >= $last_min + 60) {
			my $a = average(@min);
			my %save;

			push @hour, $a;

			if ($loop_count) {	# i.e not the first
				$h{Wind_1m} = sprintf("%0.1f", $a->{w})+0;
				$h{Dir_1m} = sprintf("%0.0f", $a->{d})+0;
				$h{Rain_1h} = $rain >= $last_rain_hour ? $rain - $last_rain_hour : $rain; # this is the rate for this hour, so far
				$h{Rain_1m} = $rain >= $last_rain_min ? $rain - $last_rain_min : $rain;
			}
			$last_rain_min = $rain;

			$j = $json->encode(\%h);
			$s = qq|{"t":$ts,"m":$j}|;
			$last_min = int($ts/60)*60;
			@min = ();
		} else {
			my $o = gen_hash_diff($last_reading, \%h);
			if ($o) {
				$j = $json->encode($o);
				$s = qq|{"t":$ts,"r":$j}|;
			} else {
				dbg "loop rec not changed" if isdbg 'chan';
			}
		}
		output_str($s) if $s;
		$last_reading = \%h;
		++$loop_count;
	} else {
		dbg "CRC check failed for LOOP data!";
	}
	return;
}

sub output_str
{
	my $s = shift;
	dbg $s;
#	say $s;
	$dlog->writenow($s);
}

sub gen_hash_diff
{
	my $last = shift;
	my $now = shift;
	my %o;
	my $count;

	while (my ($k, $v) = each %$now) {
		if ($last->{$k} ne $now->{$k}) {
			$o{$k} = $v;
			++$count;
		}
	}
	return $count ? \%o : undef;
}

sub dew_point
{
	my $temp = shift @_;
	my $rh   = shift @_;

	#  Using the simplified approximation for dew point
	#  Accurate to 1 degree C for humidities > 50 %
	#  http://en.wikipedia.org/wiki/Dew_point

	my $dewpoint = $temp - ((100 - $rh) / 5);

	# this is the more complete one (which doesn't work)
	#my $a = 6.1121;
	#my $b = 18.678;
	#my $c = 257.14;
	#my $ytrh = log(($rh/100) + ($b * $temp) / ($c + $temp));
	#my $dewpoint = ($c * $ytrh) / ($b - $ytrh); 

	return $dewpoint;
}

sub CRC_CCITT
{
    # Expects packed data...
    my $data_str = shift @_;

	my $crc = 0;
	my @lst = split //, $data_str;
	foreach my $data (@lst) {
		my $data = unpack("c",$data);

		my $crc_prev = $crc;
		my $index = $crc >> 8 ^ $data;
		my $lhs = $crc_table[$index];
		#print "lhs=$lhs, crc=$crc\n";
		my $rhs = ($crc << 8) & 0xFFFF;
		$crc = $lhs ^ $rhs;


	}

	return $crc;
}

sub f2c
{
	return ($_[0] - 32) * 5/9;
}

sub mph2mps
{
	return $_[0] * 0.44704;
}

sub in2mb
{
	return $_[0] * 33.8637526;
}

sub average
{
	my %out;
	my $count;
	
	foreach my $r (@_) {
		while (my ($k, $v) = each %$r) {
			$out{$k} += $v;
		}
		++$count;
	}
	while (my ($k, $v) = each %out) {
		$out{$k} /= $count;
	}

	return \%out;
}
