Process pending dispatch events from the receiver
(
&self,
dispatch_rx: &mut Option<mpsc::Receiver<DispatchEvent>>,
current_mode: Mode,
)
| 39 | |
| 40 | /// Process pending dispatch events from the receiver |
| 41 | pub fn process_events( |
| 42 | &self, |
| 43 | dispatch_rx: &mut Option<mpsc::Receiver<DispatchEvent>>, |
| 44 | current_mode: Mode, |
| 45 | ) -> DispatchResult { |
| 46 | let mut result = DispatchResult::default(); |
| 47 | |
| 48 | let rx = match dispatch_rx { |
| 49 | Some(rx) => rx, |
| 50 | None => return result, |
| 51 | }; |
| 52 | |
| 53 | while let Ok(event) = rx.try_recv() { |
| 54 | match event { |
| 55 | DispatchEvent::TaskAssigned(assignment) => { |
| 56 | info!( |
| 57 | task_id = %assignment.task_id, |
| 58 | mission_id = %assignment.mission_id, |
| 59 | waypoints = assignment.zone.waypoints.len(), |
| 60 | is_loop = assignment.zone.is_loop, |
| 61 | "Task assigned from dispatch" |
| 62 | ); |
| 63 | |
| 64 | result.task_assigned = Some(assignment); |
| 65 | |
| 66 | // Request autonomous mode if currently idle |
| 67 | match current_mode { |
| 68 | Mode::Idle => { |
| 69 | result.request_autonomous = true; |
| 70 | info!("Requesting transition to Autonomous mode for dispatch task"); |
| 71 | } |
| 72 | Mode::Teleop => { |
| 73 | info!("Task queued - currently in Teleop mode"); |
| 74 | } |
| 75 | Mode::Autonomous => { |
| 76 | info!("New task received while autonomous"); |
| 77 | } |
| 78 | _ => {} |
| 79 | } |
| 80 | } |
| 81 | DispatchEvent::TaskCancelled(task_id) => { |
| 82 | info!(task_id = %task_id, "Task cancelled by dispatch"); |
| 83 | result.task_cancelled = Some(task_id); |
| 84 | } |
| 85 | DispatchEvent::CommandReceived(cmd) => { |
| 86 | info!(?cmd, "Voice command received via dispatch"); |
| 87 | result.commands.push(cmd); |
| 88 | } |
| 89 | DispatchEvent::Connected(connected) => { |
| 90 | if connected { |
| 91 | info!("Connected to dispatch service"); |
| 92 | } else { |
| 93 | warn!("Disconnected from dispatch service"); |
| 94 | } |
| 95 | result.connection_changed = Some(connected); |
| 96 | } |
| 97 | } |
| 98 | } |