use offline_protocol_router::RelayRole;
pub const CHANGED_BATTERY: u8 = 0b0000_0001;
pub const CHANGED_CHARGING: u8 = 0b0000_0010;
pub const CHANGED_RELAY_ROLE: u8 = 0b0000_0100;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DeviceCapabilitySnapshot {
pub timestamp_ms: i64,
pub battery_level: Option<u8>,
pub is_charging: bool,
pub relay_role: RelayRole,
pub changed_fields: u8,
}
impl DeviceCapabilitySnapshot {
pub const NAME: &'static str = "protocol.device.capability_changed";
pub fn telemetry_name(&self) -> &'static str {
Self::NAME
}
pub fn has_changed(&self, bit: u8) -> bool {
self.changed_fields & bit != 0
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn name_is_stable() {
let snapshot = DeviceCapabilitySnapshot {
timestamp_ms: 0,
battery_level: Some(50),
is_charging: false,
relay_role: RelayRole::Regular,
changed_fields: CHANGED_BATTERY,
};
assert_eq!(
DeviceCapabilitySnapshot::NAME,
"protocol.device.capability_changed"
);
assert_eq!(
snapshot.telemetry_name(),
"protocol.device.capability_changed"
);
}
#[test]
fn has_changed_respects_bits() {
let snapshot = DeviceCapabilitySnapshot {
timestamp_ms: 0,
battery_level: None,
is_charging: true,
relay_role: RelayRole::Regular,
changed_fields: CHANGED_CHARGING | CHANGED_RELAY_ROLE,
};
assert!(snapshot.has_changed(CHANGED_CHARGING));
assert!(snapshot.has_changed(CHANGED_RELAY_ROLE));
assert!(!snapshot.has_changed(CHANGED_BATTERY));
}
}