Introduces structured child identity for organs

Establishes a `ChildIdentity` struct to encapsulate an organ's ID alongside its parent's ID.
Refactors the `Parenchyma` trait to return this new structured identity, enhancing context for each organ.
Updates the `impl_identifiable` macro and `LedPump` implementation to adopt the new model.
Adds a basic heartbeat logging mechanism to `LedPump`, leveraging the richer identity for monitoring.
This commit is contained in:
Russell Gilbert 2026-03-27 07:58:15 +00:00
parent ef10ab5998
commit 09f75f97eb
5 changed files with 35 additions and 13 deletions

View file

@ -0,0 +1,14 @@
#[derive(Debug, Clone, Copy)]
pub struct ChildIdentity {
id: u32,
parent_id: u32,
}
impl ChildIdentity {
pub fn new(id: u32, parent_id: u32) -> Self {
Self {
id,
parent_id
}
}
}

View file

@ -2,8 +2,8 @@
macro_rules! impl_identifiable { macro_rules! impl_identifiable {
($target:ident, { $($body:tt)* }) => { ($target:ident, { $($body:tt)* }) => {
impl Parenchyma for $target { impl Parenchyma for $target {
fn id(&self) -> u32 { fn identity(&self) -> child_identity::ChildIdentity {
self.id self.identity
} }
$($body)* $($body)*

View file

@ -5,6 +5,7 @@ pub mod coordinates;
pub mod protocols; pub mod protocols;
pub mod system; pub mod system;
pub mod macros; pub mod macros;
pub mod child_identity;
use std::fmt::Alignment::Left; use std::fmt::Alignment::Left;
use tracing::{debug, info}; use tracing::{debug, info};

View file

@ -1,16 +1,22 @@
use crate::impl_identifiable; use crate::child_identity::ChildIdentity;
use crate::organs::parenchyma::Parenchyma; use crate::organs::parenchyma::Parenchyma;
use crate::protocols::{OrganCommand, OrganCommandEnvelope, OrganResponse}; use crate::protocols::{OrganCommand, OrganCommandEnvelope, OrganResponse};
use crate::system::time::Time;
use crate::{child_identity, impl_identifiable};
use tracing::{info};
pub struct LedPump { pub struct LedPump {
id: u32, identity: child_identity::ChildIdentity,
parent_organ_id: u32,
last_beat: u64, last_beat: u64,
} }
impl_identifiable!(LedPump, { impl_identifiable!(LedPump, {
fn do_work(&mut self, _envelope: OrganCommandEnvelope) -> OrganResponse { fn do_work(&mut self, _envelope: OrganCommandEnvelope) -> OrganResponse {
// Your logic here // Your logic here
let now = Time::time_stamp_millis();
if now > self.last_beat + 500 {
info!("LED Pump heart beat at {} {:?}", now, self.identity);
self.last_beat = now;
}
OrganResponse::Ok OrganResponse::Ok
} }
@ -22,9 +28,8 @@ impl_identifiable!(LedPump, {
impl LedPump { impl LedPump {
pub fn new(id: u32, parent_organ_id: u32) -> Self { pub fn new(id: u32, parent_organ_id: u32) -> Self {
Self { Self {
id, identity: ChildIdentity::new(id, parent_organ_id),
parent_organ_id, last_beat: 0,
last_beat: 0
} }
} }
} }

View file

@ -1,13 +1,15 @@
use crate::child_identity::ChildIdentity;
use crate::protocols::{OrganCommand, OrganCommandEnvelope, OrganResponse};
/* /*
Parenchyma: The essential and distinctive functional tissue of an organ, Parenchyma: The essential and distinctive functional tissue of an organ,
as distinguished from its connective tissue, blood vessels, as distinguished from its connective tissue, blood vessels,
and nerves (the stroma) and nerves (the stroma)
*/ */
use crate::protocols::{OrganCommand, OrganCommandEnvelope, OrganResponse};
pub trait Parenchyma: Send { pub trait Parenchyma: Send {
fn id(&self) -> u32; fn identity(&self) -> ChildIdentity;
fn do_work (&mut self, command_envelope: OrganCommandEnvelope) -> OrganResponse; fn do_work (&mut self, command_envelope: OrganCommandEnvelope) -> OrganResponse;
fn get_supported_commands(&self) -> Vec<OrganCommand>; fn get_supported_commands(&self) -> Vec<OrganCommand>;
} }