-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.js
63 lines (49 loc) · 1.29 KB
/
main.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
const Eos = require('eosjs');
const Gpio = require('onoff').Gpio;
const conf = require('./config.js');
const device = new Gpio(conf.pins.device, 'in', 'both');
const led1 = new Gpio(conf.pins.led1, 'out');
const led2 = new Gpio(conf.pins.led2, 'out');
let eos = Eos({
keyProvider: conf.eos.privateKey,
chainId: conf.eos.chainId,
httpEndpoint: conf.eos.httpEndpoint,
});
async function sendTrx() {
try {
let contract = await eos.contract(conf.eos.contract);
let data = 10; //TODO normal
await contract.devicesignal(conf.eos.accountName, data, {authorization: conf.eos.accountName});
tickLed(led2);
} catch (e) {
console.log("EOS ERROR:", e);
}
}
/*
function sendTrx() {
setTimeout(() => {
tickLed(led2);
}, 500);
}
*/
function tickLed(led) {
led.writeSync(led.readSync() ^ 1);
setTimeout(() => {
led.writeSync(led.readSync() ^ 1);
}, 70);
}
let last = null;
device.watch(async function (err, value) {
if (err)
return console.log("Watch ERROR:", err);
if (last && new Date().getTime() - last < 100)
return;
last = new Date().getTime();
tickLed(led1);
sendTrx();
});
process.on('SIGINT', function () {
led1.unexport();
led2.unexport();
device.unexport();
});