![]() |
RumPi 1.0
A self-assembling C++ sensor & actuator library for the Raspberry Pi 5
|
Owns and drives the set of sensor components. More...
#include <ComponentManager.h>
Public Member Functions | |
| ComponentManager (const ComponentManager &)=delete | |
| ComponentManager & | operator= (const ComponentManager &)=delete |
| ComponentManager () | |
| Builds an empty component manager. More... | |
| virtual | ~ComponentManager () |
| Frees every component the manager owns, then clears the internal map. More... | |
| virtual void | TurnOn () override |
| Turns on every managed component. More... | |
| virtual void | TurnOff () override |
| Turns off every managed component. More... | |
| virtual std::string | ToString () const override |
| Serializes every managed component to a JSON string, keyed by each component's unique name. More... | |
| virtual std::string | ToString (const EComponentType theComponent) const |
| Serializes a single managed component (the first of the given type) to a JSON string. More... | |
| void | Update () |
| Polls every managed component once, refreshing their readings. More... | |
| void | UpdateComponentsOfType (const EComponentType typeOfComponentToUpdate) |
| Polls only the components of the requested type, letting a caller sample a fast sensor (e.g. More... | |
| virtual void | ProcessRawValues () override |
| Processes the manager's raw values. More... | |
| void | AddComponent (BaseComponent *componentToAdd) |
| Adds a component to the manager and gives it a unique name. More... | |
| void | RemoveComponents (const EComponentType typeOfComponentToRemove) |
| Removes and deletes every managed component of the given type, freeing their names for reuse. More... | |
| void | TurnOnComponent (const BaseComponent *componentToTurnOn) |
| Turns on the single component matching the given pointer, if the manager holds it. More... | |
| void | TurnOnComponents (const EComponentType typeOfComponentToTurnOn) |
| Turns on every managed component of the given type. More... | |
| void | TurnOffComponent (const BaseComponent *componentToTurnOff) |
| Turns off the single component matching the given pointer, if the manager holds it. More... | |
| void | TurnOffComponents (const EComponentType typeOfComponentToTurnOff) |
| Turns off every managed component of the given type. More... | |
| int | GetNumberOfComponents () const |
| Gets how many components (instances, not distinct types) the manager currently holds. More... | |
| BaseComponent * | GetComponent (const EComponentType componentType) const |
| Gets the first managed component of the given type. More... | |
| bool | GetComponentTypes (std::vector< EComponentType > &outComponentTypes) const |
| Fills the provided vector with the distinct component types the manager currently holds. More... | |
| SelectedSensorReadings | GetSelectedSensorReadings () const |
| Reads the curated set of sensor values into a SelectedSensorReadings for the Home dashboard. More... | |
Public Member Functions inherited from RumPi::BaseComponent | |
| BaseComponent () | |
| Builds a base component with an unknown type and an empty name. More... | |
| virtual | ~BaseComponent () |
| Virtual destructor so deleting a component through a BaseComponent* also cleans up the concrete subclass. More... | |
| virtual void | TurnOn ()=0 |
| Turns the component on (e.g. More... | |
| virtual void | TurnOff ()=0 |
| Turns the component off. More... | |
| virtual std::string | ToString () const =0 |
| Serializes the component's current readings to a string (usually JSON). More... | |
| virtual void | ProcessRawValues ()=0 |
| Reads and processes the component's latest raw values (called once per poll). More... | |
| virtual EComponentType | GetComponentType () const |
| Gets the component's type. More... | |
| const std::string & | GetName () const |
| Gets the component's unique per-instance name. More... | |
| void | SetName (const std::string &name) |
| Sets the component's unique per-instance name. More... | |
Private Member Functions | |
| std::string | MakeNameUnique (const std::string &desiredName) |
| Returns a unique name based on desiredName: the bare name if it is free, otherwise the name with " 2", " 3", ... appended until it is unused. More... | |
Private Attributes | |
| std::unordered_multimap< const EComponentType, BaseComponent * > | myComponents |
| Every managed component, keyed by type. A multimap, so several components can share a single type. More... | |
| std::set< std::string > | myUsedNames |
| Tracks the names handed out so far so AddComponent can guarantee every component's name is unique. More... | |
Additional Inherited Members | |
Static Public Member Functions inherited from RumPi::BaseComponent | |
| static std::string | ComponentTypeToString (const EComponentType theComponentTypeToConvert) |
| Maps an EComponentType to its string name. More... | |
| static EComponentType | StringToComponentType (const std::string &theComponentTypeName) |
| Maps a type name back to its EComponentType (the inverse of ComponentTypeToString), so the client can turn the type carried on the wire back into a value it can look an icon up with. More... | |
Protected Attributes inherited from RumPi::BaseComponent | |
| EComponentType | myComponentType |
| The kind of component this is (set by each concrete subclass). More... | |
| std::string | myName |
| A unique per-instance name, assigned when the component is added to the manager. More... | |
Owns and drives the set of sensor components.
NOT internally synchronized: it is populated on the setup thread (during RumPi::Initialize, before any poll thread starts) and thereafter driven by a single poll thread (Update/ToString/GetSelectedSensorReadings/...). Do not call AddComponent/RemoveComponents concurrently with the polling methods - add a lock here first if a future design needs concurrent access.
|
delete |
| RumPi::ComponentManager::ComponentManager | ( | ) |
Builds an empty component manager.
Components are added afterwards via AddComponent().
|
virtual |
Frees every component the manager owns, then clears the internal map.
| void RumPi::ComponentManager::AddComponent | ( | BaseComponent * | componentToAdd | ) |
Adds a component to the manager and gives it a unique name.
A blank name (e.g. the receiver, added directly rather than through the manifest) is defaulted to the type name; a duplicate name is disambiguated with a numeric suffix and a warning. Ignored if the component is null.
| [in] | componentToAdd | The component to add (the manager takes ownership and may rename it). |
| BaseComponent * RumPi::ComponentManager::GetComponent | ( | const EComponentType | componentType | ) | const |
Gets the first managed component of the given type.
| [in] | componentType | The type of component to fetch. |
| bool RumPi::ComponentManager::GetComponentTypes | ( | std::vector< EComponentType > & | outComponentTypes | ) | const |
Fills the provided vector with the distinct component types the manager currently holds.
| [out] | outComponentTypes | Filled with the distinct types present. |
| int RumPi::ComponentManager::GetNumberOfComponents | ( | ) | const |
Gets how many components (instances, not distinct types) the manager currently holds.
| SelectedSensorReadings RumPi::ComponentManager::GetSelectedSensorReadings | ( | ) | const |
Reads the curated set of sensor values into a SelectedSensorReadings for the Home dashboard.
Uses the first component of each type (the fixed tiles show one reading per kind, not an aggregate). Call it on the same thread that Update()s the components, since it reads their getters live.
|
private |
Returns a unique name based on desiredName: the bare name if it is free, otherwise the name with " 2", " 3", ... appended until it is unused.
The chosen name is recorded so future calls stay unique.
| [in] | desiredName | The name to make unique. |
|
delete |
|
overridevirtual |
Processes the manager's raw values.
A ComponentManager is itself a BaseComponent, so this simply updates everything it manages.
Implements RumPi::BaseComponent.
| void RumPi::ComponentManager::RemoveComponents | ( | const EComponentType | typeOfComponentToRemove | ) |
Removes and deletes every managed component of the given type, freeing their names for reuse.
| [in] | typeOfComponentToRemove | The type of component to remove. |
|
overridevirtual |
Serializes every managed component to a JSON string, keyed by each component's unique name.
The type travels alongside so a client can pick the right icon/panel, and "data" carries the component's own serialized readings.
Implements RumPi::BaseComponent.
|
virtual |
Serializes a single managed component (the first of the given type) to a JSON string.
If no component of that type is managed, a warning is reported and an empty JSON object is returned so the caller can still parse it.
| [in] | theComponent | The type of component to serialize. |
|
overridevirtual |
Turns off every managed component.
Implements RumPi::BaseComponent.
| void RumPi::ComponentManager::TurnOffComponent | ( | const BaseComponent * | componentToTurnOff | ) |
Turns off the single component matching the given pointer, if the manager holds it.
| [in] | componentToTurnOff | The component to turn off. |
| void RumPi::ComponentManager::TurnOffComponents | ( | const EComponentType | typeOfComponentToTurnOff | ) |
Turns off every managed component of the given type.
| [in] | typeOfComponentToTurnOff | The type of component to turn off. |
|
overridevirtual |
Turns on every managed component.
Implements RumPi::BaseComponent.
| void RumPi::ComponentManager::TurnOnComponent | ( | const BaseComponent * | componentToTurnOn | ) |
Turns on the single component matching the given pointer, if the manager holds it.
| [in] | componentToTurnOn | The component to turn on. |
| void RumPi::ComponentManager::TurnOnComponents | ( | const EComponentType | typeOfComponentToTurnOn | ) |
Turns on every managed component of the given type.
| [in] | typeOfComponentToTurnOn | The type of component to turn on. |
| void RumPi::ComponentManager::Update | ( | ) |
Polls every managed component once, refreshing their readings.
Called every tick to update the sensors.
| void RumPi::ComponentManager::UpdateComponentsOfType | ( | const EComponentType | typeOfComponentToUpdate | ) |
Polls only the components of the requested type, letting a caller sample a fast sensor (e.g.
wind) on a tighter cadence than the full Update() pass without over-polling the slower sensors.
| [in] | typeOfComponentToUpdate | The component type to poll. |
|
private |
Every managed component, keyed by type. A multimap, so several components can share a single type.
|
private |
Tracks the names handed out so far so AddComponent can guarantee every component's name is unique.