-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
51 lines (42 loc) · 1.13 KB
/
app.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
var prompt = require('prompt');
var chalk = require('chalk');
var sp = require('serialport');
var cp = require('child_process');
var _ = require('lodash');
var SerialPort = sp.SerialPort;
var serialPort = new SerialPort('/dev/tty.usbmodem1451', {
baudrate: 9600,
parser: sp.parsers.readline('\n')
}, false);
prompt.start();
serialPort.on('open', function(){
console.log(chalk.blue('Serial Port : Open'));
command();
});
serialPort.on('data', function(data) {
console.log(chalk.blue.bold('Rx'), chalk.green.underline(data));
var isPir = data.indexOf("PIR") !== -1;
if(isPir){
var value = data.split(':')[1] * 1;
if(value){lazyWarning();}
}else{
command();
}
});
serialPort.on('close', function(){
console.log(chalk.red('Serial Port : Closed'));
process.exit();
});
function warning(){
cp.exec('say warning');
}
function command(){
prompt.get(['arduino'], function (err, result) {
if(result.arduino === 'q')
serialPort.close();
else
serialPort.write(result.arduino + '\n');
});
}
serialPort.open();
var lazyWarning = _.debounce(warning, 2000, {leading:true, trailing:false});