forked from tessel/t2-cli
/
tessel.js
118 lines (102 loc) · 3.02 KB
/
tessel.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
Constructor function for Tessel objects
param connection: the Connection object that represents the physical comm bus
*/
function Tessel(connection) {
var self = this;
if (connection === undefined) {
throw new Error('Cannot create a Tessel with an undefined connection type');
}
self.usbConnection = undefined;
self.lanConnection = undefined;
self.close = function() {
// If this tessel has been closed already
if (self.closed) {
// Just resolve
return Promise.resolve();
// Otherwise
} else {
// Mark as having been closed
self.closed = true;
}
// Kill all of this Tessel's remote processes
return self.connection.end();
};
self.addConnection = function(connection) {
// Set the connection var so we have an abstract interface to relay comms
switch (connection.connectionType) {
case 'USB':
self.usbConnection = connection;
break;
case 'LAN':
self.lanConnection = connection;
break;
default:
throw new Error('Invalid connection provided! Must be USB or LAN.');
}
};
// Add this physical connection to the Tessel
self.addConnection(connection);
// The human readable name of the device
self.name = undefined;
// The unique serial number of the device
self.serialNumber = undefined;
// Whether or not this connection has been ended
self.closed = false;
}
Object.defineProperty(Tessel.prototype, 'connection', {
get: function() {
// If we have an authorized LAN connection, prefer that
if (this.lanConnection && this.lanConnection.authorized) {
return this.lanConnection;
}
// If we have a USB connection, prefer that next
else if (this.usbConnection) {
return this.usbConnection;
}
// Worse case, we just have a non authorized LAN connection
else if (this.lanConnection) {
return this.lanConnection;
}
}
});
Tessel.prototype.receive = function(remote) {
var error = '';
var received = new Buffer(0);
remote.stderr.on('data', function(buffer) {
error += buffer.toString();
});
remote.stdout.on('data', function(buffer) {
received = Buffer.concat([received, buffer]);
});
return new Promise(function(resolve, reject) {
remote.once('close', function() {
if (error) {
return reject(new Error(error));
} else {
return resolve(received);
}
});
});
};
Tessel.prototype.simpleExec = function(command) {
var self = this;
return new Promise(function(resolve, reject) {
// Stop processes and delete everything in the folder
return self.connection.exec(command)
.then(function(remoteProcess) {
return self.receive(remoteProcess).then(function(received) {
return resolve(received.toString());
}).catch(reject);
});
});
};
Tessel.PUSH_PATH = '/app/';
Tessel.RUN_PATH = '/tmp/remote-script/';
module.exports = Tessel;
require('./provision');
require('./name');
require('./deploy');
require('./erase');
require('./wifi');
require('./update');