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
use std::sync::{Arc, Mutex};
use std::time::Duration;
use asyn_rs::interfaces::motor::AsynMotor;
use asyn_rs::user::AsynUser;
use tokio::sync::mpsc;
use crate::device_state::*;
/// Commands sent to the poll loop.
#[derive(Debug)]
pub enum PollCommand {
StartPolling,
StopPolling,
ScheduleDelay(u64, Duration),
Shutdown,
}
/// Motor poll loop — one per record, stays alive for the record's lifetime.
pub struct MotorPollLoop {
cmd_rx: mpsc::Receiver<PollCommand>,
io_intr_tx: mpsc::Sender<()>,
motor: Arc<Mutex<dyn AsynMotor>>,
device_state: SharedDeviceState,
moving_poll_interval: Duration,
idle_poll_interval: Duration,
forced_fast_polls_config: u32,
forced_fast_polls_remaining: u32,
last_moving: bool,
status_seq: u64,
}
impl MotorPollLoop {
pub fn new(
cmd_rx: mpsc::Receiver<PollCommand>,
io_intr_tx: mpsc::Sender<()>,
motor: Arc<Mutex<dyn AsynMotor>>,
device_state: SharedDeviceState,
moving_poll_interval: Duration,
idle_poll_interval: Duration,
forced_fast_polls: u32,
) -> Self {
Self {
cmd_rx,
io_intr_tx,
motor,
device_state,
moving_poll_interval,
idle_poll_interval,
forced_fast_polls_config: forced_fast_polls,
forced_fast_polls_remaining: 0,
last_moving: false,
status_seq: 1, // starts at 1 (init already wrote seq=1)
}
}
/// Poll the motor and write stamped status to shared state.
async fn poll_and_notify(&mut self) {
let user = AsynUser::new(0);
let status = {
let mut motor = match self.motor.lock() {
Ok(m) => m,
Err(_) => return,
};
match motor.poll(&user) {
Ok(s) => s,
Err(_) => return,
}
};
self.last_moving = status.moving;
self.status_seq += 1;
{
match self.device_state.lock() {
Ok(mut ds) => {
ds.latest_status = Some(StampedStatus {
seq: self.status_seq,
status,
});
}
Err(e) => {
tracing::error!("device state lock poisoned in poll_and_notify: {e}");
return;
}
}
}
let _ = self.io_intr_tx.send(()).await;
}
fn effective_poll_interval(&mut self) -> Duration {
if self.forced_fast_polls_remaining > 0 {
self.forced_fast_polls_remaining -= 1;
self.moving_poll_interval
} else if self.last_moving {
self.moving_poll_interval
} else {
self.idle_poll_interval
}
}
/// Run the poll loop. Call from a spawned task.
pub async fn run(mut self) {
// Start idle: device support init() sends StartPolling after
// iocInit, matching C EPICS where the poller starts in init_record.
let mut active = false;
loop {
if active {
// Poll mode: check for commands or poll on interval
let interval = self.effective_poll_interval();
tokio::select! {
cmd = self.cmd_rx.recv() => {
match cmd {
Some(PollCommand::StartPolling) => {
active = true;
self.forced_fast_polls_remaining = self.forced_fast_polls_config;
self.poll_and_notify().await;
}
Some(PollCommand::StopPolling) => {
active = false;
}
Some(PollCommand::ScheduleDelay(delay_id, dur)) => {
active = false;
tokio::time::sleep(dur).await;
match self.device_state.lock() {
Ok(mut ds) => { ds.expired_delay_id = Some(delay_id); }
Err(e) => {
tracing::error!("device state lock poisoned in delay expiry: {e}");
continue;
}
}
let _ = self.io_intr_tx.send(()).await;
}
Some(PollCommand::Shutdown) => {
return;
}
None => {
return;
}
}
}
_ = tokio::time::sleep(interval) => {
self.poll_and_notify().await;
}
}
} else {
// Idle mode: wait for commands only
match self.cmd_rx.recv().await {
Some(PollCommand::StartPolling) => {
active = true;
self.forced_fast_polls_remaining = self.forced_fast_polls_config;
self.poll_and_notify().await;
}
Some(PollCommand::StopPolling) => {
active = false;
}
Some(PollCommand::ScheduleDelay(delay_id, dur)) => {
tokio::time::sleep(dur).await;
match self.device_state.lock() {
Ok(mut ds) => {
ds.expired_delay_id = Some(delay_id);
}
Err(e) => {
tracing::error!("device state lock poisoned in delay expiry: {e}");
continue;
}
}
let _ = self.io_intr_tx.send(()).await;
}
Some(PollCommand::Shutdown) | None => {
return;
}
}
}
}
}
}