var twitter = require('../services/twitter.js');
var request = require('request');
var fs = require('fs');
var logger = require('../services/logger.js');

var config;

var buildUrl = function (ip, uri, session) {
    return 'http://' + ip + uri.replace('{SESSION}', session);
};

var download = function (uri, filename, callback) {
    request.get(uri)
        .on('error', function (error) {
            console.log(error);
            fs.unlinkSync(filename);
        })
        .pipe(fs.createWriteStream(filename)).on('close', callback);
};

var getImageStore = function (camera) {
    return 'tmp/camera' + camera.ip + '.png';
};

var login = function (camera) {
    var session = camera.session;

    if (!session) {
        // TODO Login
    }

    return session;
};

var init = function (cfg) {
    config = cfg || {};

    twitter.init(config.services.twitter);
    logger.init(config);
};

var run = function () {
    if (!config) {
        console.log('WEBCAM', 'No configuration present');
        return;
    }

    for (var cameraId in config.cameras) {
        var camera = config.cameras[cameraId];

        console.log('WEBCAM', 'CAMERA', cameraId);

        download(buildUrl(camera.ip, camera.uri, login(camera)), getImageStore(camera), function () {
            var imageStore = getImageStore(camera);

            if (fs.access(imageStore, fs.F_OK, function (error) {
                console.log(error);
                if (error) {
                    logger.error(error);
                } else {
                    var data = fs.readFileSync(imageStore);
                    twitter.tweet(camera.displayName, data);
                    fs.unlinkSync(imageStore);
                }
            }));
        });
    }
};

exports.init = init;
exports.run = run;
@@ -302,56 +302,158 @@
 on (
-uri, filename, callback) %7B%0A    request.get(uri)%0A
+camera) %7B%0A    var promise = new Promise(function (resolve, reject) %7B%0A        var c = camera;%0A        request.get(buildUrl(c.ip, c.uri, login(c)))%0A    
     
@@ -492,32 +492,36 @@
 ) %7B%0A            
+    
 console.log(erro
@@ -512,32 +512,63 @@
 console.log(
+'WEBCAM', 'download', 'error', 
 error);%0A
             
@@ -547,32 +547,36 @@
 error', error);%0A
+    
             fs.u
@@ -589,97 +589,196 @@
 ync(
-filename);%0A        %7D)%0A        .pipe(fs.createWriteStream(filename)).on('close', callback)
+getImageStore(c));%0A            %7D)%0A            .pipe(fs.createWriteStream(getImageStore(c))).on('close', function () %7B%0A                resolve(c)%0A            %7D);%0A    %7D);%0A%0A    return promise
 ;%0A%7D;
@@ -1423,79 +1423,34 @@
 oad(
-buildUrl(camera.ip, camera.uri, login(camera)), getImageStore(camera), 
+camera)%0A            .then(
 func
@@ -1447,36 +1447,43 @@
 .then(function (
+cam
 ) %7B%0A
+    
             var 
@@ -1516,15 +1516,16 @@
 (cam
-era
 );%0A%0A
+    
     
@@ -1594,40 +1594,8 @@
     
-            console.log(error);%0A
     
@@ -1611,32 +1611,36 @@
    if (error) %7B%0A
+    
                 
@@ -1680,16 +1680,20 @@
         
+  
+  
 %7D else %7B
@@ -1693,16 +1693,54 @@
  else %7B%0A
+                        try %7B%0A        
         
@@ -1807,24 +1807,32 @@
             
+     
+   
 twitter.twee
@@ -1836,19 +1836,16 @@
 weet(cam
-era
 .display
@@ -1857,16 +1857,163 @@
  data);%0A
+                        %7D catch (er) %7B%0A                            console.log('WEBCAM', 'ERROR', er);%0A                        %7D finally %7B%0A        
         
@@ -2067,18 +2067,52 @@
         
-%7D%0A
+        %7D%0A                    %7D%0A    
         
@@ -2116,24 +2116,28 @@
        %7D));%0A
+    
         %7D);%0A

Refactor camera download to take promises