-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
162 lines (133 loc) · 4.44 KB
/
app.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
'use strict'
var express = require( 'express' ),
app = express(),
http = require( 'http' ).Server( app ),
cylon = require( 'cylon' ),
mongoose = require( 'mongoose' ),
io = require( 'socket.io' )( http ),
eeg = require( './eegFunctions' );
var port = process.env.PORT || 3001 ;
var db = mongoose.connection;
var headsetPort = '/dev/rfcomm0';
/*
this might be rfcomm0,
rfcomm1,
or rfcomm2.
Check your connection.
*/
var activeDB = 'eegControl';
/*
use 'eeg' for general testing',
'eegNap' for napping,
'eegSleep' for sleeping,
'eegAwake' for wake recording
Be sure this matches db in models/eegSnapshot
and to switch mongo to this db,
'eegControl' for control data
and 'eegMock' for mock data
*/
var uristring = process.env.MONGOLAB_URI || 'mongodb://localhost/' + activeDB;
app.use( express.static( __dirname + '/dist' ) );
db.on( 'error' , console.error );
db.once( 'open', function callback(){
console.log( 'db ' + activeDB + ' ready' );
});
http.listen( port, function(){
console.log( '\nListening on port:' + port + '\n' );
});
/*
Log out a message when a user connects to the website
*/
io.on( 'connection' , function(socket){
console.log('a user connected');
});
/*
Connect to MongoDB
*/
mongoose.connect( uristring );
/*
Initialize the Cylon robot,
save the new data to mongodb,
grab data from MongoDB and emit it to website
*/
/*setInterval(function() {
console.log('mocking headset');
mockHeadset();
}, 0);*/
cylon.robot({
connection: { name: 'neurosky', adaptor: 'neurosky', port: headsetPort },
device: { name: 'headset', driver: 'neurosky' }
})
.on( 'ready' , function(robot) {
var newData = {};
var avg10Data = {};
var avg1000Data = {};
var pauseRecord = true;
robot.headset.on('signal', function( data ){
console.log( data );
if( data !== 0 ){
pauseRecord = true;
} else {
pauseRecord = false
}
});
robot.headset.on( 'eeg' , function(data) {
if( !pauseRecord){
io.sockets.emit( 'good-signal' );
eeg.addShot( data.delta,
data.theta,
data.loAlpha,
data.hiAlpha,
data.loBeta,
data.hiBeta,
data.loGamma,
data.midGamma
),
eeg.lastShot(function(data){
newData.values = data;
}),
eeg.avgLastTen(function(data){
avg10Data.values = data;
}),
eeg.avgLast1000(function(data){
avg1000Data.values = data;
}),
sendData(newData,avg10Data,avg1000Data);
} else {
io.sockets.emit( 'poor-signal' );
}
})
})
.start();
/*
Helper Functions
*/
function sendPoorSignal(){
io.sockets.emit( 'poor-signal' );
}
// Send data to website using socket.io
function sendData(newData,avg10,avg1000){
newData.title = 'newData';
newData.description = 'Data prints in at 1/s';
avg10.title = 'avg10';
avg10.description = 'average of the last 10 shots';
avg1000.title = 'avg1000';
avg1000.description = 'average of last 1000 shots';
var brainDataChunk = [ newData, avg10, avg1000 ];
// var brainDataChunk = [ newData ];
console.log( brainDataChunk );
io.sockets.emit( 'brain-data' , brainDataChunk );
}
// Send simulated brain data using socket.io
function mockHeadset(){
var mockNew = {};
var mock10Avg = {};
var mock1000Avg = {};
mockNew.values = eeg.mockBrainData();
mock10Avg.values = eeg.mockBrainData();
mock1000Avg.values = eeg.mockBrainData();
console.log('mockNew' + mockNew);
console.log('mock10Avg' + mock10Avg);
console.log('mock1000Avg' + mock1000Avg);
sendData(mockNew,mock10Avg,mock1000Avg);
}