DeviceNameHelperRK
|
Store the device name in a file on the flash file system. More...
#include <DeviceNameHelperRK.h>
Public Member Functions | |
void | setup (const char *path="/usr/devicename") |
You must call setup() from global setup()! More... | |
Public Member Functions inherited from DeviceNameHelper | |
void | loop () |
You must call this from loop on every call to loop() | |
DeviceNameHelper & | withNameCallback (std::function< void(const char *)> nameCallback) |
Adds a function to call when the name is known. More... | |
DeviceNameHelper & | withCheckPeriod (std::chrono::seconds checkPeriod) |
Sets. More... | |
bool | hasName () const |
Returns true if the name has been retrived and is non-empty. | |
const char * | getName () const |
Returns the device name as a c-string. More... | |
long | getLastNameCheckTime () const |
Get the time the name was last fetched. More... | |
void | checkName () |
Request the name again. More... | |
void | subscriptionRemoved () |
Call if you've called Particle.unsubscribe. More... | |
Static Public Member Functions | |
static DeviceNameHelperFile & | instance () |
Get the singleton instance of this class, creating it if necessary. More... | |
Static Public Member Functions inherited from DeviceNameHelper | |
static DeviceNameHelper * | getInstance () |
Special generic instance getter. More... | |
Protected Member Functions | |
DeviceNameHelperFile () | |
Constructor - You never instantiate this class directly. More... | |
virtual | ~DeviceNameHelperFile () |
This class is a singleton and never deleted. | |
virtual void | save () |
Virtual override of base class to save data to the file. | |
Protected Member Functions inherited from DeviceNameHelper | |
DeviceNameHelper () | |
Constructor - You never instantiate this class directly. More... | |
virtual | ~DeviceNameHelper () |
This class is a singleton and never deleted. | |
DeviceNameHelper (const DeviceNameHelper &)=delete | |
This class is not copyable. | |
DeviceNameHelper & | operator= (const DeviceNameHelper &)=delete |
This class is not copyable. | |
void | commonSetup () |
All of the storage-method specific setup methods call that at the end. | |
void | stateStart () |
State handler, entry point when starting up. More... | |
void | stateSubscribe () |
Add a subscription handler, if necessary. More... | |
void | stateWaitConnected () |
Waits until Particle.connected() and Time.isValid() are true. More... | |
void | stateWaitRequest () |
Waits POST_CONNECT_WAIT_MS milliseconds (2 seconds) then publishes the request for device name event "particle/device/name". More... | |
void | stateWaitResponse () |
Waits for a device name event to be received. More... | |
void | stateWaitRetry () |
Waits 5 minutes (RETRY_WAIT_MS) and tries requesting the name again. More... | |
void | stateWaitRecheck () |
Wait until it's time to check the name again. More... | |
void | subscriptionHandler (const char *eventName, const char *eventData) |
Subscription handler for the "particle/device/name" event. More... | |
Protected Attributes | |
String | path |
Path to the data file. Default is "/usr/devicename". | |
DeviceNameHelperData | fileData |
Heap-allocated data. A pointer to this is stored in the base class' data field. More... | |
Protected Attributes inherited from DeviceNameHelper | |
DeviceNameHelperData * | data = 0 |
DeviceNameHelperData structure pointer. More... | |
std::chrono::seconds | checkPeriod = 0s |
How often to fetch the name again in seconds (0 = never check again) | |
std::function< void(const char *)> | nameCallback = 0 |
Optional function or C++11 lambda to call when the name is known. More... | |
std::function< void(DeviceNameHelper &)> | stateHandler = 0 |
Current state handler, or NULL if in done state. | |
unsigned long | stateTime = 0 |
Some states use this for timing. It's a value from millis() if used. | |
bool | hasSubscribed = false |
true if Particle.subscribe has been called | |
bool | gotResponse = false |
true if the event subscription handler was called. The name is stored in data.name. | |
bool | forceCheck = false |
Used by checkName() to force the name to be checked again. | |
Additional Inherited Members | |
Static Public Attributes inherited from DeviceNameHelper | |
static const uint32_t | DATA_MAGIC = 0x7787a2f2 |
Magic bytes used to detect if EEPROM or retained memory has been initialized. | |
Static Protected Attributes inherited from DeviceNameHelper | |
static const unsigned long | POST_CONNECT_WAIT_MS = 2000 |
Amount of time to wait after connection for the subscription to be activated (milliseconds) | |
static const unsigned long | RESPONSE_WAIT_MS = 15000 |
How long to wait for a device name response before timing out and waiting to retry (milliseconds) | |
static const unsigned long | RETRY_WAIT_MS = 5 * 60 * 1000 |
How long to wait to retry a request to get the device name (in milliseconds) | |
static DeviceNameHelper * | _instance = 0 |
Singleton instance pointer, set by the subclass instance() methods. | |
Store the device name in a file on the flash file system.
This is a good option on Gen 3 devices (Argon, Boron, B Series, Tracker SoM) running Device OS 2.0.0 or later. The flash file system is 2 MB (4 MB on Tracker) so there's plenty of space.
On Gen 3 devices, EEPROM emulation is actually just a file on the flash file system to it's not any more efficient than using a file.
|
protected |
Constructor - You never instantiate this class directly.
Instead, use DeviceNameHelperFile::instance() to get the singleton instance, creating it if necessary.
|
static |
Get the singleton instance of this class, creating it if necessary.
You cannot construct an instance of this class manually, as a global or on the stack. You must instead use instance().
void DeviceNameHelperFile::setup | ( | const char * | path = "/usr/devicename" | ) |
|
protected |
Heap-allocated data. A pointer to this is stored in the base class' data field.
It's read from the file system during setup().