var BOB_mod_express = require('express'); var BOB_mod_router = BOB_mod_express.Router(); var BOB_mod_path = require('path'); var BOB_mod_mkdirp = require('mkdirp'); var BOB_mod_os = require('os'); var BOB_mod_fs = require('fs'); // var BOB_mod_fsExtra = require('fs-extra') var BOB_tol_gphotos = null; var BOB_tol_booth = null; var BOB_cfg_config = null; var BOB_var_boothInProgress = 0; var BOB_var_gamepad = require("gamepad"); var BOB_var_ipAddress = '127.0.0.1'; var BOB_var_path = "/home/cocoon/workspace/bobinoscope"; var BOB_var_killZone = { top : 2, bottom : 2, left : 25, right : 25 } BOB_mod_mkdirp.sync(BOB_var_path+"/public/printer/todo"); BOB_mod_mkdirp.sync(BOB_var_path+"/public/printer/done"); /* GET home page. */ BOB_mod_router.get('/bobinoscope', function(req, res, next) { res.render('index', { serverIpAddress: BOB_var_ipAddress, killZone : BOB_var_killZone }); }); /* GET view page. */ BOB_mod_router.get('/', function(req, res, next) { res.render('view', {serverIpAddress: BOB_var_ipAddress}); }); BOB_mod_router.get('/booth/build/:boothId', function(req, res, next) { BOB_var_boothInProgress += 1; BOB_cfg_config.io.emit('boothState'); BOB_tol_booth.buildBooth(req.params.boothId, function(pErr) { BOB_cfg_config.io.emit('boothState'); BOB_var_boothInProgress -= 1; res.json({ error: pErr, data : null, boothId: req.params.boothId}); }); }) BOB_mod_router.get('/booth/list/:from', function(req, res, next) { BOB_tol_booth.getBoothList(req.params.from, function(pErr, pList) { res.json({error: pErr, booths: pList, inProgress : BOB_var_boothInProgress}); }) }) /* GET home page. */ BOB_mod_router.get('/dslr/takepicture/:boothId/:pictId', function(req, res, next) { // BOB_cfg_config.io.emit('boothState'); BOB_tol_gphotos.takePicture(req.params.boothId, req.params.pictId, function(pErr) { res.json({ error: pErr, data : null}); }) }); /* GET view page. */ BOB_mod_router.get('/download/final/hd/:pictId', function(req, res, next) { res.download(BOB_var_path+'/public/img/final/'+req.params.pictId); }); /* GET view page. */ BOB_mod_router.get('/print/bobinogramme/:pictId', function(req, res, next) { var srcPath = BOB_mod_path.join(BOB_var_path, '/public/img/final/'+req.params.pictId); var dstPath = BOB_mod_path.join(BOB_var_path, '/public/printer/todo/'+req.params.pictId); copyFile(srcPath, dstPath, function(pErr) { res.json({error: pErr}) }) }); BOB_mod_router.get('/print/list/todo', function(req, res, next) { var todoPath = BOB_mod_path.join(BOB_var_path, '/public/printer/todo'); BOB_mod_fs.readdir(todoPath, function(pErr, pList) { res.json({error: pErr, todos: pList}) }) }) function copyFile(source, target, cb) { var cbCalled = false; var rd = BOB_mod_fs.createReadStream(source); rd.on("error", function(err) { done(err); }); var wr = BOB_mod_fs.createWriteStream(target); wr.on("error", function(err) { done(err); }); wr.on("close", function(ex) { done(); }); rd.pipe(wr); function done(err) { if (!cbCalled) { cb(err); cbCalled = true; } } } // Initialize the library BOB_var_gamepad.init() // List the state of all currently attached devices for (var i = 0, l = BOB_var_gamepad.numDevices(); i < l; i++) { console.log(i, BOB_var_gamepad.deviceAtIndex(i)); } // Create a game loop and poll for events setInterval(BOB_var_gamepad.processEvents, 16); // Scan for new gamepads as a slower rate setInterval(BOB_var_gamepad.detectDevices, 500); // Listen for button down events on all gamepads BOB_var_gamepad.on("down", function (pId, pNum) { BOB_cfg_config.io.emit('boothClick', { id : pId, num : pNum }); }) // Get local server ip address var ifaces = BOB_mod_os.networkInterfaces(); Object.keys(ifaces).forEach(function (ifname) { var alias = 0; ifaces[ifname].forEach(function (iface) { if ('IPv4' !== iface.family || iface.internal !== false) { // skip over internal (i.e. 127.0.0.1) and non-ipv4 addresses return; } BOB_var_ipAddress = iface.address; if (alias >= 1) { // this single interface has multiple ipv4 addresses console.log(ifname + ':' + alias, iface.address); } else { // this interface has only one ipv4 adress console.log(ifname, iface.address); } }); }); module.exports = function(pConfig) { BOB_cfg_config = pConfig; BOB_tol_gphotos = require('../tools/tools-gphoto2')(pConfig); BOB_tol_booth = require('../tools/tools-photobooth')(pConfig); return BOB_mod_router; }