23
23
24
24
use Dancer2 ' :script' ;
25
25
use Dancer2::Plugin::DBIC qw( schema resultset rset) ;
26
+ use Log::Report syntax => ' LONG' ;
26
27
use Wihlo;
27
28
use Device::VantagePro;
28
29
use DateTime;
29
30
use DateTime::Format::DBI;
30
31
use HTTP::Request::Common qw( GET) ;
31
32
use LWP::UserAgent;
32
33
use URI;
33
- use Ouch;
34
- use Try::Tiny;
35
34
36
35
sub rainin ($);
37
36
42
41
$arg_hsh {port } = $config -> {stations }-> {vp }-> {device };
43
42
44
43
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;
46
45
47
46
my $timezone = $vp -> get_timezone;
48
47
my $arc_period = $vp -> get_archive_period;
63
62
$data = $vp -> do_dmpaft;
64
63
}
65
64
66
- unless ( @{$data } ) { debug " No records retrieved" };
65
+ unless ( @{$data } ) { report TRACE => " No records retrieved" };
67
66
68
67
my $dtf = schema-> storage-> datetime_parser; # XXXX Is this expensive? Move inside loop?
69
68
my $changing = 0; my $lastdata ;
83
82
minute => $d -> {min },
84
83
time_zone => $timezone ,
85
84
);
86
- } catch {
85
+ };
86
+ if ($@ ) {
87
87
# Possible invalid time, because logger forwards
88
88
# its clocks one hour too late (for GMT/BST at least).
89
89
# Attempt again, with time one hour forward.
155
155
};
156
156
if (rset(' Reading' )-> search({ datetime => $dtf -> format_datetime($row -> {datetime }) })-> count)
157
157
{
158
- warning " Reading already exists for DTG $row ->{datetime}. Not inserting." ;
158
+ report INFO => " Reading already exists for DTG $row ->{datetime}. Not inserting." ;
159
159
} else
160
160
{
161
161
my $dbrecord = rset(' Reading' )-> create($row );
162
- debug " Inserted record ID " .$dbrecord -> id;
162
+ report TRACE => " Inserted record ID " .$dbrecord -> id;
163
163
}
164
164
165
165
$lastdata = " $d ->{date_stamp},$d ->{time_stamp}" ; # if $d->{unixtime} > $lastunix;
191
191
};
192
192
193
193
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" ;
199
197
}
200
198
}
201
199
220
218
};
221
219
say STDERR $data -> {dateutc };
222
220
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" ;
228
224
}
229
225
}
230
226
@@ -245,13 +241,11 @@ sub upload_wow
245
241
246
242
my $uri = $config -> {upload }-> {wow }-> {url };
247
243
248
- eval { putdata($uri , \%newdata ) };
249
- if (hug )
244
+ try { putdata($uri , \%newdata ) };
245
+ if ($@ )
250
246
{
251
- debug " Failed to upload to WOW: " .bleep;
252
- return ;
247
+ report ERROR => " Failed to upload to WOW: $@ " ;
253
248
}
254
- return 1;
255
249
}
256
250
257
251
sub upload_wg
@@ -271,18 +265,15 @@ sub upload_wg
271
265
my $uri = $config -> {upload }-> {wunderground }-> {url };
272
266
273
267
my $content ;
274
- eval { $content = putdata($uri , \%newdata ) };
275
- if (hug )
268
+ try { $content = putdata($uri , \%newdata ) };
269
+ if ($@ )
276
270
{
277
- debug " Failed to upload to Wunderground: " .bleep;
278
- return ;
271
+ report ERROR => " Failed to upload to Wunderground: $@ " ;
279
272
}
280
273
elsif ($content ne " success" )
281
274
{
282
- debug " Failed to upload to Wunderground. Returned: $content " ;
283
- return ;
275
+ report ERROR => " Failed to upload to Wunderground. Returned: $content " ;
284
276
}
285
- return 1;
286
277
}
287
278
288
279
sub putdata ($$)
@@ -300,11 +291,11 @@ ($$)
300
291
$content =~ s /\s +$// ;
301
292
unless ($code == 200)
302
293
{
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 ;
304
295
}
305
296
}
306
297
else {
307
- ouch ' failput ' , " Failed to upload: " . $response -> status_line;
298
+ report ERROR => __x " Failed to upload: {status} " , status => $response -> status_line;
308
299
}
309
300
return $content ;
310
301
}
0 commit comments