You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
144 lines
3.8 KiB
144 lines
3.8 KiB
const exec = require('child_process').exec
|
|
const path = require('path')
|
|
const async = require('async')
|
|
const splitLines = require('split-lines')
|
|
const mkdirp = require('mkdirp')
|
|
const fs = require('fs-extra')
|
|
|
|
require('colors')
|
|
|
|
var CFG = require('../config')
|
|
|
|
let tools = {}
|
|
|
|
function execGphoto2 (pArgs, pMore, pCallback) {
|
|
let args, more, callback
|
|
if (pCallback) {
|
|
args = pArgs
|
|
more = pMore
|
|
callback = pCallback
|
|
} else {
|
|
args = pArgs
|
|
more = {}
|
|
callback = pMore
|
|
}
|
|
exec(`gphoto2 ${args}`, more, function (pErr, pStdout, pStderr) {
|
|
if (pErr) {
|
|
// console.log(`[!] Please install gphoto2`.red)
|
|
// console.log(`sudo apt-get install gphoto2`.bgMagenta)
|
|
// console.log(`[-] Bobinoscope will close in 10 seconds...`)
|
|
// console.log('[!]', pErr)
|
|
callback(pErr)
|
|
// setTimeout(function () {
|
|
// process.exit(1)
|
|
// }, 10000)
|
|
return
|
|
}
|
|
let error = null
|
|
let outInLines = []
|
|
if (/Error/i.test(pStderr)) {
|
|
error = pStderr
|
|
}
|
|
if (pStdout) {
|
|
outInLines = splitLines(pStdout)
|
|
}
|
|
callback(error, outInLines)
|
|
})
|
|
}
|
|
|
|
tools.checkVersion = function (pCb) {
|
|
console.log('[-] Checking gphoto2 installation...'.bgBlue)
|
|
execGphoto2('--version', function (pErr, pStdout) {
|
|
if (pErr) {
|
|
console.log(`KO`.red)
|
|
} else {
|
|
console.log(`OK: ${pStdout[0]}`.green)
|
|
}
|
|
pCb(pErr)
|
|
})
|
|
}
|
|
|
|
tools.checkCameraConnection = function (pCb) {
|
|
console.log('[-] Checking camera connection...'.bgBlue)
|
|
execGphoto2('--auto-detect', function (pErr, pStdout) {
|
|
let device = pStdout && pStdout.length ? pStdout[2] : null
|
|
if (pErr || !device) {
|
|
console.log(`[!] Camera not connected`.red)
|
|
console.log('1. Connect camera via USB'.bgMagenta)
|
|
console.log('2. Turn on the camera'.bgMagenta)
|
|
console.log('3. Maybe - Force manual focus on camera'.bgMagenta)
|
|
pCb(pErr || 'CameraNotFound')
|
|
// console.log(`\n\n[-] Bobinoscope will close in 10 seconds...`)
|
|
// setTimeout(function () {
|
|
// process.exit(1)
|
|
// }, 10000)
|
|
// return
|
|
} else {
|
|
console.log(`OK: ${device}`.green)
|
|
pCb()
|
|
}
|
|
})
|
|
}
|
|
|
|
tools.__takeOnePicture = function (pPath, pCb) {
|
|
let lPath = path.parse(pPath)
|
|
execGphoto2(`--capture-image-and-download --filename=${lPath.base} --force-overwrite`, {cwd: lPath.dir}, pCb)
|
|
}
|
|
|
|
tools.takeOnePicture = function (pBoothId, pPictId, pCallback) {
|
|
let destPict = '/tmp/bobine.jpg'
|
|
var destPath = path.join(CFG.paths.original, pBoothId)
|
|
var pictOrig = path.join(destPath, CFG.pictNames[pPictId])
|
|
|
|
mkdirp.sync(path.join(CFG.paths.prebuilt, pBoothId))
|
|
|
|
tools.__takeOnePicture(destPict, function (pErr) {
|
|
// If error occurs
|
|
if (pErr) return pCallback(pErr)
|
|
mkdirp(destPath, function () {
|
|
fs.copy(destPict, pictOrig, function (pErr) {
|
|
return pCallback(pErr, pictOrig)
|
|
})
|
|
})
|
|
})
|
|
}
|
|
|
|
tools.init = function (pCb) {
|
|
let tasks = []
|
|
tasks.push(tools.checkVersion)
|
|
tasks.push(tools.checkCameraConnection)
|
|
tasks.push(function (pCb) {
|
|
let output = '/tmp/bobine-dummy.jpg'
|
|
console.log('[-] Taking dummy photo...'.bgBlue)
|
|
tools.__takeOnePicture(output, function (pErr) {
|
|
if (pErr) {
|
|
console.log(`[!] Failed to take picture`.red)
|
|
console.log(pErr.grey)
|
|
console.log('1. Add one SDCard into the device')
|
|
console.log('2. Take one picture manually')
|
|
} else {
|
|
console.log(`OK: ${output}`.green)
|
|
}
|
|
pCb(pErr)
|
|
})
|
|
|
|
// opn(output)
|
|
// .then(function () {
|
|
// console.log('OK'.green)
|
|
// pCb()
|
|
// })
|
|
})
|
|
|
|
async.waterfall(tasks, function (pErr) {
|
|
if (pErr) {
|
|
console.log('[!] Bobinoscope init failed... Retry in 10 seconds'.red)
|
|
setTimeout(function () {
|
|
tools.init(pCb)
|
|
}, 10000)
|
|
} else {
|
|
pCb()
|
|
}
|
|
})
|
|
}
|
|
|
|
module.exports = tools
|