-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
93 lines (79 loc) · 2.49 KB
/
index.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
var Modem = require('./lib/modem'),
fs = require('fs');
/*
* Constructor for Minicom
* input is a config object with defaults and array of ports
* defaults = {echo: true/false, dial_timeout: # in sec}
* ports = [ {port: '/dev/ttyACM0', phone: '1234443322', type: 'some string', config: {auto_answer: true/false} } ]
*/
var Minicom = function(arg) {
this.activePorts = {};
this.detectedPorts = {};
this.config = {};
if (!(this instanceof Minicom)) return new Error('Must be initialized before being used');
var self = this;
var keys = arg ? Object.keys(arg) : [];
if (keys.length) {
if (keys.indexOf('defaults') !== -1) this.config.defaults = args.defaults;
if (keys.indexOf('ports') !== -1) this.config.ports = args.ports;
self.initPorts();
} else {
this.loadConfig('config.json', function(err) {
if (!err) self.initPorts();
else return self;
});
}
}
Minicom.prototype.initPorts = function() {
var self = this;
if (self.config && self.config.ports.length) {
self.config.ports.forEach(function(port) {
self.addPort(port); //TODO: check for failed port setup
});
}
}
Minicom.prototype.loadConfig = function(file, cb) {
var self = this;
fs.readFile(__dirname + '/' + file, function(err, data) {
if (!err) {
self.config = JSON.parse(data);
return cb(null);
} else return cb(err);
});
}
Minicom.prototype.addPort = function(ports, success_cb, error_cb) {
var self = this,
keys = Object.keys(self.activePorts),
port = ports.port;
success = (typeof success_cb === 'function') ? success_cb : self.defaultSuccessHandler;
error = (typeof error_cb === 'function') ? error_cb : self.defaultErrorHandler;
if (keys.indexOf(port) < 0) {
sp = new Modem(port);
if (sp) {
// sp.on('error', error);
// sp.on('data', success);
self.activePorts[port] = {
modem: sp,
phone: ports.phone,
port: port
}
return self.activePorts[port];
}
else {
console.log('Failed to setup port: ',port);
return new Error('Failed to setup port: ', port);
}
}
return self.activePorts[port];
}
Minicom.prototype.defaultSuccessHandler = function(obj) {
console.log('Index default success handler: ', obj.data);
}
Minicom.prototype.defaultErrorHandler = function(obj) {
var self = this,
errorPorts = [];
console.log('Index default error handler: ', obj.error);
errorPorts.push(obj.port);
}
Minicom.prototype.list = Modem.list;
exports = module.exports = Minicom;