-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
54 lines (45 loc) · 1.67 KB
/
index.js
File metadata and controls
54 lines (45 loc) · 1.67 KB
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
var fs = require('fs');
var basename = require('path').basename;
var moment = require('moment');
var accounting = require('accounting');
var Buffers = require('buffers');
var chokidar = require('chokidar');
var SerialPort = require("serialport").SerialPort;
var now = Date.now();
var devicePath = process.env.DEVICE_PATH || '/dev/tty.usbmodem1411';
var watcher = chokidar.watch(process.env.HOME + '/.Trash', { persistent: true, ignoreInitial: true });
var serialport = new SerialPort(devicePath, { baudrate: 19200 });
watcher.on('add', function(path, stats) {
console.log('detected:', path);
var bufs = new Buffers();
var filename = basename(path);
try {
var data = fs.readFileSync(path);
bufs.push(new Buffer('filename : ' + filename + '\n'));
bufs.push(new Buffer('filesize : ' + accounting.formatNumber(stats.size) + ' bytes\n'))
bufs.push(new Buffer('created at: ' + moment(stats.atime).format('YYYY/MM/DD HH:mm:ss') + '\n'));
bufs.push(new Buffer('================================' + '\n'));
bufs.push(new Buffer(data.slice(0, 255)));
bufs.push(new Buffer('\n'));
bufs.push(new Buffer('================================' + '\n'));
bufs.push(new Buffer('\n\n'));
console.log(bufs.toString());
serialport.write(bufs.toBuffer(), function() {
console.log('wrote: ', path);
serialport.flush(function() {
console.log('flushed: ', path);
});
});
} catch (e) {
console.error('error: ', e);
}
fs.unlink(path);
});
watcher.on('error', function(e) {
console.error('error :', e);
});
serialport.on('open', function() {
serialport.on('data', function() {
console.log('data from board: ', arguments);
});
});