-
Notifications
You must be signed in to change notification settings - Fork 2
/
simulate.js
83 lines (66 loc) · 1.55 KB
/
simulate.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
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
var request = require("request");
var remote = "https://agent.electricimp.com/" + process.env.IMP_AGENT;
var payloads = [
// SYSTEM_RESET (command: 208)
[255],
// PIN_MODE (pin: 1, mode: output (1))
[244, 2, 1],
// DIGITAL_WRITE (pin: 1, value: 1)
[144, 2, 1, 0],
// PIN_MODE (pin: 1, mode: pwm (3))
[244, 2, 3],
// ANALOG_WRITE (pin: 1, value: 50)
[224, 2, 10, 0],
// PIN_MODE (pin: 9, mode: input)
[244, 9, 0],
// PIN_MODE (pin: 8, mode: analog)
[244, 8, 2],
// NOTHING
[1024],
];
var reports = [
// REPORT_DIGITAL (pin: 9)
[208, 9],
// // REPORT_ANALOG (pin: 9)
[192, 8],
];
var sent = {
read: [],
write: [],
};
function read() {
if (!reports.length) {
return;
}
var report = reports.shift();
var url = remote + "?report=" + report;
sent.read.push(url);
request(url, function(err, response, body) {
console.log("-- Reporting State: ");
// console.log(" SENT: ", sent.read.indexOf(url));
console.log(" REQUEST: ", url);
console.log(" RESPONSE: ", body);
read();
});
}
read();
function write() {
if (!payloads.length) {
return;
}
var payload = payloads.shift();
var url = remote + "?payload=" + payload;
sent.write.push(url);
request(url, function(err, response, body) {
console.log("-- Reporting State: ");
// console.log(" SENT: ", sent.write.indexOf(url));
console.log(" REQUEST: ", url);
console.log(" RESPONSE: ", body);
write();
});
}
write();
// system reset
setTimeout(function() {
request(remote + "?payload=255");
}, 3000);