Skip to content

Commit 4002c92

Browse files
committed
Switch to Log::Report for read script. Abort on failed uploads.
1 parent 1305558 commit 4002c92

File tree

1 file changed

+22
-31
lines changed

1 file changed

+22
-31
lines changed

bin/read.pl

+22-31
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,14 @@
2323

2424
use Dancer2 ':script';
2525
use Dancer2::Plugin::DBIC qw(schema resultset rset);
26+
use Log::Report syntax => 'LONG';
2627
use Wihlo;
2728
use Device::VantagePro;
2829
use DateTime;
2930
use DateTime::Format::DBI;
3031
use HTTP::Request::Common qw(GET);
3132
use LWP::UserAgent;
3233
use URI;
33-
use Ouch;
34-
use Try::Tiny;
3534

3635
sub rainin($);
3736

@@ -42,7 +41,7 @@
4241
$arg_hsh{port} = $config->{stations}->{vp}->{device};
4342

4443
my $vp = new Device::VantagePro(\%arg_hsh);
45-
error "Failed to wake up device" && exit unless $vp->wake_up() == 1;
44+
report ERROR => "Failed to wake up device" unless $vp->wake_up() == 1;
4645

4746
my $timezone = $vp->get_timezone;
4847
my $arc_period = $vp->get_archive_period;
@@ -63,7 +62,7 @@
6362
$data = $vp->do_dmpaft;
6463
}
6564

66-
unless ( @{$data} ) { debug "No records retrieved" };
65+
unless ( @{$data} ) { report TRACE => "No records retrieved" };
6766

6867
my $dtf = schema->storage->datetime_parser; # XXXX Is this expensive? Move inside loop?
6968
my $changing = 0; my $lastdata;
@@ -83,7 +82,8 @@
8382
minute => $d->{min},
8483
time_zone => $timezone,
8584
);
86-
} catch {
85+
};
86+
if ($@) {
8787
# Possible invalid time, because logger forwards
8888
# its clocks one hour too late (for GMT/BST at least).
8989
# Attempt again, with time one hour forward.
@@ -155,11 +155,11 @@
155155
};
156156
if (rset('Reading')->search({ datetime => $dtf->format_datetime($row->{datetime}) })->count)
157157
{
158-
warning "Reading already exists for DTG $row->{datetime}. Not inserting.";
158+
report INFO => "Reading already exists for DTG $row->{datetime}. Not inserting.";
159159
} else
160160
{
161161
my $dbrecord = rset('Reading')->create($row);
162-
debug "Inserted record ID ".$dbrecord->id;
162+
report TRACE => "Inserted record ID ".$dbrecord->id;
163163
}
164164

165165
$lastdata = "$d->{date_stamp},$d->{time_stamp}"; # if $d->{unixtime} > $lastunix;
@@ -191,11 +191,9 @@
191191
};
192192

193193

194-
if (upload_wg($data))
195-
{
196-
$u->update({ uploaded_wg => 1 });
197-
debug "Uploaded record $dt successfully to Wunderground";
198-
}
194+
upload_wg($data); # Fatal on error
195+
$u->update({ uploaded_wg => 1 });
196+
report TRACE => "Uploaded record $dt successfully to Wunderground";
199197
}
200198
}
201199

@@ -220,11 +218,9 @@
220218
};
221219
say STDERR $data->{dateutc};
222220

223-
if (upload_wow($data))
224-
{
225-
$u->update({ uploaded_wow => 1 });
226-
debug "Uploaded record $dt successfully to WOW";
227-
}
221+
upload_wow($data); # Fatal on error
222+
$u->update({ uploaded_wow => 1 });
223+
report TRACE => "Uploaded record $dt successfully to WOW";
228224
}
229225
}
230226

@@ -245,13 +241,11 @@ sub upload_wow
245241

246242
my $uri = $config->{upload}->{wow}->{url};
247243

248-
eval { putdata($uri, \%newdata) };
249-
if (hug)
244+
try { putdata($uri, \%newdata) };
245+
if ($@)
250246
{
251-
debug "Failed to upload to WOW: ".bleep;
252-
return;
247+
report ERROR => "Failed to upload to WOW: $@";
253248
}
254-
return 1;
255249
}
256250

257251
sub upload_wg
@@ -271,18 +265,15 @@ sub upload_wg
271265
my $uri = $config->{upload}->{wunderground}->{url};
272266

273267
my $content;
274-
eval { $content = putdata($uri, \%newdata) };
275-
if (hug)
268+
try { $content = putdata($uri, \%newdata) };
269+
if ($@)
276270
{
277-
debug "Failed to upload to Wunderground: ".bleep;
278-
return;
271+
report ERROR => "Failed to upload to Wunderground: $@";
279272
}
280273
elsif ($content ne "success")
281274
{
282-
debug "Failed to upload to Wunderground. Returned: $content";
283-
return;
275+
report ERROR => "Failed to upload to Wunderground. Returned: $content";
284276
}
285-
return 1;
286277
}
287278

288279
sub putdata($$)
@@ -300,11 +291,11 @@ ($$)
300291
$content =~ s/\s+$//;
301292
unless ($code == 200)
302293
{
303-
ouch 'failupload', "Failed to upload. Return code: $code, Response: $content";
294+
report ERROR => __x"Failed to upload. Return code: {code}, Response: {content}", code => $code, content => $content;
304295
}
305296
}
306297
else {
307-
ouch 'failput', "Failed to upload: ".$response->status_line;
298+
report ERROR => __x"Failed to upload: {status}", status => $response->status_line;
308299
}
309300
return $content;
310301
}

0 commit comments

Comments
 (0)