forked from weinzmi/daumUSB2BLE
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
289 lines (256 loc) · 10.1 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/usr/bin/env node
var express = require('express')
var app = require('express')()
var server = require('http').createServer(app) // for getting IP dynamicaly in index.ejs
var io = require('socket.io')(server) // for getting IP dynamicaly in index.ejs
var path = require('path')
var MyServo = require('./BLE/MyServo')
var MyBleTrainer = require('./BLE/MyBleTrainer')
var MyAntTrainer = require('./BLE/MyAntTrainer')
var TrainerBLE = require('./BLE/trainerBLE')
const config = require('config-yml') // Use config for yaml config files in Node.js projects
var nearest = config.Servo.nearest;
var widest = config.Servo.widest;
var DEBUG = config.DEBUG.server // turn this on for debug information in consol
const { version } = require('./package.json') // get version number from package.json
// ////////////////////////////////////////////////////////////////////////
// used global variables, because I cannot code ;)
// ////////////////////////////////////////////////////////////////////////
// /////////////////////////////////////////////////////////////////////////
// server path specification
// /////////////////////////////////////////////////////////////////////////
app.use('/public/css', express.static(path.join(__dirname, 'public/css')))
app.use('/public', express.static(path.join(__dirname, 'public')))
app.use('/lib', express.static(path.join(__dirname, 'lib')))
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'ejs')
app.get('/', function (req, res) {
res.render('index')
})
// /////////////////////////////////////////////////////////////////////////
// server start listening to port 3000
// /////////////////////////////////////////////////////////////////////////
server.listen(process.env.PORT || 3000, function () { // for getting IP dynamicaly in index.ejs and not to enter it manually
if (DEBUG) console.log('[server.js] - listening on port 3000!')
});
// /////////////////////////////////////////////////////////////////////////
// instantiation
// /////////////////////////////////////////////////////////////////////////
var myServo = new MyServo();
myServo.setGear(1);
var gearvalue = widest
var brforce = 0; // breacking force init
var watt=25;
var speedms = 0.0; //init with lowest resistance
var controlled, settingservo, measuring = false;
var trainerBLE; // wait for sensor befor start advertising
var myBleTrainer = new MyBleTrainer()
var myAntTrainer = new MyAntTrainer()
// /////////////////////////////////////////////////////////////////////////
// Web server callback, listen for actions taken at the server GUI, not from Ant or BLE
// /////////////////////////////////////////////////////////////////////////
io.on('connection', socket => {
io.emit('preset_servo', [nearest,widest,gearvalue])
if (DEBUG) console.log('[server.js] - connected to socketio')
socket.on('reset', function (data) {
io.emit('key', '[server.js] - ergoFACE Server started')
})
socket.on('restart', function (data) {
if (DEBUG) console.log('[server.js] - restart')
gear = 1
myServo.setGear(gear)
io.emit('key', '[server.js] - restart')
})
socket.on('reco', function (data) {
if (DEBUG) console.log('[server.js] - reconnect')
gear = 1
myBleTrainer.recon();
trainerBLEinit ();
io.emit('key', '[server.js] - reconnect')
})
socket.on('stop', function (data) {
if (DEBUG) console.log('[server.js] - stop')
io.emit('key', '[server.js] - stop')
})
socket.on('setGear', function (data) {
if (DEBUG) console.log('[server.js] - set Gear')
gear = data
myServo.setGear(gear)
io.emit('raw', '[server.js] - set Gear: ' + gear)
})
socket.on('servosetting', function (data) {
settingservo = (data==='on') ? true:false;
if (DEBUG) console.log('[server.js] - servosetting ' + data)
if (settingservo) {
io.emit('key', '[server.js] - servosetting on')
var servo_values = [nearest,widest]
io.emit('preset_servo', servo_values)
}
else {
io.emit('key', '[server.js] - servosetting off')
}
})
socket.on('new_servo_val', function (data) {
if (DEBUG) console.log('[server.js] - new_servoval ' + data[0])
if (data[1] === 'change_min') {
nearest = data[0];
}
else {
widest = data[0];
}
myServo.setLimit(data);
})
socket.on('new_gear_val', function (data) {
if (DEBUG) console.log('[server.js] - new_gearval ' + data[0])
myServo.setDirect(data);
})
socket.on('measuring_start', function (data) {
measuring = true;
if (DEBUG) console.log('[server.js] - measuring started ')
// measuring = trainer
})
})
process.on('SIGINT', () => {
setTimeout(function(){
process.exit();
}, 3000);
})
// /////////////////////////////////////////////////////////////////////////
// Bike information transfer to BLE & Webserver
// /////////////////////////////////////////////////////////////////////////
function trainerBLEinit () {
trainerBLE = new TrainerBLE(options = {
name: 'Jo1'
},serverCallback);
trainerBLE.on('disconnect', string => {
io.emit('control', 'disconnected')
controlled = false;
});
trainerBLE.on('key', string => {
if (DEBUG) console.log('[server.js] - key: ' + string)
io.emit('key', '[server.js] - ' + string)
})
trainerBLE.on('error', string => {
if (DEBUG) console.log('[server.js] - error: ' + string)
io.emit('error', '[server.js] - ' + string)
})
}
myBleTrainer.on('notifications_true', () => {
trainerBLEinit ();
});
myBleTrainer.on('notified', data => {
// recalculate power if BLE controlled? P = F * v
if ('rpm' in data) io.emit('rpm', data.rpm);
if ('speed' in data) {
speedms = Number(data.speed/3.6).toFixed(4);
// myServo.getSpeed(data.speed, watt)
io.emit('speed', data.speed);
}
if ('power' in data && controlled == true && brforce > 0) {
var tp=brforce * data.speed/3.6
data.power = Math.round(tp);
io.emit('power', data.power);
} else {
io.emit('power', data.power);
}
if ('hr' in data) io.emit('hr', data.hr);
trainerBLE.notifyFTMS(data)
// trainerBLE.notifyCSP(data)
});
myAntTrainer.on('notifications_true', () => {
trainerBLEinit ();
myBleTrainer.discon(); //prefer Ant over BLE Sensors
});
myAntTrainer.on('notified', data => {
// recalculate power if BLE controlled? P = F * v
if ('rpm' in data) io.emit('rpm', data.rpm.toFixed(0));
if ('speed' in data) {
speedms = Number(data.speed/3.6).toFixed(4);
//myServo.getSpeed(data.speed, watt)
io.emit('speed', data.speed.toFixed(1));
}
if ('power' in data && controlled == true && brforce > 0) {
var tp=brforce * data.speed/3.6
data.power = Math.round(tp);
io.emit('power', data.power);
} else {
io.emit('power', data.power);
}
if ('hr' in data) io.emit('hr', data.hr);
if (!measuring) {
trainerBLE.notifyFTMS(data)
// trainerBLE.notifyCSP(data)
}
else {
measuring = trainerBLE.measure(data);
io.emit('key', '[server.js] - measured?')
if (!measuring) {
io.emit('measured')
io.emit('key', '[server.js] - yes!')
}
}
});
// /////////////////////////////////////////////////////////////////////////
/* BLE callback section */
// /////////////////////////////////////////////////////////////////////////
function serverCallback (message, ...args) {
console.log('[server.js] - ftms server callback', message)
var success = false
switch (message) {
case 'reset':
if (DEBUG) console.log('[server.js] - USB Reset triggered via BLE')
io.emit('key', '[server.js] - Reset triggered via BLE')
success = true
break
case 'control': // do nothing special
if (DEBUG) console.log('[server.js] - Bike under control via BLE')
io.emit('key', '[server.js] - Bike under control via BLE')
io.emit('control', 'BIKE CONTROLLED')
controlled = true
success = true
break
case 'power': // ERG Mode - receive control point value via BLE from zwift or other app
if (DEBUG) console.log('[server.js] - Bike ERG Mode')
if (args.length > 0) {
watt = args[0]
if (!settingservo || !measuring) brforce=myServo.setWatt(watt);
if (DEBUG) console.log('[server.js] - Bike in ERG Mode - set Power to: ', watt)
io.emit('raw', '[server.js] - Bike in ERG Mode - set Power to: ' + watt)
io.emit('control', 'ERG MODE')
success = true
}
break
case 'simulation': // SIM Mode - calculate power based on physics
if (DEBUG) console.log('[server.js] - Bike in SIM Mode')
var windspeed = Number(args[0]).toFixed(1)
var grade = Number(args[1]).toFixed(1)
var crr = Number(args[2]).toFixed(4)
var cw = Number(args[3]).toFixed(2)
io.emit('raw', '[server.js] - Bike SIM Mode - [wind]: ' + windspeed + ' [grade]: ' + grade + ' [crr]: ' + crr + ' [cw]: ' + cw)
io.emit('windspeed', windspeed)
io.emit('grade', grade)
io.emit('crr', crr)
io.emit('cw', cw)
var g = config.simulation.g
var mRider = config.simulation.mRider // mass in kg of the rider
var mBike = config.simulation.mBike // mass in kg of the bike
var mass = mBike + mRider
var p = config.simulation.p
var forceofgravity = g * Math.sin(Math.atan(grade / 100)) * mass
var forcerollingresistance = g * Math.cos(Math.atan(grade / 100)) * mass * crr
if (speedms > 55) speedms = 0
var cspeed = parseFloat(speedms);
var wind = parseFloat(windspeed);
var forceaerodynamic = 0.5 * cw * p * Math.pow((cspeed + wind), 2)
var force = forceofgravity + forcerollingresistance + forceaerodynamic
if (DEBUG) console.log('[server.js] - force ' + cspeed)
if (!settingservo || !measuring) {
io.emit('raw', '[server.js] - Bike in SIM Mode - set Power to : ' + force) //power
io.emit('simpower', force) //power
io.emit('control', 'SIM MODE')
}
success = true
break
}
return success
}