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>
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.
◆ SwitchAggregator()
◆ addToSwitchHistory()
◆ 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()
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()
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] | currentValue | The current switch state (0-100 from smart switch). |
[in] | doubleTap | Whether 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()
◆ 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] | currentValue | The 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()
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)
◆ TestAccess< SwitchAggregator >
◆ _aggregatedState
std::optional<uint8_t> SwitchAggregator::_aggregatedState = {} |
|
private |
◆ _behaviourHandler
◆ _behaviourState
std::optional<uint8_t> SwitchAggregator::_behaviourState = {} |
|
private |
◆ _behaviourStore
◆ _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
◆ _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
Keep up a history of switch commands.
This can be commands from any source, user or automated.
◆ _twilightHandler
◆ _twilightState
std::optional<uint8_t> SwitchAggregator::_twilightState = {} |
|
private |
The documentation for this class was generated from the following file: