Skip to main content

Heartbeat

Struct Heartbeat 

Source
pub struct Heartbeat {
    pub ok: bool,
    /* private fields */
}
Expand description

Heartbeat Monitor (FB_Heartbeat)

Monitors a counter that is incremented by a remote source (typically a server or PLC). If the counter stops changing for longer than the configured timeout, the connection is considered lost.

§Behavior

  • ok is false until the first change in beat_count is observed
  • Each time beat_count differs from the previous call, the internal watchdog timer resets and ok becomes true
  • If beat_count remains unchanged for longer than period, ok becomes false

§Example

use autocore_std::fb::Heartbeat;
use std::time::Duration;

let mut hb = Heartbeat::new();
let timeout = Duration::from_millis(100);

// First call — no previous value, ok is false
hb.call(0, timeout);
assert_eq!(hb.ok, false);

// Beat count changes — connection confirmed
hb.call(1, timeout);
assert_eq!(hb.ok, true);

// Beat count keeps changing — still ok
hb.call(2, timeout);
assert_eq!(hb.ok, true);

// Beat count stalls...
hb.call(2, timeout);
assert_eq!(hb.ok, true); // Within timeout

std::thread::sleep(Duration::from_millis(110));
hb.call(2, timeout);
assert_eq!(hb.ok, false); // Timed out — connection lost

// Beat count resumes — connection restored
hb.call(3, timeout);
assert_eq!(hb.ok, true);

§Timing Diagram

beat_count: 0  1  2  3  3  3  3  3  3  4  5
                                 ↑ timed out
        ok: F  T  T  T  T  T  T  F  F  T  T

§Use Cases

  • Monitoring a PLC ↔ server communication link
  • Detecting a stalled remote process
  • Watchdog supervision of a periodic counter

Fields§

§ok: bool

Output: true when the heartbeat is alive (counter changing within the timeout period). false on first scan or after a timeout.

Implementations§

Source§

impl Heartbeat

Source

pub const DEFAULT_PERIOD: Duration = DEFAULT_PERIOD

The default timeout period (7 seconds).

Provided as a constant for convenience so callers don’t have to hard-code the value.

§Example
use autocore_std::fb::Heartbeat;

let mut hb = Heartbeat::new();
hb.call(1, Heartbeat::DEFAULT_PERIOD);
Source

pub fn new() -> Self

Creates a new heartbeat monitor.

The monitor starts on its first scan — ok will be false until the beat count changes for the first time.

§Example
use autocore_std::fb::Heartbeat;

let hb = Heartbeat::new();
assert_eq!(hb.ok, false);
Source

pub fn call(&mut self, beat_count: i64, period: Duration)

Executes one scan cycle of the heartbeat monitor.

Call this once per control cycle with the current beat count from the remote source.

§Arguments
  • beat_count - The latest heartbeat counter value from the remote source. Any i64 value; only changes matter.
  • period - Maximum allowed time between counter changes. If the counter remains static for longer than this, ok becomes false. A typical default is 7 seconds.
§Example
use autocore_std::fb::Heartbeat;
use std::time::Duration;

let mut hb = Heartbeat::new();

// Call cyclically in your control loop
hb.call(remote_counter, Duration::from_secs(7));
if !hb.ok {
    // Handle connection loss
}
Source

pub fn with_defaults() -> Self

Creates a new heartbeat monitor with the default 7-second period.

This is a convenience alias; the period is still passed per-call via call(), but this documents the standard default.

§Example
use autocore_std::fb::Heartbeat;
use std::time::Duration;

let mut hb = Heartbeat::with_defaults();
// Equivalent to Heartbeat::new(), period is passed to call()
hb.call(0, Duration::from_secs(7));

Trait Implementations§

Source§

impl Clone for Heartbeat

Source§

fn clone(&self) -> Heartbeat

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Heartbeat

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Heartbeat

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V