use crate::{
Address,
operation::{DeviceIdentity, ReadDeviceIdentity},
service::{ExchangeError, LinkClient, RetryPolicy},
};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct KnownDevice {
pub address: Address,
pub identity: DeviceIdentity,
}
#[derive(Debug)]
pub enum RecoveryStatus {
Restored(DeviceIdentity),
Replaced(DeviceIdentity),
Unavailable(ExchangeError),
}
#[derive(Debug, Default)]
pub struct RecoveryReport {
pub devices: Vec<(Address, RecoveryStatus)>,
}
pub async fn reconcile_devices(
link: &LinkClient,
known: &[KnownDevice],
retry: RetryPolicy,
) -> RecoveryReport {
let mut report = RecoveryReport::default();
for previous in known {
let status = match link
.execute(
previous.address,
&ReadDeviceIdentity,
link.default_queue_id(),
retry,
)
.await
{
Ok(identity) if same_device(&identity, &previous.identity) => {
RecoveryStatus::Restored(identity)
}
Ok(identity) => RecoveryStatus::Replaced(identity),
Err(error) => RecoveryStatus::Unavailable(error),
};
report.devices.push((previous.address, status));
}
report
}
fn same_device(left: &DeviceIdentity, right: &DeviceIdentity) -> bool {
left.manufacturer_id == right.manufacturer_id
&& left.address_device_type() == right.address_device_type()
&& left.device_id == right.device_id
}