extract duplicate code

This commit is contained in:
Max Stockner 2016-10-15 23:23:51 +02:00
parent ef0ecab18e
commit d164721938
2 changed files with 16 additions and 22 deletions

View file

@ -70,12 +70,8 @@ function DMXWeb() {
res.status(404).json({"error": "universe not found"}) res.status(404).json({"error": "universe not found"})
return return
} }
var universe = dmx.universes[req.params.universe]
var u = {} res.json({"state": dmx.universeAsObject(req.params.universe)})
for(var i = 0; i < 256; i++) {
u[i] = universe.get(i)
}
res.json({"state": u})
}) })
app.post('/state/:universe', function(req, res) { app.post('/state/:universe', function(req, res) {
@ -85,13 +81,7 @@ function DMXWeb() {
} }
dmx.update(req.params.universe, req.body) dmx.update(req.params.universe, req.body)
res.json({"state": dmx.universeAsObject(req.params.universe)})
var universe = dmx.universes[req.params.universe]
var u = {}
for(var i = 0; i < 256; i++) {
u[i] = universe.get(i)
}
res.json({"state": u})
}) })
app.post('/animation/:universe', function(req, res) { app.post('/animation/:universe', function(req, res) {
@ -99,10 +89,7 @@ function DMXWeb() {
var universe = dmx.universes[req.params.universe] var universe = dmx.universes[req.params.universe]
// preserve old states // preserve old states
var old = {} var old = dmx.universeAsObject(req.params.universe)
for(var i = 0; i < 256; i++) {
old[i] = universe.get(i)
}
var animation = new A() var animation = new A()
for(var step in req.body) { for(var step in req.body) {
@ -126,11 +113,7 @@ function DMXWeb() {
socket.on('request_refresh', function() { socket.on('request_refresh', function() {
for(var universe in config.universes) { for(var universe in config.universes) {
var u = {} socket.emit('update', universe, dmx.universeAsObject(req.params.universe))
for(var i = 0; i < 256; i++) {
u[i] = dmx.universes[universe].get(i)
}
socket.emit('update', universe, u)
} }
}) })

11
dmx.js
View file

@ -36,6 +36,17 @@ DMX.prototype.update = function(universe, channels) {
DMX.prototype.updateAll = function(universe, value) { DMX.prototype.updateAll = function(universe, value) {
this.universes[universe].updateAll(value) this.universes[universe].updateAll(value)
this.emit('updateAll', universe, value) this.emit('updateAll', universe, value)
this.universes[universe].updateAll(value)
this.emit('updateAll', universe, value)
}
DMX.prototype.universeToObject(universe) {
var universe = this.universes[req.params.universe]
var u = {}
for(var i = 0; i < universe.length; i++) {
u[i] = universe.get(i)
}
return u
} }
module.exports = DMX module.exports = DMX