sub process_data {
if ($debug) {
say "Processing polled data...."
}
my $json_line = shift(@_);
my $dt = DateTime->now();
$dt->set_time_zone( 'America/Chicago' );
my $sth;
my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile" ,"","");
$q = "insert into weather_data values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
$sth = $dbh->prepare($q);
if ( (exists($json->{'bbl'})) and (not exists($json->{'ra'}))) {
$sth->execute(undef, $dt->datetime(), $dt->ymd(), $dt->hms(),
$json->{'wd'}, $json->{'wv'}, $json->{'tF'}, $json->{'tC'},
$json->{'bp'}, $json->{'rh'}, $json->{'li'}, $json->{'ov'},
$json->{'lux'}, $json->{'bbl'}, $json->{'irl'}, 'null', 'null'
);
} elsif ( (exists($json->{'ra'})) and (exists($json->{'bbl'}))) {
$sth->execute(undef,$dt->datetime(), $dt->ymd(), $dt->hms(),
$json->{'wd'}, $json->{'wv'}, $json->{'tF'}, $json->{'tC'},
$json->{'bp'}, $json->{'rh'}, $json->{'li'}, $json->{'ov'},
$json->{'lux'}, $json->{'bbl'}, $json->{'irl'}, $json->{'ra'}, $json->{'rr'}
);
} elsif ( (exists($json->{'ra'})) and (not exists($json->{'bbl'}))) {
$sth->execute(undef,$dt->datetime(), $dt->ymd(), $dt->hms(),
$json->{'wd'}, $json->{'wv'}, $json->{'tF'}, $json->{'tC'},
$json->{'bp'}, $json->{'rh'}, $json->{'li'}, $json->{'ov'},
'null', 'null', 'null', $json->{'ra'}, $json->{'rr'}
);
} elsif ( (not exists($json->{'ra'})) and (not exists($json->{'bbl'}))) {
$sth->execute(undef,$dt->datetime(), $dt->ymd(), $dt->hms(),
$json->{'wd'}, $json->{'wv'}, $json->{'tF'}, $json->{'tC'},
$json->{'bp'}, $json->{'rh'}, $json->{'li'}, $json->{'ov'},
'null', 'null', 'null', 'null', 'null'
);
}
$sth->finish;
$dbh->disconnect;
}
####
sub process_data {
if ($debug) {
say "Processing polled data...."
}
my $json_line = shift(@_);
my $dt = DateTime->now();
$dt->set_time_zone( 'America/Chicago' );
my $sth;
my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile" ,"","");
$q = "insert into weather_data values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
$sth = $dbh->prepare($q);
if ( not exists($json->{'ra'}) ) {
$json->{ra} = $json->{rr} = 'null';
}
if ( not exists($json->{'bbl'}) ) {
$json->{bbl} = $json->{irl} = $json->{lux} = 'null';
}
$sth->execute(undef,$dt->datetime(), $dt->ymd(), $dt->hms(),
$json->{'wd'}, $json->{'wv'}, $json->{'tF'}, $json->{'tC'},
$json->{'bp'}, $json->{'rh'}, $json->{'li'}, $json->{'ov'},
$json->{'lux'}, $json->{'bbl'}, $json->{'irl'}, $json->{'ra'}, $json->{'rr'}
);
$sth->finish;
$dbh->disconnect;
}
####
# Up above your main loop
my $DBH;
my $STH = setup_database();
while (1) {
. . . SNIP . . .
}
$DBH->disconnect;
sub setup_database {
my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile" ,"","");
my $q = "insert into weather_data values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
return $dbh->prepare($q);
}
sub process_data {
if ($debug) {
say "Processing polled data...."
}
my $json_line = shift(@_);
my $dt = DateTime->now();
$dt->set_time_zone( 'America/Chicago' );
if ( not exists($json->{'ra'}) ) {
$json->{ra} = $json->{rr} = 'null';
}
if ( not exists($json->{'bbl'}) ) {
$json->{bbl} = $json->{irl} = $json->{lux} = 'null';
}
$STH->execute(undef,$dt->datetime(), $dt->ymd(), $dt->hms(),
$json->{'wd'}, $json->{'wv'}, $json->{'tF'}, $json->{'tC'},
$json->{'bp'}, $json->{'rh'}, $json->{'li'}, $json->{'ov'},
$json->{'lux'}, $json->{'bbl'}, $json->{'irl'}, $json->{'ra'}, $json->{'rr'}
);
}
####
my $q = "insert into weather_data("
. " reading_DTM, reading_YMD, reading_HMS, wd,"
. " wv, tF, tC, bp,"
. " rh, li, ov, lux,"
. " bbl, irl, ra, rr"
. ")"
. "values ("
. " ?, ?, ?, ?,"
. " ?, ?, ?, ?,"
. " ?, ?, ?, ?,"
. " ?, ?, ?, ?"
. ")";
####
sub process_data {
my $rData = shift; # Fetch the reference to the $json data
my $json = { %$data }; # Make a copy of it
. . . the rest of the subroutine . . .
}
####
my $json = # create a new scalar
{ # and we'll make it a new hash reference containing:
% # the list of key/value elements in the hash
$data # in the $data hash reference
};
####
sub process_data {
if ($debug) {
say "Processing polled data...."
}
my $data = shift(@_);
my %JSON = %$data; ### Copy the data into a hash ###
my $dt = DateTime->now();
$dt->set_time_zone( 'America/Chicago' );
if ( not exists($JSON{ra}) ) {
$JSON{ra} = $JSON{rr} = 'null';
}
if ( not exists($JSON{bbl}) ) {
$JSON{bbl} = $JSON{irl} = $JSON{lux} = 'null';
}
$STH->execute($dt->datetime(), $dt->ymd(), $dt->hms(), $JSON{'wd'},
$JSON{'wv'}, $JSON{'tF'}, $JSON{'tC'}, $JSON{'bp'},
$JSON{'rh'}, $JSON{'li'}, $JSON{'ov'}, $JSON{'lux'},
$JSON{'bbl'}, $JSON{'irl'}, $JSON{'ra'}, $JSON{'rr'}
);
}
####
$STH->execute($dt->datetime(), $dt->ymd(), $dt->hms(), $JSON{'wd'},
$JSON{'wv'}, $JSON{'tF'}, $JSON{'tC'}, $JSON{'bp'},
$JSON{'rh'}, $JSON{'li'}, $JSON{'ov'}, $JSON{'lux'},
$JSON{'bbl'}, $JSON{'irl'}, $JSON{'ra'}, $JSON{'rr'}
);
####
$STH->execute($dt->datetime(), $dt->ymd(), $dt->hms(),
@JSON{ 'wd', 'wv', 'tF', 'tC', 'bp', 'rh',
'li', 'ov', 'lux', 'bbl', 'irl', 'ra', 'rr'
}
);