SmsWebhookRK
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Static Protected Attributes
SmsWebhook Class Reference

Class for the library. More...

#include <SmsWebhookRK.h>

Public Member Functions

void setup ()
 You must call setup() from global application setup()! More...
 
void loop ()
 You must call loop() from global application loop()! More...
 
void queueSms (SmsMessage smsMessage)
 Queue a SmsMessage to send. More...
 
SmsWebhookwithEventName (const char *eventName)
 Sets the event name to use. This must match the webhook. Default is "SendSmsEvent". More...
 
const char * getEventName () const
 Get the previously set event name (or the default, if it hasn't been set yet) More...
 
SmsWebhookwithRecipientCallback (std::function< bool(String &phone)> recipientCallback)
 Sets a function to call to get the recipient if the SmsMessage recipient field is black. More...
 
SmsWebhookwithRetryNoRecipientMs (unsigned long milliseconds)
 Sets the retry time when no recipient is specified in milliseconds. Default is 15 seconds. More...
 
unsigned long getRetryNoRecipientMs () const
 Get the previously set retry when no recipient is specified value (or the default, if it hasn't been set yet) More...
 
SmsWebhookwithRetryPublishFailMs (unsigned long milliseconds)
 Sets the retry time if the publish fails. Default is 15 seconds. More...
 
unsigned long getRetryPublishFailMs () const
 Get the previously set retry when publish fails value (or the default, if it hasn't been set yet) More...
 
SmsWebhookwithPublishRateLimitMs (unsigned long milliseconds)
 Sets publish rate limit. If multiple publishes are required, wait this long between publishes. Default is 1010 milliseconds. More...
 
unsigned long getPublishRateLimitMs () const
 Get the previously set publish rate limit value (or the default, if it hasn't been set yet) More...
 

Static Public Member Functions

static SmsWebhookinstance ()
 Get the singleton instance of this class.
 

Protected Member Functions

 SmsWebhook ()
 Constructor (protected) More...
 
virtual ~SmsWebhook ()
 Destructor - never used. More...
 
 SmsWebhook (const SmsWebhook &)=delete
 This class is not copyable.
 
SmsWebhookoperator= (const SmsWebhook &)=delete
 This class is not copyable.
 
void stateWaitForMessage ()
 State handler for waiting for a message and cloud connected.
 
void stateWaitPublish ()
 State handler for waiting for publish to complete.
 
void stateWaitRetry ()
 State handler for waiting for to retry.
 

Protected Attributes

String eventName = "SendSmsEvent"
 Event name to use. Default is "SendSmsEvent". Use withEventName() to change.
 
std::function< bool(String &)> recipientCallback = 0
 Recipient callbac. Use withRecipientCallback() to change. Default: none.
 
os_mutex_t sendQueueMutex = 0
 Mutex to protect sendQueue from access from multiple threads simultaneously.
 
std::deque< SmsMessagesendQueue
 Queue of SmsMessage objects to send. More...
 
particle::Future< bool > publishFuture
 Future used to monitor the state of Particle.publish(). More...
 
unsigned long stateTime = 0
 millis() value used for various timing purposes. This is always the start time.
 
unsigned long retryTimeMs = 0
 How long to wait for retry in stateWaitRetry. More...
 
unsigned long retryNoRecipientMs = 15000
 Retry time when no recipient is specified in milliseconds. Default is 15 seconds.
 
unsigned long retryPublishFailMs = 15000
 Retry time when no if publish fails in milliseconds.
 
unsigned long publishRateLimitMs = 1010
 publish rate limit. If multiple publishes are required, wait this long between publishes.
 
std::function< void(SmsWebhook &)> stateHandler = 0
 State handler for the main state machine. More...
 

Static Protected Attributes

static const size_t JSON_BUF_SIZE = 256
 Sets the temporary buffer for the JSON data for the publish. More...
 
static SmsWebhook_instance
 Singleton instance of this class. More...
 

Detailed Description

Class for the library.

It's a singleton, so you always access it using SmsWebhook::instance(). You never allocate one as a global variable, stack variable, or with new.

You must call SmsWebhook::instance().setup() from your global application setup().

You must call SmsWebhook::instance().loop() from your global application loop(). Failure to do so will cause no messages to ever be sent.

To queue a message to be sent, use SmsWebhook::instance().queueSms(mesg). If the device is connected to the Particle cloud, it will go out almost immediately. If an error occurs, it will retry later.

Constructor & Destructor Documentation

◆ SmsWebhook()

SmsWebhook::SmsWebhook ( )
protected

Constructor (protected)

You never construct one of these - use the singleton instance using SmsWebhook::instance().

◆ ~SmsWebhook()

SmsWebhook::~SmsWebhook ( )
protectedvirtual

Destructor - never used.

The singleton cannot be deleted.

Member Function Documentation

◆ getEventName()

const char* SmsWebhook::getEventName ( ) const
inline

Get the previously set event name (or the default, if it hasn't been set yet)

Returns
A c-string (null terminated)

◆ getPublishRateLimitMs()

unsigned long SmsWebhook::getPublishRateLimitMs ( ) const
inline

Get the previously set publish rate limit value (or the default, if it hasn't been set yet)

Returns
An unsigned long value of milliseconds.

◆ getRetryNoRecipientMs()

unsigned long SmsWebhook::getRetryNoRecipientMs ( ) const
inline

Get the previously set retry when no recipient is specified value (or the default, if it hasn't been set yet)

Returns
An unsigned long value of milliseconds.

◆ getRetryPublishFailMs()

unsigned long SmsWebhook::getRetryPublishFailMs ( ) const
inline

Get the previously set retry when publish fails value (or the default, if it hasn't been set yet)

Returns
An unsigned long value of milliseconds.

◆ loop()

void SmsWebhook::loop ( )

You must call loop() from global application loop()!

It's normally done like:

Failure to call loop will prevent the library from sending messages.

◆ queueSms()

void SmsWebhook::queueSms ( SmsMessage  smsMessage)

Queue a SmsMessage to send.

Parameters
smsMessageInformation about the message to be sent, typically containing a recipient phone number and the message content.

The smsMessage object is copied by this call. It's safe to make this call from other threads. It cannot be made at ISR time as it does memory allocation.

◆ setup()

void SmsWebhook::setup ( )

You must call setup() from global application setup()!

It's normally done like:

Failure to call setup will prevent the library from doing anything.

◆ withEventName()

SmsWebhook& SmsWebhook::withEventName ( const char *  eventName)
inline

Sets the event name to use. This must match the webhook. Default is "SendSmsEvent".

Parameters
eventNamethe event name to use

Event naming rules: 1 - 64 ASCII characters; only use letters, numbers, underscores, dashes and slashes. Spaces and special characters should not be used). Also note that the event name is a prefix, so any event name beginning with that string will trigger the webhook.

◆ withPublishRateLimitMs()

SmsWebhook& SmsWebhook::withPublishRateLimitMs ( unsigned long  milliseconds)
inline

Sets publish rate limit. If multiple publishes are required, wait this long between publishes. Default is 1010 milliseconds.

Parameters
millisecondsNew value in milliseconds

In addition to the Particle publish rate limit, you could also hit a rate limit at Twilio if you need to send a lot of SMS messages.

◆ withRecipientCallback()

SmsWebhook& SmsWebhook::withRecipientCallback ( std::function< bool(String &phone)>  recipientCallback)
inline

Sets a function to call to get the recipient if the SmsMessage recipient field is black.

Parameters
recipientCallbackthe function to call to find the recipient
Returns
*this, so you can chain this function fluent-style.

The recipientCallback has this prototype:

bool recipientCallback(String &phone);

The callback returns true if the recipient is know, or false if not. The if false is returned, then an attempt will be made again after the timeout.

The phone number must begin with "+" and the country code, so, for example in the US: +15558675310 .

◆ withRetryNoRecipientMs()

SmsWebhook& SmsWebhook::withRetryNoRecipientMs ( unsigned long  milliseconds)
inline

Sets the retry time when no recipient is specified in milliseconds. Default is 15 seconds.

Parameters
millisecondsNew value in milliseconds

If the recipient is not specified in the SmsMessage object, then the recipient callback is used. If the recipient callback returns false (not known yet), the message is left in the queue. This is common if the recipient SMS is sent from the cloud (Particle function, device notes, or Google sheets). This parameter determines how long to wait before checking again.

◆ withRetryPublishFailMs()

SmsWebhook& SmsWebhook::withRetryPublishFailMs ( unsigned long  milliseconds)
inline

Sets the retry time if the publish fails. Default is 15 seconds.

Parameters
millisecondsNew value in milliseconds

If Particle.publish() fails, this is how long to wait before retrying. Note that if the webhook fails this is not consulted, but does take care of the situation where you have poor connectivity and the publish is not able to be sent.

Field Documentation

◆ _instance

SmsWebhook * SmsWebhook::_instance
staticprotected

Singleton instance of this class.

Use instance() to obtain this, dereferenced.

◆ JSON_BUF_SIZE

const size_t SmsWebhook::JSON_BUF_SIZE = 256
staticprotected

Sets the temporary buffer for the JSON data for the publish.

This is allocated on the stack, but is only done from loop() which as a 6K stack. It doesn't make sense for this to be larger than the maximum publish size (622 bytes). Currently the only thing in it is the message and recipient, which probably fit in 160-ish bytes but 256 leaves a little extra room just in case.

◆ publishFuture

particle::Future<bool> SmsWebhook::publishFuture
protected

Future used to monitor the state of Particle.publish().

The publish call uses a future so it does not block loop until completion.

◆ retryTimeMs

unsigned long SmsWebhook::retryTimeMs = 0
protected

How long to wait for retry in stateWaitRetry.

This is compared to stateTime in a way that works across millis() rollover every 49 days.

◆ sendQueue

std::deque<SmsMessage> SmsWebhook::sendQueue
protected

Queue of SmsMessage objects to send.

Objects are enqueued using queueSms() and removed from the state handlers called from loop().

◆ stateHandler

std::function<void(SmsWebhook &)> SmsWebhook::stateHandler = 0
protected

State handler for the main state machine.

The state handler are class method with the prototype:

void stateHandlerMethod();


The documentation for this class was generated from the following files:
SmsWebhook::loop
void loop()
You must call loop() from global application loop()!
Definition: SmsWebhookRK.cpp:21
SmsWebhook::setup
void setup()
You must call setup() from global application setup()!
Definition: SmsWebhookRK.cpp:16
SmsWebhook::instance
static SmsWebhook & instance()
Get the singleton instance of this class.
Definition: SmsWebhookRK.cpp:9