forked from kinetechcloudCEO/Synesthesia
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
201 lines (172 loc) · 4.94 KB
/
app.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
//////////////////////////////////////////
///
/// SYNESTHESIA
/// A collaboration between Kinetech and Hack Reactor
///
/// November 2013
/// Weidong Yang
/// David Ryan Hall
/// George Bonner
/// Kate Jenkins
/// Joey Yang
///
/// Check out http://kine-tech.org/ for more information.
///
//////////////////////////////////////////
// Instantiate server
var express = require('express');
var app = express();
var http = require('http');
var server = http.createServer(app);
var port = process.env.PORT || 8080;
server.listen(port);
var io = require('socket.io').listen(server);
app.set('io', io);
// var db = require('./server/database_server');
// var helpers = require('./server/helpers');
var routes = require('./config/routes.js');
var middleware = require('./config/middleware.js');
// define socket.io spaces
var conductor = io.of('/conductor');
var clients = io.of('/client');
var fireworks = io.of('/fireworks');
var dancer = io.of('/dancer');
var audio = io.of('/audio');
// instantiate state object (keeps track of performance state)
var state = {
connections: 0,
strobe: false,
audio: false,
audioLights: false,
motionTrack: false,
currentColor: '#000000',
resetMC: function() {
this.strobe = false;
this.audio = false;
this.audioLights = false;
this.motionTrack = false;
}
};
// set middleware
middleware.setSettings(app, express);
//////////////////////////////////////////
/// ROUTES
//////////////////////////////////////////
// render routes
app.get('/', routes.renderClient);
app.get('/conductor', routes.renderConductor);
app.get('/fireworks', routes.renderFireworks);
app.get('/audio', routes.renderAudio);
app.get('/dancer', routes.renderDancer);
app.get('/update', routes.renderUpdate);
app.get('*', routes.render404);
app.use(function(err, req, res, next){
res.send(500, 'Houston, your server has a problem.');
});
//////////////////////////////////////////
/// EVENTS
//////////////////////////////////////////
//////////////////////////////////////////
/// Visualizer events
//////////////////////////////////////////
fireworks.on('connection', function (firework) {
firework.emit("welcome", "Visualizer connected.");
});
//////////////////////////////////////////
/// Dancer / Motion Tracker events
//////////////////////////////////////////
dancer.on('connection', function (dancer) {
dancer.emit('welcome', {
message: "Connected for motion tracking.",
tracking: state.motionTrack
});
dancer.on('motionData', function (data) {
fireworks.emit('motionData', data);
});
});
//////////////////////////////////////////
/// Conductor events
//////////////////////////////////////////
conductor.on('connection', function (conductor) {
state.resetMC();
clients.emit('reset');
dancer.emit('reset');
audio.emit('reset');
conductor.emit("welcome");
conductor.on('changeColor',function (data){
var clients = io.of('/client');
state.currentColor = data.color;
clients.emit('changeColor', data);
});
conductor.on('randomColor', function (data){
var clients = io.of('/client');
state.currentColor = '#000000'; // Set current to black in the case of random
clients.emit('randomColor', data);
});
conductor.on('toggleSound', function (data){
state.audio = data.sound;
audio.emit('toggleSound', data);
});
conductor.on('toggleMotion', function (data){
var dancer = io.of('/dancer');
if (data.motion) {
state.motionTrack = true;
} else if (!data.paint) {
state.motionTrack = false;
}
dancer.emit('toggleMotion', data);
});
conductor.on('toggleStrobe', function (data){
var clients = io.of('/client');
if (data.strobe) {
state.strobe = true;
} else {
state.strobe = false;
}
clients.emit('toggleStrobe');
});
conductor.on('audioLightControl', function (data){
var clients = io.of('/client');
if (data.audio) {
state.audioLights = true;
} else {
state.audioLights = false;
}
});
conductor.on('newFadeTime', function (data){
var clients = io.of('/client');
clients.emit('newFadeTime', data);
});
});
//////////////////////////////////////////
/// Client events
//////////////////////////////////////////
clients.on('connection', function (client) {
state.connections += 1;
client.emit("welcome", {
id: client.id,
message: "welcome!",
mode: {
color: state.currentColor,
strobe: state.strobe,
audioLights: state.audioLights
}
});
client.on('disconnect', function (){
state.connections -= 1;
});
});
//////////////////////////////////////////
/// Audio events
//////////////////////////////////////////
audio.on('connection', function (audio) {
audio.emit('welcome', {audio: state.audio});
audio.on('audio', function (data){
console.log(data); // Leave in for test logging until Monday
var clients = io.of('/client');
if (state.audioLights) {
clients.emit('audio', data);
}
fireworks.emit('audio', data);
});
});