use std::{string::String, time::SystemTime};
use crate::{
CommandCode,
operation::{
AdditionalStatus, DynamicValues, LoopSignal, PrimaryValue, ReadAdditionalStatus,
ReadDynamicValues, ReadLongTag, ReadLoopSignal, ReadPrimaryValue, ReadTagDescriptorDate,
TagDescriptorDate,
},
service::{DeviceSession, SessionError, SessionProfile},
};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SnapshotOptions {
pub process_values: bool,
pub tags: bool,
pub diagnostics: bool,
}
impl SnapshotOptions {
pub const IDENTITY_ONLY: Self = Self {
process_values: false,
tags: false,
diagnostics: false,
};
pub const FULL: Self = Self {
process_values: true,
tags: true,
diagnostics: true,
};
pub const fn with_process_values(mut self, enabled: bool) -> Self {
self.process_values = enabled;
self
}
pub const fn with_tags(mut self, enabled: bool) -> Self {
self.tags = enabled;
self
}
pub const fn with_diagnostics(mut self, enabled: bool) -> Self {
self.diagnostics = enabled;
self
}
}
impl Default for SnapshotOptions {
fn default() -> Self {
Self::FULL
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SnapshotError {
pub command: CommandCode,
pub message: String,
}
#[derive(Debug, Clone, PartialEq)]
pub enum SnapshotField<T> {
NotRequested,
Unsupported {
reason: &'static str,
},
Value(T),
Error(SnapshotError),
}
impl<T> SnapshotField<T> {
pub const fn value(&self) -> Option<&T> {
match self {
Self::Value(value) => Some(value),
Self::NotRequested | Self::Unsupported { .. } | Self::Error(_) => None,
}
}
pub const fn error(&self) -> Option<&SnapshotError> {
match self {
Self::Error(error) => Some(error),
Self::NotRequested | Self::Unsupported { .. } | Self::Value(_) => None,
}
}
pub const fn is_error(&self) -> bool {
matches!(self, Self::Error(_))
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct DeviceSnapshot {
pub captured_at: SystemTime,
pub profile: SessionProfile,
pub primary_value: SnapshotField<PrimaryValue>,
pub loop_signal: SnapshotField<LoopSignal>,
pub dynamic_values: SnapshotField<DynamicValues>,
pub tag: SnapshotField<TagDescriptorDate>,
pub long_tag: SnapshotField<String>,
pub additional_status: SnapshotField<AdditionalStatus>,
}
impl DeviceSnapshot {
pub const fn has_errors(&self) -> bool {
self.primary_value.is_error()
|| self.loop_signal.is_error()
|| self.dynamic_values.is_error()
|| self.tag.is_error()
|| self.long_tag.is_error()
|| self.additional_status.is_error()
}
}
pub(crate) async fn capture_snapshot(
session: &DeviceSession,
options: SnapshotOptions,
) -> DeviceSnapshot {
let captured_at = SystemTime::now();
let primary_value = if options.process_values {
capture(
CommandCode::new(1),
session.execute_default(&ReadPrimaryValue).await,
)
} else {
SnapshotField::NotRequested
};
let loop_signal = if options.process_values {
capture(
CommandCode::new(2),
session.execute_default(&ReadLoopSignal).await,
)
} else {
SnapshotField::NotRequested
};
let dynamic_values = if options.process_values {
capture(
CommandCode::new(3),
session.execute_default(&ReadDynamicValues).await,
)
} else {
SnapshotField::NotRequested
};
let tag = if options.tags {
capture(
CommandCode::new(13),
session.execute_default(&ReadTagDescriptorDate).await,
)
} else {
SnapshotField::NotRequested
};
let long_tag = if !options.tags {
SnapshotField::NotRequested
} else if session.profile().identity.universal_revision < 6 {
SnapshotField::Unsupported {
reason: "long tag requires HART 6 or newer",
}
} else {
capture(
CommandCode::new(20),
session.execute_default(&ReadLongTag).await,
)
};
let additional_status = if options.diagnostics {
capture(
CommandCode::new(48),
session.execute_default(&ReadAdditionalStatus).await,
)
} else {
SnapshotField::NotRequested
};
DeviceSnapshot {
captured_at,
profile: session.profile().clone(),
primary_value,
loop_signal,
dynamic_values,
tag,
long_tag,
additional_status,
}
}
fn capture<T>(command: CommandCode, result: Result<T, SessionError>) -> SnapshotField<T> {
match result {
Ok(value) => SnapshotField::Value(value),
Err(error) => SnapshotField::Error(SnapshotError {
command,
message: error.to_string(),
}),
}
}