Bluenet  5.7.0
Bluenet, firmware for nRF52 smart home devices
Loading...
Searching...
No Matches
BehaviourHandler Class Reference

#include <cs_BehaviourHandler.h>

Inheritance diagram for BehaviourHandler:
Collaboration diagram for BehaviourHandler:

Public Member Functions

virtual cs_ret_code_t init () override
 Obtains a pointer to presence handler, if it exists. More...
 
virtual ~BehaviourHandler ()=default
 
virtual void handleEvent (event_t &evt)
 Computes the intended behaviour state of this crownstone based on the stored behaviours, and then dispatches an event for that. More...
 
bool update ()
 Acquires the current time and presence information. More...
 
std::optional< uint8_t > getValue ()
 Returns currentIntendedState variable and updates the previousIntendedState to currentIntendedState to match previousIntendedState. More...
 
bool requiresPresence (Time t)
 Returns true if a behaviour at given time requires presence. More...
 
bool requiresAbsence (Time t)
 Returns true if a behaviour at given time requires absence. More...
 
bool validateBehaviour (Behaviour *behaviour) const
 Checks if the given behaviour is valid. More...
 
- Public Member Functions inherited from EventListener
 EventListener ()
 
virtual ~EventListener ()
 unregisters the listener. More...
 
virtual void handleEvent (event_t &event)=0
 Handle events. More...
 
void listen ()
 Registers this with the EventDispatcher. More...
 
- Public Member Functions inherited from Component
template<class T >
T * getComponent (Component *requester=nullptr)
 Returns a component of type T* from _parent->children(), If not found try again with ancestors: _parent-> ... ->_parent->children(). More...
 
virtual cs_ret_code_t init ()
 Components can implement this if they need to get references to sibling or if they need to do specific initialization. More...
 
void parentAllChildren ()
 utility that loops over all elements of getChildren() and setParent on the non-nullptr ones. More...
 
virtual ~Component ()=default
 

Private Member Functions

std::optional< uint8_t > computeIntendedState (Time currenttime, PresenceStateDescription currentpresence) const
 Given current time/presence, query the behaviourstore and check if there any valid ones. More...
 
SwitchBehaviourresolveSwitchBehaviour (Time currentTime, PresenceStateDescription currentPresence) const
 Returns most specific active switch behaviour, resolving conflicts. More...
 
void handleGetBehaviourDebug (event_t &evt)
 
SwitchBehaviourvalidateSwitchBehaviour (Behaviour *behaviour, Time currentTime, PresenceStateDescription currentPresence) const
 
TwilightBehaviourvalidateTwilightBehaviour (Behaviour *behaviour, Time currentTime, PresenceStateDescription currentPresence) const
 
void onBehaviourSettingsMeshMsg (behaviour_settings_t settings)
 
void onBehaviourSettingsChange (behaviour_settings_t settings)
 To be called when the behaviour settings were changed, and on sync response. More...
 
bool onBehaviourSettingsOutgoingSyncRequest ()
 Returns true when the behaviour settings should be synced. More...
 
void onMeshSyncFailed ()
 Finalizes the behaviour settings sync. More...
 
void tryFinalizeBehaviourSettingsSync ()
 If there was any behaviour settings sync response, the behaviour settings sync will be finalized. More...
 
void onBehaviourSettingsIncomingSyncRequest ()
 Sends a sync response. More...
 

Private Attributes

PresenceHandler_presenceHandler = nullptr
 Cached reference to the presence handler. More...
 
BehaviourStore_behaviourStore = nullptr
 cached reference to the behaviour store. More...
 
std::optional< uint8_t > previousIntendedState = {}
 The last value returned by getValue. More...
 
std::optional< uint8_t > currentIntendedState = {}
 The last value that was updated by the update method. More...
 
bool _isActive = true
 setting this to false will result in a BehaviourHandler that will not have an opinion about the state anymore (getValue returns std::nullopt). More...
 
bool _behaviourSettingsSynced = false
 Whether the behaviour settings are synced. More...
 
std::optional< behaviour_settings_t_receivedBehaviourSettings = {}
 Cache the received behaviour settings during syncing. More...
 

Friends

class TestAccess< BehaviourHandler >
 

Additional Inherited Members

- Protected Member Functions inherited from Component
virtual std::vector< Component * > getChildren ()
 Components with children can override this method to return them. More...
 
cs_ret_code_t initChildren ()
 
void setParent (Component *p)
 Children that are instantiated later can also be added individually. More...
 

Constructor & Destructor Documentation

◆ ~BehaviourHandler()

virtual BehaviourHandler::~BehaviourHandler ( )
virtualdefault

Member Function Documentation

◆ computeIntendedState()

std::optional< uint8_t > BehaviourHandler::computeIntendedState ( Time  currenttime,
PresenceStateDescription  currentpresence 
) const
private

Given current time/presence, query the behaviourstore and check if there any valid ones.

Returns an empty optional when:

  • this BehaviourHandler is inactive, or
  • _presenceHandler is nullptr, or
  • more than one valid behaviours contradicted each other.

Returns a non-empty optional if a valid behaviour is found or multiple agreeing behaviours have been found. In this case its value contains the desired state value. When no behaviours are valid at given time/presence the intended value is 0. (house is 'off' by default)

◆ getValue()

std::optional< uint8_t > BehaviourHandler::getValue ( )

Returns currentIntendedState variable and updates the previousIntendedState to currentIntendedState to match previousIntendedState.

◆ handleEvent()

virtual void BehaviourHandler::handleEvent ( event_t evt)
virtual

Computes the intended behaviour state of this crownstone based on the stored behaviours, and then dispatches an event for that.

Events:

  • EVT_PRESENCE_MUTATION
  • EVT_BEHAVIOURSTORE_MUTATION
  • STATE_BEHAVIOUR_SETTINGS
  • CMD_GET_BEHAVIOUR_DEBUG

Implements EventListener.

◆ handleGetBehaviourDebug()

void BehaviourHandler::handleGetBehaviourDebug ( event_t evt)
private

◆ init()

virtual cs_ret_code_t BehaviourHandler::init ( )
overridevirtual

Obtains a pointer to presence handler, if it exists.

Reimplemented from Component.

◆ onBehaviourSettingsChange()

void BehaviourHandler::onBehaviourSettingsChange ( behaviour_settings_t  settings)
private

To be called when the behaviour settings were changed, and on sync response.

◆ onBehaviourSettingsIncomingSyncRequest()

void BehaviourHandler::onBehaviourSettingsIncomingSyncRequest ( )
private

Sends a sync response.

To be called on a behaviour settings sync request

◆ onBehaviourSettingsMeshMsg()

void BehaviourHandler::onBehaviourSettingsMeshMsg ( behaviour_settings_t  settings)
private

◆ onBehaviourSettingsOutgoingSyncRequest()

bool BehaviourHandler::onBehaviourSettingsOutgoingSyncRequest ( )
private

Returns true when the behaviour settings should be synced.

To be called when a sync request will be sent out.

◆ onMeshSyncFailed()

void BehaviourHandler::onMeshSyncFailed ( )
private

Finalizes the behaviour settings sync.

To be called when the overall sync failed.

◆ requiresAbsence()

bool BehaviourHandler::requiresAbsence ( Time  t)

Returns true if a behaviour at given time requires absence.

◆ requiresPresence()

bool BehaviourHandler::requiresPresence ( Time  t)

Returns true if a behaviour at given time requires presence.

◆ resolveSwitchBehaviour()

SwitchBehaviour * BehaviourHandler::resolveSwitchBehaviour ( Time  currentTime,
PresenceStateDescription  currentPresence 
) const
private

Returns most specific active switch behaviour, resolving conflicts.

None if no behaviours are active. Requires _behaviourStore to be non-null and currentTime.isValid() == true.

Parameters
currentTime
currentPresence
Returns

◆ tryFinalizeBehaviourSettingsSync()

void BehaviourHandler::tryFinalizeBehaviourSettingsSync ( )
private

If there was any behaviour settings sync response, the behaviour settings sync will be finalized.

◆ update()

bool BehaviourHandler::update ( )

Acquires the current time and presence information.

Checks and updates the currentIntendedState by looping over the active behaviours.

If isActive is false, or _presenceHandler is nullptr, this method has no effect.

Returns true.

◆ validateBehaviour()

bool BehaviourHandler::validateBehaviour ( Behaviour behaviour) const

Checks if the given behaviour is valid.

I.e. its presence clause and time constraints are met.

Presence and time are obtained from PresenceHandler and SystemTime for this check.

See also
Behaviour::isValid.

◆ validateSwitchBehaviour()

SwitchBehaviour * BehaviourHandler::validateSwitchBehaviour ( Behaviour behaviour,
Time  currentTime,
PresenceStateDescription  currentPresence 
) const
private
Returns
Switch behaviour if the behaviour is a valid switch behaviour, and active at this time/presence.
nullptr otherwise.

◆ validateTwilightBehaviour()

TwilightBehaviour * BehaviourHandler::validateTwilightBehaviour ( Behaviour behaviour,
Time  currentTime,
PresenceStateDescription  currentPresence 
) const
private
Returns
Twilight behaviour if the behaviour is a valid twilight behaviour, and active at this time/presence.
nullptr otherwise.

Friends And Related Function Documentation

◆ TestAccess< BehaviourHandler >

friend class TestAccess< BehaviourHandler >
friend

Member Data Documentation

◆ _behaviourSettingsSynced

bool BehaviourHandler::_behaviourSettingsSynced = false
private

Whether the behaviour settings are synced.

◆ _behaviourStore

BehaviourStore* BehaviourHandler::_behaviourStore = nullptr
private

cached reference to the behaviour store.

(obtained at init)

◆ _isActive

bool BehaviourHandler::_isActive = true
private

setting this to false will result in a BehaviourHandler that will not have an opinion about the state anymore (getValue returns std::nullopt).

◆ _presenceHandler

PresenceHandler* BehaviourHandler::_presenceHandler = nullptr
private

Cached reference to the presence handler.

(obtained at init)

◆ _receivedBehaviourSettings

std::optional<behaviour_settings_t> BehaviourHandler::_receivedBehaviourSettings = {}
private

Cache the received behaviour settings during syncing.

◆ currentIntendedState

std::optional<uint8_t> BehaviourHandler::currentIntendedState = {}
private

The last value that was updated by the update method.

◆ previousIntendedState

std::optional<uint8_t> BehaviourHandler::previousIntendedState = {}
private

The last value returned by getValue.


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