/*
** Analysis Example
** Sending downlink using dashboard
**
** Using an Input Widget in the dashboard, you will be able to trigger a downlink to
** any LoraWaN network server.
** IMPORTANT: Your device is required to send an Uplink before any downlink can be send.
**
** Environment Variables
** In order to use this analysis, you must setup the Environment Variable table.
**
** default_PORT: The default port to be used if not sent by the dashboard.
** device_id: The default device id to be used if not sent by the dashboard (OPTIONAL).
** payload: The default payload to be used if not sent by the dashboard (OPTIONAL).
**
** How to use:
** To analysis works, you need to add a new policy in your account. Steps to add a new policy:
** 2 - In the Target selector, with the field set as "ID", choose your Analysis in the list;
** 3 - Click the "Click to add a new permission" element and select "Device" with the rule "Access" with the field as "Any";
** 4 - To save your new Policy, click the save button in the bottom right corner;
*/
const { Analysis, Utils, Resources } = require("@tago-io/sdk");
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("Downlink analysis started");
// Get the environment variables.
const environment = Utils.envToJson(context.environment);
// Get the variables form_payload and form_port sent by the widget/dashboard.
let payload = scope.find((x) => x.variable === "form_payload");
let port = scope.find((x) => x.variable === "form_port");
// Setup from environment variable if widget hadn't been used to trigger the analysis.
if (!payload) {
payload = { value: environment.payload, device: environment.device_id };
}
if (!port) {
port = { value: environment.default_PORT };
}
// Error to make sure analysis have the information it needs.
if (!payload.value || !payload.device) {
return context.log('Missing "form_payload" in the data scope.');
} else if (!port || !port.value) {
return context.log('Missing "form_port" in the data scope o.');
}
// All variables that trigger the analysis have the "device" parameter, with the TagoIO Device ID.
// Otherwise it will get from the environment variable.
const device_id = payload.device;
if (!device_id) {
return context.log("Device key <device> not found in the variables sent by the widget/dashboard.");
}
const resources = new Resources(context.token);
const result = await Utils.sendDownlink(resources, device_id, {
payload: payload.value,
port: Number(port.value),
confirmed: false,
}).catch((error) => error);
console.log(result);
}
module.exports = new Analysis(init);
I have added an Access policy where all rules including "Access" & "Token access" selected with the field as "Any" but still get authorization denied.
My current workaround is to use the Account_token when declaring the new Resources:
const resources = new Resources({ token: environment.account_token });
I thought the new SDK approach was to use the Access policy instead. Any advice, please.
(If Account_token is no longer used) as per the snippet example comment above.
Thanks