var { Analysis, Account, Resources } = require("@tago-io/sdk");
var axios = require("axios").default;
async function startAnalysis(context, scope) {
try {
const account = new Account({
token: "123456789"
});
// Ensure that scope is not empty before accessing scope[0]
if (scope && scope.length > 0 && scope[0]) {
const resources = new Resources({account, token: context.token});
const device = resources.devices;
const device_id = device.info;
console.log("Device Token/ Device", device);
console.log("Device ID", device_id);
const payload = scope[0];
const dataToSend = [];
const payloadData = payload.d;
const timeStamp = payload.ts;
// Organizing data to be sent in the dataToSend object
for (const [variable, value] of Object.entries(payloadData)) {
dataToSend.push({
variable: variable,
value: Number(value),
time: timeStamp
});
}
if (dataToSend.length > 0) {
await device.sendData(dataToSend);
await sendTelemetryToApi(device_id, dataToSend);
}
} else {
console.error("Invalid or empty scope:", scope);
}
} catch (error) {
console.error("Error in analysis:", error);
}
}