From 9f16f2ed67b1c0b632135eee8d41f32ba844130e Mon Sep 17 00:00:00 2001 From: Max Stockner Date: Tue, 30 Jul 2013 20:24:45 +0200 Subject: [PATCH 1/4] add foursquare callback for a simple animation demo --- web.js | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/web.js b/web.js index 3708eac..51db2df 100644 --- a/web.js +++ b/web.js @@ -1,7 +1,8 @@ var http = require('http'), path = require('path'), io = require('socket.io'), - fs = require('fs') + fs = require('fs'), + A = require('./anim.js').Anim ; exports.init = function(dmx) { @@ -13,7 +14,34 @@ exports.init = function(dmx) { }); request.on("end", function () { - var filePath = '.' + request.url; + var urlData = require('url').parse(request.url); + + if(urlData.pathname == '/foursquare_checkin') { + response.end(); + + // save old states + var universe = 'office', old = {}, black = {}; + for(var i = 0; i < 256; i++) { + old[i] = dmx.drivers[universe].get(i); + } + for(var i = 0; i < 256; i++) { + black[i] = 0; + } + + var x = new A(dmx.drivers[universe]) + .add(black, 1000) + .delay(2000) + .add({ 0:16, 1:255, 2:0, 3:255, 4: 39, 5:0, 15: 1, 16:255, 17:0, 18:255, 19: 255, 20:0, 21:0, 22: 0, 23:0, 24:128, 25: 0, 26:255, 31:255, 32: 60 }, 1000) + .delay(2000) + .add(black, 1000) + .delay(2000) + .add(old, 1000) + + x.run(function () {}); + return; + } + + var filePath = '.' + urlData.pathname; if (filePath == './') filePath = './index.html'; From 645773b09cb428932911e03ff2d3db432ecb0269 Mon Sep 17 00:00:00 2001 From: Max Stockner Date: Wed, 31 Jul 2013 11:54:18 +0200 Subject: [PATCH 2/4] change animation api: set universe when calling run --- anim.js | 5 ++--- test.js | 8 ++++---- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/anim.js b/anim.js index 237eeaa..ce5efac 100644 --- a/anim.js +++ b/anim.js @@ -1,8 +1,7 @@ var ease = require('./easing.js').ease var resolution = 25; -exports.Anim = function(universe) { - this.universe = universe; +exports.Anim = function() { this.fx_stack = []; this.add = function(to, duration, options) { var options = options || {}; @@ -14,7 +13,7 @@ exports.Anim = function(universe) { this.delay = function(duration) { return this.add({}, duration); } - this.run = function(onFinish) { + this.run = function(universe, onFinish) { var config = {} , t = 0 , d = 0 diff --git a/test.js b/test.js index 265338d..e671a2e 100644 --- a/test.js +++ b/test.js @@ -43,7 +43,7 @@ green_water(universe, [3+15, 6+15, 9+15], 4000); return; -var x = new A(universe) +var x = new A() .add({1: 255, 6: 110, 7: 255, 8: 10}, 1200) .delay(1000) .add({1: 0}, 600) @@ -71,8 +71,8 @@ var x = new A(universe) .delay(200) .add({2: 0}); -var y = new A(universe) +var y = new A() .add({9: 255}, 10000); -x.run(done); -y.run(done); \ No newline at end of file +x.run(universe, done); +y.run(universe, done); \ No newline at end of file From 70d960667a297e2f810696ce54cd929bc62751c6 Mon Sep 17 00:00:00 2001 From: Max Stockner Date: Wed, 31 Jul 2013 12:28:45 +0200 Subject: [PATCH 3/4] add web api for posting stuff --- web.js | 48 +++++++++++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/web.js b/web.js index 51db2df..7695277 100644 --- a/web.js +++ b/web.js @@ -14,30 +14,32 @@ exports.init = function(dmx) { }); request.on("end", function () { - var urlData = require('url').parse(request.url); + var urlData = require('url').parse(request.url), + urlPath = urlData.pathname.split('/'); - if(urlData.pathname == '/foursquare_checkin') { + if(urlPath.length == 3 && urlPath[1] == 'animation') { + try { + // save old states + var universe = dmx.drivers[urlPath[2]], old = {}, black = {}; + for(var i = 0; i < 256; i++) { + old[i] = universe.get(i); + black[i] = 0; + } + + var jsonAnim = JSON.parse(reqBody), animation = new A(); + for(var step in jsonAnim) { + animation.add(jsonAnim[step].to, jsonAnim[step].duration || 0, jsonAnim[step].options || {}); + } + + animation.add(old, 0); + animation.run(universe); + response.write('{ "success": true }'); + } catch(e) { + response.write('{ "error": "broken json" }'); + } + response.end(); - // save old states - var universe = 'office', old = {}, black = {}; - for(var i = 0; i < 256; i++) { - old[i] = dmx.drivers[universe].get(i); - } - for(var i = 0; i < 256; i++) { - black[i] = 0; - } - - var x = new A(dmx.drivers[universe]) - .add(black, 1000) - .delay(2000) - .add({ 0:16, 1:255, 2:0, 3:255, 4: 39, 5:0, 15: 1, 16:255, 17:0, 18:255, 19: 255, 20:0, 21:0, 22: 0, 23:0, 24:128, 25: 0, 26:255, 31:255, 32: 60 }, 1000) - .delay(2000) - .add(black, 1000) - .delay(2000) - .add(old, 1000) - - x.run(function () {}); return; } @@ -79,8 +81,8 @@ exports.init = function(dmx) { var app = http.createServer(handler) app.listen(dmx.config.port, '::', null, function() { try { - process.setgid(dmx.config.gid); - process.setuid(dmx.config.uid); + //process.setgid(dmx.config.gid); + //process.setuid(dmx.config.uid); } catch (err) { console.log(err); process.exit(1); From 3ffa9f45b8fa86075d572b81dc12af6ab7f194c2 Mon Sep 17 00:00:00 2001 From: Max Stockner Date: Wed, 31 Jul 2013 12:32:29 +0200 Subject: [PATCH 4/4] uncomment priviledge drop --- web.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/web.js b/web.js index 7695277..51e582f 100644 --- a/web.js +++ b/web.js @@ -81,8 +81,8 @@ exports.init = function(dmx) { var app = http.createServer(handler) app.listen(dmx.config.port, '::', null, function() { try { - //process.setgid(dmx.config.gid); - //process.setuid(dmx.config.uid); + process.setgid(dmx.config.gid); + process.setuid(dmx.config.uid); } catch (err) { console.log(err); process.exit(1);