-
Notifications
You must be signed in to change notification settings - Fork 0
/
eegFunctions.js
142 lines (134 loc) · 5.37 KB
/
eegFunctions.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
var eegSnapshot = require('./models/eegSnapshot');
module.exports = {
addShot: function(delta, theta, loAlpha, hiAlpha, loBeta, hiBeta,loGamma, midGamma){
var args = Array.prototype.slice.call( arguments );
for ( var i = 0 ; i < args.length ; i++){
if ( typeof args[i] !== "number" ){
throw new Error( args[i]
+ ' is of type '
+ typeof args[i]
+ '. Value needs to be a number, instead'
)
}
}
var newShot = new eegSnapshot({
delta: delta,
theta: theta,
loAlpha: loAlpha,
hiAlpha: hiAlpha,
loBeta: loBeta,
hiBeta: hiBeta,
loGamma: loGamma,
midGamma: midGamma
});
console.log('Saving Shot to EEG Database: ' + '\n' + newShot + '\n');
newShot.save(function (err) {
if (err){
throw new Error('Shot did not save to database');
} else {
console.log ('Save successful');
}
});
},
mockData: function(){
return Math.round(Math.random()*100000);
},
mockBrainData: function(){
var fakeBrainData = {
_id: "Data",
delta: this.mockData(),
theta: this.mockData(),
loAlpha: this.mockData(),
hiAlpha: this.mockData(),
loBeta: this.mockData(),
hiBeta: this.mockData(),
loGamma: this.mockData(),
midGamma: this.mockData()
};
console.log(fakeBrainData);
return fakeBrainData;
},
findAll: function(callback){
eegSnapshot.find({}, function(err, eegData){
/* console.log('eegData' + eegData + '\n');*/
return callback(eegData);
});
},
lastShot: function(callback){
eegSnapshot.aggregate(
[
{ $sort : { timeStamp: -1 } },
{ $limit: 1},
{
$group:
{
_id: "New EEG Data",
timeStamp: { $first: "$timeStamp" },
delta: { $avg: "$delta" },
theta: { $avg: "$theta" },
loAlpha: { $avg: "$loAlpha" },
hiAlpha: { $avg: "$hiAlpha" },
loBeta: { $avg: "$loBeta" },
hiBeta: { $avg: "$hiBeta" },
loGamma: { $avg: "$loGamma" },
midGamma: { $avg: "$midGamma" }
}
}
], function(err, eegData){
return callback(eegData[0]);
}
);
},
avgLastTen: function(callback){
eegSnapshot.aggregate(
[
{ $sort : { timeStamp: -1 } },
{ $limit: 10 },
{
$group:
{
_id: "Avg Last 10 EEG Data",
timeWindowStart: { $last: "$timeStamp" },
timeWindowEnd: { $first: "$timeStamp" },
delta: { $avg: "$delta" },
theta: { $avg: "$theta" },
loAlpha: { $avg: "$loAlpha" },
hiAlpha: { $avg: "$hiAlpha" },
loBeta: { $avg: "$loBeta" },
hiBeta: { $avg: "$hiBeta" },
loGamma: { $avg: "$loGamma" },
midGamma: { $avg: "$midGamma" }
}
}
], function(err, eegData){
return callback(eegData[0]);
}
);
},
avgLast1000: function(callback){
eegSnapshot.aggregate(
[
{ $sort : { timeStamp: -1 } },
{ $limit: 1000 },
{
$group:
{
_id: "Avg Last 1000 EEG Data",
timeWindowStart: { $last: "$timeStamp" },
timeWindowEnd: { $first: "$timeStamp" },
delta: { $avg: "$delta" },
theta: { $avg: "$theta" },
loAlpha: { $avg: "$loAlpha" },
hiAlpha: { $avg: "$hiAlpha" },
loBeta: { $avg: "$loBeta" },
hiBeta: { $avg: "$hiBeta" },
loGamma: { $avg: "$loGamma" },
midGamma: { $avg: "$midGamma" }
}
}
], function(err, eegData){
return callback(eegData[0]);
}
);
}
}