DeviceNotesHelperRK
Public Types | Public Member Functions | Protected Member Functions | Protected Attributes
DeviceNotesHelper Class Reference

Public Types

enum  GetFrequency { MANUAL, AT_BOOT, PERIODIC }
 The frequency for how often to get. More...
 

Public Member Functions

 DeviceNotesHelper ()
 Construct the helper object. More...
 
virtual ~DeviceNotesHelper ()
 Destructor. More...
 
void setup ()
 Call this method from setup(). Required!
 
void loop ()
 Call this method from loop(). Required!
 
DeviceNotesHelperwithGetAtBoot ()
 Sets the get data at boot flag. Default is manually. More...
 
DeviceNotesHelperwithGetPeriodic (unsigned long periodSecs)
 Gets data periodically. Default is manually. More...
 
DeviceNotesHelperwithBufferSize (size_t bufferSize)
 Sets the buffer size. More...
 
DeviceNotesHelperwithDataUpdatedCallback (std::function< void(JsonParser &)> callback)
 Register a callback to be called when the device notes have been successfully retrieved. More...
 
DeviceNotesHelperwithSubscriptionName (const char *name)
 Change the event name used for the subscription. More...
 
DeviceNotesHelperwithGetEventName (const char *name)
 Change the event name used for the get event. More...
 
DeviceNotesHelperwithPutEventName (const char *name)
 Change the event name used for the put event. More...
 
void getFromCloud ()
 Get data from the cloud. More...
 
void putToCloud ()
 Put the data to the cloud. More...
 
bool getHasValidData () const
 Returns true if the data has been successfully parsed. More...
 
JsonParser & getJsonParser ()
 Access the JsonParser object. More...
 

Protected Member Functions

void hookResponseHandler (const char *event, const char *data)
 Used internally to subscribe to the subscriptionName event ("DeviceNotesResponse")
 

Protected Attributes

GetFrequency getFrequency = GetFrequency::MANUAL
 How often to get the value from the cloud.
 
bool getAtBootCompleted = false
 If using GetFrequency::AT_BOOT, true if it's been done.
 
unsigned long getPeriodicPeriodMs = 60000
 Period in milliseconds for GetFrequency::PERIODIC.
 
unsigned long lastGetFromCloud = 0
 Last millis() value we got data from the cloud.
 
JsonParser jp
 JsonParser object. Parses data from cloud and is updated by JsonModifier.
 
std::function< void(JsonParser &)> dataUpdatedCallback = 0
 Function that gets called when data is gotten from the cloud.
 
bool callDataUpdatedCallback = false
 Flag to indicate that dataUpdateCallback should be called from loop()
 
bool doGetFromCloud = false
 Flag to indicate that we should get data from cloud (when Particle.connected)
 
bool hasValidData = false
 True after data has been downloaded from the cloud the first time.
 
const char * subscriptionName = "DeviceNotesResponse"
 Event name used for subscription. Set before setup()
 
const char * getEventName = "DeviceNotesGet"
 Event name used to get data from the cloud.
 
const char * putEventName = "DeviceNotesPut"
 Event name to put data to the cloud.
 

Member Enumeration Documentation

◆ GetFrequency

The frequency for how often to get.

This is set by withGetAtBook() and withGetPeriodic() so you typically don't need to use these constants directly. Default is MANUAL.

Constructor & Destructor Documentation

◆ DeviceNotesHelper()

DeviceNotesHelper::DeviceNotesHelper ( )

Construct the helper object.

Typically you create only one, and as global variable.

◆ ~DeviceNotesHelper()

DeviceNotesHelper::~DeviceNotesHelper ( )
virtual

Destructor.

Not normally needed as you create one from a global variable.

Member Function Documentation

◆ getFromCloud()

void DeviceNotesHelper::getFromCloud ( )
inline

Get data from the cloud.

This is typically needed if using GetFrequency::MANUAL. It can also be used in AT_BOOT or PERIODIC modes to request data manually. If using PERIODIC mode, this will adjust the period to start from now.

◆ getHasValidData()

bool DeviceNotesHelper::getHasValidData ( ) const
inline

Returns true if the data has been successfully parsed.

If there are multiple parts to the get requests, this is true after the last part. If ther data in the device notes is not valid JSON, this will never be true.

◆ getJsonParser()

JsonParser& DeviceNotesHelper::getJsonParser ( )
inline

Access the JsonParser object.

This is used to get data that was sent and also to modify the data. The data is updated when the getFromCloud() method is called, or at the appropriate get frequency (at boot or periodic).

◆ putToCloud()

void DeviceNotesHelper::putToCloud ( )

Put the data to the cloud.

Since you can't send multi-part data to the cloud, this is limited to 622 bytes.

◆ withBufferSize()

DeviceNotesHelper& DeviceNotesHelper::withBufferSize ( size_t  bufferSize)
inline

Sets the buffer size.

It is most efficient if you set this to be larger than the largest data you will receive.

If you only get data (don't put it to the cloud) this can be a multiple part response that is quite large.

If you put data to the cloud, then you're limited to 622 bytes, the size of a single publish.

◆ withDataUpdatedCallback()

DeviceNotesHelper& DeviceNotesHelper::withDataUpdatedCallback ( std::function< void(JsonParser &)>  callback)
inline

Register a callback to be called when the device notes have been successfully retrieved.

Parameters
callbackA function or lambda function to be called when the data has been received and parsed.

The function prototype for the callback is:

void callback(JsonParser &jp);

The callback is called from within the loop() method (not from the subscription callback) so there are fewer concurrency issues to worry about in your callback. In particular, it's safe to publish from the data updated callback if you want to.

You can only register one callback. If call it again, the existing callback will be replaced. Setting it to 0 (NULL) removes the callback.

◆ withGetAtBoot()

DeviceNotesHelper& DeviceNotesHelper::withGetAtBoot ( )
inline

Sets the get data at boot flag. Default is manually.

You should call this before setup().

◆ withGetEventName()

DeviceNotesHelper& DeviceNotesHelper::withGetEventName ( const char *  name)
inline

Change the event name used for the get event.

Parameters
nameA c-string containing the name. This string will not be modified. It also will not be copied so it must remain valid after this call returns. Since you'll typically pass in a string constant that will be fine. You must generate a name in a stack allocated buffer.

Default is "DeviceNotesGet". This must match the event field in the DeviceNotesGet webhook.

◆ withGetPeriodic()

DeviceNotesHelper & DeviceNotesHelper::withGetPeriodic ( unsigned long  periodSecs)

Gets data periodically. Default is manually.

Parameters
periodSecsThe number of seconds between fetches. Minimum is 10 seconds.

◆ withPutEventName()

DeviceNotesHelper& DeviceNotesHelper::withPutEventName ( const char *  name)
inline

Change the event name used for the put event.

Parameters
nameA c-string containing the name. This string will not be modified. It also will not be copied so it must remain valid after this call returns. Since you'll typically pass in a string constant that will be fine. You must generate a name in a stack allocated buffer.

Default is "DeviceNotesPut". This must match the event field in the DeviceNotesPut webhook.

◆ withSubscriptionName()

DeviceNotesHelper& DeviceNotesHelper::withSubscriptionName ( const char *  name)
inline

Change the event name used for the subscription.

Parameters
nameA c-string containing the name. This string will not be modified. It also will not be copied so it must remain valid after this call returns. Since you'll typically pass in a string constant that will be fine. You must generate a name in a stack allocated buffer.

Default is "DeviceNotesResponse". This must match the hook-response field in the DeviceNotesGet webhook.


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