-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathtessel-simulator.js
83 lines (64 loc) · 1.92 KB
/
tessel-simulator.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
// Test dependencies are required and exposed in common/bootstrap.js
require('./bootstrap');
function TesselSimulator(options) {
// If options weren't provided
if (!options) {
// create a default
options = {};
}
var simConnection = {
exec: function(command, options, callback) {
// Account for the case where options are not provided but a callback is
if (typeof options === 'function') {
callback = options;
options = {};
}
// Account for the case where a callback wasn't provided
if (callback === undefined) {
// Dummy callback
callback = function() {};
}
if (!Array.isArray(command)) {
throw new Error('Invalid command passed to exec.');
}
tessel._rps.control.write(command.join(' '));
return callback(null, tessel._rps);
},
end: function() {
return Promise.resolve();
},
close: function() {
return Promise.resolve();
},
open: function() {
return Promise.resolve();
},
connectionType: options.type || 'USB',
};
var tessel = new Tessel(simConnection);
tessel._rps = new RemoteProcessSimulator();
// If a name was provided, set the Tessel name
if (options.name) {
tessel.name = options.name;
}
// If a serial number was provided and this is a USB connection
if (options.serialNumber && options.type === 'USB') {
// Set the serialNumber
tessel.serialNumber = options.serialNumber;
}
// If options specifies authorized, make this Tessel authorized
if (options.type === 'LAN') {
if (options.authorized !== true) {
simConnection.authorized = options.authorized || false;
} else {
simConnection.authorized = options.authorized;
}
}
return tessel;
}
Tessel.prototype.mockClose = function() {
this.close();
process.removeAllListeners('exit');
process.removeAllListeners('SIGINT');
};
module.exports = TesselSimulator;