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

Author: Crownstone Team Copyright: Crownstone (https://crownstone.rocks) Date: Sep 26, 2019 License: LGPLv3+, Apache License 2.0, and/or MIT (triple-licensed) More...

#include <cs_SwitchAggregator.h>

Inheritance diagram for SwitchAggregator:
Collaboration diagram for SwitchAggregator:

Public Member Functions

 SwitchAggregator ()
 
void init (const boards_config_t &board)
 
virtual void handleEvent (event_t &evt) override
 When swSwitch is locked, only CMD_SWITCH_LOCKED events will be handled. More...
 
void switchPowered ()
 To be called when there is enough power to use the switch. 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
 

Protected Member Functions

std::vector< Component * > getChildren () override
 Components with children can override this method to return them. More...
 
- 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...
 

Private Member Functions

cs_ret_code_t updateState (bool allowOverrideReset, const cmd_source_with_counter_t &source)
 Checks the behaviourState and overrideState, to set the swSwitch to the desired value: More...
 
bool updateBehaviourHandlers ()
 Calls update on the behaviour handlers and returns true if after the update is allowed to reset the overrideState. More...
 
void update ()
 Updates internal state and adjusts the switch values based on the new state. More...
 
bool handleStateIntentionEvents (event_t &evt)
 Triggers an updateState() call on all handled events and adjusts at least one of behaviourState or overrideState. More...
 
void executeStateIntentionUpdate (uint8_t value, cmd_source_with_counter_t &source)
 Tries to update [overrideState] to [value] and then calls updateState(false). More...
 
bool registerSwitchcraftEvent (uint8_t currentValue)
 Registers a switchcraft event and checks if it's a double tap action. More...
 
uint8_t getStateIntentionSwitchcraft (uint8_t currentValue, bool doubleTap)
 Get the state intention from a switchcraft event. More...
 
bool handleTimingEvents (event_t &evt)
 EVT_TICK, STATE_TIME and EVT_TIME_SET events possibly trigger a new aggregated state. More...
 
bool handlePresenceEvents (event_t &evt)
 EVT_PRESENCE_MUTATION. More...
 
bool handleSwitchAggregatorCommand (event_t &evt)
 Clearing private variables, setting them to specific values, etc. More...
 
bool handleBehaviourEvents (event_t &evt)
 Handles the following events: More...
 
void handleSwitchStateChange (uint8_t newIntensity)
 
uint8_t aggregatedBehaviourIntensity ()
 Aggregates the value of twilightHandler and behaviourHandler into a single intensity value. More...
 
uint8_t resolveOverrideState (uint8_t overrideState)
 Returns the switch state that should be set according to the override state. More...
 
bool checkAndSetOwner (const cmd_source_with_counter_t &source)
 Tries to set source as owner of the switch. More...
 
void handleGetBehaviourDebug (event_t &evt)
 
void addToSwitchHistory (const cs_switch_history_item_t &cmd)
 
void printSwitchHistory ()
 
void printStates (uint32_t lineNumber)
 
void pushTestDataToHost ()
 

Private Attributes

TwilightHandler _twilightHandler
 
BehaviourHandler _behaviourHandler
 
BehaviourStore_behaviourStore
 
SmartSwitch _smartSwitch
 
std::optional< uint8_t > _overrideState = {}
 
std::optional< uint8_t > _behaviourState = {}
 
std::optional< uint8_t > _twilightState = {}
 
std::optional< uint8_t > _aggregatedState = {}
 
uint32_t _lastTimestamp = 0
 
uint16_t _switchcraftDoubleTapCountdown = 0
 Set on switchcraft event, then decremented each tick event until 0. More...
 
uint8_t _lastSwitchcraftOnValue = 0
 Keeps up the switch value (1-100 from smart switch) of the last time it was on, before being turned off by switchcraft. More...
 
cmd_source_with_counter_t _source = cmd_source_with_counter_t(CS_CMD_SOURCE_NONE)
 Which source claimed the switch. More...
 
uint32_t _ownerTimeoutCountdown = 0
 
CircularBuffer< cs_switch_history_item_t_switchHistory
 Keep up a history of switch commands. More...
 

Static Private Attributes

static const uint8_t _maxSwitchHistoryItems = 10
 

Friends

class TestAccess< SwitchAggregator >
 

Detailed Description

Author: Crownstone Team Copyright: Crownstone (https://crownstone.rocks) Date: Sep 26, 2019 License: LGPLv3+, Apache License 2.0, and/or MIT (triple-licensed)

Handler that aggregates events related to switching such as SwitchCraft, Behaviours, Twilight and App/User side actions. Based on the incoming data this object decides what state to set the SmartSwitch to.

Constructor & Destructor Documentation

◆ SwitchAggregator()

Member Function Documentation

◆ addToSwitchHistory()

void SwitchAggregator::addToSwitchHistory ( const cs_switch_history_item_t cmd)
private

◆ aggregatedBehaviourIntensity()

uint8_t SwitchAggregator::aggregatedBehaviourIntensity ( )
private

Aggregates the value of twilightHandler and behaviourHandler into a single intensity value.

This will return the minimum of the respective handler values when both are defined, otherwise return the value of the one that is defined, otherwise 100.

◆ checkAndSetOwner()

bool SwitchAggregator::checkAndSetOwner ( const cmd_source_with_counter_t source)
private

Tries to set source as owner of the switch.

Returns true on success, false if switch is already owned by a different source, and given source does not overrule it.

◆ executeStateIntentionUpdate()

void SwitchAggregator::executeStateIntentionUpdate ( uint8_t  value,
cmd_source_with_counter_t source 
)
private

Tries to update [overrideState] to [value] and then calls updateState(false).

If smartswitch disallows this operation at the end of updateState, revert back [overrideState] to the value it had before this function call.

◆ getChildren()

std::vector< Component * > SwitchAggregator::getChildren ( )
overrideprotectedvirtual

Components with children can override this method to return them.

This is used by getComponent<> to search for available components.

Reimplemented from Component.

◆ getStateIntentionSwitchcraft()

uint8_t SwitchAggregator::getStateIntentionSwitchcraft ( uint8_t  currentValue,
bool  doubleTap 
)
private

Get the state intention from a switchcraft event.

Parameters
[in]currentValueThe current switch state (0-100 from smart switch).
[in]doubleTapWhether the switchcraft event was a double tap.
Returns
The switch value to be set.

◆ handleBehaviourEvents()

bool SwitchAggregator::handleBehaviourEvents ( event_t evt)
private

Handles the following events:

  • CMD_GET_BEHAVIOUR_DEBUG: Fills in the current state of the SwitchAggregator as response to a query from host.
  • EVT_BEHAVIOURSTORE_MUTATION: If mutation is of type Add or Update, checks if the changed behaviour. If it was active, the override will be reset in order to show a user the effect of its change.

◆ handleEvent()

virtual void SwitchAggregator::handleEvent ( event_t evt)
overridevirtual

When swSwitch is locked, only CMD_SWITCH_LOCKED events will be handled.

Else events may alter the intended states and subsequently trigger an actual state change.

Implements EventListener.

◆ handleGetBehaviourDebug()

void SwitchAggregator::handleGetBehaviourDebug ( event_t evt)
private

◆ handlePresenceEvents()

bool SwitchAggregator::handlePresenceEvents ( event_t evt)
private

EVT_PRESENCE_MUTATION.

returns true when the event should be considered 'consumed'. (which is when evt is of one of these types.)

◆ handleStateIntentionEvents()

bool SwitchAggregator::handleStateIntentionEvents ( event_t evt)
private

Triggers an updateState() call on all handled events and adjusts at least one of behaviourState or overrideState.

◆ handleSwitchAggregatorCommand()

bool SwitchAggregator::handleSwitchAggregatorCommand ( event_t evt)
private

Clearing private variables, setting them to specific values, etc.

Debug or power user features.

◆ handleSwitchStateChange()

void SwitchAggregator::handleSwitchStateChange ( uint8_t  newIntensity)
private

◆ handleTimingEvents()

bool SwitchAggregator::handleTimingEvents ( event_t evt)
private

EVT_TICK, STATE_TIME and EVT_TIME_SET events possibly trigger a new aggregated state.

This handling function takes care of that.

returns true when the event should be considered 'consumed'. (which is when evt is of one of these types.)

◆ init()

void SwitchAggregator::init ( const boards_config_t board)

◆ printStates()

void SwitchAggregator::printStates ( uint32_t  lineNumber)
private

◆ printSwitchHistory()

void SwitchAggregator::printSwitchHistory ( )
private

◆ pushTestDataToHost()

void SwitchAggregator::pushTestDataToHost ( )
private

◆ registerSwitchcraftEvent()

bool SwitchAggregator::registerSwitchcraftEvent ( uint8_t  currentValue)
private

Registers a switchcraft event and checks if it's a double tap action.

Sets _lastSwitchcraftOnValue and _switchcraftDoubleTapCountdown.

Parameters
[in]currentValueThe current switch value.
Returns
true When this event is a double tap.

◆ resolveOverrideState()

uint8_t SwitchAggregator::resolveOverrideState ( uint8_t  overrideState)
private

Returns the switch state that should be set according to the override state.

When override state is the special value 'smart on' it should be interpreted according to the values of twilightHandler and behaviourHandler. This getter centralizes that.

◆ switchPowered()

void SwitchAggregator::switchPowered ( )

To be called when there is enough power to use the switch.

◆ update()

void SwitchAggregator::update ( )
private

Updates internal state and adjusts the switch values based on the new state.

Also keeps switchHistory up to date.

◆ updateBehaviourHandlers()

bool SwitchAggregator::updateBehaviourHandlers ( )
private

Calls update on the behaviour handlers and returns true if after the update is allowed to reset the overrideState.

Returns true when behaviour value changed since last time it was called.

◆ updateState()

cs_ret_code_t SwitchAggregator::updateState ( bool  allowOverrideReset,
const cmd_source_with_counter_t source 
)
private

Checks the behaviourState and overrideState, to set the swSwitch to the desired value:

  • if swSwitch doesn't allow switching, nothing happens, else,
  • when overrideState has a value, swSwitch is set to that value, else,
  • when behaviourState has a value, swSwitch is set to that value, else,
  • nothing happens.

This method will clear the overrideState when it matches the behaviourState, unless the switch is locked or allowOverrideReset is false.

(Disallowing override state to reset is used for commands that want to change the value and trigger a reset which are not initated through behaviour handlers)

Friends And Related Function Documentation

◆ TestAccess< SwitchAggregator >

friend class TestAccess< SwitchAggregator >
friend

Member Data Documentation

◆ _aggregatedState

std::optional<uint8_t> SwitchAggregator::_aggregatedState = {}
private

◆ _behaviourHandler

BehaviourHandler SwitchAggregator::_behaviourHandler
private

◆ _behaviourState

std::optional<uint8_t> SwitchAggregator::_behaviourState = {}
private

◆ _behaviourStore

BehaviourStore* SwitchAggregator::_behaviourStore
private

◆ _lastSwitchcraftOnValue

uint8_t SwitchAggregator::_lastSwitchcraftOnValue = 0
private

Keeps up the switch value (1-100 from smart switch) of the last time it was on, before being turned off by switchcraft.

◆ _lastTimestamp

uint32_t SwitchAggregator::_lastTimestamp = 0
private

◆ _maxSwitchHistoryItems

const uint8_t SwitchAggregator::_maxSwitchHistoryItems = 10
staticprivate

◆ _overrideState

std::optional<uint8_t> SwitchAggregator::_overrideState = {}
private

◆ _ownerTimeoutCountdown

uint32_t SwitchAggregator::_ownerTimeoutCountdown = 0
private

◆ _smartSwitch

SmartSwitch SwitchAggregator::_smartSwitch
private

◆ _source

Which source claimed the switch.

Until timeout, nothing with a different source can set the switch. Unless that source overrules the current source.

◆ _switchcraftDoubleTapCountdown

uint16_t SwitchAggregator::_switchcraftDoubleTapCountdown = 0
private

Set on switchcraft event, then decremented each tick event until 0.

◆ _switchHistory

CircularBuffer<cs_switch_history_item_t> SwitchAggregator::_switchHistory
private

Keep up a history of switch commands.

This can be commands from any source, user or automated.

◆ _twilightHandler

TwilightHandler SwitchAggregator::_twilightHandler
private

◆ _twilightState

std::optional<uint8_t> SwitchAggregator::_twilightState = {}
private

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