aquarium_control/water/refill_monitor_view.rs
1/* Copyright 2025 Uwe Martin
2
3Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
5The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
7THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8*/
9use std::fmt;
10
11/// Struct collects the information for the reporting to the monitor
12/// The information represents no execution error, but critical states of the control object.
13/// In this case, the monitor view informs about excessive triggering of refill operation
14/// which may be caused by a defective sensor or leakage of the main tank.
15#[derive(PartialEq, Debug, Clone)]
16pub struct RefillMonitorView {
17 /// refill volume within the last 24h exceeded the threshold
18 pub refill_check_volume_last_24h: bool,
19
20 /// refill count within the last 24h exceeded the threshold
21 pub refill_check_count_last_24h: bool,
22
23 /// refill volume within the last hour exceeded the threshold
24 pub refill_check_volume_last_hour: bool,
25
26 /// refill count within the last hour exceeded the threshold
27 pub refill_check_count_last_hour: bool,
28
29 /// time interval since the last refill is too short
30 pub refill_check_interval_last_refill: bool,
31}
32
33#[cfg(test)]
34impl RefillMonitorView {
35 pub(crate) fn default() -> Self {
36 RefillMonitorView {
37 refill_check_volume_last_24h: false,
38 refill_check_count_last_24h: false,
39 refill_check_volume_last_hour: false,
40 refill_check_count_last_hour: false,
41 refill_check_interval_last_refill: false,
42 }
43 }
44}
45
46#[cfg(test)]
47impl RefillMonitorView {
48 // Helper function to assert the state of a RefillMonitorView
49 pub fn assert_refill_monitor_view(
50 &self,
51 expected_volume_last24h: bool,
52 expected_count_last24h: bool,
53 expected_volume_last_h: bool,
54 expected_count_last_h: bool,
55 expected_interval_last_refill: bool,
56 ) {
57 assert_eq!(
58 self.refill_check_volume_last_24h, expected_volume_last24h,
59 "refill_check_volume_last_24h mismatch"
60 );
61 assert_eq!(
62 self.refill_check_count_last_24h, expected_count_last24h,
63 "refill_check_count_last_24h mismatch"
64 );
65 assert_eq!(
66 self.refill_check_volume_last_hour, expected_volume_last_h,
67 "refill_check_volume_last_hour mismatch"
68 );
69 assert_eq!(
70 self.refill_check_count_last_hour, expected_count_last_h,
71 "refill_check_count_last_hour mismatch"
72 );
73 assert_eq!(
74 self.refill_check_interval_last_refill, expected_interval_last_refill,
75 "refill_check_interval_last_refill mismatch"
76 );
77 }
78}
79
80// Implement the Display trait for RefillMonitorView to provide a human-readable string representation.
81impl fmt::Display for RefillMonitorView {
82 /// Formats the `RefillMonitorView` flags into a human-readable string.
83 ///
84 /// This implementation provides a concise, single-line summary of any active
85 /// refill warnings. If no flags are set, it reports that all checks are OK.
86 /// Otherwise, it lists the specific warnings, separated by commas.
87 ///
88 /// # Arguments
89 /// * `f` - A mutable reference to a `Formatter` where the output will be written.
90 ///
91 /// # Returns
92 /// An `Ok(())` on successful formatting.
93 ///
94 /// # Errors
95 /// Returns an `Err` containing a `std::fmt::Error` if an I/O error occurs
96 /// while writing to the formatter.
97 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
98 let mut checks = Vec::new();
99
100 if self.refill_check_volume_last_24h {
101 checks.push("Volume last 24h exceeded");
102 }
103 if self.refill_check_count_last_24h {
104 checks.push("Count last 24h exceeded");
105 }
106 if self.refill_check_volume_last_hour {
107 checks.push("Volume last hour exceeded");
108 }
109 if self.refill_check_count_last_hour {
110 checks.push("Count last hour exceeded");
111 }
112 if self.refill_check_interval_last_refill {
113 checks.push("Interval since last refill too short");
114 }
115
116 if checks.is_empty() {
117 write!(f, "All refill checks are OK.")
118 } else {
119 write!(f, "Refill issues: {}", checks.join(", "))
120 }
121 }
122}