DeviceInfoLedger
|
#include <DeviceInfoLedger.h>
Public Member Functions | |
DeviceConfigLedger & | withConfigDefaultLedgerEnabled (bool value=true) |
Enable default configuration from the cloud using a cloud to device product, owner, or organization ledger. | |
DeviceConfigLedger & | withConfigDeviceLedgerEnabled (bool value=true) |
Enable per-device configuration from the cloud using a cloud to device ledger. | |
DeviceConfigLedger & | withConfigDefaultLedgerName (const char *configDefaultLedgerName) |
Set a cloud to device ledger to use to configure default settings. | |
DeviceConfigLedger & | withConfigDeviceLedgerName (const char *configDeviceLedgerName) |
Set a cloud to device ledger to use to configure device settings. | |
DeviceConfigLedger & | withLocalConfig (const char *jsonStr) |
Use a locally defined configuration (specified with a string of JSON) instead of configDefaultLedger. | |
DeviceConfigLedger & | withUpdateCallback (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 DeviceConfigLedger & | instance () |
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 | |
DeviceConfigLedger & | operator= (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. | |
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().
|
protected |
The constructor is protected because the class is a singleton.
Use DeviceConfigLedger::instance() to instantiate the singleton.
|
protecteddelete |
This class is a singleton and cannot be copied
void DeviceConfigLedger::callUpdateCallbacks | ( | ) |
Call updateCallbacks.
This is used internally after the ledger sync callback is called.
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.
|
inline |
Get a bool (boolean) configuration setting from local settings or cloud configuration (default or device override)
key | Key to read in the top level of the configuration object |
defaultValue | Value to be returned if the key does not exist |
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.
|
inline |
Get a double (64-bit floating point) configuration setting from local settings or cloud configuration (default or device override)
key | Key to read in the top level of the configuration object |
defaultValue | Value to be returned if the key does not exist |
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.
|
inline |
Get an int (32-bit signed integer) configuration setting from local settings or cloud configuration (default or device override)
key | Key to read in the top level of the configuration object |
defaultValue | Value to be returned if the key does not exist |
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.
|
inline |
Get a String configuration setting from local settings or cloud configuration (default or device override)
key | Key to read in the top level of the configuration object |
defaultValue | Value to be returned if the key does not exist |
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.
Variant DeviceConfigLedger::getConfigVariant | ( | const char * | key, |
Variant | defaultValue = {} ) const |
Get a configuration setting from local settings or cloud configuration (default or device override)
key | Top level key in the ledger |
defaultValue | Value to be returned if the key does not exist |
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.
|
static |
Gets the singleton instance of this class, allocating it if necessary.
Use DeviceConfigLedger::instance() to instantiate the singleton.
|
protecteddelete |
This class is a singleton and cannot be copied
|
inline |
Update the defaultConfig ledger data Variant and call the update callbacks.
data | The 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.
|
inline |
Update the deviceConfig ledger data Variant and call the update callbacks.
data | The 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.
|
inline |
Update the localConfig ledger data Variant and call the update callbacks.
data | The 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.
|
inline |
Set a local config setting using a bool.
key | Key to set |
value | Value to set to |
|
inline |
Set a local config setting using a double (64-bit floating point)
key | Key to set |
value | Value to set to |
|
inline |
Set a local config setting using an int (signed 32-bit integer)
key | Key to set |
value | Value to set to |
|
inline |
Set a local config setting using a c-string (null-terminated UTF-8, const char *)
key | Key to set |
value | Value to set to |
|
inline |
Set a local config setting using a Variant.
key | Key to set |
value | Value to set to |
See also overloads for specific types such as setLocalConfigBool, setLocalConfigInt, etc.
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!
|
inline |
Enable default configuration from the cloud using a cloud to device product, owner, or organization ledger.
value | enable (true) or disable (false) |
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().
|
inline |
Set a cloud to device ledger to use to configure default settings.
configDefaultLedgerName | Name of the ledger to use |
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().
|
inline |
Enable per-device configuration from the cloud using a cloud to device ledger.
value | enable (true) or disable (false) |
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().
|
inline |
Set a cloud to device ledger to use to configure device settings.
configDeviceLedgerName | Name of the ledger to use |
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().
|
inline |
Use a locally defined configuration (specified with a string of JSON) instead of configDefaultLedger.
jsonStr | The JSON value as a c-string |
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.
|
inline |
Adds a function to call when configuration is updated.
callback |
You can use this more than one; the callbacks are a vector. You cannot remove one once added!
|
staticprotected |
Singleton instance of this class.
The object pointer to this class is stored here. It's NULL at system boot.
|
protected |
Name of the default config ledger.
Enable the ledger using withConfigDefaultLedgerEnabled (before setup), Change name using withConfigDefaultLedgerName() (before setup).
|
protected |
Name of the device-specific config override ledger.
Enable the ledger using withConfigDeviceLedgerEnabled (before setup), Change name using withConfigDeviceLedgerName() (before setup).