RumPi 1.0
A self-assembling C++ sensor & actuator library for the Raspberry Pi 5
RumPi::AlertManager Class Reference

A singleton that components hand readings to. More...

#include <AlertManager.h>

Public Member Functions

std::size_t Subscribe (std::function< void(const AlertEvent &)> callback)
 Registers a callback (e.g. More...
 
void Unsubscribe (std::size_t subscription)
 Removes a previously registered callback by its handle; unknown or already-removed handles are ignored. More...
 
void Check (const std::string &metricName, const float value, const float warningThreshold, const float alertThreshold, const bool higherIsWorse)
 A component calls this after each read: evaluates the reading, and only when the metric changes level, records the new level and notifies subscribers (snapshotting them under the lock and dispatching outside it). More...
 
void Report (const EAlertLevel level, const std::string &message)
 Logs a free-text message (a failure, error, or trace) through the alert system. More...
 
 AlertManager (const AlertManager &)=delete
 Singletons must not be copied or moved. More...
 
AlertManageroperator= (const AlertManager &)=delete
 

Static Public Member Functions

static AlertManagerGetInstance ()
 Gets the singleton instance, constructing it thread-safely on first call. More...
 
static std::string LevelToString (const EAlertLevel level)
 Converts an alert level to its display string, useful for subscribers that want to format the level. More...
 

Protected Member Functions

EAlertLevel Evaluate (const float value, const float warningThreshold, const float alertThreshold, const bool higherIsWorse)
 Evaluates a reading against its warning/alert thresholds, using higherIsWorse to choose the direction (high is bad for temperature/disk/load, low is bad for voltage/RSSI). More...
 
std::string BuildMessage (const std::string &metricName, const float value, const float threshold)
 Builds a "metric = value (threshold N)" summary string for an alert event. More...
 
void CopySubscribersLocked (std::vector< std::function< void(const AlertEvent &)> > &outSubscribers) const
 Copies the current subscriber callbacks into outSubscribers so the dispatch loop can run them after the lock is released. More...
 

Protected Attributes

std::mutex myMutex
 Guards the subscriber map, the per-metric levels, and the subscription-id counter below. More...
 
std::map< std::size_t, std::function< void(const AlertEvent &)> > mySubscribers
 Who to notify, keyed by the handle Subscribe() hands out so Unsubscribe() can remove one. More...
 
std::map< std::string, EAlertLevelmyLastLevels
 Last level seen per metric. More...
 
std::size_t myNextSubscriberId
 Monotonic source of subscription handles; only ever increments so a handle is never reused. More...
 

Private Member Functions

 AlertManager ()
 Builds the alert manager, starting subscription handles at 1. More...
 
virtual ~AlertManager ()
 Destroys the alert manager. More...
 

Detailed Description

A singleton that components hand readings to.

It evaluates each reading against its thresholds, tracks the last level per metric, and notifies subscribers ONLY when a metric changes level (so it never re-fires while a reading stays in the same band). Safe to call from multiple component threads.

Author
Eddie O'Hagan
Date
8/13/2026

Constructor & Destructor Documentation

◆ AlertManager() [1/2]

RumPi::AlertManager::AlertManager ( )
private

Builds the alert manager, starting subscription handles at 1.

Private so only GetInstance can construct it.

Author
Eddie O'Hagan
Date
8/13/2026
//Constructed once by AlertManager::GetInstance().

◆ ~AlertManager()

RumPi::AlertManager::~AlertManager ( )
privatevirtual

Destroys the alert manager.

Author
Eddie O'Hagan
Date
8/13/2026
//Destroyed at program exit (singleton).

◆ AlertManager() [2/2]

RumPi::AlertManager::AlertManager ( const AlertManager )
delete

Singletons must not be copied or moved.

Member Function Documentation

◆ BuildMessage()

std::string RumPi::AlertManager::BuildMessage ( const std::string &  metricName,
const float  value,
const float  threshold 
)
protected

Builds a "metric = value (threshold N)" summary string for an alert event.

Author
Eddie O'Hagan
Date
8/13/2026
std::string summary = BuildMessage("SystemTemperature", 82.0f, 80.0f);
std::string BuildMessage(const std::string &metricName, const float value, const float threshold)
Builds a "metric = value (threshold N)" summary string for an alert event.
Definition: AlertManager.cpp:354

Output: SystemTemperature = 82 (threshold 80)

Parameters
[in]metricNameThe metric name.
[in]valueThe reading value.
[in]thresholdThe threshold the reading crossed.
Returns
The formatted summary string.

◆ Check()

void RumPi::AlertManager::Check ( const std::string &  metricName,
const float  value,
const float  warningThreshold,
const float  alertThreshold,
const bool  higherIsWorse 
)

A component calls this after each read: evaluates the reading, and only when the metric changes level, records the new level and notifies subscribers (snapshotting them under the lock and dispatching outside it).

Author
Eddie O'Hagan
Date
8/13/2026
manager.Check("SystemTemperature", 75.0f, 70.0f, 80.0f, true);
Parameters
[in]metricNameThe metric being checked.
[in]valueThe reading to evaluate.
[in]warningThresholdThe warning threshold.
[in]alertThresholdThe alert threshold.
[in]higherIsWorseTrue if higher readings are worse, false if lower readings are worse.

◆ CopySubscribersLocked()

void RumPi::AlertManager::CopySubscribersLocked ( std::vector< std::function< void(const AlertEvent &)> > &  outSubscribers) const
protected

Copies the current subscriber callbacks into outSubscribers so the dispatch loop can run them after the lock is released.

The caller must already hold myMutex.

Author
Eddie O'Hagan
Date
8/13/2026
std::vector<std::function<void(const AlertEvent&)>> snapshot;
void CopySubscribersLocked(std::vector< std::function< void(const AlertEvent &)> > &outSubscribers) const
Copies the current subscriber callbacks into outSubscribers so the dispatch loop can run them after t...
Definition: AlertManager.cpp:115
Parameters
[out]outSubscribersThe vector the subscriber callbacks are copied into.

◆ Evaluate()

EAlertLevel RumPi::AlertManager::Evaluate ( const float  value,
const float  warningThreshold,
const float  alertThreshold,
const bool  higherIsWorse 
)
protected

Evaluates a reading against its warning/alert thresholds, using higherIsWorse to choose the direction (high is bad for temperature/disk/load, low is bad for voltage/RSSI).

Author
Eddie O'Hagan
Date
8/13/2026
EAlertLevel level = Evaluate(75.0f, 70.0f, 80.0f, true);
EAlertLevel Evaluate(const float value, const float warningThreshold, const float alertThreshold, const bool higherIsWorse)
Evaluates a reading against its warning/alert thresholds, using higherIsWorse to choose the direction...
Definition: AlertManager.cpp:142
EAlertLevel
Severity of a metric relative to its thresholds.
Definition: Common.h:94
Parameters
[in]valueThe reading to evaluate.
[in]warningThresholdThe warning threshold.
[in]alertThresholdThe alert threshold.
[in]higherIsWorseTrue if higher readings are worse, false if lower readings are worse.
Returns
The alert level (OK, Warning, or Alert).

◆ GetInstance()

AlertManager & RumPi::AlertManager::GetInstance ( )
static

Gets the singleton instance, constructing it thread-safely on first call.

Author
Eddie O'Hagan
Date
8/13/2026
AlertManager()
Builds the alert manager, starting subscription handles at 1.
Definition: AlertManager.cpp:16
static AlertManager & GetInstance()
Gets the singleton instance, constructing it thread-safely on first call.
Definition: AlertManager.cpp:48
Returns
The single AlertManager instance.

◆ LevelToString()

std::string RumPi::AlertManager::LevelToString ( const EAlertLevel  level)
static

Converts an alert level to its display string, useful for subscribers that want to format the level.

Author
Eddie O'Hagan
Date
8/13/2026
static std::string LevelToString(const EAlertLevel level)
Converts an alert level to its display string, useful for subscribers that want to format the level.
Definition: AlertManager.cpp:317
@ Warning
The metric has crossed its warning threshold.
Definition: Common.h:96

Output: WARNING

Parameters
[in]levelThe alert level to convert.
Returns
The level's display string ("OK", "WARNING", "ALERT", or "UNKNOWN").

◆ operator=()

AlertManager & RumPi::AlertManager::operator= ( const AlertManager )
delete

◆ Report()

void RumPi::AlertManager::Report ( const EAlertLevel  level,
const std::string &  message 
)

Logs a free-text message (a failure, error, or trace) through the alert system.

Unlike Check(), this always fires - every call is recorded, not just level transitions - snapshotting subscribers under the lock and dispatching outside it.

Author
Eddie O'Hagan
Date
8/13/2026
manager.Report(EAlertLevel::Warning, "Something went wrong.");
Parameters
[in]levelThe alert level of the message.
[in]messageThe free-text message to record.

◆ Subscribe()

std::size_t RumPi::AlertManager::Subscribe ( std::function< void(const AlertEvent &)>  callback)

Registers a callback (e.g.

a logger or console printer) and returns a handle to pass to Unsubscribe().

Author
Eddie O'Hagan
Date
8/13/2026
std::size_t handle = manager.Subscribe(OnAlert);
Parameters
[in]callbackThe callback to invoke on each alert.
Returns
A handle identifying this subscription.

◆ Unsubscribe()

void RumPi::AlertManager::Unsubscribe ( std::size_t  subscription)

Removes a previously registered callback by its handle; unknown or already-removed handles are ignored.

Author
Eddie O'Hagan
Date
8/13/2026
manager.Unsubscribe(handle);
Parameters
[in]subscriptionA handle previously returned by Subscribe().

Member Data Documentation

◆ myLastLevels

std::map<std::string, EAlertLevel> RumPi::AlertManager::myLastLevels
protected

Last level seen per metric.

◆ myMutex

std::mutex RumPi::AlertManager::myMutex
protected

Guards the subscriber map, the per-metric levels, and the subscription-id counter below.

◆ myNextSubscriberId

std::size_t RumPi::AlertManager::myNextSubscriberId
protected

Monotonic source of subscription handles; only ever increments so a handle is never reused.

◆ mySubscribers

std::map<std::size_t, std::function<void(const AlertEvent&)> > RumPi::AlertManager::mySubscribers
protected

Who to notify, keyed by the handle Subscribe() hands out so Unsubscribe() can remove one.


The documentation for this class was generated from the following files: