Storing HTTP Post Server response data

Hi,

I have created a short analysis script that does an HTTP post to our LoRa Cloud solver of a particular message that a LoRa tracker sent to Tago. I am able to successfully retrieve the response from the server. The response is an array of objects that I now need to parse. Based on the parsing results I might have to create another action which triggers a second analysis with a second HTTP post.

How do I store this server response (response.data) in a data bucket so I can action off it etc ?

Thanks

Patrick

const { Analysis } = require(’@tago-io/sdk’);
const axios = require(‘axios’);

async function startAnalysis(context,scope) {

if (!scope.length)
return context.log(‘Analysis must be triggered by an action’)
else
{
const das_token = context.environment.find(x => x.key === ‘das_token’).value;

// retreive the DAS Message
var lora_cloud_das_msg = scope.find(x => x.variable === 'das_msg').value;
//context.log("This is the DAS uplink Message: ",lora_cloud_das_msg);

//define the LoRaCloud URL
const lora_cloud = 'https://das.loracloud.com/api/v1/device/send';

const response = await axios.post(lora_cloud,lora_cloud_das_msg,{headers:{Authorization:das_token}});
context.log("this is the response data from the server: ", response.data);
context.log("server status response: ", response.status);
context.log("HTTP Status text from the server: ", response.statusText);

if (server_response.result.stream_records.length > 0)
  context.log("the ROSE stream_records are : ",JSON.stringify(server_response.result.stream_records));
else
  context.log("the ROSE stream_records are empty!");

}
}

Hi @pdvaneijk

to store it, you will need to use the environment variables present in the Analysis to define the token of the device, in the startAnalysis function you need to define this device and then send the data, it will be stored in the bucket of said device. Here is code:

const { Analysis, Device, Utils } = require("@tago-io/sdk");
const axios = require("axios");

async function startAnalysis(context,scope) {

  const env_vars = Utils.envToJson(context.environment);
  if (!scope.length) return context.log("Analysis must be triggered by an action");
  else {
    if (!env_vars.device_token) return context.log("Missing device_token environment variable");
  
    const device = new Device({ token: env_vars.device_token });
    const das_token = context.environment.find(x => x.key === "das_token").value;

    // retreive the DAS Message
    var lora_cloud_das_msg = scope.find(x => x.variable === 'das_msg').value;
    //context.log("This is the DAS uplink Message: ",lora_cloud_das_msg);

    //define the LoRaCloud URL
    const lora_cloud = 'https://das.loracloud.com/api/v1/device/send';

    const response = await axios.post(lora_cloud,lora_cloud_das_msg,{headers:{Authorization:das_token}});
    context.log("this is the response data from the server: ", response.data);
    context.log("server status response: ", response.status);
    context.log("HTTP Status text from the server: ", response.statusText);

    if (server_response.result.stream_records.length > 0)
      context.log("the ROSE stream_records are : ",JSON.stringify(server_response.result.stream_records));
    else
      context.log("the ROSE stream_records are empty!");

    try {
      await device.sendData(response.data);
      context.log("Successfully Stored");
    } catch (error) {
      context.log("Error when storing:", error);
    }
  }
}

maybe there will be a need for some changes, but sending the data should be fine. Hope it helps.

1 Like

Thank you! This did the trick

Patrick