|
LocationFusionRK
|
#include <LocationFusionRK.h>
Data Structures | |
| struct | CmdHandler |
| This class is used internally for registerCommand. More... | |
Public Types | |
| enum class | PublishFrequency { manual , once , periodic } |
| How often to publish location. More... | |
| enum class | Status : int { idle = 0 , publishing = 1 , publishSuccess = 2 , publishFail = 3 , locEnhancedWait = 4 , locEnhancedSuccess = 5 , locEnhancedFail = 6 } |
| Current status of this library. More... | |
Public Member Functions | |
| void | setup () |
| Perform setup operations; call this from global application setup() | |
| LocationFusionRK & | withPublishManual () |
| Set the publish frequency to manual. Call requestPublish() to do so. This is the default. | |
| LocationFusionRK & | withPublishOnce () |
| Set the publish frequency to publish once after connecting to the cloud. | |
| LocationFusionRK & | withPublishPeriodic (std::chrono::milliseconds ms) |
| Set the publish frequency to publish periodically when cloud connected. | |
| PublishFrequency | getPublishFrequency () const |
| Get the current publish frequency. Default is manual. | |
| LocationFusionRK & | withAddWiFi (bool enable=true) |
| Add Wi-Fi access points nearby to the loc event. Default is false. | |
| LocationFusionRK & | withAddTower (bool enable=true) |
| Add serving cellular tower information to the loc event. Default is false. | |
| LocationFusionRK & | withAddToEventHandler (std::function< void(Variant &eventData, Variant &locVariant)> handler) |
| Add an "add to event" handler. | |
| LocationFusionRK & | withCmdHandler (const char *cmd, std::function< void(const Variant &data)> handler) |
| Adds a handler when the Particle function "cmd" is received. | |
| LocationFusionRK & | withEnableCmdFunction (bool enable) |
| Enables or disables the "cmd" function handler. Default is enabled. | |
| LocationFusionRK & | withLocEnhancedHandler (std::function< void(const Variant &data)> handler) |
| Adds a handler when loc-enhanced data is calculated by the cloud. | |
| LocationFusionRK & | withStatusHandler (const char *cmd, std::function< void(Status)> handler) |
| Adds a handler when the status has changed. Added in version 0.0.3. | |
| LocationFusionRK & | withThreadStackSize (size_t size) |
| Sets the worker thread stack size. Must be set before setup() | |
| void | requestPublish () |
| Request a publish now. | |
| Status | getStatus () const |
| Get the current status of the library (idle, publishing, etc.) | |
| void | lock () |
| Locks the mutex that protects shared resources. | |
| bool | tryLock () |
| Attempts to lock the mutex that protects shared resources. | |
| void | unlock () |
| Unlocks the mutex that protects shared resources. | |
Static Public Member Functions | |
| static LocationFusionRK & | instance () |
| Gets the singleton instance of this class, allocating it if necessary. | |
Protected Member Functions | |
| LocationFusionRK () | |
| The constructor is protected because the class is a singleton. | |
| virtual | ~LocationFusionRK () |
| The destructor is protected because the class is a singleton and cannot be deleted. | |
| LocationFusionRK (const LocationFusionRK &)=delete | |
| LocationFusionRK & | operator= (const LocationFusionRK &)=delete |
| void | updateStatus (Status status) |
| Update the status and call the status handlers. Used internally. Added in 0.0.3. | |
| os_thread_return_t | threadFunction (void) |
| Worker thread function. | |
| void | stateIdle () |
| Internal state handler for idle and not connected to the cloud. | |
| void | stateConnected () |
| Internal state handler for connected to the cloud. | |
| void | stateBuildPublish () |
| Internal state handler for building a publish. | |
| void | statePublishWait () |
| Internal state handler for waiting for the publish to complete. | |
| void | stateLocEnhancedWait () |
| Internal state handler for waiting for the loc-enhanced to be received. | |
| int | functionHandler (const Variant &eventData) |
| Called from the Particle.function handler for "cmd". | |
| void | locEnhanced (const Variant &eventData) |
| Called when a "loc-enhanced" cmd function is received. | |
Static Protected Member Functions | |
| static int | functionHandlerStatic (String cmd) |
| Called from the Particle.function handler for "cmd". | |
| static void | locEnhancedStatic (const Variant &eventData) |
| Called when a "loc-enhanced" cmd function is received. | |
Protected Attributes | |
| os_mutex_t | mutex = 0 |
| Mutex to protect shared resources. | |
| Thread * | thread = 0 |
| Worker thread instance class. | |
| size_t | threadStackSize = 6144 |
| Size of the work thread stack. Must set before setup() | |
| PublishFrequency | publishFrequency = PublishFrequency::manual |
| How often to publish (manual, once, periodic) | |
| std::chrono::milliseconds | publishPeriod = 5min |
| If publishing periodic, how often to publish. | |
| std::chrono::milliseconds | publishFailureRetry = 1min |
| If publish fails, how long to wait before trying again. The location will be built again. | |
| std::chrono::milliseconds | locEnhancedTimeout = 1min |
| Amount of time to wait for loc-enhanced. | |
| bool | locEnhancedReceived = false |
| Used to handle loc-enhanced state changes. | |
| unsigned long | stateTime = 0 |
| Used for calculating the locEnhancedTimeout. | |
| std::function< void(LocationFusionRK &) | stateHandler ) = &LocationFusionRK::stateIdle |
| State handler. Run from the worker thread. | |
| bool | addWiFi = false |
| When building a location publish, add Wi-Fi access point information. | |
| bool | addTower = false |
| When building a location publish, add serving tower information. | |
| std::vector< std::function< void(Variant &eventData, Variant &locVariant)> | addToEventHandlers ) |
| Vector of handlers to add more information to the location event. | |
| bool | enableCmdFunction = true |
| Add a function handler for "cmd". | |
| std::vector< CmdHandler > | commandHandlers |
| Handler functions to call when a cmd Particle.function is received. | |
| std::vector< std::function< void(const Variant &eventData)> | locEnhancedHandlers ) |
| Handler functions to call when loc-enhanced is received on-device. | |
| bool | manualPublishRequested = false |
| true when a manual publish has been requested | |
| int | publishCount = 0 |
| Number of successful publishes. THis is used to handle once mode. | |
| CloudEvent | event |
| Event being built or sent. | |
| Variant | eventData |
| The data payload being build for the loc event. | |
| uint64_t | nextPublishMs = 0 |
| When to publish next in periodic mode. Compare to System.millis(). | |
| int | locRequestId = 1 |
| loc events contain a request ID, this is the next one to use | |
| Status | status = Status::idle |
| Current status pof the library. | |
| std::vector< std::function< void(Status)> > | statusHandlers |
| Handlers to call when the status changes. | |
Static Protected Attributes | |
| static LocationFusionRK * | _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: LocationFusionRK::instance().setup();
|
strong |
|
strong |
Current status of this library.
Note that the publishSuccess and publishFailed status codes are only useful when using the handler callback. Almost immediately after that notification, the status goes to idle, so it would be hard to catch this status by polling.
The locEnhancedSuccess and locEnhancedFail also require the callback to be useful. Note the locEnhanced status events are only used if you have enabled a loc-enhanced callback to receive the events on-device. If you do not do this, the loc-enhanced will only exist in the cloud and not be sent back to the device.
If you are implementing sleep control, you can go go sleep if the status is idle.
|
protected |
The constructor is protected because the class is a singleton.
Use LocationFusionRK::instance() to instantiate the singleton.
|
protecteddelete |
This class is a singleton and cannot be copied
|
protected |
Called from the Particle.function handler for "cmd".
| eventData |
Calls commandHandlers, added with withCmdHandler.
|
staticprotected |
Called from the Particle.function handler for "cmd".
| cmd |
Parses cmd as JSON and the calls the non-static functionHandler.
|
inline |
Get the current publish frequency. Default is manual.
|
inline |
Get the current status of the library (idle, publishing, etc.)
This method is used for polling. The be called when the status changes, see withStatusHandler.
|
static |
Gets the singleton instance of this class, allocating it if necessary.
Use LocationFusionRK::instance() to instantiate the singleton.
|
protected |
Called when a "loc-enhanced" cmd function is received.
| eventData |
Calls locEnhancedHandlers.
|
staticprotected |
Called when a "loc-enhanced" cmd function is received.
| eventData |
This just calls the non-static method with the singleton instance.
|
inline |
Locks the mutex that protects shared resources.
This is compatible with WITH_LOCK(*this).
The mutex is not recursive so do not lock it within a locked section.
|
protecteddelete |
This class is a singleton and cannot be copied
|
inline |
Request a publish now.
Works in all modes (manual, once, and periodic). Can be called when offline; it will only be calculated when connected to the cloud (breathing cyan).
| void LocationFusionRK::setup | ( | ) |
Perform setup operations; call this from global application setup()
You typically use LocationFusionRK::instance().setup();
Typically you use all of the withXXX() functions before setup.
|
protected |
Internal state handler for building a publish.
May be in this state for a while, but the state handlers are run from a worker thread so it won't affect operation of the rest of the system typically.
Exit conditions:
|
protected |
Internal state handler for connected to the cloud.
Exit conditions:
|
protected |
Internal state handler for idle and not connected to the cloud.
Exit conditions:
|
protected |
Internal state handler for waiting for the loc-enhanced to be received.
Exit conditions:
|
protected |
Internal state handler for waiting for the publish to complete.
Exit conditions:
May set
|
protected |
Worker thread function.
This method is called to perform operations in the worker thread.
You generally will not return from this method.
|
inline |
Attempts to lock the mutex that protects shared resources.
|
protected |
Update the status and call the status handlers. Used internally. Added in 0.0.3.
| status |
This only calls the status handlers if the status changes.
|
inline |
Add an "add to event" handler.
| handler |
The "add to event" handler allows you to add data to the loc event. You might do this if you want to support a cellular modem that has built-in GNSS, or use an external GNSS receiver.
The handler can be a C function or C++11 lambda and has the following prototype:
void handler(Variant &eventData, Variant &locVariant)
|
inline |
Add serving cellular tower information to the loc event. Default is false.
| enable |
This can be called on devices without cellular (P2, for example) and it will be ignored.
|
inline |
Add Wi-Fi access points nearby to the loc event. Default is false.
| enable |
This can be called on devices without Wi-Fi (B-SoM, for example) and it will be ignored.
| LocationFusionRK & LocationFusionRK::withCmdHandler | ( | const char * | cmd, |
| std::function< void(const Variant &data)> | handler ) |
Adds a handler when the Particle function "cmd" is received.
| cmd | |
| handler |
Your handler is called when the "cmd" field within the function JSON matches.
The handler can be a C function or C++11 lambda. The prototype is:
|
inline |
Enables or disables the "cmd" function handler. Default is enabled.
| enable |
Must be called before setup()!
If disabled, withCmdHandler() and withLocEnhancedHandler() will not function because they require the cmd handler.
If you need to additional custom cmd handlers instead of disabling support, use withCmdHandler() to hook your code into this library instead of the other direction.
|
inline |
Adds a handler when loc-enhanced data is calculated by the cloud.
| handler |
The handler prototype is:
void handler(const Variant &data);
Fields typically in data in the handler:
If you do not add a handler, the loc-enhanced data is not sent to the device. Handling loc-enhanced data locally on device adds one data operation.
|
inline |
Set the publish frequency to manual. Call requestPublish() to do so. This is the default.
|
inline |
Set the publish frequency to publish once after connecting to the cloud.
|
inline |
Set the publish frequency to publish periodically when cloud connected.
| ms |
Even though the parameter is in milliseconds, you probably shouldn't publish more often than every few minutes.
If you are using location fusion on a non-Tracker device it costs 50 data operations per fusion request, so doing location fusion frequently on the free plan may cause your account to be paused due to running out of data operations.
|
inline |
Adds a handler when the status has changed. Added in version 0.0.3.
| cmd | |
| handler |
Your handler is called when the "cmd" field within the function JSON matches.
The handler can be a C function or C++11 lambda. The prototype is:
void handler(Status status)
This method uses a callback to be notified when the status changes. It is called from the library's worker thread so you should not do anything that will block in your callback.
|
inline |
Sets the worker thread stack size. Must be set before setup()
| size | in bytes. The default if you do not call this is 6144 |
Prior to version 0.0.4 the stack was 3072 bytes, but it was increased due to SOS+13 on b5som devices. You can further customize the size by calling this method prior to setup().
|
staticprotected |
Singleton instance of this class.
The object pointer to this class is stored here. It's NULL at system boot.
|
protected |
Vector of handlers to add more information to the location event.
Add using withAddToEventHandler(). You can add multiple handlers.
|
protected |
When building a location publish, add serving tower information.
Default is false, set uskng withAddTower().
|
protected |
When building a location publish, add Wi-Fi access point information.
Default is false, set uskng withAddWiFi().
|
protected |
Add a function handler for "cmd".
This is enabled by default and must be enabled for loc-enhanced on device to work.
|
protected |
true when a manual publish has been requested
This can be requested when offline as it will be handled when online. This can also be used in once and periodic modes to publish npw, out of schedule.
|
protected |
|
protected |
When to publish next in periodic mode. Compare to System.millis().
Since this is a uint64_t it does not roll over so comparisons are easier.
|
protected |