aquarium_control/utilities/
perform_schedule_check.rs

1/* Copyright 2024 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 perform a request-response check with the schedule_check module.
10///
11/// This macro encapsulates the logic for:
12/// 1. Sending a `ScheduleCheck` command.
13/// 2. Receiving a boolean response.
14/// 3. Handling send/receive errors with a "log-once" pattern.
15/// 4. Assigning the result, defaulting to `false` on any communication failure.
16#[macro_export]
17macro_rules! perform_schedule_check {
18    (
19        $channels:expr,
20        $result_var:expr,
21        $send_lock:expr,
22        $receive_lock:expr,
23        $location:expr
24    ) => {
25        match $channels.send_to_schedule_check(InternalCommand::ScheduleCheck) {
26            Ok(_) => {
27                $send_lock = false;
28                // Now listen for the response
29                $result_var = match $channels.receive_from_schedule_check() {
30                    Ok(c) => {
31                        $receive_lock = false;
32                        c
33                    }
34                    Err(e) => {
35                        if !$receive_lock {
36                            error!(
37                                target: $location,
38                                "Receiving from schedule check failed ({e:?})"
39                            );
40                            $receive_lock = true;
41                        }
42                        // Assume allowed to actuate if communication fails
43                        true
44                    }
45                };
46            }
47            Err(e) => {
48                if !$send_lock {
49                    error!(
50                        target: $location,
51                        "Channel communication to schedule check failed ({e:?})"
52                    );
53                    $send_lock = true;
54                }
55                // On send failure, also assume allowed to actuate
56                $result_var = true;
57            }
58        }
59    };
60}