use crate::error::Result;
use crate::protocol::{builder, constants};
use crate::types::DeviceInfo;
use super::Device;
fn strip_km_prefix(mut s: String) -> String {
if s.starts_with("km.") {
s.drain(..3);
}
s
}
impl Device {
pub fn version(&self) -> Result<String> {
self.query(constants::CMD_VERSION).map(strip_km_prefix)
}
pub fn device_info(&self) -> Result<DeviceInfo> {
let firmware = self.version()?;
let port = self.port_name().to_string();
Ok(DeviceInfo { port, firmware })
}
pub fn serial(&self) -> Result<String> {
self.query(constants::CMD_SERIAL_GET).map(strip_km_prefix)
}
pub fn set_serial(&self, value: &str) -> Result<String> {
let cmd = builder::build_serial_set(value)?;
self.query_dynamic(cmd.as_bytes()).map(strip_km_prefix)
}
pub fn reset_serial(&self) -> Result<String> {
self.query(constants::CMD_SERIAL_RESET).map(strip_km_prefix)
}
}
#[cfg(feature = "async")]
use super::AsyncDevice;
#[cfg(feature = "async")]
impl AsyncDevice {
pub async fn version(&self) -> Result<String> {
let v = self.query(constants::CMD_VERSION).await?;
Ok(strip_km_prefix(v))
}
pub async fn device_info(&self) -> Result<DeviceInfo> {
let firmware = self.version().await?;
let port = self.port_name().to_string();
Ok(DeviceInfo { port, firmware })
}
pub async fn serial(&self) -> Result<String> {
self.query(constants::CMD_SERIAL_GET)
.await
.map(strip_km_prefix)
}
pub async fn set_serial(&self, value: &str) -> Result<String> {
let cmd = builder::build_serial_set(value)?;
self.query_dynamic(cmd.as_bytes())
.await
.map(strip_km_prefix)
}
pub async fn reset_serial(&self) -> Result<String> {
self.query(constants::CMD_SERIAL_RESET)
.await
.map(strip_km_prefix)
}
}