-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbrain.js
58 lines (46 loc) · 1.3 KB
/
brain.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
var net = require('net'),
events = require('events'),
util = require('util');
var ThinkGearClient = function (opts) {
opts || (opts = {});
this.port = opts.port || 13854;
this.host = opts.host || 'localhost';
var enableRawOutput = !!opts.enableRawOutput;
this.config = {
enableRawOutput: enableRawOutput,
format: "Json"
};
events.EventEmitter.call(this);
};
util.inherits(ThinkGearClient, events.EventEmitter);
ThinkGearClient.prototype.connect = function () {
var self = this;
var client = this.client = net.connect(this.port, this.host, function () {
client.write(JSON.stringify(self.config));
});
client.on('data', function (data) {
try {
var json = JSON.parse(data.toString());
if (json['rawEeg']) {
self.emit('raw', json);
} else if (json['blinkStrength']) {
self.emit('blink', json);
} else {
self.emit('data', json);
}
} catch (e) {
self.emit('error', data.toString());
}
});
};
var Brain = function () {
var brain = new ThinkGearClient({
});
return {
think: function () {
brain.connect();
return brain;
}
};
}
module.exports = Brain;