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
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,
poll_interval: Duration,
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,
poll_interval: Duration,
) -> Self {
Self {
cmd_rx,
io_intr_tx,
motor,
device_state,
poll_interval,
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.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;
}
/// Run the poll loop. Call from a spawned task.
pub async fn run(mut self) {
// Start active: initial poll triggers I/O Intr so the record
// picks up the first motor status (clears LVIO, sets MSTA, etc.)
let mut active = true;
loop {
if active {
// Poll mode: check for commands or poll on interval
tokio::select! {
cmd = self.cmd_rx.recv() => {
match cmd {
Some(PollCommand::StartPolling) => {
active = true;
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(self.poll_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.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;
}
}
}
}
}
}