From 86310486ad43871854af6b3bc115fea41357d753 Mon Sep 17 00:00:00 2001 From: Justin Rainbow Date: Sun, 9 Oct 2011 14:52:00 -0700 Subject: [PATCH] Moving around the node server.. and... - New capture script - Hook script now publishes to Redis - Node server almost is publishing to socket.io clients when Redis messages are send (pubsub) --- .gitmodules | 3 + app.js | 133 ++++++++++++++++++ autoload.php | 1 + capture.php | 24 ++++ hook.php | 30 +++- server/package.json => package.json | 0 {server/public => public}/js/jquery.min.js | 0 .../stylesheets/bootstrap.css | 0 public/stylesheets/bootstrap.less | 32 +++++ .../public => public}/stylesheets/forms.less | 0 .../public => public}/stylesheets/mixins.less | 0 .../stylesheets/patterns.less | 0 .../public => public}/stylesheets/reset.less | 0 .../stylesheets/scaffolding.less | 0 .../public => public}/stylesheets/style.css | 0 .../public => public}/stylesheets/style.less | 0 .../public => public}/stylesheets/tables.less | 0 .../public => public}/stylesheets/type.less | 0 .../stylesheets/variables.less | 0 run.sh | 7 +- server/app.js | 93 ------------ server/public/stylesheets/bootstrap.less | 32 ----- server/views/index.ejs | 13 -- src/Photobooth/Command/BaseCommand.php | 2 +- .../Command/CapturePhotosCommand.php | 2 +- src/Photobooth/Process/CaptureCommand.php | 10 +- vendor/predis | 1 + {server/views => views}/config.ejs | 0 views/index.ejs | 30 ++++ {server/views => views}/layout.ejs | 0 30 files changed, 260 insertions(+), 153 deletions(-) create mode 100644 app.js create mode 100755 capture.php rename server/package.json => package.json (100%) rename {server/public => public}/js/jquery.min.js (100%) rename {server/public => public}/stylesheets/bootstrap.css (100%) create mode 100644 public/stylesheets/bootstrap.less rename {server/public => public}/stylesheets/forms.less (100%) rename {server/public => public}/stylesheets/mixins.less (100%) rename {server/public => public}/stylesheets/patterns.less (100%) rename {server/public => public}/stylesheets/reset.less (100%) rename {server/public => public}/stylesheets/scaffolding.less (100%) rename {server/public => public}/stylesheets/style.css (100%) rename {server/public => public}/stylesheets/style.less (100%) rename {server/public => public}/stylesheets/tables.less (100%) rename {server/public => public}/stylesheets/type.less (100%) rename {server/public => public}/stylesheets/variables.less (100%) delete mode 100644 server/app.js delete mode 100644 server/public/stylesheets/bootstrap.less delete mode 100644 server/views/index.ejs create mode 160000 vendor/predis rename {server/views => views}/config.ejs (100%) create mode 100644 views/index.ejs rename {server/views => views}/layout.ejs (100%) diff --git a/.gitmodules b/.gitmodules index 324d7bf..926a8cb 100644 --- a/.gitmodules +++ b/.gitmodules @@ -10,3 +10,6 @@ [submodule "vendor/Symfony/Component/Finder"] path = vendor/Symfony/Component/Finder url = https://github.com/symfony/Finder.git +[submodule "vendor/predis"] + path = vendor/predis + url = https://github.com/nrk/predis.git diff --git a/app.js b/app.js new file mode 100644 index 0000000..a31b190 --- /dev/null +++ b/app.js @@ -0,0 +1,133 @@ + +/** + * Module dependencies. + */ +var express = require('express'), + fs = require('fs'), + spawn = require('child_process').spawn, + + app = module.exports = express.createServer(), + io = require('socket.io').listen(app), + db = require('redis').createClient(), + photobooth, + cfg; + +// Configuration + +app.configure(function(){ + app.set('views', __dirname + '/views'); + app.set('view engine', 'ejs'); + app.use(express.bodyParser()); + app.use(express.methodOverride()); + app.use(express.compiler({ src: __dirname + '/public', enable: ['less'] })); + app.use(app.router); + app.use(express.static(__dirname + '/public')); +}); + +app.configure('development', function(){ + app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); +}); + +app.configure('production', function(){ + app.use(express.errorHandler()); +}); + +// database + +db.on('connected', function () { + db.connected = true; +}); + +db.on("message", function (channel, message) { + console.log("Message: "+channel+" - "+message); +}); +db.subscribe("booth:capture"); + + +// Routes + +app.get('/', function(req, res){ + res.render('index', { + title: 'PhotoBooth' + }); +}); + +function takePhotos(cb) { + photobooth = spawn('./capture.php', [], { + cwd: __dirname + }); + + photobooth.stderr.on('data', function (data) { + console.log("stderr: " + data.toString()) + }) + + photobooth.on('exit', function (data) { + photobooth = false; + cb(); + }); + + photobooth.stdout.on('data', function (data) { + console.log("stdout: " + data.toString()); + }); + + photobooth.stdin.end(); +} + +app.get('/snap', function (req, res) { + db.exists('photolock', function (err, response) { + if (!response) { + db.multi() + .set('photolock', 1) + .expire('photolock', 60) // only lock the process for 60 seconds + .exec(); + + takePhotos(function () { + db.del('photolock'); + }); + + res.json({ type: "success" }); + } else { + res.json({ type: "failure", msg: "Already running" }); + } + }); +}); + +app.get('/config', function (req, res) { + var cfg = JSON.parse(fs.readFileSync(__dirname+"/config.json")); + + findPrinters(function(printers) { + res.render('config', { + title: 'PhotoBooth Config', + cfg: cfg, + printers: printers + }); + }); +}); + +app.post('/config', function (req, res) { + fs.writeFile(__dirname+"/config.json", JSON.stringify(req.body)); + + res.json({ msg: "Saved" }); +}); + +io.sockets.on('connection', function (socket) { + socket.emit('news', { hello: 'world' }); + socket.on('my other event', function (data) { + console.log(data); + }); +}); + +function findPrinters(cb) { + var printers = spawn(__dirname+"/printers.php"); + printers.on('exit', function (data) { + printers = false; + }); + printers.stdout.on('data', function (data) { + cb(JSON.parse(data.toString())); + }); + printers.stdin.end(); +}; + +app.listen(3000); +console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env); + diff --git a/autoload.php b/autoload.php index 4c64590..e2dafaa 100644 --- a/autoload.php +++ b/autoload.php @@ -8,6 +8,7 @@ $loader->registerNamespaces(array( 'Symfony' => __DIR__.'/vendor', 'Photobooth'=> __DIR__.'/src', + 'Predis' => __DIR__.'/vendor/predis/lib', )); $loader->register(); \ No newline at end of file diff --git a/capture.php b/capture.php new file mode 100755 index 0000000..0ca3a9d --- /dev/null +++ b/capture.php @@ -0,0 +1,24 @@ +#!/usr/bin/env php +run(function($type, $data) { + echo $data; +}); + + diff --git a/hook.php b/hook.php index abfbb1a..8320cb3 100755 --- a/hook.php +++ b/hook.php @@ -1,20 +1,38 @@ #!/usr/bin/env php publish('booth:capture', json_encode(array( + 'action' => $_SERVER['ACTION'], + 'id' => $strip_id + ))); + break; case 'download': - $file = $_SERVER['ARGUMENT']; - break; + $file = realpath(getcwd().'/'.$_SERVER['ARGUMENT']); - case 'stop': + $redis->publish('booth:capture', json_encode(array( + 'action' => $_SERVER['ACTION'], + 'file' => $file, + 'id' => $strip_id + ))); + + $redis->rpush('photos:'.$strip_id, $file); break; } diff --git a/server/package.json b/package.json similarity index 100% rename from server/package.json rename to package.json diff --git a/server/public/js/jquery.min.js b/public/js/jquery.min.js similarity index 100% rename from server/public/js/jquery.min.js rename to public/js/jquery.min.js diff --git a/server/public/stylesheets/bootstrap.css b/public/stylesheets/bootstrap.css similarity index 100% rename from server/public/stylesheets/bootstrap.css rename to public/stylesheets/bootstrap.css diff --git a/public/stylesheets/bootstrap.less b/public/stylesheets/bootstrap.less new file mode 100644 index 0000000..3c1d0d4 --- /dev/null +++ b/public/stylesheets/bootstrap.less @@ -0,0 +1,32 @@ +/*! + * Bootstrap @VERSION + * + * Copyright 2011 Twitter, Inc + * Licensed under the Apache License v2.0 + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Designed and built with all the love in the world @twitter by @mdo and @fat. + * Date: @DATE + */ + +// CSS Reset +@import "/public/stylesheets/reset.less"; + +// Core variables and mixins +@import "/public/stylesheets/variables.less"; // Modify this for custom colors, font-sizes, etc +@import "/public/stylesheets/mixins.less"; + +// Grid system and page structure +@import "/public/stylesheets/scaffolding.less"; + +// Styled patterns and elements +@import "/public/stylesheets/type.less"; +@import "/public/stylesheets/forms.less"; +@import "/public/stylesheets/tables.less"; +@import "/public/stylesheets/patterns.less"; + + +form { + background: #fff; + text-align: left; +} \ No newline at end of file diff --git a/server/public/stylesheets/forms.less b/public/stylesheets/forms.less similarity index 100% rename from server/public/stylesheets/forms.less rename to public/stylesheets/forms.less diff --git a/server/public/stylesheets/mixins.less b/public/stylesheets/mixins.less similarity index 100% rename from server/public/stylesheets/mixins.less rename to public/stylesheets/mixins.less diff --git a/server/public/stylesheets/patterns.less b/public/stylesheets/patterns.less similarity index 100% rename from server/public/stylesheets/patterns.less rename to public/stylesheets/patterns.less diff --git a/server/public/stylesheets/reset.less b/public/stylesheets/reset.less similarity index 100% rename from server/public/stylesheets/reset.less rename to public/stylesheets/reset.less diff --git a/server/public/stylesheets/scaffolding.less b/public/stylesheets/scaffolding.less similarity index 100% rename from server/public/stylesheets/scaffolding.less rename to public/stylesheets/scaffolding.less diff --git a/server/public/stylesheets/style.css b/public/stylesheets/style.css similarity index 100% rename from server/public/stylesheets/style.css rename to public/stylesheets/style.css diff --git a/server/public/stylesheets/style.less b/public/stylesheets/style.less similarity index 100% rename from server/public/stylesheets/style.less rename to public/stylesheets/style.less diff --git a/server/public/stylesheets/tables.less b/public/stylesheets/tables.less similarity index 100% rename from server/public/stylesheets/tables.less rename to public/stylesheets/tables.less diff --git a/server/public/stylesheets/type.less b/public/stylesheets/type.less similarity index 100% rename from server/public/stylesheets/type.less rename to public/stylesheets/type.less diff --git a/server/public/stylesheets/variables.less b/public/stylesheets/variables.less similarity index 100% rename from server/public/stylesheets/variables.less rename to public/stylesheets/variables.less diff --git a/run.sh b/run.sh index 01c2647..490727c 100755 --- a/run.sh +++ b/run.sh @@ -1,7 +1,10 @@ #!/bin/bash -./console photos:capture -./console photos:process -o strip.jpg tools/raw/ +DIR=`dirname $0` + +# ./console photos:capture $DIR/tools/raw/ $DIR/hook.php + +./console photos:process -o $DIR/strip.jpg $DIR/tools/raw/ # lp -d "C3400__192_168_100_101____MiniMax-2" -o page-top=0 -o page-bottom=0 -o scaling=100 strip.jpg # lp -d "HP_Color_LaserJet_CM3530_MFP" strip.jpg diff --git a/server/app.js b/server/app.js deleted file mode 100644 index 1870eca..0000000 --- a/server/app.js +++ /dev/null @@ -1,93 +0,0 @@ - -/** - * Module dependencies. - */ - -var express = require('express'), - fs = require('fs'), - spawn = require('child_process').spawn, - - app = module.exports = express.createServer(), - io = require('socket.io').listen(app), - photobooth; - -// Configuration - -app.configure(function(){ - app.set('views', __dirname + '/views'); - app.set('view engine', 'ejs'); - app.use(express.bodyParser()); - app.use(express.methodOverride()); - app.use(express.compiler({ src: __dirname + '/public', enable: ['less'] })); - app.use(app.router); - app.use(express.static(__dirname + '/public')); -}); - -app.configure('development', function(){ - app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); -}); - -app.configure('production', function(){ - app.use(express.errorHandler()); -}); - -// Routes - -app.get('/', function(req, res){ - res.render('index', { - title: 'PhotoBooth' - }); -}); - -app.get('/snap', function (req, res) { - if (!photobooth) { - photobooth = spawn(__dirname+'/../run.sh'); - photobooth.on('exit', function (data) { - photobooth = false; - }); - photobooth.stdout.on('data', function (data) { - console.log(data.toString()); - }); - } - - res.send(); -}); - -app.get('/config', function (req, res) { - var cfg = JSON.parse(fs.readFileSync(__dirname+"/../config.json")); - - findPrinters(function(printers) { - res.render('config', { - title: 'PhotoBooth Config', - cfg: cfg, - printers: printers - }); - }); -}); - -app.post('/config', function (req, res) { - fs.writeFile(__dirname+"/../config.json", JSON.stringify(req.body)); - res.json({ msg: "Saved" }, { 'Content-Type': 'application/json' }); -}); - -io.sockets.on('connection', function (socket) { - socket.emit('news', { hello: 'world' }); - socket.on('my other event', function (data) { - console.log(data); - }); -}); - -function findPrinters(cb) { - var printers = spawn(__dirname+"/../printers.php"); - printers.on('exit', function (data) { - printers = false; - }); - printers.stdout.on('data', function (data) { - cb(JSON.parse(data.toString())); - }); - printers.stdin.end(); -}; - -app.listen(3000); -console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env); - diff --git a/server/public/stylesheets/bootstrap.less b/server/public/stylesheets/bootstrap.less deleted file mode 100644 index 7b0ec36..0000000 --- a/server/public/stylesheets/bootstrap.less +++ /dev/null @@ -1,32 +0,0 @@ -/*! - * Bootstrap @VERSION - * - * Copyright 2011 Twitter, Inc - * Licensed under the Apache License v2.0 - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Designed and built with all the love in the world @twitter by @mdo and @fat. - * Date: @DATE - */ - -// CSS Reset -@import "/server/public/stylesheets/reset.less"; - -// Core variables and mixins -@import "/server/public/stylesheets/variables.less"; // Modify this for custom colors, font-sizes, etc -@import "/server/public/stylesheets/mixins.less"; - -// Grid system and page structure -@import "/server/public/stylesheets/scaffolding.less"; - -// Styled patterns and elements -@import "/server/public/stylesheets/type.less"; -@import "/server/public/stylesheets/forms.less"; -@import "/server/public/stylesheets/tables.less"; -@import "/server/public/stylesheets/patterns.less"; - - -form { - background: #fff; - text-align: left; -} \ No newline at end of file diff --git a/server/views/index.ejs b/server/views/index.ejs deleted file mode 100644 index 67ede22..0000000 --- a/server/views/index.ejs +++ /dev/null @@ -1,13 +0,0 @@ - - Just Take It - - - - - \ No newline at end of file diff --git a/src/Photobooth/Command/BaseCommand.php b/src/Photobooth/Command/BaseCommand.php index 9cdb93e..66b20f8 100644 --- a/src/Photobooth/Command/BaseCommand.php +++ b/src/Photobooth/Command/BaseCommand.php @@ -4,7 +4,7 @@ use Symfony\Component\Console\Command\Command; -abstract BaseCommand extends Command +abstract class BaseCommand extends Command { public function getConfig() { diff --git a/src/Photobooth/Command/CapturePhotosCommand.php b/src/Photobooth/Command/CapturePhotosCommand.php index 92f46fe..709939a 100644 --- a/src/Photobooth/Command/CapturePhotosCommand.php +++ b/src/Photobooth/Command/CapturePhotosCommand.php @@ -11,7 +11,7 @@ use Symfony\Component\Process\Process; use Symfony\Component\Process\ExecutableFinder; -use PhotoBooth\Process\CaptureCommand; +use Photobooth\Process\CaptureCommand; class CapturePhotosCommand extends BaseCommand { diff --git a/src/Photobooth/Process/CaptureCommand.php b/src/Photobooth/Process/CaptureCommand.php index 6bdc6a1..80d58e8 100644 --- a/src/Photobooth/Process/CaptureCommand.php +++ b/src/Photobooth/Process/CaptureCommand.php @@ -24,13 +24,13 @@ public function getCommand() $value = $this->{$var}; if (!empty($value)) { - $options[$var] = escapeshellarg($var); + $options[$var] = is_numeric($value) ? $value : escapeshellarg($value); } } - if (null !== $this->hook) { - $options['hook-script'] = escapeshellarg($this->hook); - } + // if (null !== $this->hook) { + // $options['hook-script'] = escapeshellarg($this->hook); + // } $args = array(); foreach ($options AS $name => $value) { @@ -41,7 +41,7 @@ public function getCommand() $gphoto2 = $finder->find('gphoto2'); $cmd = $gphoto2 . ' --capture-image-and-download ' . join(" ", $args); - + echo $cmd,"\n"; return $cmd; } } \ No newline at end of file diff --git a/vendor/predis b/vendor/predis new file mode 160000 index 0000000..e070f83 --- /dev/null +++ b/vendor/predis @@ -0,0 +1 @@ +Subproject commit e070f83dc50d0b333a314133e81b9196b56475b8 diff --git a/server/views/config.ejs b/views/config.ejs similarity index 100% rename from server/views/config.ejs rename to views/config.ejs diff --git a/views/index.ejs b/views/index.ejs new file mode 100644 index 0000000..63e5400 --- /dev/null +++ b/views/index.ejs @@ -0,0 +1,30 @@ + + + + + \ No newline at end of file diff --git a/server/views/layout.ejs b/views/layout.ejs similarity index 100% rename from server/views/layout.ejs rename to views/layout.ejs