![]() |
RumPi 1.0
A self-assembling C++ sensor & actuator library for the Raspberry Pi 5
|
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... | |
| AlertManager & | operator= (const AlertManager &)=delete |
Static Public Member Functions | |
| static AlertManager & | GetInstance () |
| 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, EAlertLevel > | myLastLevels |
| 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... | |
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.
|
private |
Builds the alert manager, starting subscription handles at 1.
Private so only GetInstance can construct it.
|
privatevirtual |
Destroys the alert manager.
|
delete |
Singletons must not be copied or moved.
|
protected |
Builds a "metric = value (threshold N)" summary string for an alert event.
Output: SystemTemperature = 82 (threshold 80)
| [in] | metricName | The metric name. |
| [in] | value | The reading value. |
| [in] | threshold | The threshold the reading crossed. |
| 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).
| [in] | metricName | The metric being checked. |
| [in] | value | The reading to evaluate. |
| [in] | warningThreshold | The warning threshold. |
| [in] | alertThreshold | The alert threshold. |
| [in] | higherIsWorse | True if higher readings are worse, false if lower readings are worse. |
|
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.
| [out] | outSubscribers | The vector the subscriber callbacks are copied into. |
|
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).
| [in] | value | The reading to evaluate. |
| [in] | warningThreshold | The warning threshold. |
| [in] | alertThreshold | The alert threshold. |
| [in] | higherIsWorse | True if higher readings are worse, false if lower readings are worse. |
|
static |
Gets the singleton instance, constructing it thread-safely on first call.
|
static |
Converts an alert level to its display string, useful for subscribers that want to format the level.
Output: WARNING
| [in] | level | The alert level to convert. |
|
delete |
| 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.
| [in] | level | The alert level of the message. |
| [in] | message | The free-text message to record. |
| 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().
| [in] | callback | The callback to invoke on each alert. |
| void RumPi::AlertManager::Unsubscribe | ( | std::size_t | subscription | ) |
Removes a previously registered callback by its handle; unknown or already-removed handles are ignored.
| [in] | subscription | A handle previously returned by Subscribe(). |
|
protected |
Last level seen per metric.
|
protected |
Guards the subscriber map, the per-metric levels, and the subscription-id counter below.
|
protected |
Monotonic source of subscription handles; only ever increments so a handle is never reused.
|
protected |
Who to notify, keyed by the handle Subscribe() hands out so Unsubscribe() can remove one.