OpenGbg/Service/AirQuality/Measurement.pm

filter: Statementstmt 34.2% 26 / 76 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 27.2% 9 / 33 Podpod n/a totaltotal 26.5% 35 / 132 CC 11 SCAR 18.7
CC
11
Cov
27%
index_to_levels
30.0
microutf_to_ascii
12.0
solar_insolation
6.0
CRAP 6.5
↑ Summary OpenGbg/Service/StyrOchStall.pm »
1 1 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package OpenGbg::Service::AirQuality::Measurement;
6  
7 # ABSTRACT: An air quality measurement
8 our $AUTHORITY = 'cpan:CSSON'; # AUTHORITY
9 our $VERSION = '0.1402';
10  
11 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use syntax 'qs';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use XML::Rabbit;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use DateTime::Format::HTTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use MooseX::AttributeShortcuts;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use OpenGbg::DateTimeType qw/DateTime/;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # any attributes/methods added here should also be 'handled' in GetLatestMeasurement.
19  
20 has_xpath_value _starttime => './x:StartTime';
21 has_xpath_value _endtime => './x:StopTime';
22  
23 has_xpath_value temperature => './x:Weather/x:Temperature/x:Value';
24 has_xpath_value _temperature_unit => './x:Weather/x:Temperature/x:Unit';
25 has_xpath_value humidity => './x:Weather/x:Humidity/x:Value';
26 has_xpath_value humidity_unit => './x:Weather/x:Humidity/x:Unit';
27 has_xpath_value _solar_insolation => './x:Weather/x:SolarInsolation/x:Value';
28 has_xpath_value solar_insolation_unit => './x:Weather/x:SolarInsolation/x:Unit';
29 has_xpath_value air_pressure => './x:Weather/x:AirPressure/x:Value';
30 has_xpath_value air_pressure_unit => './x:Weather/x:AirPressure/x:Unit';
31 has_xpath_value wind_speed => './x:Weather/x:WindSpeed/x:Value';
32 has_xpath_value wind_speed_unit => './x:Weather/x:WindSpeed/x:Unit';
33 has_xpath_value wind_direction => './x:Weather/x:WindDirection/x:Value';
34 has_xpath_value _wind_direction_unit => './x:Weather/x:WindDirection/x:Unit';
35 has_xpath_value _rainfall => './x:Weather/x:RainFall/x:Value';
36 has_xpath_value rainfall_unit => './x:Weather/x:RainFall/x:Unit';
37  
38 has_xpath_value total_index => './x:AirQuality/x:TotalIndex';
39  
40 has_xpath_value no2 => './x:AirQuality/x:NO2/x:Value';
41 has_xpath_value _no2_unit => './x:AirQuality/x:NO2/x:Unit';
42 has_xpath_value no2_index => './x:AirQuality/x:NO2/x:Index';
43  
44 has_xpath_value so2 => './x:AirQuality/x:SO2/x:Value';
45 has_xpath_value _so2_unit => './x:AirQuality/x:SO2/x:Unit';
46 has_xpath_value so2_index => './x:AirQuality/x:SO2/x:Index';
47  
48 has_xpath_value o3 => './x:AirQuality/x:O3/x:Value';
49 has_xpath_value _o3_unit => './x:AirQuality/x:O3/x:Unit';
50 has_xpath_value o3_index => './x:AirQuality/x:O3/x:Index';
51  
52 has_xpath_value pm10 => './x:AirQuality/x:PM10/x:Value';
53 has_xpath_value _pm10_unit => './x:AirQuality/x:PM10/x:Unit';
54 has_xpath_value pm10_index => './x:AirQuality/x:PM10/x:Index';
55  
56 has_xpath_value co => './x:AirQuality/x:CO/x:Value';
57 has_xpath_value _co_unit => './x:AirQuality/x:CO/x:Unit';
58 has_xpath_value co_index => './x:AirQuality/x:CO/x:Index';
59  
60 has_xpath_value nox => './x:AirQuality/x:NOx/x:Value';
61 has_xpath_value _nox_unit => './x:AirQuality/x:NOx/x:Unit';
62 has_xpath_value nox_index => './x:AirQuality/x:NOx/x:Index';
63  
64 has_xpath_value pm2_5 => './x:AirQuality/x:PM2_5/x:Value';
65 has_xpath_value _pm2_5_unit => './x:AirQuality/x:PM2_5/x:Unit';
66 has_xpath_value pm2_5_index => './x:AirQuality/x:PM2_5/x:Index';
67  
68  
69 sub solar_insolation {
70 0 ▶ my $self = shift;
Subroutine solar_insolation: not called CC=2 SCAR=17.9
71 0 ▶ return length $self->_solar_insolation ? $self->_solar_insolation : 0;
Branch: 1 branch
BranchTrueFalseTotal
length $self->_solar_insolation ? : 0 0 0
72 }
73 sub rainfall {
74 0 ▶ my $self = shift;
Subroutine rainfall: not called CC=2 SCAR=17.9
75 0 ▶ return length $self->_rainfall ? $self->_rainfall : 0;
Branch: 1 branch
BranchTrueFalseTotal
length $self->_rainfall ? : 0 0 0
76 }
77 sub temperature_unit {
78 0 ▶ my $self = shift;
Subroutine temperature_unit: not called CC=2 SCAR=17.9
79 0 ▶ return $self->_temperature_unit eq '°C' ? 'degrees C' : $self->_temperature_unit;
Branch: 1 branch
BranchTrueFalseTotal
$self->_temperature_unit eq "\260C" ? : 0 0 0
80 }
81 sub wind_direction_unit {
82 0 ▶ my $self = shift;
Subroutine wind_direction_unit: not called CC=2 SCAR=17.9
83 0 ▶ return $self->_wind_direction_unit eq '°' ? 'degrees' : $self->_wind_direction_unit;
Branch: 1 branch
BranchTrueFalseTotal
$self->_wind_direction_unit eq "\260" ? : 0 0 0
84 }
85  
86 sub microutf_to_ascii {
87 0 ▶ my $self = shift;
Subroutine microutf_to_ascii: not called CC=3 SCAR=24.8
88 0 my $unit = shift;
89  
90 0 ▶ return $self->_pm2_5_unit eq 'µg/m3' || $self->_pm2_5_unit eq 'ug/m3' ? 'microgram/m3' : $unit;
Branch: 1 branch
BranchTrueFalseTotal
$self->_pm2_5_unit eq "\265g/m3" || $self->_pm2_5_unit eq 'ug/m3' ? : 0 0 0
Condition0%0/3
$self->_pm2_5_unit eq "\x{b5}g/m3" or $self->_pm2_5_unit eq "ug/m3"
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->_pm2_5_unit eq "\x{b5}g/m3" or $self->_pm2_5_unit eq "ug/m3"
$self->_pm2_5_unit eq "\x{b5}g/m3" $self->_pm2_5_unit eq "ug/m3"
Truth table0%0/3
A or B
A= $self->_pm2_5_unit eq "\x{b5}g/m3"B= $self->_pm2_5_unit eq "ug/m3"
ABresult
1X1
011
000
91 }
92  
93 sub no2_unit {
94 0 ▶ my $self = shift;
Subroutine no2_unit: not called CC=1 SCAR=6.9
95 0 return $self->microutf_to_ascii($self->_no2_unit);
96 }
97 sub so2_unit {
98 0 ▶ my $self = shift;
Subroutine so2_unit: not called CC=1 SCAR=6.9
99 0 return $self->microutf_to_ascii($self->_so2_unit);
100 }
101 sub o3_unit {
102 0 ▶ my $self = shift;
Subroutine o3_unit: not called CC=1 SCAR=6.9
103 0 return $self->microutf_to_ascii($self->_o3_unit);
104 }
105 sub co_unit {
106 0 ▶ my $self = shift;
Subroutine co_unit: not called CC=1 SCAR=6.9
107 0 return $self->microutf_to_ascii($self->_co_unit);
108 }
109 sub pm10_unit {
110 0 ▶ my $self = shift;
Subroutine pm10_unit: not called CC=1 SCAR=6.9
111 0 return $self->microutf_to_ascii($self->_pm10_unit);
112 }
113 sub nox_unit {
114 0 ▶ my $self = shift;
Subroutine nox_unit: not called CC=1 SCAR=6.9
115 0 return $self->microutf_to_ascii($self->_nox_unit);
116 }
117 sub pm2_5_unit {
118 0 ▶ my $self = shift;
Subroutine pm2_5_unit: not called CC=1 SCAR=6.9
119 0 return $self->microutf_to_ascii($self->_pm2_5_unit);
120 }
121  
122 sub index_to_levels {
123 0 ▶ my $index = shift;
Subroutine index_to_levels: not called CC=5 SCAR=34.0
124 0 ▶ return $index if !$index;
Branch: 1 branch
BranchTrueFalseTotal
unless $index 0 0 0
125 0 ▶ return $index >= 3 ? 'very high levels'
Branch: 3 branches
BranchTrueFalseTotal
$index >= 3 ? : 0 0 0
$index >= 2 ? : 0 0 0
$index >= 1 ? : 0 0 0
126 : $index >= 2 ? 'high levels'
127 : $index >= 1 ? 'medium levels'
128 : 'low levels'
129 ;
130 }
131 sub total_levels {
132 0 ▶ my $self = shift;
Subroutine total_levels: not called CC=1 SCAR=6.9
133 0 return $self->index_to_levels($self->total_index);
134 }
135 sub no2_levels {
136 0 ▶ my $self = shift;
Subroutine no2_levels: not called CC=1 SCAR=6.9
137 0 return $self->index_to_levels($self->no2_index);
138 }
139 sub so2_levels {
140 0 ▶ my $self = shift;
Subroutine so2_levels: not called CC=1 SCAR=6.9
141 0 return $self->index_to_levels($self->so2_index);
142 }
143 sub o3_levels {
144 0 ▶ my $self = shift;
Subroutine o3_levels: not called CC=1 SCAR=6.9
145 0 return $self->index_to_levels($self->o3_index);
146 }
147 sub co_levels {
148 0 ▶ my $self = shift;
Subroutine co_levels: not called CC=1 SCAR=6.9
149 0 return $self->index_to_levels($self->co_index);
150 }
151 sub pm10_levels {
152 0 ▶ my $self = shift;
Subroutine pm10_levels: not called CC=1 SCAR=6.9
153 0 return $self->index_to_levels($self->pm10_index);
154 }
155 sub nox_levels {
156 0 ▶ my $self = shift;
Subroutine nox_levels: not called CC=1 SCAR=6.9
157 0 return $self->index_to_levels($self->nox_index);
158 }
159 sub pm2_5_levels {
160 0 ▶ my $self = shift;
Subroutine pm2_5_levels: not called CC=1 SCAR=6.9
161 0 return $self->index_to_levels($self->pm2_5_index);
162 }
163  
164  
165 has starttime => (
166 is => 'ro',
167 isa => DateTime,
168 lazy => 1,
169 builder => 1,
170 );
171  
172 has endtime => (
173 is => 'ro',
174 isa => DateTime,
175 lazy => 1,
176 builder => 1,
177 );
178  
179 sub _build_starttime {
180 0 ▶ my $self = shift;
Subroutine _build_starttime: not called CC=1 SCAR=6.9
181 0 return DateTime::Format::HTTP->parse_datetime($self->_starttime);
182 }
183 sub _build_endtime {
184 0 ▶ my $self = shift;
Subroutine _build_endtime: not called CC=1 SCAR=6.9
185 0 return DateTime::Format::HTTP->parse_datetime($self->_endtime);
186 }
187  
188 sub to_text {
189 0 ▶ my $self = shift;
Subroutine to_text: not called CC=1 SCAR=6.9
190 0 return sprintf "\n%s %s -- %s %s\n%s%s", $self->starttime->ymd('-'),
191 $self->starttime->hms(':'),
192 $self->endtime->ymd('-'),
193 $self->endtime->hms(':'),
194 $self->weather_to_text,
195 $self->air_quality_to_text;
196 }
197  
198 sub weather_to_text {
199 my $self = shift;
200  
201 return sprintf qs{
202 Temperature: %7.2f %s
203 Humiditiy: %7.2f %s
204 Solar insolation: %7.2f %s
205 Air pressure: %7.2f %s
206 Wind speed: %7.2f %s
207 Wind direction: %7.2f %s
208 Rainfall: %7.2f %s
209 },
210 $self->temperature,
211 $self->temperature_unit,
212 $self->humidity,
213 $self->humidity_unit,
214 $self->solar_insolation,
215 $self->solar_insolation_unit,
216 $self->air_pressure,
217 $self->air_pressure_unit,
218 $self->wind_speed,
219 $self->wind_speed_unit,
220 $self->wind_direction,
221 $self->wind_direction_unit,
222 $self->rainfall,
223 $self->rainfall_unit
224 ;
225 }
226  
227 sub air_quality_to_text {
228 my $self = shift;
229  
230 no warnings 'numeric';
231 return sprintf qs{
232 Total index: [ %4s ] [ %-16s ]
233 Nitrogen dioxide: %7.2f %s [ %4s ] [ %-16s ]
234 Nitrogen oxides: %7.2f %s [ %4s ] [ %-16s ]
235 Sulfur dioxide: %7.2f %s [ %4s ] [ %-16s ]
236 Carbon monoxide: %7.2f %s [ %4s ] [ %-16s ]
237 Ground level ozone: %7.2f %s [ %4s ] [ %-16s ]
238 <10mm particulates: %7.2f %s [ %4s ] [ %-16s ]
239 <2.5mm particulates: %7.2f %s [ %4s ] [ %-16s ]
240 },
241 $self->total_index,
242 $self->total_levels,
243 $self->no2,
244 $self->no2_unit,
245 $self->no2_index,
246 $self->no2_levels,
247 $self->so2,
248 $self->so2_unit,
249 $self->so2_index,
250 $self->so2_levels,
251 $self->o3,
252 $self->o3_unit,
253 $self->o3_index,
254 $self->o3_levels,
255 $self->pm10,
256 $self->pm10_unit,
257 $self->pm10_index,
258 $self->pm10_levels,
259 $self->co,
260 $self->co_unit,
261 $self->co_index,
262 $self->co_levels,
263 $self->nox,
264 $self->nox_unit,
265 $self->nox_index,
266 $self->nox_levels,
267 $self->pm2_5,
268 $self->pm2_5_unit,
269 $self->pm2_5_index,
270 $self->pm2_5_levels,
271 ;
272 }
273  
274 finalize_class();
275  
276 1;
277  
278 __END__
279  
280 =pod
281  
282 =encoding utf-8
283  
284 =head1 SYNOPSIS
285  
286 my $service = OpenGbg->new->air_quality;
287 my $response = $service->get_latest_measurement;
288 my $measurement = $response->measurement;
289  
290 printf '%5.2f %s' => $measurement->temperature, $measurement->temperature_unit;
291  
292 =head1 WEATHER ATTRIBUTES
293  
294 All of these attribues are decimal numbers.
295  
296 For all weather attributes there is also a unit attribute (C<${attribute_name}_unit>) that returns a textual representation the unit.
297  
298 =head2 temperature
299  
300 The temperature (in degrees centigrade) during the measurement.
301  
302 =head2 humidity
303  
304 The humidity (in percent) during the measurement.
305  
306 =head2 solar_insolation
307  
308 The solar insolation (in Watts/square metre) during the measurement.
309  
310 =head2 air_pressure
311  
312 The air pressure (in hPa) during the measurement.
313  
314 =head2 wind_speed
315  
316 The wind speed (in metres/second) during the mesaurement.
317  
318 =head2 wind_direction
319  
320 The wind direction (in degrees) during the measurement.
321  
322 =head2 rainfall
323  
324 The rainfall (in mm) during the measurement.
325  
326  
327 =head1 AIR QUALITY ATTRIBUTES
328  
329 All of these attribues are normally decimal numbers. Empty string or undef denotes missing information.
330  
331 The unit for these attributes is microgram/cubic metre.
332  
333 All air quality attributes also have these additional attributes:
334  
335 C<${attribute_name}_unit> returns a textual representation the unit the value is given in.
336  
337 C<${attribute_name}_index> returns a decimal number indicating the polution level for that particular attribute.
338  
339 C<${attribute_name}_levels> is like the C<*_index> attribute, but in text. Values are C<low levels>, C<medium levels>, C<high levels>, C<very high levels>). Empty string/undef if the index is the empty string/undef.
340  
341  
342 B<Note:> The web service sometimes returns I<negative> values for some of these measurements.
343  
344 =head2 total_index
345  
346 The overall Air Quality Index. Calculated from some of the other indices.
347  
348 =head2 co
349  
350 The amount of carbon monoxide in the air.
351  
352 =head2 o3
353  
354 The amount of ground level ozone in the air.
355  
356 =head2 no2
357  
358 The amount of nitrogen dioxide in the air.
359  
360 =head2 nox
361  
362 The amount of nitrogen oxides in the air.
363  
364 =head2 pm2_5
365  
366 The amount of particulates (less than 2.5 micrometers) in the air.
367  
368 =head2 pm10
369  
370 The amount of particulates (less than 10 micrometers) in the air.
371  
372 =head2 so2
373  
374 The amount of sulfur dioxide in the air.
375  
376 =head1 METHODS
377  
378 =head2 to_text()
379  
380 Returns a string with the station data in a table.
381  
382 =cut
383  
384 __END__
385  
386 =pod
387  
388 =encoding UTF-8
389  
390 =head1 NAME
391  
392 OpenGbg::Service::AirQuality::Measurement - An air quality measurement
393  
394 =head1 VERSION
395  
396 Version 0.1402, released 2016-08-12.
397  
398 =head1 SOURCE
399  
400 L<https://github.com/Csson/p5-OpenGbg>
401  
402 =head1 HOMEPAGE
403  
404 L<https://metacpan.org/release/OpenGbg>
405  
406 =head1 AUTHOR
407  
408 Erik Carlsson <info@code301.com>
409  
410 =head1 COPYRIGHT AND LICENSE
411  
412 This software is copyright (c) 2016 by Erik Carlsson.
413  
414 This is free software; you can redistribute it and/or modify it under
415 the same terms as the Perl 5 programming language system itself.
416  
417 =cut
↑ Summary OpenGbg/Service/StyrOchStall.pm »