From d16472193895e6190465a2f66c0a33f9cfe3fb6e Mon Sep 17 00:00:00 2001 From: Max Stockner Date: Sat, 15 Oct 2016 23:23:51 +0200 Subject: [PATCH] extract duplicate code --- dmx-web.js | 27 +++++---------------------- dmx.js | 11 +++++++++++ 2 files changed, 16 insertions(+), 22 deletions(-) diff --git a/dmx-web.js b/dmx-web.js index f7698e0..ad029e9 100755 --- a/dmx-web.js +++ b/dmx-web.js @@ -70,12 +70,8 @@ function DMXWeb() { res.status(404).json({"error": "universe not found"}) return } - 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}) + + res.json({"state": dmx.universeAsObject(req.params.universe)}) }) app.post('/state/:universe', function(req, res) { @@ -85,13 +81,7 @@ function DMXWeb() { } dmx.update(req.params.universe, req.body) - - 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}) + res.json({"state": dmx.universeAsObject(req.params.universe)}) }) app.post('/animation/:universe', function(req, res) { @@ -99,10 +89,7 @@ function DMXWeb() { var universe = dmx.universes[req.params.universe] // preserve old states - var old = {} - for(var i = 0; i < 256; i++) { - old[i] = universe.get(i) - } + var old = dmx.universeAsObject(req.params.universe) var animation = new A() for(var step in req.body) { @@ -126,11 +113,7 @@ function DMXWeb() { socket.on('request_refresh', function() { for(var universe in config.universes) { - var u = {} - for(var i = 0; i < 256; i++) { - u[i] = dmx.universes[universe].get(i) - } - socket.emit('update', universe, u) + socket.emit('update', universe, dmx.universeAsObject(req.params.universe)) } }) diff --git a/dmx.js b/dmx.js index d3258cf..395dcc1 100644 --- a/dmx.js +++ b/dmx.js @@ -36,6 +36,17 @@ DMX.prototype.update = function(universe, channels) { DMX.prototype.updateAll = function(universe, value) { this.universes[universe].updateAll(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