This commit is contained in:
Generated
+2
-2
@@ -583,9 +583,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde_json"
|
name = "serde_json"
|
||||||
version = "1.0.151"
|
version = "1.0.154"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "c841b55ecdae098c80dcae9cf767f6f8a0c2cdb3416bbef72181df4d0fe73f14"
|
checksum = "e7e9cc8b1b85264074fbcc02a88680c4096b1e47df8f739dceb03bf482f04bd6"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"itoa",
|
"itoa",
|
||||||
"memchr",
|
"memchr",
|
||||||
|
|||||||
+1
-1
@@ -11,7 +11,7 @@ thiserror = "2.0.21"
|
|||||||
strum = "0.28.0"
|
strum = "0.28.0"
|
||||||
strum_macros = "0.28.0"
|
strum_macros = "0.28.0"
|
||||||
serde = { version = "1", features = ["derive"], optional = true }
|
serde = { version = "1", features = ["derive"], optional = true }
|
||||||
serde_json = { version = "1", optional = true }
|
serde_json = { version = "1.0.154", optional = true }
|
||||||
serde_with = "3.24.0"
|
serde_with = "3.24.0"
|
||||||
rand = "0.10.3"
|
rand = "0.10.3"
|
||||||
|
|
||||||
|
|||||||
@@ -40,9 +40,12 @@ pub struct DSP408 {
|
|||||||
state: types::DSPState,
|
state: types::DSPState,
|
||||||
input_meters: types::InputMeters,
|
input_meters: types::InputMeters,
|
||||||
output_meters: types::OutputMeters,
|
output_meters: types::OutputMeters,
|
||||||
|
reconnect_count: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DSP408 {
|
impl DSP408 {
|
||||||
|
// ---------- Initializers ----------
|
||||||
|
|
||||||
pub fn new(host: IpAddr, port: u16, device_id: u8) -> Result<Self, DSPError> {
|
pub fn new(host: IpAddr, port: u16, device_id: u8) -> Result<Self, DSPError> {
|
||||||
if matches!(host, IpAddr::V6(_)) {
|
if matches!(host, IpAddr::V6(_)) {
|
||||||
return Err(DSPError::InvalidHost("DSP doesn't support IPv6"));
|
return Err(DSPError::InvalidHost("DSP doesn't support IPv6"));
|
||||||
@@ -73,6 +76,7 @@ impl DSP408 {
|
|||||||
|
|
||||||
input_meters: default_input_meters(),
|
input_meters: default_input_meters(),
|
||||||
output_meters: default_output_meters(),
|
output_meters: default_output_meters(),
|
||||||
|
reconnect_count: 0,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -92,9 +96,19 @@ impl DSP408 {
|
|||||||
|
|
||||||
input_meters: default_input_meters(),
|
input_meters: default_input_meters(),
|
||||||
output_meters: default_output_meters(),
|
output_meters: default_output_meters(),
|
||||||
|
reconnect_count: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ---------- Network Helpers ----------
|
||||||
|
|
||||||
|
fn is_connection_error(err: &DSPError) -> bool {
|
||||||
|
matches!(
|
||||||
|
err,
|
||||||
|
DSPError::ConnectionClosed | DSPError::Timeout | DSPError::Io(_)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
fn send_raw(socket: &mut TcpStream, data: &[u8]) -> Result<(), DSPError> {
|
fn send_raw(socket: &mut TcpStream, data: &[u8]) -> Result<(), DSPError> {
|
||||||
socket.write_all(data)?;
|
socket.write_all(data)?;
|
||||||
|
|
||||||
@@ -118,7 +132,7 @@ impl DSP408 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn send_recv(&self, command: &[u8]) -> Result<types::DeviceResponseType, DSPError> {
|
fn send_recv_once(&self, command: &[u8]) -> Result<types::DeviceResponseType, DSPError> {
|
||||||
let mut socket_guard = self
|
let mut socket_guard = self
|
||||||
.socket
|
.socket
|
||||||
.lock()
|
.lock()
|
||||||
@@ -127,16 +141,32 @@ impl DSP408 {
|
|||||||
let socket = socket_guard.as_mut().ok_or(DSPError::NotConnected)?;
|
let socket = socket_guard.as_mut().ok_or(DSPError::NotConnected)?;
|
||||||
|
|
||||||
Self::send_raw(socket, command)?;
|
Self::send_raw(socket, command)?;
|
||||||
|
|
||||||
let raw = Self::recv_raw(socket)?;
|
let raw = Self::recv_raw(socket)?;
|
||||||
|
|
||||||
let frame = extract_frame(&raw)?;
|
let frame = extract_frame(&raw)?;
|
||||||
|
|
||||||
let response = map_response(&frame)?;
|
Ok(map_response(&frame)?)
|
||||||
|
|
||||||
Ok(response)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn send_recv(&mut self, command: &[u8]) -> Result<types::DeviceResponseType, DSPError> {
|
||||||
|
match self.send_recv_once(command) {
|
||||||
|
Err(err)
|
||||||
|
if DSP408::is_connection_error(&err)
|
||||||
|
&& self.mode == types::DSPMode::Normal
|
||||||
|
&& self.state.connection == types::ConnectionStatus::Connected =>
|
||||||
|
{
|
||||||
|
let _ = self.disconnect();
|
||||||
|
self.connect()?;
|
||||||
|
|
||||||
|
self.reconnect_count += 1;
|
||||||
|
|
||||||
|
self.send_recv_once(command)
|
||||||
|
}
|
||||||
|
other => other,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------- Getters ----------
|
||||||
|
|
||||||
pub fn mode(&self) -> types::DSPMode {
|
pub fn mode(&self) -> types::DSPMode {
|
||||||
self.mode
|
self.mode
|
||||||
}
|
}
|
||||||
@@ -165,6 +195,12 @@ impl DSP408 {
|
|||||||
&mut self.state
|
&mut self.state
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn reconnect_count(&self) -> u32 {
|
||||||
|
self.reconnect_count
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------- Helpers ----------
|
||||||
|
|
||||||
fn initialize_state(&mut self) -> Result<(), DSPError> {
|
fn initialize_state(&mut self) -> Result<(), DSPError> {
|
||||||
let name = self.get_device_name()?;
|
let name = self.get_device_name()?;
|
||||||
let flags = self.get_device_flags()?;
|
let flags = self.get_device_flags()?;
|
||||||
|
|||||||
Reference in New Issue
Block a user