Skip to content
This repository has been archived by the owner on Apr 17, 2019. It is now read-only.

Commit

Permalink
Moving around the node server.. and...
Browse files Browse the repository at this point in the history
 - New capture script
 - Hook script now publishes to Redis
 - Node server almost is publishing to socket.io
   clients when Redis messages are send (pubsub)
  • Loading branch information
justinrainbow committed Oct 9, 2011
1 parent 1acddef commit 8631048
Show file tree
Hide file tree
Showing 30 changed files with 260 additions and 153 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Expand Up @@ -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
133 changes: 133 additions & 0 deletions 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);

1 change: 1 addition & 0 deletions autoload.php
Expand Up @@ -8,6 +8,7 @@
$loader->registerNamespaces(array(
'Symfony' => __DIR__.'/vendor',
'Photobooth'=> __DIR__.'/src',
'Predis' => __DIR__.'/vendor/predis/lib',
));

$loader->register();
24 changes: 24 additions & 0 deletions capture.php
@@ -0,0 +1,24 @@
#!/usr/bin/env php
<?php

require __DIR__.'/autoload.php';

use Symfony\Component\Process\Process;
$redis = new Predis\Client();

$strip_id = sha1(microtime(true));

$env = $_ENV;
$cmd = sprintf(
'STRIP_ID=%s gphoto2 --capture-image-and-download --interval 1 --frames 3 --hook-script %s',
$strip_id, escapeshellarg( __DIR__.'/hook.php')
);
$dir = __DIR__.'/tools/raw/' . $strip_id;
mkdir($dir);

$process = new Process($cmd, $dir);
$process->run(function($type, $data) {
echo $data;
});


30 changes: 24 additions & 6 deletions hook.php
@@ -1,20 +1,38 @@
#!/usr/bin/env php
<?php

file_put_contents(__DIR__.'/hook.log', print_r($_SERVER, true), FILE_APPEND);
require __DIR__.'/autoload.php';

use Symfony\Component\Process\Process;
$redis = new Predis\Client();


// file_put_contents(__DIR__.'/hook.log', print_r($_SERVER, true), FILE_APPEND);

$strip_id = $_SERVER['STRIP_ID'];


switch ($_SERVER['ACTION']) {
case 'init':
break;

case 'start':
case 'stop':
$redis->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;
}

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
32 changes: 32 additions & 0 deletions 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;
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
7 changes: 5 additions & 2 deletions 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
93 changes: 0 additions & 93 deletions server/app.js

This file was deleted.

32 changes: 0 additions & 32 deletions server/public/stylesheets/bootstrap.less

This file was deleted.

0 comments on commit 8631048

Please sign in to comment.