-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
211 lines (171 loc) · 5.39 KB
/
server.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
const DEBUG = false
var myArgs = process.argv.slice(2)
if (myArgs.length < 2) {
console.log("Error: Please, provide power meter Id's ")
return
}
var PowerMeterA_Id = myArgs[0]
var PowerMeterB_Id = myArgs[1]
if (!myArgs[2]) {
var average_factor = 3 //3 sec averaging default
} else {
var average_factor = myArgs[2]
}
var start_timeA = Date.now()
var start_timeB = Date.now()
var measureBufferA = []
var measureBufferB = []
var frozenWatts = 0;
//ANT+ receiver
const Ant = require('ant-plus')
let AntPlusStick = new Ant.GarminStick2()
let PowerMeterA = new Ant.BicyclePowerSensor(AntPlusStick)
let PowerMeterB = new Ant.BicyclePowerSensor(AntPlusStick)
AntPlusStick.on('startup', function () {
console.log("Ant+ device ready")
console.log("Averaging measures to " + average_factor + " seconds \n")
})
if (!AntPlusStick.open()) {
console.log("Ant+ device not found!")
}
PowerMeterA.on('powerData', AntData => {
let timeA = Date.now(); //Time of measure
measureBufferA.push(AntData.Power)
if (timeA - start_timeA > average_factor * 1000) {
if(measureBufferA.length > 0) {
AntData.timestamp = timeA;
AntData.Power = average(measureBufferA)
AntData.Averaging = average_factor
let data2send = JSON.stringify({ "measures0": AntData })
if (DEBUG) console.log("[" + new Date(start_timeA) + "] " + data2send)
if (sockets[0]) sockets[0].send(data2send)
start_timeA = timeA
measureBufferA = []
}
}
})
PowerMeterA.on('error', err => {
})
PowerMeterB.on('powerData', AntData => {
let timeB = Date.now(); //Time of measure
measureBufferB.push(AntData.Power)
if (timeB - start_timeB > average_factor * 1000) {
if(measureBufferB.length > 0) {
AntData.timestamp = timeB;
AntData.Power = average(measureBufferB)
AntData.Averaging = average_factor
let data2send = JSON.stringify({ "measures1": AntData })
if (DEBUG) console.log("[" + new Date(start_timeB) + "] " + data2send)
if (sockets[0]) sockets[0].send(data2send)
start_timeB = timeB
measureBufferB = []
}
}
})
PowerMeterB.on('error', err => {
})
// HTTP server
const express = require('express')
const app = express()
const bodyParser = require('body-parser')
const multer = require('multer')
const fs = require('fs');
//MULTER Config
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'uploads')
},
filename: function (req, file, cb) {
cb(null, file.originalname)
}
})
var upload = multer({ storage: storage })
const port = 8090
app.use(bodyParser.urlencoded({ extended: true }))
app.use(express.static(__dirname + "/public"))
app.get('/', function (req, res) {
res.sendFile('index.html')
})
app.get('/filecompare', function (req, res) {
res.sendFile(__dirname + '/public/filecompare.html')
})
app.post('/upload', upload.array('inputfit', 12), (req, res, next) => {
console.log(req)
const files = req.files
if (!files) {
const error = new Error('No files')
error.httpStatusCode = 400
return next(error)
}
res.json(files);
})
app.get('/docompare/:file', function (req, res) {
console.log("Do Compare")
const PwrParser = require("./pwrParser");
var file = __dirname+'/uploads/'+req.params.file;
var pwrParser = new PwrParser.FitParser(file);
let rawBuffer = pwrParser.getRawData();
pwrParser.parseRawData(rawBuffer).then(function (data) {
pwrParser
.getPower(data)
.then(function (data) {
//console.log(data)
var sum = 0;
var elem = data.length
data.forEach(entry => {
sum += entry.power
})
res.json(data);
})
});
})
app.listen(port, function () {
console.log("App running http://localhost:" + port)
});
//WebSocket Server
const sockets = [];
const WebSocket = require('ws')
const ws_port = 8091;
const wss = new WebSocket.Server({
port: ws_port
});
wss.on('connection', function connection(ws) {
console.log('Client connected')
sockets[0] = ws
ws.on('message', function incoming(message) {
console.log('Received: %s', message)
switch(message){
case 'start':
console.log("Attaching devices")
PowerMeterA.attach(0, PowerMeterA_Id)
PowerMeterB.attach(1, PowerMeterB_Id)
break;
case 'stop':
console.log("Dettaching devices")
PowerMeterA.detach()
PowerMeterB.detach()
break;
}
})
ws.send(JSON.stringify({ "devices": [PowerMeterA_Id, PowerMeterB_Id] }))
})
function average(powerData) {
const checkEqual = powerData => powerData.every(v => v === powerData[0])
var measures = powerData.length
var sum = 0;
for (var i = 0; i < measures; i++) {
sum += powerData[i]
}
if (checkEqual(powerData) === true) {
frozenWatts++;
} else {
frozenWatts = 0;
}
if (frozenWatts > 1) {
//Favero Assioma curiosity
//Asuming if two buffers have all the measures equal we are not pedaling.
return 0
} else {
return (sum / measures)
}
}