-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
52 lines (47 loc) · 1.3 KB
/
test.js
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
/* Test script
** - Does not required a tethered Arduino
** - Generates a random number for each endpoint for each request
** - Only tests three endpoints: A0, A1, D2
*/
// import dependencies
require("dotenv").config();
const fetch = require("node-fetch");
// get interval
const interval = +process.env.INTERVAL; // time interval for API requests
// random number generator
function getRandomNumberAnalog() {
const min = Math.ceil(0);
const max = Math.floor(1023);
return Math.floor(Math.random() * (max - min) + min);
}
function getRandomNumberDigital() {
return Math.floor(Math.random() * 2);
}
// convert data to API requests at regular intervals
var sendData = setInterval(function () {
const allPins = [
{
id: "A0",
value: getRandomNumberAnalog(),
},
{
id: "A1",
value: getRandomNumberAnalog(),
},
{
id: "D2",
value: getRandomNumberDigital(),
},
];
for (const pin of allPins) {
console.log(`${process.env.API_HOST}/pins/${pin.id}`);
fetch(`${process.env.API_HOST}/pins/${pin.id}`, {
method: "PUT",
body: JSON.stringify(pin),
headers: { "Content-Type": "application/json" },
})
.then((res) => res.json())
.then((json) => console.log(json))
.catch((err) => console.log(err));
}
}, interval);