1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//! Timer module for managing asynchronous timeouts in elevator control logic.
//!
//! This module defines two core components:
//! - [`Timer`]: A general-purpose timer that supports both soft (elapsed time) and hard (manual) timeouts.
//! - [`ElevatorTimers`]: A struct that bundles all timers used in the elevator FSM, including door timeout,
//! cab call priority grace period, and general error detection.
//!
//! These components are used throughout the elevator state machine to control behavior based on timeouts,
//! such as how long to keep doors open, how long to prioritize internal cab calls, or when to enter an error state
//! due to inactivity or unresponsiveness.
//!
//! # Usage
//! Timers are used in `fsm.rs` to manage:
//! - Door open duration (`door` timer)
//! - Grace period for cab button prioritization (`cab_priority` timer)
//! - Communication or logic errors (`error` timer)
//!
//! # Example (using Timer standalone)
//! ```rust,no_run
//! use tokio::time::Duration;
//! use crate::elevator_logic::timer::Timer;
//!
//! let mut door_timer = Timer::new(Duration::from_secs(3));
//! door_timer.timer_start();
//!
//! // In FSM loop:
//! if door_timer.timer_timeouted() {
//! // Trigger door close logic
//! }
//! ```
//!
//! # ElevatorTimers Usage
//! ElevatorTimers simplifies timer management by grouping all related timers into one struct:
//!
//! ```rust,no_run
//! let mut timers = ElevatorTimers::new(
//! Duration::from_secs(3), // door
//! Duration::from_secs(10), // cab priority
//! Duration::from_secs(7), // error
//! );
//!
//! timers.door.timer_start();
//! if timers.cab_priority.timer_timeouted() {
//! // Prioritization window over
//! }
//! ```
//!
//! # Timer Behavior
//! - A timer is **inactive** until [`timer_start()`](Timer::timer_start) is called.
//! - Once active, it compares current time with the internal start time.
//! - The timer can be **manually forced** to timeout using [`release_timer()`](Timer::release_timer).
//! - A call to [`timer_timeouted()`](Timer::timer_timeouted) returns `true` if either a soft or hard timeout has occurred.
//!
//! # Related
//! Used heavily in the [`fsm`](crate::elevator_logic::fsm) module.
use Duration;
/// A simple timer utility for managing soft and hard timeouts in asynchronous contexts.
///
/// The timer can be started and queried to check whether the timeout duration has been exceeded.
/// In addition to the regular (soft) timeout based on elapsed time, a "hard timeout" flag can be manually triggered
/// to force the timer into a timeout state regardless of elapsed time.
/// Collection of timers used in the elevator's finite state machine (FSM).
///
/// This struct encapsulates all timers that track different timeout conditions
/// such as door closing, inside call priority window, and general error state.
/// Also includes state tracking related to inside call grace period.