Sometimes you don’t need to stream the individual messages from your device(s) but want to stream aggregated data generated by the calculators.
For example, you want to know when and where a vehicle left a specific zone (geofence). You will have a calculator created for this purpose that will look like this:
and
Then you will have your device assigned to this calculator. Now you are ready to consume intervals (once they are generated, of course).
Below are the possible ways to arrange this with flespi.
[Option 1] Subscribe to the proper MQTT topic
The most obvious way is to subscribe to the proper topic in the MQTT client. For example, with the case above, you want to subscribe to flespi/state/gw/calcs/{id}/devices/+/last to retrieve the last generated interval from the calculator per each device and also receive any further updates to them. The result will look like
Note that in the case with geofence ins/outs events you may want to use the flespi/state/gw/calcs/{id}/devices/+/activated,deactivated topic instead to get the specific moments in time when crossing the geofence border occurs.
Find more flespi topics in our comprehensive MQTT topics list.
[Option 2] Stream data from an MQTT channel
If it’s not convenient for you to consume intervals directly via MQTT, you can do it via flespi stream to your platform. Here’s what you should do.
1. Create an MQTT channel that will connect to the flespi MQTT broker. Specify the topic with the desired intervals.
2. Create a stream and configure it to forward data to your server.
3. Assign the MQTT channel to the stream:
4. Now as the new intervals get generated, they will be forwarded to the target platform via the stream.
[Option 3] Use webhooks to forward interval events to your lambda
Another alternative will be to create webhook, configure it to catch interval events and forward them to your HTTP lambda.
To handle all possible events that may happen with device intervals you may configure webhook with topic: flespi/interval/gw/calcs/{calc-id}/devices/+/+. Such topic will report any interval creation, update, deletion as well as any activation and deactivation device statuses.
We suggest to use next configuration for body parameter in webhook to deliver all important properties of interval events:
{"payload": %payload%, "event": "%topics[7]%", "device_id": %tonumber(topics[6])%, "reason": %tonumber(user_properties["reason_code"])%}
This configuration will add to the HTTP request body full interval JSON, device ID, event type that happened to the interval and its reason.
You can read more about configuring the webhook for catching interval events here.