-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspouter.js
177 lines (167 loc) · 6.24 KB
/
spouter.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
"use strict";
var _mongoose = require('mongoose'),
_config = require("./config.js"),
WebSocket = require("ws").Server;
var DescriptionSchema = new _mongoose.Schema({
subActivityName : {type: String},
index : {type : Number},
start : {type: Number},
end : {type: Number}
});
var DataSchema = new _mongoose.Schema({
mac : {type : String},
timestamp : {type: Number},
accel_X : {type : Number},
accel_Y : {type : Number},
accel_Z : {type : Number},
gyro_X : {type : Number},
gyro_Y : {type : Number},
gyro_Z : {type : Number},
magneto_X : {type : Number},
magneto_Y : {type : Number},
magneto_Z : {type : Number}
});
var TimeSchema = new _mongoose.Schema({
mac : {type : String},
timestamp : {type: Number}
});
DescriptionSchema.set('collection', _config.mongo.collection + "_descriptions");
DataSchema.set('collection', _config.mongo.collection + "_datas");
TimeSchema.set('collection', _config.mongo.collection + "_times");
var DescriptionModel = _mongoose.model('Descriptions', DescriptionSchema);
var DataModel = _mongoose.model('Datas', DataSchema);
var TimeModel = _mongoose.model('Times', TimeSchema);
class Spouter{
constructor(frequency){
_mongoose.connect('mongodb://'+ (_config.mongo.host || 'localhost') +':'+ (_config.mongo.port || 27017) +'/'+_config.mongo.database);
this.dataset = _mongoose.connection;
this.dataset.once("open", function(){
console.log("Connected to database");
});
this.frequency = frequency;
this.recording = false;
this.startTime = null;
this.edisonsTime = {};
this.stopTime = null;
this.server = new WebSocket({"host":_config.server, "port":_config.websocket}, function(){
console.log("Server start on ws://"+_config.server+":"+_config.websocket);
});
this.server.on('connection', function (ws) {
ws.on('message', this.onDataReceived.bind(this));
ws.on('close', function close() {
console.log('disconnected');
});
ws.on("error", function(e){
console.log("Server error " + e);
});
}.bind(this));
}
deleteCollection(iteration){
DescriptionModel.find({index: iteration}, {start:1, end:1}).sort({_id:-1}).exec(function(err, docs){
if(err) console.log("delete error : " + err);
docs.forEach(function(elem){
DataModel.remove({timestamp : {$gte: elem.start, $lte: elem.end}}, function(err){
if(err) throw err;
DescriptionModel.remove({index: iteration}, function(err){
if(err) console.log("delete error : " + err);
});
});
});
});
}
onDataReceived(message){
if(!this.recording) return;
var data = message.split(',');
data.pop();
var mac = data[1];
if(this.edisonsTime.hasOwnProperty(mac)) {
this.edisonsTime[mac] += this.frequency;
} else {
this.edisonsTime[mac] = this.startTime;
}
if(this.stopTime != null){
var nbEdisonDone = 0;
for(var key in this.edisonsTime){
if(this.edisonsTime.hasOwnProperty(key)){
if(this.stopTime <= this.edisonsTime[key]) nbEdisonDone++;
}
}
if(nbEdisonDone == Object.keys(this.edisonsTime).length) {
this.recording = false;
console.log("All data received for iteration " + this.currentIteration + ".");
}
if(this.stopTime < this.edisonsTime[mac]) return;
}
var timeToSave = new TimeModel({
mac : mac,
timestamp: data[0]
});
timeToSave.save(function (err) {
if(err) console.log("time save error : " + err);
});
var toSave = new DataModel({
mac : mac,
timestamp : this.edisonsTime[mac],
accel_X : data[2],
accel_Y : data[3],
accel_Z : data[4],
gyro_X : data[5],
gyro_Y : data[6],
gyro_Z : data[7],
magneto_X : data[8],
magneto_Y : data[9],
magneto_Z : data[10]
});
toSave.save(function (err) {
if(err) console.log("data save error : " + err);
});
}
startRecordingActivity(activity, iteration){
if(activity == undefined || activity == ""){
throw "Activity undefined";
}
this.currentSubActivityName = activity;
this.currentIteration = iteration;
this.startTime = new Date().getTime();
this.currentSubActivityStart = new Date().getTime();
this.edisonsTime = {};
this.recording = true;
this.stopTime = null;
}
changeSubActivity(activity, iteration){
if(activity == undefined || activity == ""){
throw "Activity undefined";
}
if(this.currentSubActivityName != undefined) {
var data = new DescriptionModel({
subActivityName: this.currentSubActivityName,
index: this.currentIteration,
start: this.currentSubActivityStart,
end: new Date().getTime()
});
data.save();
DataModel.find({mac: _config.mac.edison1}).count(function(err, count){
console.log("Edison1 : " + count);
});
DataModel.find({mac: _config.mac.edison2}).count(function(err, count){
console.log("Edison2 : " + count);
});
}
this.currentSubActivityName = activity;
this.currentIteration = iteration;
this.currentSubActivityStart = new Date().getTime();
}
stopRecording(){
if(this.currentSubActivityName != undefined) {
var data = new DescriptionModel({
subActivityName: this.currentSubActivityName,
index: this.currentIteration,
start: this.currentSubActivityStart,
end: new Date().getTime()
});
data.save();
}
this.stopTime = new Date().getTime();
}
}
module.exports = Spouter;