SensESP 3.0.1
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
sensesp::SmartSwitchController Class Reference

A high level transform designed to control a digital output (such as a relay) via manual button presses or programatic commands. More...

#include <sensesp/controllers/smart_switch_controller.h>

Inheritance diagram for sensesp::SmartSwitchController:
[legend]

Classes

class  SyncPath
 Used to store configuration internally. More...
 

Public Member Functions

 SmartSwitchController (bool auto_initialize=true, String config_path="", const char *sk_sync_paths[]=NULL)
 
virtual bool to_json (JsonObject &doc) override
 
virtual bool from_json (const JsonObject &config) override
 
- Public Member Functions inherited from sensesp::ValueProducer< bool >
 ValueProducer ()
 
 ValueProducer (const bool &initial_value)
 
virtual const bool & get () const
 
std::enable_if< std::is_base_of< ValueConsumer< typenameVConsumer::input_type >, VConsumer >::value &&std::is_convertible< bool, typenameVConsumer::input_type >::value, std::shared_ptr< VConsumer > >::type connect_to (std::shared_ptr< VConsumer > consumer)
 Connect a producer to a transform with a different input type.
 
std::enable_if< std::is_base_of< ValueConsumer< typenameVConsumer::input_type >, VConsumer >::value &&std::is_convertible< bool, typenameVConsumer::input_type >::value, VConsumer * >::type connect_to (VConsumer *consumer)
 
std::enable_if< std::is_base_of< ValueConsumer< typenameVConsumer::input_type >, VConsumer >::value &&std::is_convertible< bool, typenameVConsumer::input_type >::value, VConsumer * >::type connect_to (VConsumer &consumer)
 
void emit (const bool &new_value)
 
- Public Member Functions inherited from sensesp::Observable
 Observable ()
 
 Observable (Observable &&other)
 Move constructor.
 
void notify ()
 
void attach (std::function< void()> observer)
 

Public Attributes

LambdaConsumer< ClickTypesclick_consumer_
 
LambdaConsumer< String > truthy_string_consumer_
 
LambdaConsumer< bool > swich_consumer_
 

Protected Attributes

bool is_on_ = false
 
bool auto_initialize_
 
std::set< SyncPathsync_paths_
 
- Protected Attributes inherited from sensesp::ValueProducer< bool >
bool output_
 

Detailed Description

A high level transform designed to control a digital output (such as a relay) via manual button presses or programatic commands.

To accomplish this, the controller accepts inputs from a generic boolean producer (usually a SignalK listener), as well as String and ClickType inputs. The latter allows a physical button to control the load as well as add special behaviors to the sensor application. In particular, a double click can be configured to set the state of multiple signal k paths, and an ultra long press of the button will cause the MCU to reboot.

A SmartSwitchController object behaves differently depending on the type of input it receives. If the input is a boolean or a "valid truth type" (which is a specialized type of boolean - see below), SmartSwitchController's output will be "on" if the input is "true", or "off" if the input is "false". If the input is a ClickType, or a String that's NOT a "valid truth type", SmartSwitchController's output will simply toggle the output back and forth between "on" and "off" with each input. There are two exceptions to this if the input is a ClickType:

  1. A value of ClickTypes::DoubleClick will toggle the output as well as send PUT requests to synchronize zero or more paths that are configured to do so
  2. A value of ClickTypes::UltraLongSingleClick will reboot the MCU.

Incoming String values are evaluated to see if they represent a "valid truth type". Examples include "on", "ON", "off", "true", "false", "one", "1", "123" (or any other string that represents a non-zero value), etc. Case is insensitive. Any incoming String that doesn't evaluate to a "valid truth type" will be treated as a "single click", and will toggle the output btween "on" and "off".

See also
TextToTruth
ClickType

Definition at line 46 of file smart_switch_controller.h.

Constructor & Destructor Documentation

◆ SmartSwitchController()

sensesp::SmartSwitchController::SmartSwitchController ( bool auto_initialize = true,
String config_path = "",
const char * sk_sync_paths[] = NULL )

The constructor

Parameters
auto_initializeIf TRUE, the controller will emit an initial "off" status when enabled. This is generally the desired case unless this controller is mirroring the state of a remote load.
config_pathThe path to save configuration data (blank for no saving)
sk_sync_pathsAn optional array of Signal K paths that should synchronize their status whenever a double click ClickType is received. Each path listed will have a PUT request issued to set the status to be the same as this SmartSwitchController each time a double click occurs. This list, if specified, should have a zero length string as its last entry.

Definition at line 8 of file smart_switch_controller.cpp.

Here is the call graph for this function:

Member Function Documentation

◆ from_json()

bool sensesp::SmartSwitchController::from_json ( const JsonObject & root)
overridevirtual

Deserializes the current object data from a JsonObject.

Reimplemented from sensesp::Serializable.

Definition at line 38 of file smart_switch_controller.cpp.

◆ to_json()

bool sensesp::SmartSwitchController::to_json ( JsonObject & root)
overridevirtual

Serializes the current object data into a JsonObject.

Reimplemented from sensesp::Serializable.

Definition at line 30 of file smart_switch_controller.cpp.

Member Data Documentation

◆ auto_initialize_

bool sensesp::SmartSwitchController::auto_initialize_
protected

Definition at line 129 of file smart_switch_controller.h.

◆ click_consumer_

LambdaConsumer<ClickTypes> sensesp::SmartSwitchController::click_consumer_
Initial value:
{[this](ClickTypes new_value) {
if (!ClickType::is_click(new_value)) {
return;
}
ESP.restart();
return;
}
this->is_on_ = !this->is_on_;
this->emit(this->is_on_);
if (new_value == ClickTypes::DoubleClick) {
for (auto& path : sync_paths_) {
ESP_LOGD(__FILENAME__, "Sync status to %s", path.sk_sync_path_.c_str());
path.put_request_->set(this->is_on_);
}
}
}}
static bool is_click(ClickTypes value)
void emit(const bool &new_value)

Definition at line 71 of file smart_switch_controller.h.

◆ is_on_

bool sensesp::SmartSwitchController::is_on_ = false
protected

Definition at line 128 of file smart_switch_controller.h.

◆ swich_consumer_

LambdaConsumer<bool> sensesp::SmartSwitchController::swich_consumer_
Initial value:
{[this](bool value) {
this->is_on_ = value;
this->emit(is_on_);
}}

Definition at line 108 of file smart_switch_controller.h.

◆ sync_paths_

std::set<SyncPath> sensesp::SmartSwitchController::sync_paths_
protected

Definition at line 130 of file smart_switch_controller.h.

◆ truthy_string_consumer_

LambdaConsumer<String> sensesp::SmartSwitchController::truthy_string_consumer_
Initial value:
{[this](String new_value) {
if (TextToTruth::is_valid_true(new_value)) {
this->is_on_ = true;
} else if (TextToTruth::is_valid_false(new_value)) {
this->is_on_ = false;
} else {
this->is_on_ = !this->is_on_;
}
this->emit(this->is_on_);
}}
static bool is_valid_false(const String &value)
static bool is_valid_true(const String &value)
Definition truth_text.cpp:7

Definition at line 96 of file smart_switch_controller.h.


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