forked from graysonarts/imuduino-3js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
40 lines (32 loc) · 917 Bytes
/
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
var ble_package_name = './lib/imuduino'
var express = require('express'),
app = express(),
server = require('http').Server(app),
io = require('socket.io')(server),
ble = require(ble_package_name),
_ = require('lodash'),
connection = null,
radianScale = (Math.PI / 180.0)
var start = new Date().getTime()
server.listen(4200)
console.log('server url http://localhost:' + 4200)
app.use(express.static('public'))
function degreeToRadians(n) {
if (isNaN(n)) {
return n
}
return n * radianScale
}
var IMUduino = new ble()
IMUduino.on('packet', function (p) {
p = _.mapValues(p, degreeToRadians)
p.time = new Date().getTime()
p.duration = (new Date().getTime()) - start
io.emit(p.type || 'unknown', p)
start = new Date().getTime() // Times are deltas
})
io.on('connection', function (socket) {
if (connection) {
socket.emit('device_status', connection)
}
})