This repository has been archived by the owner on Jul 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
monitor.js
49 lines (43 loc) · 1.7 KB
/
monitor.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
module.exports = function(app) {
var event = app.event
, handle = require('./handlers.js')
, utils = require('./utils.js')
, endpoints = {}
;
// add handlers that depend on current socket endpoint
handle.SocketSend = function(packetType, data, callback){
if(callback)
endpoints.socket.emit(packetType, data, callback);
else if(data)
endpoints.socket.emit(packetType, data);
}
// add handlers that depend on current port endpoint
handle.PortWrite = function(data){
endpoints.port.write(data, function(err, results) {
if(err) utils.log('Serial write error: ' + err);
if(results != 0) utils.logText('Serial data write - ' + results + ' bytes');
});
}
// When the port connects, enable the serial port writer
event.on('port-start', function(port){
endpoints.port = port;
event.on('port-read', handle.SerialRead);
event.on('port-write', handle.PortWrite);
});
// When the port disconnects, disable the serial port writer
event.on('port-stop', function(){
event.removeListener('port-read', handle.SerialRead);
event.removeListener('port-write', handle.PortWrite);
});
// When the socket connects, enable the port reader and socket sender
event.on('socket-start', function(socket){
endpoints.socket = socket;
//event.on('port-read', handle.SerialRead);
event.on('socket-send', handle.SocketSend);
});
// When the socket disconnects, disable the port reader and socket sender
event.on('socket-stop', function(){
//event.removeListener('port-read', handle.SerialRead);
event.removeListener('socket-send', handle.SocketSend);
});
}