-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
111 lines (91 loc) · 3.19 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
'use strict';
/**
* Module dependencies.
*/
var conf = require('./lib/config')();
var app = require('express')();
var http = require('http').Server(app);
var sketches = require('./routes/sketches');
var io = require('socket.io')(http);
var lumi = require('./lib/lumi');
var sketchRunner = require('./lib/sketch_runner');
var bodyParser = require('body-parser');
var favicon = require('serve-favicon');
var morgan = require('morgan');
var methodOverride = require('method-override');
var serveStatic = require('serve-static');
var errorhandler = require('errorhandler');
io.on('connection', function (socket) {
// var fpscStartTime = Date.now();
// var fpscFramesCounter = 0;
socket.on('frame', function(data) {
lumi.frame(data.data, socket.id);
// if (Date.now() - fpscStartTime > 1000) {
// console.log('last clientID: ' + socket.id);
// console.log(socket.id, Date.now(),'client-fps: ' + fpscFramesCounter);
// fpscFramesCounter = 0;
// fpscStartTime = Date.now();
// }
// fpscFramesCounter++;
});
socket.on('reset', function() {
lumi.reset(socket.id);
});
socket.on('iframe', function(data) {
lumi.indexedFrame(data.data, socket.id);
});
socket.on('palette', function(data) {
lumi.setPalette(data.data, socket.id);
});
socket.on('createDistPal', function(data) {
lumi.setDistPalette(data.data, socket.id);
});
socket.on('disconnect', function(){
lumi.reset(socket.id);
});
socket.on('runSketch', function(data) {
var name = data.name;
sketchRunner.runSketch(name, 'http://localhost:'+(process.env.PORT || 3000)+'/play/'+name);
});
socket.on('stopSketch', function(data) {
var id = data.id;
sketchRunner.stopSketchById(id);
});
socket.on('stopSketchNamed', function(data) {
var name = data.name;
sketchRunner.stopSketchById(name);
});
socket.on('stopAllSketch',function(){
sketchRunner.stopAll();
});
});
sketchRunner.addListener(function(name,state, id){
console.log('SKETCH_RUNNER: id:' + id + ' name:' + name + ' state:'+state);
io.sockets.emit('rskstate', { name: name, state: state, id: id });
});
// Configuration
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'hjs');
app.set('view options', {layout: false});
app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(morgan({ format: 'dev', immediate: true }));
app.use(bodyParser());
app.use(methodOverride('X-HTTP-Method-Override'));
app.use(serveStatic('public/'));
app.use(errorhandler());
var spiDevice = process.env.SPI_DEVICE || conf.device;
console.log('Using device: ' + spiDevice);
lumi.openPort(spiDevice);
//Routes
app.get('/', sketches.get);
app.get('/sketches', sketches.list(sketchRunner));
app.get('/sketches/:name', sketches.get);
app.delete('/sketches/:name', sketches.delete);
app.get('/remotes', sketches.remotes(sketchRunner));
app.get('/play/:name',sketches.play);
//upsert
app.post('/sketches', sketches.upsert);
http.listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});