################################################################################ # stoker.pm - Read Stoker WEB page and convert to JSON # # o JSON format follows the format of the new beta version of the stoker # # software # # o HTML regular expression search assumes unmodified names (stock) # ################################################################################ # # # stoker.pm - Access Stoker BBQ monitor/controller. # # Copyright (C) 2010 Curt Timmerman # # # # 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, either version 3 of the License, or # # (at your option) any later version. # # # # This program is distributed in the hope that it will 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, see . # # # ################################################################################ package stoker ; use strict ; use warnings ; use JSON ; use LWP::UserAgent ; use HTTP::Request::Common ; use Data::Dumper ; $Data::Dumper::Indent = 1 ; my $StokerIP = '192.168.0.1' ; # NEED Stoker IP address #---------------------------------------------------------------------- # new #---------------------------------------------------------------------- sub new { my $Class = shift ; bless { } , $Class ; } ## new ## #---------------------------------------------------------------------- # get_html #---------------------------------------------------------------------- sub get_html { my $self = shift ; my $UserAgent = LWP::UserAgent->new ('agent' => 'stoker post') ; my $StokerURL = "http://$StokerIP" ; my $Response ; $Response = $UserAgent->get ( $StokerURL ) ; if (! $Response->is_success) { return ; } return ($Response->content) ; } ## get_html ## #---------------------------------------------------------------------- # get_reading #---------------------------------------------------------------------- sub get_reading { my $self = shift ; my $Stoker = { 'sensors' => [] , 'blowers' => [] , } ; #{ "stoker" : { #"sensors" : #[ #{ "id" : id, "name" : name, "al" : al, "ta" : ta, "th" : th, "tl" : tl, "tc" : tc, "blower" : blower_id }, #... #], #"blowers" : #[ #{ "id" : id, "name" : name, "on": on }, #... #] #} #} my $Html = $self->get_html() ; if (! $Html) { return ({'stoker' => $Stoker }) ; } while ($Html =~ /]*><\/td>\s*([^<]+)<\/td>/gx) { my $Id = $1 ; my $Name = $2 ; my $Current = $3 ; push (@{$Stoker->{'sensors'}} , { "id" => $Id , "name" => $Name , "al" => '' , "ta" => '' , "th" => '' , "tl" => '' , "tc" => $Current , "blower" => undef , }) ; } ## print Dumper ($Stoker) ; return ({'stoker' => $Stoker }) ; } ## get_reading ## #---------------------------------------------------------------------- # get_json #---------------------------------------------------------------------- sub get_json { my $self = shift ; my $Json = JSON->new () ; return ($Json->pretty->encode ($self->get_reading())) ; } ## get_json ## 1 ;