Skip to content

Commit

Permalink
Merge pull request #4 from tago-io/feat/newSdk/LCD-93
Browse files Browse the repository at this point in the history
updating tagoio sdk
  • Loading branch information
vitorfdl authored Sep 1, 2023
2 parents 4fe271b + b4341f3 commit 3b3cec1
Show file tree
Hide file tree
Showing 4 changed files with 322 additions and 273 deletions.
35 changes: 33 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,36 @@ You can just upload the `analysis.js` file.

or

You can run locally, you should have last node version and install all dependencies running `npm install` on your terminal in this project folder,
after that, you can run the project just calling analysis, `node analysis.js`;
Run it locally, you should have lastest node version and install all the dependencies by running `npm install` in your terminal in the project's directory,
after that, you can run the project by calling the analysis, `node analysis.js`;

## What the analysis does

This analysis must run by triggered by a Time Interval. It checks whether devices with given Tags checked in, in the past minutes. If not, it sends an email or sms alert.

## How to use this analysis internally at TagoIO servers

In order to use this analysis, you must to add a new policy in your account and setup the Environment Variable table.<br>

Environment Variables:

1 - checkin_time: Minutes between the last input of the device before sending the notification.

2 - tag_key: Device tag Key to filter the devices.

3 - tag_value: Device tag Value to filter the devices.

4 - email_list: Email list comma separated.

5 - sms_list: Phone number list comma separated. The phone number must include the country code.


Steps to add a new policy:

1 - Click the button "Add Policy" at this url: https://admin.tago.io/am;

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;<br>
23 changes: 9 additions & 14 deletions analysis.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,28 @@
** Environment Variables
** In order to use this analysis, you must setup the Environment Variable table.
**
** account_token: Your account token
** checkin_time: Minutes between the last input of the device before sending the notification.
** tag_key: Device tag Key to filter the devices.
** tag_value: Device tag Value to filter the devices.
** email_list: Email list comma separated.
** sms_list: Phone number list comma separated. The phone number must include the country code
**
** Steps to generate an account_token:
** 1 - Enter the following link: https://admin.tago.io/account/
** 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.
** How to use:
** To analysis works, you need to add a new policy in your account. Steps to add a new policy:
** 1 - Click the button "Add Policy" at this url: https://admin.tago.io/am;
** 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, Account, Services, Utils } = require("@tago-io/sdk");
const { Analysis, Services, Utils, Resources } = require("@tago-io/sdk");
const dayjs = require("dayjs");

async function myAnalysis(context) {
// Transform all Environment Variable to JSON.
const env = Utils.envToJson(context.environment);

if (!env.account_token) {
return context.log("You must setup an account_token in the Environment Variables.");
} else if (!env.checkin_time) {
if (!env.checkin_time) {
return context.log("You must setup a checkin_time in the Environment Variables.");
} else if (!env.tag_key) {
return context.log("You must setup a tag_key in the Environment Variables.");
Expand All @@ -45,12 +42,10 @@ async function myAnalysis(context) {
const checkin_time = Number(env.checkin_time);
if (Number.isNaN(checkin_time)) return context.log("The checkin_time must be a number.");

const account = new Account({ token: env.account_token });

// You can remove the comments on line 51 and 57 to use the Tag Filter.
//const filter = { tags: [{ key: env.tag_key, value: env.tag_value }] };

const devices = await account.devices.list({
const devices = await Resources.devices.list({
page: 1,
amount: 1000,
fields: ["id", "name", "last_input"],
Expand Down
Loading

0 comments on commit 3b3cec1

Please sign in to comment.