Skip to content
Snippets Groups Projects
Commit f8b2457f authored by Jan Grewe's avatar Jan Grewe
Browse files

try to download missing photos after receiving the first one and when reconnecting

parent d016fea8
Branches
No related tags found
No related merge requests found
...@@ -5,12 +5,17 @@ var pathPhotos = config.pathPhotos; ...@@ -5,12 +5,17 @@ var pathPhotos = config.pathPhotos;
var previewWidth = config.previewWidth; var previewWidth = config.previewWidth;
var previewHeight = config.previewHeight; var previewHeight = config.previewHeight;
var cardAddr = config.broadcastAddr; var cardAddr = config.broadcastAddr;
var pathPreviews = "./public/previews"; var cardPath = null;
var pathPreviews = './public/previews';
var itvPing = null; var itvPing = null;
var cardFound = false; var cardFound = false;
var alreadySearching = false; var alreadySearching = false;
var alreadyDownloading = false;
var downloadPrevious = true;
var downloadList = new Array();
var os = require('os');
var http = require('http'); var http = require('http');
var path = require('path'); var path = require('path');
var net = require('net'); var net = require('net');
...@@ -39,7 +44,7 @@ app.set('view engine', 'jade'); ...@@ -39,7 +44,7 @@ app.set('view engine', 'jade');
// uncomment after placing your favicon in /public // uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico')); //app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev')); //app.use(logger('dev'));
app.use(bodyParser.json()); app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false })); app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser()); app.use(cookieParser());
...@@ -93,13 +98,16 @@ console.log("#########################################"); ...@@ -93,13 +98,16 @@ console.log("#########################################");
findCard(); findCard();
function downloadPhoto(path) { function downloadPhotos() {
path = path.substr(5).replace(/\0/g, ''); if(alreadyDownloading == true || downloadList.length < 1)
var photo = path.split('/').pop(); return true;
alreadyDownloading = true;
var photo = downloadList.pop();
var localFile = pathPhotos+'/'+photo; var localFile = pathPhotos+'/'+photo;
var localPreview = pathPreviews+'/'+photo; var localPreview = pathPreviews+'/'+photo;
console.log('['+photo+'] Downloading from http://'+cardAddr+path); console.log('['+photo+'] Downloading from http://'+cardAddr+cardPath+'/'+photo);
var file = fs.createWriteStream(localFile); var file = fs.createWriteStream(localFile);
...@@ -109,6 +117,7 @@ function downloadPhoto(path) { ...@@ -109,6 +117,7 @@ function downloadPhoto(path) {
file.on('finish', function() { file.on('finish', function() {
file.close(); file.close();
alreadyDownloading = false;
console.log('['+photo+'] Saved as '+localFile); console.log('['+photo+'] Saved as '+localFile);
gm(localFile).autoOrient().resize(previewWidth, previewHeight).write(localPreview, function (err) { gm(localFile).autoOrient().resize(previewWidth, previewHeight).write(localPreview, function (err) {
...@@ -125,12 +134,22 @@ function downloadPhoto(path) { ...@@ -125,12 +134,22 @@ function downloadPhoto(path) {
}); });
if(downloadPrevious == true) {
getPhotoList();
}
if(downloadList.length > 0) {
downloadPhotos();
}else{
console.log("All photos downloaded, waiting for new ones...");
}
}); });
var options = { var options = {
hostname: cardAddr, hostname: cardAddr,
port: 80, port: 80,
path: path, path: cardPath+'/'+photo,
method: 'GET' method: 'GET'
}; };
...@@ -144,6 +163,52 @@ function downloadPhoto(path) { ...@@ -144,6 +163,52 @@ function downloadPhoto(path) {
} }
function getPhotoList() {
downloadPrevious = false;
var options = {
host: cardAddr,
port: 80,
path: '/cgi-bin/tslist?PATH=/www'+cardPath+'&keepfresh='+Date.now().toString()
};
http.get(options, function(resp){
console.log("Getting list of photos on card...");
resp.on('data', function(data){
var strFiles = data.toString().split(os.EOL)[2];
var regex = /FileName\d+=([a-zA-Z0-9_\.]+)&FileType\d+=File&/g;
var arrPhotos = new Array();
while (match = regex.exec(strFiles)) {
arrPhotos.push(match[1]);
}
var i = 0;
arrPhotos.forEach(function(photo) {
fs.exists(pathPhotos+'/'+photo, function(exists) {
if (exists) {
//console.log('['+photo+'] Photo '+photo+' already downloaded!');
}else{
console.log('['+photo+'] Photo '+photo+' not downloaded yet, adding to download list!');
downloadList.push(photo);
}
i++;
if(i == arrPhotos.length-1) {
if(downloadList.length > 0) {
downloadPhotos();
}else{
console.log("All photos already downloaded!");
}
}
});
});
});
}).on("error", function(e){
console.log("Error getting photo list: " + e.message);
getPhotoList();
});
}
function enableShootAndView(ip) { function enableShootAndView(ip) {
...@@ -153,6 +218,9 @@ function enableShootAndView(ip) { ...@@ -153,6 +218,9 @@ function enableShootAndView(ip) {
client.on('connect', function() { client.on('connect', function() {
console.log('Shoot & View enabled, waiting for photos...'); console.log('Shoot & View enabled, waiting for photos...');
if(cardPath != null) {
getPhotoList();
}
}); });
client.on('error', function(err) { client.on('error', function(err) {
...@@ -161,7 +229,11 @@ function enableShootAndView(ip) { ...@@ -161,7 +229,11 @@ function enableShootAndView(ip) {
}); });
client.on('data', function(data) { client.on('data', function(data) {
downloadPhoto(data.toString()); var path = data.toString().substr(5).replace(/\0/g, '');
var photo = path.split('/').pop();
cardPath = path.substring(0, path.lastIndexOf('/'));
downloadList.push(photo);
downloadPhotos();
}); });
client.on('end', function() { client.on('end', function() {
...@@ -188,6 +260,7 @@ function pingCard(ip) { ...@@ -188,6 +260,7 @@ function pingCard(ip) {
req.setTimeout(5000, function() { req.setTimeout(5000, function() {
cardFound = false; cardFound = false;
downloadPrevious = true;
console.log('Card has disappeared!'); console.log('Card has disappeared!');
req.destroy(); req.destroy();
clearInterval(itvPing); clearInterval(itvPing);
...@@ -239,7 +312,7 @@ function findCard() { ...@@ -239,7 +312,7 @@ function findCard() {
sendSearch(); sendSearch();
itvSearch = setInterval(function() { itvSearch = setInterval(function() {
sendSearch(); sendSearch();
}, 5000); }, 2000);
function sendSearch() { function sendSearch() {
socket.send(message, 0, message.length, 55777, cardAddr, function(err, bytes) { socket.send(message, 0, message.length, 55777, cardAddr, function(err, bytes) {
......
...@@ -3,7 +3,7 @@ var router = express.Router(); ...@@ -3,7 +3,7 @@ var router = express.Router();
/* GET home page. */ /* GET home page. */
router.get('/', function(req, res) { router.get('/', function(req, res) {
res.render('index', { title: 'Express' }); res.render('index', { title: 'WiPho' });
}); });
module.exports = router; module.exports = router;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment