Skip to content

Commit 2012625

Browse files
Response parser now emits events for log, data, and event messages.
1 parent a202423 commit 2012625

11 files changed

+315
-52
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# node-aurora
22
Node.js client library for interacting with Aurora Dreamband.
33

4-
This is not considered stable yet. Please consider this a WIP until 1.0 is released.
4+
Official documentation coming soon. Until then, consider this package a work in progress, subject to breaking changes.

src/Aurora.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ class Aurora extends EventEmitter {
198198

199199
this._processQueue();
200200

201-
})
201+
});
202202

203203
});
204204

@@ -298,15 +298,21 @@ class Aurora extends EventEmitter {
298298

299299
}).catch (error => {
300300

301-
console.log(error)
302-
301+
console.log(error);
303302
console.timeEnd('command completed');
304303

305-
if (this.usbConnected){
306-
this._processQueue();
307-
}
304+
this.cmdCurrent = null;
305+
306+
this._serial.flush(() => {
307+
308+
AuroraResponseSerialParser.reset();
309+
310+
if (this.usbConnected){
311+
this._processQueue();
312+
}
313+
314+
});
308315

309-
//TODO: understand why I can't just return the error directly here....
310316
return Promise.reject(error);
311317
});
312318

@@ -318,7 +324,7 @@ class Aurora extends EventEmitter {
318324

319325
if (this.cmdCurrent) {
320326
this.cmdCurrent.triggerError(-1, "Lost connection to Aurora.");
321-
this.cmdCurrent = false;
327+
this.cmdCurrent = null ;
322328
}
323329

324330
this._serial.removeAllListeners();

src/AuroraCmd.js

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default class AuroraCmd {
1818
static defaultOptions = {
1919

2020
packetMode: false, //commands that return a lot of data can use this mode
21-
respWatchdogTimeout: 1000, //how long the command has to finish before timing out
21+
respWatchdogTimeout: 3000, //how long the command has to finish before timing out
2222
respTypeSuccess: AuroraCmd.RespTypes.ARRAY, //by default, success response returns array of lines
2323
respTypeError: AuroraCmd.RespTypes.OBJECT, //and error returns object { error: #, message: "" }
2424
respTypeSuccessOptions: {},
@@ -34,9 +34,6 @@ export default class AuroraCmd {
3434

3535
this._reject = function () {};
3636
this._fulfill = function () {};
37-
38-
//used to indicate whether response piped to respOutputStream is an error response
39-
this.error = false;
4037
}
4138

4239
//called when a command is added to the processing queue
@@ -117,13 +114,12 @@ export default class AuroraCmd {
117114
this._setupRespSuccess();
118115
this._setupRespError();
119116

120-
this.error = false;
117+
this.respSuccessStreamBack.on('data', this._onRespSuccessData);
118+
this.respErrorStreamBack.on('data', this._onRespErrorData);
121119

122-
this.respSuccessStreamBack.on('data', this._onRespSuccessData.bind(this));
123-
this.respErrorStreamBack.on('data', this._onRespErrorData.bind(this));
124120

125-
this.respSuccessStreamBack.on('finish', this._commandResponse.bind(this));
126-
this.respErrorStreamBack.on('finish', this._commandResponse.bind(this));
121+
this.respSuccessStreamBack.on('finish', this._onSuccess);
122+
this.respErrorStreamBack.on('finish', this._onError);
127123

128124
//write command string to input stream,
129125
Aurora._serial.write(this.toString() + '\n');
@@ -144,25 +140,13 @@ export default class AuroraCmd {
144140
}, this.options.respWatchdogTimeout);
145141
}
146142
}
147-
148-
fail() {
149-
150-
}
151-
152-
succeed() {
153-
154-
}
155143

156144
triggerError(errorCode, errorMessage){
157145

158-
this.error = true;
159146
this.respTypeError = AuroraCmd.RespTypes.OBJECT;
160147
this.respError = { error: errorCode, message: errorMessage};
161-
162148

163-
if (this.respErrorStreamFront){
164-
this.respErrorStreamFront.end();
165-
}
149+
this.respErrorStreamFront.end();
166150
}
167151

168152
//process response
@@ -203,7 +187,7 @@ export default class AuroraCmd {
203187
}
204188
}
205189

206-
_commandResponse(){
190+
_onError(){
207191

208192
clearTimeout(this.respTimer);
209193

@@ -212,12 +196,19 @@ export default class AuroraCmd {
212196
this.respSuccessStreamBack.removeAllListeners();
213197
this.respErrorStreamBack.removeAllListeners();
214198

215-
if (this.error) {
216-
this._reject(this.respError);
217-
}
218-
else {
219-
this._fulfill(this.respSuccess);
220-
}
199+
this._reject(this.respError);
200+
}
201+
202+
_onSuccess(){
203+
204+
clearTimeout(this.respTimer);
205+
206+
this.respSuccessStreamFront.removeAllListeners();
207+
this.respErrorStreamFront.removeAllListeners();
208+
this.respSuccessStreamBack.removeAllListeners();
209+
this.respErrorStreamBack.removeAllListeners();
210+
211+
this._fulfill(this.respSuccess);
221212
}
222213

223214
toString() {

src/AuroraResponseSerialParser.js

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
import EventEmitter from 'events';
22
import AuroraConstants from './AuroraConstants';
3+
import moment from 'moment';
34

45
class AuroraResponseSerialParser extends EventEmitter {
56

7+
static logTypesToEvents = {
8+
9+
INFO: 'responseLogInfo',
10+
WARN: 'responseLogWarning',
11+
ERRO: 'responseLogError',
12+
DATA: 'responseLogData',
13+
EVNT: 'responseLogEvent'
14+
};
15+
616
constructor() {
717

818
super();
@@ -15,7 +25,7 @@ class AuroraResponseSerialParser extends EventEmitter {
1525
this.unparsedBuffer = null;
1626
this.responseState = AuroraConstants.ResponseStates.NO_COMMAND;
1727
}
18-
git
28+
1929
parseChunk(chunk) {
2030

2131
//don't do anything if chunk is empty
@@ -62,15 +72,16 @@ git
6272

6373
if (cmdPromptIndex) {
6474

65-
this.emit('response', bufferLine.slice(0, cmdPromptIndex));
75+
this._parseNonCommandResponseLine(bufferLine.slice(0, cmdPromptIndex));
76+
6677
bufferLine = bufferLine.slice(cmdPromptIndex);
6778
}
6879

6980
this.emit('commandBegin', bufferLine.slice(AuroraConstants.COMMAND_PROMPT.length));
7081
}
7182
else {
7283

73-
this.emit('response', bufferLine);
84+
this._parseNonCommandResponseLine(bufferLine);
7485
}
7586
}
7687
else if (bufferLine.indexOf(AuroraConstants.COMMAND_DIVIDER_SUCCESS_STRING) === 0) {
@@ -190,6 +201,44 @@ git
190201
}
191202
}
192203
}
204+
205+
_parseNonCommandResponseLine = (line) => {
206+
207+
if (line.charAt(0) == '<'){
208+
209+
const logParts = line.match(/\< (WARN|ERRO|INFO|DATA|EVNT) \| (\d{2}:\d{2}:\d{2}\.\d{3}) > (.+)/i);
210+
211+
if (logParts && logParts.length == 4){
212+
213+
const logDate = moment(logParts[2], "HH:mm:ss.SSS", true).toDate();
214+
215+
this.emit(AuroraResponseSerialParser.logTypesToEvents[logParts[1].toUpperCase()], logParts[3], logDate);
216+
return;
217+
}
218+
}
219+
else if (line.slice(0, 6) == 'event-'){
220+
221+
const eventParts = line.match(/event-(\d{1,2}): (\d+)/i);
222+
223+
if (eventParts && eventParts.length == 3){
224+
225+
this.emit('responseEvent', +eventParts[1], +eventParts[2]);
226+
return;
227+
}
228+
}
229+
else {
230+
231+
const dataParts = line.split(': ');
232+
233+
if (dataParts.length == 2) {
234+
235+
this.emit('responseData', dataParts[0], dataParts[1].split(',').map(Number));
236+
return;
237+
}
238+
}
239+
240+
this.emit('responseUnknown', line);
241+
}
193242
}
194243

195244
export default new AuroraResponseSerialParser();

test/AuroraOsInfoMock.txt

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)