FishFarm.pm

filter: Statementstmt 87.0% 134 / 154 Branchbran 46.8% 15 / 32 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 22 / 22 Podpod 100% 12 / 12 totaltotal 81.7% 184 / 225 CC 18 SCAR 10.7
CC
18
Cov
79%
check_feeder
7.0
render_leds
5.7
render_buzzer
4.1
CRAP 2.9
« FishFarm/WaterFiltration.pm ↑ Summary FishFarm/WaterLevelMaintainer.pm »
1 package Acme::FishFarm;
2  
3 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Carp "croak";
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Acme::FishFarm::Feeder;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Acme::FishFarm::OxygenMaintainer;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Acme::FishFarm::WaterConditionMonitor;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Acme::FishFarm::WaterLevelMaintainer;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Acme::FishFarm::WaterFiltration;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =head1 NAME
15  
16 Acme::FishFarm - A Fish Farm with Automated Systems
17  
18 =head1 VERSION
19  
20 Version 1.00
21  
22 =cut
23  
24 2 our $VERSION = '1.00';
25  
26  
27 =head1 SYNOPSIS
28  
29 use Acme::FishFarm;
30 # missing stuff will be added in the next release
31
32 =head1 EXPORT
33  
34 The tag C<:all> can be used to import all the functions available in this module.
35  
36 =cut
37  
38 2 ▶use Exporter qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
39 2 our @EXPORT_OK = qw(
40 install_all_systems
41 reduce_precision consume_oxygen
42 check_DO check_pH check_temperature check_turbidity check_water_filter check_water_level check_feeder
43 render_leds render_buzzer
44 );
45 2 our %EXPORT_TAGS = (
46 all => [ qw( install_all_systems reduce_precision consume_oxygen check_DO check_pH check_temperature
47 check_turbidity check_water_filter check_water_level check_feeder render_leds
48 render_buzzer ) ],
49 );
50  
51 =head1 SYSTEM INSTALLATION RELATED SUBROUTINES
52  
53 =head2 install_all_systems
54  
55 Installs all the available systems with default values and returns them as a list of C<Acme::FishFarm::*> objects in the following sequence:
56  
57 C<(Feeder, OxygenMaintainer, WaterConditionMonitor, WaterLevelMaintainer, WaterFiltration)>
58  
59 Take note that only the water condition monitor currently supports the oxygen maintainer only.
60  
61 =cut
62  
63 sub install_all_systems {
64 1 ▶ my $feeder = Acme::FishFarm::Feeder->install;
Subroutine install_all_systems: called CC=1 SCAR=0.0
Pod documented
65 1 my $oxygen_maintainer = Acme::FishFarm::OxygenMaintainer->install;
66 1 my $water_monitor = Acme::FishFarm::WaterConditionMonitor->install;
67 1 my $water_level = Acme::FishFarm::WaterLevelMaintainer->install;
68 1 my $water_filter = Acme::FishFarm::WaterFiltration->install;
69
70 # remember to connect water oxygem maintainer to water condition monitoring :)
71 1 $water_monitor->add_oxygen_maintainer( $oxygen_maintainer );
72
73 1 ( $feeder, $oxygen_maintainer, $water_monitor, $water_level, $water_filter );
74 }
75  
76  
77 =head1 SENSOR READING RELATED SUBROUTINES
78  
79 =head2 reduce_precision ( $decimal )
80  
81 Reduces positive or negative C<$decimal> to a 3-decimal value. Make sure to pass in a decimal with more than 3 decimal points.
82  
83 Returns the reduced precision value.
84  
85 This subroutine is useful if you are trying to set the current sensor readings randomly using the built-in C<rand> function as you do not want to end up with too many decimals on the screen.
86  
87 =cut
88  
89 sub reduce_precision {
90 3 ▶ my $sensor_reading = shift;
Subroutine reduce_precision: called CC=2 SCAR=6.9
Pod documented
91 3 ▶ croak "Please pass in a decimal value" if not $sensor_reading =~ /\./;
Branch: 1 branch
BranchTrueFalseTotal
unless $sensor_reading =~ /\./ 1 2 3
92
93 2 $sensor_reading =~ /(-?\d+\.\d{3})/;
94 2 return $1;
95 }
96  
97 =head1 AUTOMATED SYSTEMS RELATED SUBROUTINES
98  
99 All of the subroutines here will give output.
100  
101 Take note that there are some systems that can't be connected to the water monitoring system and therefore will not effect the LEDs or buzzers. These systems are:
102  
103 =over 4
104  
105 =item * Acme::FishFarm::WaterFiltration
106  
107 =item * Acme::FishFarm::WaterLevelMaintainer
108  
109 =back
110  
111 =head2 consume_oxygen ( $oxygen_maintainer, $consumed_oxygen )
112  
113 This will cause the oxygen level (DO level) of the C<Acme::FishFarm::OxygenMaintainer> to reduce by C<$consumed_oxygen mg/L>
114  
115 Returns 1 upon success.
116 =cut
117  
118 sub consume_oxygen {
119 2 ▶ my $o2_maintainer = shift;
Subroutine consume_oxygen: called CC=1 SCAR=0.0
Pod documented
120 2 my $consumed_oxygen = shift;
121 2 print "$consumed_oxygen mg/L of oxygen consumed...\n";
122 2 my $o2_remaining = $o2_maintainer->current_DO - $consumed_oxygen;
123 2 $o2_maintainer->current_DO( $o2_remaining );
124 2 1;
125 }
126  
127 =head2 check_DO ( $oxygen_maintainer, $current_DO_reading )
128  
129 This checks and outputs the condition of the current DO level.
130  
131 Take note that this process will also trigger the LED and buzzer if abnormal condition is present.
132  
133 Returns 1 upon success.
134 =cut
135  
136 sub check_DO {
137 1 ▶ my ( $oxygen, $current_reading ) = @_;
Subroutine check_DO: called CC=2 SCAR=7.1
Pod documented
138 1 my $DO_threshold = $oxygen->DO_threshold;
139  
140 1 $oxygen->current_DO( $current_reading );
141 1 $oxygen->is_low_DO;
142
143 1 print "Current Oxygen Level: ", $current_reading, " mg/L",
144 " (low: < ", $DO_threshold, ")\n";
145  
146 1 ▶ if ( $oxygen->is_low_DO ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($oxygen->is_low_DO) { } 0 1 1
147 0 print " !! Low oxygen level!\n";
148 } else {
149 1 print " Oxygen level is normal.\n";
150 }
151 1 1;
152 }
153  
154 =head2 check_pH ( $water_monitor, $current_ph_reading )
155  
156 This checks and outputs the condition of the current pH value.
157  
158 Take note that this process will also trigger the LED and buzzer if abnormal condition is present.
159  
160 Returns 1 upon success.
161  
162 =cut
163  
164 sub check_pH {
165 1 ▶ my ( $water_monitor, $current_reading ) = @_;
Subroutine check_pH: called CC=2 SCAR=7.1
Pod documented
166 1 my $ph_range = $water_monitor->ph_threshold;
167
168 1 $water_monitor->current_ph( $current_reading );
169 1 $water_monitor->ph_is_normal;
170
171 1 print "Current pH: ", $water_monitor->current_ph,
172 " (normal range: ", $ph_range->[0], "~", $ph_range->[1], ")\n";
173  
174 1 ▶ if ( !$water_monitor->ph_is_normal ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $water_monitor->ph_is_normal) { } 1 0 1
175 1 print " !! Abnormal pH!\n";
176 } else {
177 0 print " pH is normal.\n";
178 }
179 1 1;
180 }
181  
182 =head2 check_temperature ( $water_monitor, $current_temperature_reading )
183  
184 This checks and outputs the condition of the current temperature.
185  
186 Take note that this process will also trigger the LED and buzzer if abnormal condition is present.
187  
188 Returns 1 upon success.
189  
190 =cut
191  
192 sub check_temperature {
193 1 ▶ my ( $water_monitor, $current_reading ) = @_;
Subroutine check_temperature: called CC=2 SCAR=7.1
Pod documented
194 1 my $temperature_range = $water_monitor->temperature_threshold;
195  
196 1 $water_monitor->current_temperature( $current_reading );
197 1 $water_monitor->temperature_is_normal;
198
199 1 print "Current temperature: ", $water_monitor->current_temperature, " C",
200 " (normal range: ", $temperature_range->[0], " C ~ ", $temperature_range->[1], " C)\n";
201  
202 1 ▶ if ( !$water_monitor->temperature_is_normal ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $water_monitor->temperature_is_normal) { } 0 1 1
203 0 print " !! Abnormal temperature!\n";
204 } else {
205 1 print " Temperature is normal.\n";
206 }
207 1 1;
208 }
209  
210 =head2 check_turbidity ( $water_monitor, $current_turbidity_reading )
211  
212 This checks and outputs the condition of the current temperature.
213  
214 Take note that this process will also trigger the LED and buzzer if abnormal condition is present.
215  
216 Returns 1 upon success.
217  
218 =cut
219  
220 sub check_turbidity {
221 1 ▶ my ( $water_monitor, $current_reading ) = @_;
Subroutine check_turbidity: called CC=2 SCAR=7.1
Pod documented
222 1 my $turbidity_threshold = $water_monitor->turbidity_threshold;
223  
224 1 $water_monitor->current_turbidity( $current_reading );
225
226 1 print "Current Turbidity: ", $water_monitor->current_turbidity, " ntu",
227 " (dirty: > ", $turbidity_threshold, ")\n";
228  
229 1 ▶ if ( $water_monitor->water_dirty ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($water_monitor->water_dirty) { } 1 0 1
230 1 print " !! Water is dirty!\n";
231 } else {
232 0 print " Water is still clean.\n";
233 }
234 1 1;
235 }
236  
237  
238 =head2 check_water_filter ( $water_filter, $current_waste_count, $reduce_waste_by )
239  
240 This checks, performs necessary actions and outputs the condition of the current waste count in the filtering cylinder.
241  
242 Take note that this process B<DOES NOT> trigger the LED and buzzer if abnormal condition is present.
243  
244 Returns 1 upon success.
245  
246 =cut
247  
248 sub check_water_filter {
249 1 ▶ my $water_filter = shift;
Subroutine check_water_filter: called CC=3 SCAR=11.2
Pod documented
250 1 my $current_reading = shift;
251 1 ▶ my $reduce_waste_by = shift || $water_filter->reduce_waste_count_by;
Condition33%1/3
shift() || $water_filter->reduce_waste_count_by
l!l&&r!l&&!r
010
MC/DC0%0/2
shift() || $water_filter->reduce_waste_count_by
shift() $water_filter->reduce_waste_count_by
Truth table33%1/3
A || B
A= shift()B= $water_filter->reduce_waste_count_by
ABresult
1X1
011
000
252 1 my $waste_threshold = $water_filter->waste_count_threshold;
253
254 1 $water_filter->current_waste_count( $current_reading );
255 1 print "Waste Count to Reduce: ", $water_filter->reduce_waste_count_by, "\n";
256 1 print "Current Waste Count: ", $current_reading, " (high: >= ", $waste_threshold, ")\n";
257  
258 1 ▶ if ( $water_filter->is_cylinder_dirty ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($water_filter->is_cylinder_dirty) { } 1 0 1
259 1 print " !! Filtering cylinder is dirty!\n";
260 1 $water_filter->turn_on_spatulas;
261 1 print " Cleaning spatulas turned on.\n";
262 1 $water_filter->clean_cylinder( $reduce_waste_by );
263 1 print " Cleaning the cylinder...Done!\n";
264 1 print "Current waste count: ", $water_filter->current_waste_count, "\n";
265 } else {
266 0 print " Filtering cylinder is still clean.\n";
267 }
268 1 1;
269 }
270  
271 =head2 check_water_level ( $water_level_maintainer, $current_water_level )
272  
273 This checks, performs necessary actions and outputs the condition of the current waste count in the filtering cylinder.
274  
275 Take note that this process B<DOES NOT> trigger the LED and buzzer if abnormal condition is present.
276  
277 Returns 1 upon success.
278  
279 =cut
280  
281 sub check_water_level {
282 1 ▶ my $water_level = shift;
Subroutine check_water_level: called CC=2 SCAR=7.0
Pod documented
283 1 my $current_reading = shift;
284 1 my $height_increase = $water_level->water_level_increase_height; # for output
285 1 my $water_level_threshold = $water_level->low_water_level_threshold;
286
287 1 $water_level->current_water_level( $current_reading ); # input by user
288 1 print "Current Water Level: ", $current_reading, " m (low: < ", $water_level_threshold, " m)\n";
289  
290 1 ▶ if ( $water_level->is_low_water_level ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($water_level->is_low_water_level) { } 1 0 1
291 1 print " !! Water level is low!\n";
292 1 $water_level->pump_water_in;
293 1 print " Pumping in ", $height_increase, " m of water...\n";
294 1 print "Current Water Level: ", $water_level->current_water_level, " m\n";
295 } else {
296 0 print " Water level is still normal.\n";
297 }
298 1 1;
299 }
300  
301 =head2 check_feeder ( $feeder, $verbose )
302  
303 This checks, performs necessary actions and outputs the condition of the feeder. Each call will tick the clock inside the feeder. See C<Acme::FishFarm::Feeder> for more info.
304  
305 If the food tank is empty, it will be filled to the default. So if you want to fill a different amount, please set the amount before hand. See <Acme::FishFarm::Feeder>.
306  
307 Setting C<$verbose> to 1 will give more output about the empty food tank.
308  
309 Take note that this process B<DOES NOT> trigger the LED and buzzer if abnormal condition is present.
310  
311 Returns 1 upon success.
312  
313 =cut
314  
315 sub check_feeder {
316 2 ▶ my ( $feeder, $verbose ) = @_;
Subroutine check_feeder: called CC=4 SCAR=19.4
Pod documented
317 2 ▶ if ( $feeder->timer_is_up ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($feeder->timer_is_up) { } 0 2 2
318 0 print "Timer is up, time to feed the fish!\n";
319
320 0 ▶ if ( $verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) { } 0 0 0
321 0 $feeder->feed_fish( verbose => 1 );
322 } else {
323 0 $feeder->feed_fish;
324 }
325 0 print " Feeding ", $feeder->feeding_volume, " cm^3 of fish food to the fish...\n";
326
327 } else {
328 2 print $feeder->time_remaining, " hours left until it's time to feed the fish.\n";
329 }
330
331 2 ▶ if ( $feeder->food_remaining <= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($feeder->food_remaining <= 0) 0 2 2
332 0 print " !! Food tank empty!\n";
333 0 $feeder->refill; # default back to 500 cm^3
334 0 print " Refilled food tank back to ", $feeder->food_tank_capacity, " cm^3.\n";
335 }
336  
337 2 print " Food Remaining: ", $feeder->food_remaining, "cm^3.\n";
338
339 2 $feeder->tick_clock;
340 2 1;
341 }
342  
343 =head2 render_leds ( $water_monitor )
344  
345 Outputs which LEDs are lighted up. Returns 1 upon success.
346  
347 Currently this subroutine only shows the LEDs present in C<$water_monitor> ie. C<Acme::FishFarm::WaterConditionMonitor> object. See that module for more details about the available LEDs.
348  
349 More LEDs will be available in the future. You can append your own LEDs by yourself if you really need to :)
350  
351 =cut
352  
353 sub render_leds {
354 1 ▶ my $water_monitor = shift;
Subroutine render_leds: called CC=5 SCAR=17.3
Pod documented
355  
356 # must check condition first! If not it won't work
357 # this process will update the LEDs status
358 1 $water_monitor->ph_is_normal;
359 1 $water_monitor->temperature_is_normal;
360 1 $water_monitor->lacking_oxygen;
361 1 $water_monitor->water_dirty;
362
363 1 print "Total LEDs up: ", $water_monitor->lighted_LED_count, "\n";
364  
365 1 ▶ if ( $water_monitor->is_on_LED_pH ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($water_monitor->is_on_LED_pH) { } 1 0 1
366 1 print " pH LED: on\n";
367 } else {
368 0 print " pH LED: off\n";
369 }
370
371 1 ▶ if ( $water_monitor->is_on_LED_temperature ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($water_monitor->is_on_LED_temperature) { } 0 1 1
372 0 print " Temperature LED: on\n";
373 } else {
374 1 print " Temperature LED: off\n";
375 }
376
377 1 ▶ if ( $water_monitor->is_on_LED_DO ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($water_monitor->is_on_LED_DO) { } 0 1 1
378 0 print " Low DO LED: on\n";
379 } else {
380 1 print " Low DO LED: off\n";
381 }
382
383 1 ▶ if ( $water_monitor->is_on_LED_turbidity ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($water_monitor->is_on_LED_turbidity) { } 1 0 1
384 1 print " Turbidity LED: on\n";
385 } else {
386 0 print " Turbidity LED: off\n";
387 }
388 1 1;
389 }
390  
391 =head2 render_buzzer ( $water_monitor )
392  
393 Outputs which buzzer is buzzing. Returns 1 upon success.
394  
395 See C<Acme::FishFarm::WaterConditionMonitor> for details on how the short and long buzzers are switched on and off.
396  
397 =cut
398  
399 sub render_buzzer {
400 1 ▶ my $water_monitor = shift;
Subroutine render_buzzer: called CC=3 SCAR=14.2
Pod documented
401
402 #$water_monitor->_tweak_buzzers;
403 1 ▶ if ( $water_monitor->is_on_buzzer_long ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($water_monitor->is_on_buzzer_long) { } 1 0 1
elsif ($water_monitor->is_on_buzzer_short) { } 0 0 0
404 1 print "Long buzzer is on, water condition very critical!\n";
405 } elsif ( $water_monitor->is_on_buzzer_short ) {
406 0 print "Short buzzer is on, water condition is a bit worrying :|\n";
407 } else {
408 0 print "No buzzers on, what a peaceful moment.\n";
409 }
410 1 1;
411 }
412  
413  
414 =head1 AUTHOR
415  
416 Raphael Jong Jun Jie, C<< <ellednera at cpan.org> >>
417  
418 =head1 BUGS
419  
420 Please report any bugs or feature requests to C<bug-acme-fishfarm at rt.cpan.org>, or through
421 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Acme-FishFarm>. I will be notified, and then you'll
422 automatically be notified of progress on your bug as I make changes.
423  
424  
425  
426  
427 =head1 SUPPORT
428  
429 You can find documentation for this module with the perldoc command.
430  
431 perldoc Acme::FishFarm
432  
433  
434 You can also look for information at:
435  
436 =over 4
437  
438 =item * RT: CPAN's request tracker (report bugs here)
439  
440 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Acme-FishFarm>
441  
442 =item * CPAN Ratings
443  
444 L<https://cpanratings.perl.org/d/Acme-FishFarm>
445  
446 =item * Search CPAN
447  
448 L<https://metacpan.org/release/Acme-FishFarm>
449  
450 =back
451  
452  
453 =head1 ACKNOWLEDGEMENTS
454  
455 Besiyata d'shmaya
456  
457 =head1 LICENSE AND COPYRIGHT
458  
459 This software is Copyright (c) 2021 by Raphael Jong Jun Jie.
460  
461 This is free software, licensed under:
462  
463 The Artistic License 2.0 (GPL Compatible)
464  
465  
466 =cut
467  
468 2 1; # End of Acme::FishFarm
« FishFarm/WaterFiltration.pm ↑ Summary FishFarm/WaterLevelMaintainer.pm »