-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
90 lines (75 loc) · 2.24 KB
/
index.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
var _ = require('lodash');
var EventEmitter = require('events').EventEmitter;
var bike = {
dummy: false,
status: {
speed: 1337,
inclination: 42,
acceleration: 0,
braking: false,
blinkingRight: false,
blinkingLeft: false,
noLights:true
},
settings: {
wheelDiameter: 50,
refreshRate:4
},
emitter: new EventEmitter()
};
if (bike.dummy) {
var gyro = require("./lib/dummyGyro.js");
var speed = require("./lib/dummySpeed.js");
var lights = require("./lib/dummyLights.js");
} else {
var gyro = require("./lib/gyro.js");
var speed = require("./lib/speed.js");
var lights = require("./lib/lights.js");
}
bike.pullValues = function() {
bike.status.speed = speed.speed();
bike.status.inclination = Math.round(gyro.inclination());
bike.status.braking = gyro.braking();
bike.status.blinkingLeft = lights.left() && (!bike.status.braking);
bike.status.blinkingRight = lights.right() && (!bike.status.braking);
bike.status.noLights = ! (bike.status.blinkingRight || bike.status.blinkingLeft || bike.status.braking)
lights.braking(bike.status.braking);
}
bike.refreshInterval = setInterval(function(){
bike.pullValues();
},1000/bike.settings.refreshRate)
/* Express http Server */
var express = require('express');
var app = express();
app.use(express.static('public'));
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Test app online at http://%s:%s', host, port);
});
/* Socket Communication */
var io = require('socket.io')(server);
io.on('connection', function (socket) {
bike.emitter.on('sendStatus',function(data){
socket.emit('status', {
status: bike.status
});
});
bike.emitter.on('sendSettings',function(data){
socket.emit('settings', {
settings: bike.settings
});
});
bike.emitter.emit('sendSettings',{reason:'welcome'});
bike.emitter.emit('sendStatus',{reason:'welcome'});
socket.on('settings', function (data) {
_.assign(bike.settings, data.settings);
bike.emitter.emit('sendSettings',{reason:'update'});
});
socket.on('settingsRequest', function (data) {
bike.emitter.emit('sendSettings',{reason:'request'});
});
socket.on('statusRequest', function (data) {
bike.emitter.emit('sendStatus',{reason:'request'});
});
});