Repository navigation
Expand file tree
/
Copy pathanalysis.js
More file actions
109 lines (91 loc) · 3.07 KB
/
Copy pathanalysis.js
File metadata and controls
109 lines (91 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
* Analysis Example
* Minimum, maximum, and average
*
* Get the minimum, maximum, and the average value of the variable temperature from your device,
* and save these values in new variables
*
* Instructions
* To run this analysis you need to add a device token to the environment variables,
* To do that, go to your device, then token and copy your token.
* Go the the analysis, then environment variables,
* type device_token on key, and paste your token on value
*/
const { Analysis, Device, Utils } = require("@tago-io/sdk");
// The function myAnalysis will run when you execute your analysis
async function startAnalysis(context) {
// reads the values from the environment and saves it in the variable env_vars
const env_vars = Utils.envToJson(context.environment);
if (!env_vars.device_token) {
return context.log("Device token not found on environment parameters");
}
const device = new Device({ token: env_vars.device_token });
// This is a filter to get the minimum value of the variable temperature in the last day
const minFilter = {
variable: "temperature",
query: "min",
start_date: "1 day",
};
// Now we use the filter for the device to get the data
// check if the variable min has any value
// if so, we crete a new object to send to TagoIO
const [min] = await device.getData(minFilter);
if (min) {
const minValue = {
variable: "temperature_minimum",
value: min.value,
unit: "F",
};
// Now we send the new object with the minimum value
await device
.sendData(minValue)
.then(context.log("Temperature Minimum Updated"));
} else {
context.log("Minimum value not found");
}
// This is a filter to get the maximum value of the variable temperature in the last day
const maxFilter = {
variable: "temperature",
query: "max",
start_date: "1 day",
};
const [max] = await device.getData(maxFilter);
if (max) {
const maxValue = {
variable: "temperature_maximum",
value: max.value,
unit: "F",
};
await device
.sendData(maxValue)
.then(context.log("Temperature Maximum Updated"));
} else {
context.log("Maximum value not found");
}
// This is a filter to get the last 1000 values of the variable temperature in the last day
const avgFilter = {
variable: "temperature",
qty: 1000,
start_date: "1 day",
};
const dataAvgArray = await device.getData(avgFilter);
if (dataAvgArray.length) {
let temperatureSum = dataAvgArray.reduce((previousValue, currentValue) => {
return previousValue + Number(currentValue.value);
}, 0);
temperatureSum = temperatureSum / dataAvgArray.length;
const avgValue = {
variable: "temperature_average",
value: temperatureSum,
unit: "F",
};
await device
.sendData(avgValue)
.then(context.log("Temperature Average Updated"));
} else {
context.log("No result found for the avg calculation");
}
}
Analysis.use(startAnalysis);
// To run analysis on your machine (external)
// Analysis.use(myAnalysis, { token: "YOUR-TOKEN" });