-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor.js
61 lines (50 loc) · 1.88 KB
/
sensor.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
"use strict";
var EventEmitter = require('events').EventEmitter;
var SerialPort = require('serialport').SerialPort;
var GPIO = require('onoff').Gpio;
var winston = require('winston');
exports.init = function(options, logger) {
var timerRFIDWatchdog;
var eventEmitter = new EventEmitter();
var serialPort = new SerialPort('/dev/ttyAMA0', {
baudrate: 9600,
databits: 8,
parity: 'none',
stopbits: 1
});
// delay, so events can be processed
process.nextTick(function() {
serialPort.on("open", function (error) {
if (error) {
logger.error('failed to open serial port: ' + error);
eventEmitter.emit("error");
} else {
logger.info('opened serial port');
var resetPin = new GPIO(17, 'out');
var reset = function() {
resetPin.writeSync(0);
setTimeout(function() {
resetPin.writeSync(1);
}, 50);
setTimeout(reset, 3000);
};
reset();
eventEmitter.emit("open");
serialPort.on('data', function (data) {
if (data && data.length && data[0]) {
//logger.trace('serial data received: ' + data);
if (timerRFIDWatchdog) {
clearTimeout(timerRFIDWatchdog);
}
timerRFIDWatchdog = setTimeout(function() {
timerRFIDWatchdog = undefined;
eventEmitter.emit("RFIDWatchdogExpired");
}, options.RFID_WATCHDOG_TIMEOUT);
eventEmitter.emit("RFIDDataReceived");
}
});
}
});
});
return eventEmitter;
};