|
| 1 | +use crate::tabs::advanced_tab::ntrip_tab::OutputType; |
| 2 | +use crate::types::Result; |
| 3 | +use crate::utils::pythonhome_dir; |
| 4 | +use anyhow::Context; |
| 5 | +use crossbeam::channel::Receiver; |
| 6 | +use log::{error, info}; |
| 7 | +use std::io::Write; |
| 8 | +use std::process::{Command, Stdio}; |
| 9 | +use std::{io, thread}; |
| 10 | + |
| 11 | +pub struct MessageConverter { |
| 12 | + in_rx: Receiver<Vec<u8>>, |
| 13 | + output_type: OutputType, |
| 14 | +} |
| 15 | + |
| 16 | +impl MessageConverter { |
| 17 | + pub fn new(in_rx: Receiver<Vec<u8>>, output_type: OutputType) -> Self { |
| 18 | + Self { in_rx, output_type } |
| 19 | + } |
| 20 | + |
| 21 | + pub fn start<W: Write + Send + 'static>(&mut self, out: W) -> Result<()> { |
| 22 | + match self.output_type { |
| 23 | + OutputType::RTCM => self.output_rtcm(out), |
| 24 | + OutputType::SBP => self.output_sbp(out), |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + /// Just redirects directly to writer |
| 29 | + fn output_rtcm<W: Write + Send + 'static>(&mut self, mut out: W) -> Result<()> { |
| 30 | + let in_rx = self.in_rx.clone(); |
| 31 | + thread::spawn(move || loop { |
| 32 | + if let Ok(data) = in_rx.recv() { |
| 33 | + if let Err(e) = out.write(&data) { |
| 34 | + error!("failed to write to device {e}"); |
| 35 | + } |
| 36 | + } |
| 37 | + }); |
| 38 | + Ok(()) |
| 39 | + } |
| 40 | + |
| 41 | + /// Runs rtcm3tosbp converter |
| 42 | + fn output_sbp<W: Write + Send + 'static>(&mut self, mut out: W) -> Result<()> { |
| 43 | + let mut child = if cfg!(target_os = "windows") { |
| 44 | + let mut cmd = Command::new("cmd"); |
| 45 | + let rtcm = pythonhome_dir()? |
| 46 | + .join("binaries") |
| 47 | + .join("windows") |
| 48 | + .join("rtcm3tosbp.exe") |
| 49 | + .to_string_lossy() |
| 50 | + .to_string(); |
| 51 | + info!("running rtcm3tosbp from \"{}\"", rtcm); |
| 52 | + cmd.args(["/C", &rtcm]); |
| 53 | + cmd |
| 54 | + } else if cfg!(target_os = "macos") { |
| 55 | + let mut cmd = Command::new("sh"); |
| 56 | + let rtcm = pythonhome_dir()? |
| 57 | + .join("binaries") |
| 58 | + .join("mac") |
| 59 | + .join("rtcm3tosbp") |
| 60 | + .to_string_lossy() |
| 61 | + .to_string(); |
| 62 | + info!("running rtcm3tosbp from \"{}\"", rtcm); |
| 63 | + cmd.args(["-c", &rtcm]); |
| 64 | + cmd |
| 65 | + } else { |
| 66 | + let mut cmd = Command::new("sh"); |
| 67 | + let rtcm = pythonhome_dir()? |
| 68 | + .join("binaries") |
| 69 | + .join("linux") |
| 70 | + .join("rtcm3tosbp") |
| 71 | + .to_string_lossy() |
| 72 | + .to_string(); |
| 73 | + info!("running rtcm3tosbp from \"{}\"", rtcm); |
| 74 | + cmd.args(["-c", &rtcm]); |
| 75 | + cmd |
| 76 | + }; |
| 77 | + let mut child = child |
| 78 | + .stdin(Stdio::piped()) |
| 79 | + .stdout(Stdio::piped()) |
| 80 | + .stderr(Stdio::null()) |
| 81 | + .spawn() |
| 82 | + .context("rtcm converter process failed")?; |
| 83 | + |
| 84 | + let mut child_in = child.stdin.take().context("rtcm3tosbp stdin missing")?; |
| 85 | + let mut child_out = child.stdout.take().context("rtcm3tosbp stdout missing")?; |
| 86 | + let in_rx = self.in_rx.clone(); |
| 87 | + |
| 88 | + thread::spawn(move || { |
| 89 | + if let Err(e) = io::copy(&mut child_out, &mut out) { |
| 90 | + error!("failed to write to device {e}"); |
| 91 | + } |
| 92 | + }); |
| 93 | + thread::spawn(move || { |
| 94 | + while let Ok(data) = in_rx.recv() { |
| 95 | + if let Err(e) = child_in.write_all(&data) { |
| 96 | + error!("failed to write to rtcm3tosbp {e}") |
| 97 | + } |
| 98 | + } |
| 99 | + }); |
| 100 | + thread::spawn(move || child.wait()); |
| 101 | + Ok(()) |
| 102 | + } |
| 103 | +} |
0 commit comments