var FileOnWrite = require('file-on-write');
var Camera = require('./mjpeg-camera');

var camera = new Camera({
  url: 'http://rax1.bsn.net/mjpg/video.mjpg?streamprofile=Balanced'
});

// camera.getScreenshot(function(err, screenshot) {
//   console.log(arguments);
// });

var writer = new FileOnWrite({
 path: './images',
 ext: '.jpg',
 filter: function(chunk) { 
   if (chunk && Buffer.isBuffer(chunk.data)) {
     console.log('valid chunk');
     return true; 
   }
   console.log('bad data', chunk);
   return false;
 },
 transform: function(chunk) {
   console.log(Date.now());
   return chunk.data;
 }
});

camera.pipe(writer);
camera.start();

camera.on('error', function(err) {
 console.trace();
 console.log('error:', err);
});

camera.on('close', function() {
 console.log('close');
});

camera.on('end', function() {
 console.log('end');
});

setTimeout(function() {
 console.log('grabbing screenshot...');
 camera.getScreenshot(function(err, screenshot) {
   console.log(screenshot);                   
 });
}, 3000);
