Payload Parser Follow
Every connected device has a payload containing the data your application has been built to collect. In the tracking application, the most important data is the latitude and the longitude.
The payload parser translates the raw payload sent by the device into relevant data points – for example, transforming a binary payload sent by a device into Latitude and Longitude
File Structure
Payload parsers are located inside the parsers folder. We provide the codebase for device types we support. But at times, you may want to provide your own parser for your own device.
|-- src
|------ parsers
|---------- index.js
Code Structure
The parsers we provide are under the iotninja folder, inside the zip (.tgz) file
|-- iotninja
|------ iotninja-react-webui-1.0.0-beta.tgz
import {
decode as decodeOyster,
} from '@iotninja/react-webui/dist/parsers';
A local function named decode is created to provide decoding service. The service accepts payload as string. This contains the data for your application. It also has decoderType which is used to discriminate which payload parser to use. An optional port parameter is also available for use in LoRaWAN payload.
In code definition below, notice that when you want to decode the binary payload of a digitalmatter lorawan payload, you need to call this function with decoderType = 'dm-oyster'. If you don't provide a decoderType, it will return a static location.
const decode = (payload, decoderType = 'default', port = 1) => {
switch(decoderType){
case 'dm-oyster':
console.log('Decoder: Digital Matter Oyster...');
const bytes = hexToBytes(payload);\
return decodeOyster(bytes, +port);
case 'default':
default:
console.log('No Decoder: Go to the sea...');
return {
lat: "-35.0000001",
lng: "170.0000001",
speed: 0,
heading: 0,
}
}
}
So if you want to provide your own decoder, you just add another entry on the switch statement, and call the decoder function you write.
![]() |
For code readability, always create a function for your decoder |
The decode function is then made available to shuriken application using the export function.
Comments
0 comments
Please sign in to leave a comment.