DeviceInfoLedger
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Static Protected Attributes
DeviceConfigLedger Class Reference

#include <DeviceInfoLedger.h>

Public Member Functions

DeviceConfigLedgerwithConfigDefaultLedgerEnabled (bool value=true)
 Enable default configuration from the cloud using a cloud to device product, owner, or organization ledger.
 
DeviceConfigLedgerwithConfigDeviceLedgerEnabled (bool value=true)
 Enable per-device configuration from the cloud using a cloud to device ledger.
 
DeviceConfigLedgerwithConfigDefaultLedgerName (const char *configDefaultLedgerName)
 Set a cloud to device ledger to use to configure default settings.
 
DeviceConfigLedgerwithConfigDeviceLedgerName (const char *configDeviceLedgerName)
 Set a cloud to device ledger to use to configure device settings.
 
DeviceConfigLedgerwithLocalConfig (const char *jsonStr)
 Use a locally defined configuration (specified with a string of JSON) instead of configDefaultLedger.
 
DeviceConfigLedgerwithUpdateCallback (std::function< void()> callback)
 Adds a function to call when configuration is updated.
 
void callUpdateCallbacks ()
 Call updateCallbacks.
 
void setup ()
 Perform setup operations; call this from global application setup()
 
bool getConfigBool (const char *key, bool defaultValue=false) const
 Get a bool (boolean) configuration setting from local settings or cloud configuration (default or device override)
 
bool setLocalConfigBool (const char *key, bool value)
 Set a local config setting using a bool.
 
int getConfigInt (const char *key, int defaultValue=0) const
 Get an int (32-bit signed integer) configuration setting from local settings or cloud configuration (default or device override)
 
bool setLocalConfigInt (const char *key, int value)
 Set a local config setting using an int (signed 32-bit integer)
 
double getConfigDouble (const char *key, double defaultValue=0.0) const
 Get a double (64-bit floating point) configuration setting from local settings or cloud configuration (default or device override)
 
bool setLocalConfigDouble (const char *key, double value)
 Set a local config setting using a double (64-bit floating point)
 
String getConfigString (const char *key, const char *defaultValue="") const
 Get a String configuration setting from local settings or cloud configuration (default or device override)
 
bool setLocalConfigString (const char *key, const char *value)
 Set a local config setting using a c-string (null-terminated UTF-8, const char *)
 
Variant getConfigVariant (const char *key, Variant defaultValue={}) const
 Get a configuration setting from local settings or cloud configuration (default or device override)
 
bool setLocalConfigVariant (const char *key, Variant value)
 Set a local config setting using a Variant.
 
void clear ()
 Clears the LedgerData (defaultConfig, localConfig, deviceConfig)
 
void setLocalConfig (const LedgerData &data)
 Update the localConfig ledger data Variant and call the update callbacks.
 
void setDefaultConfig (const LedgerData &data)
 Update the defaultConfig ledger data Variant and call the update callbacks.
 
void setDeviceConfig (const LedgerData &data)
 Update the deviceConfig ledger data Variant and call the update callbacks.
 

Static Public Member Functions

static DeviceConfigLedgerinstance ()
 Gets the singleton instance of this class, allocating it if necessary.
 

Protected Member Functions

 DeviceConfigLedger ()
 The constructor is protected because the class is a singleton.
 
virtual ~DeviceConfigLedger ()
 The destructor is protected because the class is a singleton and cannot be deleted.
 
 DeviceConfigLedger (const DeviceConfigLedger &)=delete
 
DeviceConfigLedgeroperator= (const DeviceConfigLedger &)=delete
 

Protected Attributes

LedgerData defaultConfig
 The current default config from the cloud.
 
LedgerData localConfig
 Local configuration (set by JSON or accessors)
 
LedgerData deviceConfig
 The current device override config from the cloud.
 
String configDefaultLedgerName = "device-info-defaults"
 Name of the default config ledger.
 
String configDeviceLedgerName = "device-info-config"
 Name of the device-specific config override ledger.
 
Ledger configDefaultLedger
 Ledger default configuration, initialized during setup if enabled.
 
Ledger configDeviceLedger
 Ledger device-specific override configuration, initialized during setup if enabled.
 
bool configDefaultLedgerEnabled = false
 Flag if config defaults ledger is enabled.
 
bool configDeviceLedgerEnabled = false
 Flags if config device-specific overrides are enabled.
 
std::vector< std::function< void()> > updateCallbacks
 Callback functions to call when configuration is updated.
 

Static Protected Attributes

static DeviceConfigLedger_instance
 Singleton instance of this class.
 

Detailed Description

This class is a singleton; you do not create one as a global, on the stack, or with new.

From global application setup you must call: DeviceConfigLedger::instance().setup();

Set the options using the withXXX() methods before calling setup().

Constructor & Destructor Documentation

◆ DeviceConfigLedger() [1/2]

DeviceConfigLedger::DeviceConfigLedger ( )
protected

The constructor is protected because the class is a singleton.

Use DeviceConfigLedger::instance() to instantiate the singleton.

◆ DeviceConfigLedger() [2/2]

DeviceConfigLedger::DeviceConfigLedger ( const DeviceConfigLedger & )
protecteddelete

This class is a singleton and cannot be copied

Member Function Documentation

◆ callUpdateCallbacks()

void DeviceConfigLedger::callUpdateCallbacks ( )

Call updateCallbacks.

This is used internally after the ledger sync callback is called.

◆ clear()

void DeviceConfigLedger::clear ( )

Clears the LedgerData (defaultConfig, localConfig, deviceConfig)

It does not reset other settings. You will not normally need to use this, but this method is used by the unit tests.

◆ getConfigBool()

bool DeviceConfigLedger::getConfigBool ( const char * key,
bool defaultValue = false ) const
inline

Get a bool (boolean) configuration setting from local settings or cloud configuration (default or device override)

Parameters
keyKey to read in the top level of the configuration object
defaultValueValue to be returned if the key does not exist
Returns
true or false depending on the configuration setting or defaultValue.

The defaultValue parameter is optional. It may make more sense to set a local configuration with all of the default values so the defaults are stored in one place, rather than every place the value is accessed.

◆ getConfigDouble()

double DeviceConfigLedger::getConfigDouble ( const char * key,
double defaultValue = 0.0 ) const
inline

Get a double (64-bit floating point) configuration setting from local settings or cloud configuration (default or device override)

Parameters
keyKey to read in the top level of the configuration object
defaultValueValue to be returned if the key does not exist
Returns
true or false depending on the configuration setting or defaultValue.

The defaultValue parameter is optional. It may make more sense to set a local configuration with all of the default values so the defaults are stored in one place, rather than every place the value is accessed.

◆ getConfigInt()

int DeviceConfigLedger::getConfigInt ( const char * key,
int defaultValue = 0 ) const
inline

Get an int (32-bit signed integer) configuration setting from local settings or cloud configuration (default or device override)

Parameters
keyKey to read in the top level of the configuration object
defaultValueValue to be returned if the key does not exist
Returns
true or false depending on the configuration setting or defaultValue.

The defaultValue parameter is optional. It may make more sense to set a local configuration with all of the default values so the defaults are stored in one place, rather than every place the value is accessed.

◆ getConfigString()

String DeviceConfigLedger::getConfigString ( const char * key,
const char * defaultValue = "" ) const
inline

Get a String configuration setting from local settings or cloud configuration (default or device override)

Parameters
keyKey to read in the top level of the configuration object
defaultValueValue to be returned if the key does not exist
Returns
true or false depending on the configuration setting or defaultValue.

The defaultValue parameter is optional. It may make more sense to set a local configuration with all of the default values so the defaults are stored in one place, rather than every place the value is accessed.

◆ getConfigVariant()

Variant DeviceConfigLedger::getConfigVariant ( const char * key,
Variant defaultValue = {} ) const

Get a configuration setting from local settings or cloud configuration (default or device override)

Parameters
keyTop level key in the ledger
defaultValueValue to be returned if the key does not exist
Returns
Variant Return Variant, see also getConfigBool, getConfigInt, ... that wrap this method

The defaultValue parameter is optional. It may make more sense to set a local configuration with all of the default values so the defaults are stored in one place, rather than every place the value is accessed.

This can be used for both primitive types like bool, int, string, etc. as well as VariantArray and VariantMap.

◆ instance()

DeviceConfigLedger & DeviceConfigLedger::instance ( )
static

Gets the singleton instance of this class, allocating it if necessary.

Use DeviceConfigLedger::instance() to instantiate the singleton.

◆ operator=()

DeviceConfigLedger & DeviceConfigLedger::operator= ( const DeviceConfigLedger & )
protecteddelete

This class is a singleton and cannot be copied

◆ setDefaultConfig()

void DeviceConfigLedger::setDefaultConfig ( const LedgerData & data)
inline

Update the defaultConfig ledger data Variant and call the update callbacks.

Parameters
dataThe new data to replace the old data

You will probably never need to call this; it's used internally from the ledger sync callback. It's public because it's used by the unit tests.

◆ setDeviceConfig()

void DeviceConfigLedger::setDeviceConfig ( const LedgerData & data)
inline

Update the deviceConfig ledger data Variant and call the update callbacks.

Parameters
dataThe new data to replace the old data

You will probably never need to call this; it's used internally from the ledger sync callback. It's public because it's used by the unit tests.

◆ setLocalConfig()

void DeviceConfigLedger::setLocalConfig ( const LedgerData & data)
inline

Update the localConfig ledger data Variant and call the update callbacks.

Parameters
dataThe new data to replace the old data

You will probably never need to call this; it's used internally from the ledger sync callback. It's public because it's used by the unit tests.

◆ setLocalConfigBool()

bool DeviceConfigLedger::setLocalConfigBool ( const char * key,
bool value )
inline

Set a local config setting using a bool.

Parameters
keyKey to set
valueValue to set to
Returns
true if successfully set, false if not

◆ setLocalConfigDouble()

bool DeviceConfigLedger::setLocalConfigDouble ( const char * key,
double value )
inline

Set a local config setting using a double (64-bit floating point)

Parameters
keyKey to set
valueValue to set to
Returns
true if successfully set, false if not

◆ setLocalConfigInt()

bool DeviceConfigLedger::setLocalConfigInt ( const char * key,
int value )
inline

Set a local config setting using an int (signed 32-bit integer)

Parameters
keyKey to set
valueValue to set to
Returns
true if successfully set, false if not

◆ setLocalConfigString()

bool DeviceConfigLedger::setLocalConfigString ( const char * key,
const char * value )
inline

Set a local config setting using a c-string (null-terminated UTF-8, const char *)

Parameters
keyKey to set
valueValue to set to
Returns
true if successfully set, false if not

◆ setLocalConfigVariant()

bool DeviceConfigLedger::setLocalConfigVariant ( const char * key,
Variant value )
inline

Set a local config setting using a Variant.

Parameters
keyKey to set
valueValue to set to
Returns
true if successfully set, false if not

See also overloads for specific types such as setLocalConfigBool, setLocalConfigInt, etc.

◆ setup()

void DeviceConfigLedger::setup ( )

Perform setup operations; call this from global application setup()

You typically use DeviceConfigLedger::instance().setup();

This must only be called once at boot and not again. You cannot use this to reconfigure settings!

◆ withConfigDefaultLedgerEnabled()

DeviceConfigLedger & DeviceConfigLedger::withConfigDefaultLedgerEnabled ( bool value = true)
inline

Enable default configuration from the cloud using a cloud to device product, owner, or organization ledger.

Parameters
valueenable (true) or disable (false)
Returns
DeviceConfigLedger& A reference to this object to chain withXXX() calls, fluent style.

The class default is disabled; calling this with no parameter will enable it. The default name is "device-info-defaults".

Must be called before setup().

◆ withConfigDefaultLedgerName()

DeviceConfigLedger & DeviceConfigLedger::withConfigDefaultLedgerName ( const char * configDefaultLedgerName)
inline

Set a cloud to device ledger to use to configure default settings.

Parameters
configDefaultLedgerNameName of the ledger to use
Returns
DeviceConfigLedger& A reference to this object to chain withXXX() calls, fluent style.

The default Ledger name is "device-info-defaults" but defaults to disabled. To use the default name, use withConfigDefaultLedgerEnabled() to enable the cloud to device default settings ledger.

Making this call sets the name and also enables it.

Must be called before setup().

◆ withConfigDeviceLedgerEnabled()

DeviceConfigLedger & DeviceConfigLedger::withConfigDeviceLedgerEnabled ( bool value = true)
inline

Enable per-device configuration from the cloud using a cloud to device ledger.

Parameters
valueenable (true) or disable (false)
Returns
DeviceConfigLedger& A reference to this object to chain withXXX() calls, fluent style.

The class default is disabled; calling this with no parameter will enable it. The default name is "device-info-config".

Must be called before setup().

◆ withConfigDeviceLedgerName()

DeviceConfigLedger & DeviceConfigLedger::withConfigDeviceLedgerName ( const char * configDeviceLedgerName)
inline

Set a cloud to device ledger to use to configure device settings.

Parameters
configDeviceLedgerNameName of the ledger to use
Returns
DeviceConfigLedger& A reference to this object to chain withXXX() calls, fluent style.

The default Ledger name is "device-info-config" but defaults to disabled. To use the default name, use withConfigDeviceLedgerEnabled() to enable the cloud to device device settings ledger.

Making this call sets the name and also enables it.

Must be called before setup().

◆ withLocalConfig()

DeviceConfigLedger & DeviceConfigLedger::withLocalConfig ( const char * jsonStr)
inline

Use a locally defined configuration (specified with a string of JSON) instead of configDefaultLedger.

Parameters
jsonStrThe JSON value as a c-string
Returns
DeviceConfigLedger& A reference to this object to chain withXXX() calls, fluent style.

A key's value is determined by the per-device cloud settings (highest preference), cloud default config, and local config (lowest preference). If you are not using cloud ledgers, then of course the local config is always used since the others will not exist.

◆ withUpdateCallback()

DeviceConfigLedger & DeviceConfigLedger::withUpdateCallback ( std::function< void()> callback)
inline

Adds a function to call when configuration is updated.

Parameters
callback
Returns
DeviceConfigLedger&

You can use this more than one; the callbacks are a vector. You cannot remove one once added!

Field Documentation

◆ _instance

DeviceConfigLedger * DeviceConfigLedger::_instance
staticprotected

Singleton instance of this class.

The object pointer to this class is stored here. It's NULL at system boot.

◆ configDefaultLedgerName

String DeviceConfigLedger::configDefaultLedgerName = "device-info-defaults"
protected

Name of the default config ledger.

Enable the ledger using withConfigDefaultLedgerEnabled (before setup), Change name using withConfigDefaultLedgerName() (before setup).

◆ configDeviceLedgerName

String DeviceConfigLedger::configDeviceLedgerName = "device-info-config"
protected

Name of the device-specific config override ledger.

Enable the ledger using withConfigDeviceLedgerEnabled (before setup), Change name using withConfigDeviceLedgerName() (before setup).


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