#!/usr/bin/perl
#
#  Copyright 2010 Eric Sandeen <sandeen@sandeen.net>
#  All Rights Reserved.
# 
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License as
#  published by the Free Software Foundation.
# 
#  This program is distributed in the hope that it would be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
# 
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write the Free Software Foundation,
#  Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# ##############################################################
#
# Get solar production info from an Enphase Envoy monitoring unit
# and send them to a pachube.com feed for further use.
#
# Put this script in a cron job, no more frequent than 10 minutes:
#
# */10 * * * * root LANG=C LC_ALL=C /usr/local/bin/enphase-production-pachube.pl
#
# because the envoy only updates every 5 minutes anyway,
# and it's so gutless more frequent queries bog it down.
#
# You'll need an account & API key from pachube.com
# and then fill in the host/api/feed variables below. 
#
# ##############################################################

use WWW::Mechanize;
use LWP::UserAgent;
use strict;

my $debug = 0;

# Put the IP address or hostname of your envoy box here
my $host = "envoy";
# Put your pachube api key here
my $apikey = "big_long_api_key_string";
# And put your feed ID here
my $feed_id = "0000";
my $url="http://www.pachube.com/api/feeds/" . $feed_id . ".csv";
my $a = WWW::Mechanize->new();

my $raw_html = $a->content;

warn "Getting production screen..." if $debug;

$a->get("http://$host/production");
die "Couldn't get to envoy, got return code " . $a->status . "\n" if $a->status != '200';

$raw_html = $a->content;

my $current_watts;
my $daily_watthours;
my $weekly_watthours;
my $lifetime_watthours;
my $lifetime_units;

if ($raw_html =~ s!<td>Currently</td><td>(.*?) W</td></tr>.*<td>Today</td><td>(.*?) kWh</td></tr>.*<td>Past Week</td><td>(.*?) kWh</td></tr>.*<td>Since Installation</td><td>(.*?) (.)Wh</td></tr>!!gsi) {
  $current_watts = $1;
  $daily_watthours = $2;
  $weekly_watthours = $3;
  $lifetime_watthours = $4;
  $lifetime_units = $5;
} else {
  print "===\n" . $raw_html . "===\n" if $debug;
  open FILE, ">/tmp/badout.txt" ;
  print FILE "===\n" . $raw_html . "===\n";
  die "Couldn't get power production, something's wrong, bailing out.\n";
}

if ($lifetime_units eq 'M') {
	$lifetime_watthours *= 1000;
}

print "Now: $current_watts\n" if $debug;
print "Today: $daily_watthours\n" if $debug;
print "This Week: $weekly_watthours\n" if $debug;
print "Lifetime: $lifetime_watthours $lifetime_units Wh\n" if $debug;

exit if $debug;

# Send this stuff to pachube

my $ua = LWP::UserAgent->new;
$ua->default_header(
                "X-PachubeApiKey" => $apikey,
                "Content-Type" => "text/csv; charset=utf-8");
my $request = HTTP::Request->new(PUT => $url);
$request->content($current_watts . "," . $daily_watthours . "," . $weekly_watthours . "," . $lifetime_watthours);
my $res = $ua->request($request);
if (! $res->is_success) {
	print $res->status_line, "\n" if $debug;
}
print " sent  " if $debug;

exit;
