General Axios Code for Sending Downlink from an Analysis

General Axios Code for Sending Downlink from an Analysis

Team,

I don't write defensively, so error checking is weak... But this code took me some time to figure out. Hope it helps someone!

General Strategy:
1) Put parameters in DEVICE TO BE CONTROLLED - including the HEX commands.
2) Make a general device so you have a bucket.
3) Any widget or analysis that sends a value to the general bucket like this: device_id: value (like a text "turn_on" will work
4) Put in a trigger on the bucket where your trigger KEY is set to the device_id will work.
5) Script grabs trigger VALUE for the triggered device id.
6) script then gets the device parameters and completes the axios form.

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


async function init(context, scope) {
// Remove code below if you want to trigger by schedule action and using environment variables.
if (!scope[0]) {
return context.log("This analysis must be triggered by a widget.");
}

//context.log(" ****** Context *******", context);
//context.log(" ****** SCOPE ******", scope);

context.log("Downlink analysis started");
// Get the environment variables.
const env = Utils.envToJson(context.environment);
if (!env.account_token) {
return context.log('Missing "account_token" environment variable');
} else if (env.account_token.length !== 36) {
return context.log('Invalid "account_token" in the environment variable');
}


const account = new Account({ token: env.account_token });
const device_id = scope[0].variable;
const command_text = scope[0].value;
const sensor_info = account.devices.info(device_id);
const my_device_token = await Utils.getTokenByName( account, device_id );
const my_device_too = await Utils.getDevice( account , device_id );

const myDevice = new Device({ token: my_device_token });
const parameters = await myDevice.getParameters();

let i = 0;
for( i = 0 ; i<parameters.length ; i++) {
if( parameters[i].key == "downlink_key") {
var key = parameters[i].value;
}
if( parameters[i].key == "dl_url") {
var url = parameters[i].value;
}
if( parameters[i].key == "downlink_port") {
var port = parameters[i].value;
}
if( parameters[i].key == "downlink_commands") {
var commands = parameters[i].value;
}
}

json_commands = JSON.parse(commands);
const downlink_code = json_commands[command_text];

const frm_payload = Buffer.from( downlink_code , 'hex').toString('base64');

const headers = {
'Authorization': 'Bearer '+key,
'content-type': 'application/json'
}
const body =
{ "downlinks": [
{
"frm_payload": frm_payload,
"f_port": Number(port),
"priority": "HIGH",
"confirmed": true
}
]
}

const debug = 1;
if( debug === 1 ) {
context.log( " Body Command -----> " , body);
context.log( " Header Command -----> " , headers);
context.log( " URL Command -----> " , url);
}


const result = await axios.post( url , body , {headers})
.catch((error) => error);




//context.log("result ----> " + result[0].key + " <---------");
}

module.exports = new Analysis(init);