-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (36 loc) · 1.07 KB
/
index.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
const awsIot = require('aws-iot-device-sdk');
const { Gpio } = require('onoff');
const device = awsIot.device({
keyPath: './certs/MagicLock.private.key',
certPath: './certs/MagicLock.cert.pem',
caPath: './certs/root-CA.crt',
clientId: 'MagicLockInitial',
host: 'aj5n8a300b2x9.iot.us-east-1.amazonaws.com',
keepalive: 120
});
console.log('comecei');
device.on('connect', () => {
console.log('conectei');
device.subscribe('magiclock/open');
console.log('me inscrevi');
device.publish('magiclock/thing', JSON.stringify({ status: 'I\'m alive' }));
});
device.on('message', (topic, payload) => {
const lock = new Gpio(4, 'out');
const message = JSON.parse(payload.toString());
console.log(message);
if (message.magic === 'open') {
console.log('ok, i\'ll open your lock');
lock.writeSync(1);
setTimeout(() => {
console.log('desligando');
lock.writeSync(0);
console.log('unexport');
lock.unexport();
}, 500);
}
});
device.on('error', function(error) {
console.log('me feriu aqui, ó');
console.log('error', error);
});