const express = require('express') const router = express.Router() const fs = require('fs-extra') const gm = require('gm').subClass({ imageMagick: true }) const path = require('path') const gamepad = require('gamepad') require('colors') const gphoto2 = require('../tools/tools-gphoto2') const booth = require('../tools/tools-photobooth') const CFG = require('../config') const CTX = { __io: null, io: function () { return CTX.__io }, inProgress: 0, testPath: path.join(CFG.output, 'bobinoscope-test.jpg') } booth.init() setGamepad() // router.use(function (req, res, next) { // console.log(req.url) // next() // }) // //////////////////////////////////////////////////////////////////////////// // DSLR router.get('/dslr/takepicture/:boothId/:pictId', function (req, res, next) { // return res.json({}) gphoto2.takeOnePicture(req.params.boothId, req.params.pictId, function (pErr) { let error = typeof pErr === 'string' ? pErr : JSON.stringify(pErr) if (pErr) console.error(pErr) res.json({ error: error, data: null }) }) }) router.get('/build/:boothId', function (req, res, next) { console.log('Building') // res.json({ok: 1}) CTX.inProgress += 1 CTX.io().emit('boothState') booth.buildBooth(req.params.boothId, function (pErr) { CTX.io().emit('boothState') CTX.inProgress -= 1 res.json({ error: pErr, data: null, boothId: req.params.boothId }) }) }) router.use('/build/result/low', express.static(CFG.paths.final_ld)) router.use('/build/result', express.static(CFG.paths.final)) // //////////////////////////////////////////////////////////////////////////// // PRINTER router.get('/list/:from', function (req, res, next) { booth.getBoothList(req.params.from, function (pErr, pList) { res.json({error: pErr, booths: pList, inProgress: CTX.inProgress}) }) }) router.get('/toprint', function (req, res, next) { var todoPath = path.join(CFG.paths.toprint) fs.readdir(todoPath, function (pErr, pList) { res.json({error: pErr, todos: pList}) }) }) router.get('/print/:pictId', function (req, res, next) { let srcPath = path.join(CFG.paths.final, req.params.pictId) let dstPath = path.join(CFG.paths.toprint, req.params.pictId) fs.copy(srcPath, dstPath, function (pErr) { res.json({error: pErr}) }) }) // //////////////////////////////////////////////////////////////////////////// // TEST router.get('/test/takepicture', function (req, res, next) { let tmpPict = path.join(CFG.output, 'bobinoscope-test.tmp.jpg') gphoto2.__takeOnePicture(tmpPict, function (pErr) { if (pErr) return res.status(500).send(pErr) gm(tmpPict) .autoOrient() .gravity('Center') .write(CTX.testPath, function (pErr) { if (pErr) console.log('Failed to resize picture', pErr) res.json({ok: 1}) }) }) }) router.get('/test/takepicture/result', function (req, res, next) { res.sendFile(path.join(CFG.output, 'bobinoscope-test.jpg')) }) router.setSocketIo = function (pIo) { CTX.__io = pIo } function setGamepad () { // Initialize the library gamepad.init() // List the state of all currently attached devices // for (var i = 0, l = gamepad.numDevices(); i < l; i++) { // console.log(i, gamepad.deviceAtIndex(i)) // } if (!gamepad.numDevices()) { console.log('[!] No gamepad found'.yellow) } else { console.log(`[!] ${gamepad.numDevices()} gamepad(s) found`.bgBlue) // Create a game loop and poll for events setInterval(gamepad.processEvents, 16) // Scan for new gamepads as a slower rate setInterval(gamepad.detectDevices, 500) // Listen for button down events on all gamepads gamepad.on('down', function (pId, pNum) { console.log(`[!] gamepad action received | ${pId}:${pNum}`.bgBlue) CTX.io().emit('boothClick', { id: pId, num: pNum }) }) } } module.exports = router