-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnexabridge.js
76 lines (56 loc) · 1.78 KB
/
nexabridge.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
64
65
66
67
68
69
70
71
72
73
74
75
76
var util = require('util'),
serialport = require('serialport'),
winston = require('winston');
var EventEmitter = require('events').EventEmitter;
var log = new winston.Logger({
transports: [
new winston.transports.Console( {level: 'debug', colorize: true, timeStamp: true} )
]
});
function NexaBridge( port, nexaId ) {
// TODO: Make find mech
/*var nexaPort = findNexaPort();*/
var nexaPort = port;
return new nexabridge( nexaPort, nexaId );
}
function nexabridge( nexaPort, nexaId ) {
EventEmitter.call( this );
var self = this;
self.id = nexaId;
var SerialPort = serialport.SerialPort;
var serial = new SerialPort("/dev/ttyACM1", {
baudrate: 9600,
parser: serialport.parsers.readline("\n")
});
serial.on( 'open', function() {
/*log.info( 'Opened serial connection' );*/
log.info( 'Opened serial conn' );
self.emit( 'connect' );
// TODO: Arduino bridge must report on ready state instead of timeout here
setTimeout( function() {
self.emit( 'ready' );
}, 2000 );
});
serial.on( 'data', function(data) {
log.debug( 'DATA:' + data );
self.emit( 'data', data );
});
function setId(id) {
nexaId = id;
}
function send( device, state ) {
serial.write( generateCommand( self.id, device, state, false ) + '\n' );
}
function sendln( data ) {
log.debug( 'Sending data: ' + data );
serial.write( data + "\n" );
serial.flush( function() { log.debug( 'blæh' ) });
}
function generateCommand( nexaId, device, state, group ) {
return util.format( 'SEND ADV %s %s %s %s', nexaId, device, (state) ? '1' : '0', (group) ? '1' : '0' );
}
this.sendln = sendln;
this.send = send;
}
util.inherits( nexabridge, EventEmitter );
module.exports.NexaBridge = NexaBridge;