You can access the global variable normalizedPayload which contains the following metadata:
{"deveui": string,// The device's DevEUI"port": number,// The message's port"counter": number,"frequency": number,// GHz"data_rate": string,// Data rate according to https://blog.dbrgn.ch/2017/6/23/lorawan-data-rates/"coding_rate": string,"gateways": ["id": string,"datetime": date,"channel": number,"rssi": number,"snr": number ]}
This data is extracted from the LoRaWAN Network Server as being forwarded to Datacake over the Webhook. This information is normalized, meaning no matter whether you use TTN, TTI, Loriot, ChirpStack, or Wanesy, the information is always equally available and always formatted in the same way.
Raw Metadata
If you rather want to access the raw payload received from the LNS, you can do so via the global rawPayload variable.
Please note that when you access the rawPayload, the information is not normalized and the content of rawPayload looks different between different LoRaWAN Network Server!
Example Usage
In the following snippet, you find an example of how to read the Metadata from your Payload and how to bring that into your Datacake Payload Decoder section.
functionDecoder(payload, port) {// Output normalized Payloadconsole.log(JSON.stringify(normalizedPayload,0,4));// Output raw payload coming from webhook of your LNSconsole.log(JSON.stringify(rawPayload,0,4)); // Extract RSSI and Data-Rate from normalized Payload:// As being "normalized" this works between all LNS:// Including safety check so that these values really existvarLORA_RSSI= (!!normalizedPayload.gateways &&!!normalizedPayload.gateways[0] &&normalizedPayload.gateways[0].rssi) ||0;varLORA_SNR= (!!normalizedPayload.gateways &&!!normalizedPayload.gateways[0] &&normalizedPayload.gateways[0].snr) ||0;varLORA_DATARATE=normalizedPayload.data_rate; // extract timestamp from gatewayvar ts =rawPayload.recvTime; // Kerlink Wanesy// use normalized payload - NOTE: must have at least one Gateway!var ts =normalizedPayload.gateways[0]["datetime"];// Build up an array that can be used to forward it onto Datacake:var decoded = [ { field:"LORA_RSSI", value:LORA_RSSI, timestamp: ts }, { field:"LORA_DATARATE", value:LORA_DATARATE, timestamp: ts } ];return decoded;}
In order to store those Metadata-Fields, you can create new Fields on your Device and have historical Data for each field that you forward and store. Here is how this looks for the above example:
Extract Location
Some LNS (depending also on settings) embed gateway information in uplinks and send it with the webhook forward to Datacake.
You can use the rawPayload variable to access it and forward it onto a location field on your device.
Example
The Things Network / The Things Stack
Because we are using the rawPayload Object, the following example only works with The Things Stack LNS, aka The Things Network
functionDecoder(bytes, port) {var datacakeFields = [];try {// Please note that there can be more than just one Gateway// in the webhook data. This examples always uses the first.var lat =rawPayload.uplink_message.rx_metadata[0].location.latitudevar lon =rawPayload.uplink_message.rx_metadata[0].location.longitude// in order to be able to forward location data to Datacake// We need to build a tuple that contains latitude + longitude// surrounded by parenthesisvar location ="("+ lat +","+ lon +")";// now forward the data to datacake// please make sure to create a location field called GATEWAY_LOCATIONdatacakeFields.push({ field:"GATEWAY_LOCATION", value: location }); } catch (e) {console.log("Error fetching location. Probably gateway data missing."); }return datacakeFields;}