aquarium_control/utilities/
update_thermal_set_values.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*/
9/// A macro to update set values from a data source and handle logging on failure.
10///
11/// This macro encapsulates the logic for:
12/// 1. Calling an `update_set_value` method on a given updater.
13/// 2. On failure, logging the error using a "log-once" pattern.
14/// 3. On success, resetting the "log-once" flag.
15#[macro_export]
16macro_rules! update_thermal_set_values {
17    (
18        $updater:expr,
19        $off_temp:expr,
20        $on_temp:expr,
21        $lock_flag:expr,
22        $location:expr
23    ) => {
24        if let Err(e) = $updater.update_set_value(&mut $off_temp, &mut $on_temp) {
25            if !$lock_flag {
26                log::error!(
27                    target: $location,
28                    concat!(
29                        "Error occurred when trying to retrieve thermal set values from database, ",
30                        "maintaining the current set values (off={:?}, on={:?}): {:?}"
31                    ),
32                    $off_temp,
33                    $on_temp,
34                    e
35                );
36                $lock_flag = true;
37            }
38        } else {
39            $lock_flag = false;
40        }
41    };
42}