Bucket variables - adding variables, updating values,recalling values and deleting variables

Bucket variables - adding variables, updating values,recalling values and deleting variables

@Steven Flaxman

Hi Everyone

Please could you help me with my analysis script. Everything seems to work fine until script tries to read the variables from the bucket. I realize that it needs to put the contents into an array then get the .last_item but this has had me going around in circles.

This analysis is for a livestock tracker and will send a notification to the devices user when there is higher than usual activity. This will indicate that the livestock being tracked is either being hunted or herded.

In order to do this i added 5 variables to the bucket using the device emulator. Two of the variables are added to the bucket using a Input Form on the TagoIO Run Dashboard.

The analysis script gets the variable values from the bucket.
Increments current event.
If its the first entry to the analysis it sets saved time to Date.now
Sets the current period to saved time - Date.now
If the current event = event and current period = period then send notification
Zero current event
Zero saved time
Zero current period
Update bucket

/* ver 1/13/2021
** Analysis to send a notification to the devices user when there is higher
** than usual activity. This will indicate that the livestock being tracked is
** either being hunted or herded.
**
** Environment Variables
** In order to use this analysis, you must setup the Environment Variable table.
** account_token: Your account token. Check bellow how to get this.
**
** Also add a device token to the environment variables,
** Check bellow how to get this.

** Steps to generate an account_token:
** 1 - Enter the following link: Admin
** 2 - Select your Profile.
** 3 - Enter Tokens tab.
** 4 - Generate a new Token with Expires Never.
** 5 - Press the Copy Button and place at the Environment Variables tab of this analysis.
**
** Steps to generate a device_token:

  • 1 - Go to your device, then token and copy your token.
  • 2 - Go to the analysis, then environment variables,
  • 3 - Type device_token on key, and paste your token on value
    */
    const {Analysis, Utils, Services, Account, Device, Types} = require("/sdk");
    //import getDevice from “./lib/getDevice”;
    //import { parseTagoObject } from “./lib/data.logic”;

async function predatorWarning() {
// Send the notifications and output the results to the analysis console.
//To enable email notification add email_tag in enviroment variables.
if (environment_variables.email_tag) {
context.log(‘about to send email’);
await email_service.send({
to: email,
subject: ‘Notification alert’,
message: Unusual activity warning for the device: ${device_name}. Variable: ${scope[0].variable}, Value: ${scope[0].value} ,
}),
context.log('email sent!!! ');

} else {
context.log(‘Emailing not enabled for this device.’);
}

if (phone_tag) {
await sms_service.send({
to: phone_tag.value,
message: Unusual activity warning for the device: ${device_name}. Variable: ${scope[0].variable}, Value: ${scope[0].value} ,
}).then(context.log).catch(context.log);
} else {
context.log(‘Phone number not found for this device.’);
}

if (userID_tag) {
await account.run.notificationCreate(userID_tag.value, {
title: ‘Notification Alert’,
message: Unusual activity warning for the device: ${device_name}. Variable: ${scope[0].variable}, Value: ${scope[0].value} ,
}).then(context.log).catch(context.log);
} else {
context.log(‘User ID not found for this device.’);
}
}

async function predator(context, scope) {
if (!scope[0]) return context.log(‘This analysis must be triggered by an action.’);

//context.log(JSON.stringify(scope));
context.log(“Running Analysis”);

//must have the environment variables set at analysis → environment variables
const environment = Utils.envToJson(context.environment);
if (!environment.account_token)
return context.log(‘Missing “account_token” environment variable’);
else if (environment.account_token.length !== 36)
return context.log(‘Invalid “account_token” in the environment variable’);
context.log(“Enviroment Set”);

//instancing user device and account
const account = new Account({ token: environment.account_token });
context.log(“Instance New Account”);
context.log('Token: ', environment.account_token);
const config_dev = new Device({ token: environment.device_token });
context.log("Instance New Device ");
context.log('Token: ', environment.device_token);

const customer_dev = await getDevice(account, scope[0].origin);
//choose the variable name to analysis here
const a_event = await customer_dev.getData({ qty: 9999, variable: “event” }); //This value can be set in a Input Form on the TagoIO Run Dashboard
context.log('a_event: ', a_event.value);
const a_period = await customer_dev.getData({ qty: 9999, variable: “period” }); //This value can be set in a Input Form on the TagoIO Run Dashboard
context.log('a_period : ', a_period.value);
const a_current_event = await customer_dev.getData({ qty: 9999, variable: “current_event” });
context.log('a_current_event : ', a_current_event.value);
const a_current_period = await customer_dev.getData({ qty: 9999, variable: “current_period” });
context.log('a_current_period: ', a_current_period.value);
const a_saved_time = await customer_dev.getData({ qty: 9999, variable: “saved_time” });
context.log('a_saved_time: ', a_saved_time.value);

//data to be saved in the bucket
const data_to_bucket = {
current_event: 0,
saved_time : 0000000000000,
current_period : 0,

};

//get the last item of object a_current_event and increment the values
a_current_event.last_item((x) => (data_to_bucket.current_event = data_to_bucket.current_event + 1));
a_current_event.value = x.value;

if (data_to_bucket.current_event = 1) {
//get the last item of object a_saved_time and set to current time
a_saved_time.last_item((y) => (data_to_bucket.saved_time = Date.now() ));
}
a_saved_time.value = y.value;

//get the last item of object a_current_period and recalculate period
a_current_period.last_item((z) => (data_to_bucket.current_period = (data_to_bucket.current_period + (Date.now() - data_to_bucket.saved_time) )));
a_current_period.value = z;

//if user defined number of events occur within user define period initiate predator warning notification
if ((a_current_event.value = a_event.value) && (a_current_period.value = period.value)) {
//call predatorWarning
await predatorWarning(contest,scope);

//reset the variables and parse to tagoIO structure to be saved on bucket
 data_to_bucket.current_event = 0;
 data_to_bucket.saved_time = 0000000000000;
 data_to_bucket.current_period = 0;
await config_dev.sendData(parseTagoObject(data_to_bucket)); 

} else if ((a_current_event.value < a_event.value) && (a_current_period.value < period.value)) {
//parsing updated data to tagoIO structure to be saved on bucket for use in the next event
await config_dev.sendData(parseTagoObject(data_to_bucket));

} else if (a_current_period.value > period.value) {
//reset the variables and parse to tagoIO structure to be saved on bucket
data_to_bucket.current_event = 0;
data_to_bucket.saved_time = 0000000000000;
data_to_bucket.current_period = 0;
await config_dev.sendData(parseTagoObject(data_to_bucket));
}

//console logging
console.log(“End of script”);
}

module.exports = new Analysis(predator);

// Insert your token here
// To run analysis on your machine (external)
//export default new Analysis(predator, { token: “*****************” });

Thanks for help
Steven