use std::sync::Arc;
use std::sync::atomic::AtomicU64;
use std::time::Duration;
use modem_contract::registry::modem::{Subject, key};
use modem_device::OperStatus;
use tokio::time::Instant;
use zenkey::origin::LocalOrigin;
use zenoh::Session;
use crate::clock::{clock_synced, now_ms, ulid};
use crate::host::{Host, Node, Plane};
use crate::plane::SduPlane;
pub(crate) const FLOOR: Duration = Duration::from_secs(60);
pub(crate) enum Source {
Sdu(Arc<dyn SduPlane>),
Device(Arc<dyn modem_device::Device>),
}
impl Source {
pub(crate) fn of(node: &Node) -> Self {
match node.plane() {
Plane::Sdu(plane) => Self::Sdu(plane.clone()),
Plane::Netdev(dev) => Self::Device(dev.clone()),
}
}
pub(super) fn oper(&self) -> OperStatus {
match self {
Self::Sdu(plane) => plane.oper(),
Self::Device(dev) => dev.oper(),
}
}
pub(super) fn report(&self) -> modem_device::Report {
match self {
Self::Sdu(plane) => plane.report(),
Self::Device(dev) => dev.report(),
}
}
pub(super) fn security(&self) -> Option<modem_device::SecurityProperties> {
match self {
Self::Sdu(plane) => plane.security(),
Self::Device(dev) => dev.security(),
}
}
pub(super) fn schema(&self) -> Option<modem_device::config::Schema> {
match self {
Self::Sdu(plane) => plane.configuration().map(|c| c.schema()),
Self::Device(dev) => dev.configuration().map(|c| c.schema()),
}
}
pub(super) fn effective(&self) -> Option<modem_device::config::Effective> {
match self {
Self::Sdu(plane) => plane.configuration().map(|c| c.effective()),
Self::Device(dev) => dev.configuration().map(|c| c.effective()),
}
}
pub(super) fn action_specs(&self) -> Option<Vec<modem_device::action::ActionSpec>> {
match self {
Self::Sdu(plane) => plane.actions().map(|a| a.specs()),
Self::Device(dev) => dev.actions().map(|a| a.specs()),
}
}
pub(super) async fn invoke(
&self,
name: &str,
arguments: &std::collections::BTreeMap<String, modem_device::config::ConfigValue>,
) -> Result<modem_device::action::ActionOutcome, modem_device::action::ActionError> {
let a = match self {
Self::Sdu(plane) => plane.actions(),
Self::Device(dev) => dev.actions(),
};
match a {
Some(a) => a.invoke(name, arguments).await,
None => Err(modem_device::action::ActionError::Unsupported(
name.to_string(),
)),
}
}
pub(super) fn identity(&self) -> modem_device::Identity {
match self {
Self::Sdu(plane) => plane.identity(),
Self::Device(dev) => dev.identity(),
}
}
fn health(&self) -> Option<tokio::sync::watch::Receiver<bool>> {
match self {
Self::Sdu(plane) => Some(plane.health()),
Self::Device(_) => None,
}
}
}
pub(crate) struct Damper {
floor: Duration,
last: Option<Instant>,
folded: u64,
}
impl Damper {
pub(crate) fn new(floor: Duration) -> Self {
Self {
floor,
last: None,
folded: 0,
}
}
pub(crate) fn observe(&mut self, now: Instant) -> Option<u64> {
match self.last {
Some(t) if now.duration_since(t) < self.floor => {
self.folded += 1;
None
}
_ => {
self.last = Some(now);
Some(1)
}
}
}
pub(crate) fn flush(&mut self, now: Instant) -> Option<u64> {
if self.folded == 0 {
return None;
}
let expired = self
.last
.is_none_or(|t| now.duration_since(t) >= self.floor);
if !expired {
return None;
}
let n = self.folded;
self.folded = 0;
self.last = Some(now);
Some(n)
}
pub(crate) fn deadline(&self) -> Option<Instant> {
if self.folded == 0 {
return None;
}
Some(self.last.map_or_else(Instant::now, |t| t + self.floor))
}
}
#[allow(clippy::too_many_lines)]
pub(super) async fn publish_loop(
session: Session,
origin: LocalOrigin,
host: Arc<Host>,
device: String,
source: Source,
ttl_s: u64,
poll: Duration,
) {
let subject = Subject::device_oper(&device);
let doc_key = key(&origin, &subject);
let qos = subject.qos();
let refresh = Duration::from_secs((ttl_s / 2).max(1));
let counter = AtomicU64::new(0);
let mut damper = Damper::new(FLOOR);
let mut health = source.health();
let mut current = source.oper();
let document = |status: &OperStatus| {
super::Oper::of(
&device,
status,
host.uptime_s(),
host.boot_id(),
clock_synced(),
)
};
crate::state::put(&session, &doc_key, &document(¤t), qos).await;
let mut refreshed = Instant::now();
loop {
let wake = [
Some(Instant::now() + poll),
Some(refreshed + refresh),
damper.deadline(),
]
.into_iter()
.flatten()
.min()
.expect("the poll is always a deadline");
tokio::select! {
biased;
changed = health_changed(health.as_mut()) => {
if changed.is_err() {
return;
}
}
() = tokio::time::sleep_until(wake) => {}
}
let now = Instant::now();
let observed = source.oper();
if observed != current {
current = observed;
crate::state::put(&session, &doc_key, &document(¤t), qos).await;
refreshed = now;
if let Some(n) = damper.observe(now) {
let event_id = ulid(&counter);
let subject = Subject::device_oper_change(&device, &event_id);
let doc = super::OperChange::of(&device, event_id, now_ms(), ¤t, n);
crate::events::publish(&session, &origin, &subject, &doc, "oper_change").await;
}
} else if now >= refreshed + refresh {
crate::state::put(&session, &doc_key, &document(¤t), qos).await;
refreshed = now;
}
if let Some(n) = damper.flush(now) {
let event_id = ulid(&counter);
let subject = Subject::device_oper_change(&device, &event_id);
let doc = super::OperChange::of(&device, event_id, now_ms(), ¤t, n);
crate::events::publish(&session, &origin, &subject, &doc, "oper_change").await;
}
}
}
async fn health_changed(
health: Option<&mut tokio::sync::watch::Receiver<bool>>,
) -> Result<(), tokio::sync::watch::error::RecvError> {
match health {
Some(rx) => rx.changed().await,
None => std::future::pending().await,
}
}
#[cfg(test)]
mod tests {
use super::*;
fn at(base: Instant, s: u64) -> Instant {
base + Duration::from_secs(s)
}
#[test]
fn the_first_transition_publishes_at_once() {
let base = Instant::now();
let mut d = Damper::new(FLOOR);
assert_eq!(d.observe(base), Some(1));
assert_eq!(d.flush(base), None);
assert_eq!(d.deadline(), None);
}
#[test]
fn transitions_inside_the_floor_fold_into_one_trailing_event() {
let base = Instant::now();
let mut d = Damper::new(FLOOR);
assert_eq!(d.observe(base), Some(1));
for s in 1..=5 {
assert_eq!(d.observe(at(base, s)), None, "folded at {s}s");
assert_eq!(
d.flush(at(base, s)),
None,
"the floor has not expired at {s}s"
);
}
assert_eq!(d.deadline(), Some(at(base, 60)));
assert_eq!(d.flush(at(base, 59)), None);
assert_eq!(d.flush(at(base, 60)), Some(5));
assert_eq!(d.deadline(), None, "nothing is folded after a flush");
assert_eq!(d.observe(at(base, 61)), None);
assert_eq!(
d.flush(at(base, 119)),
None,
"not yet: the floor restarted at 60"
);
assert_eq!(d.flush(at(base, 120)), Some(1));
}
#[test]
fn a_transition_after_a_quiet_floor_publishes_at_once() {
let base = Instant::now();
let mut d = Damper::new(FLOOR);
assert_eq!(d.observe(base), Some(1));
assert_eq!(
d.observe(at(base, 61)),
Some(1),
"the floor expired with nothing folded"
);
}
}