function retrieveLatestAudio(req, res) {

    if (latestAudio[req.params.dronename] != undefined) {
        try {
            var imageData = fs.readFileSync(latestAudio[req.params.dronename]);
            res.set('Content-Type', 'audio/x-wav');
            logger.info("Sending 1 ");
            res.send(imageData);
            return;
        } catch (e) {
            logger.error("Image Cache Error.");
            logger.error(e.message);
            delete latestAudio[req.params.dronename];
        }
    }

    var audio = cloudant.use(req.params.dronename + '_audio');
    audio.list({"descending": true, "include_docs": true, "limit": 2}, function (err, body) {
        if (err) {
            logger.error(err);
            res.status(404).send("Internal error");
        } else {
            if(body.rows.length == 0){
                res.status(404).send("No audio found");
                return;
            }

            if(body.rows[0].id.indexOf('_design') > -1){
                body.rows.splice(0, 1);
            }

            if(body.rows.length == 0){
                res.status(404).send("No audio found in database");
                return;
            }
            var filename = body.rows[0].id;
            audio.attachment.get(filename, "audio", function (err, body) {
                if (err) { // Would reflect data input error
                    logger.error(err);
                    res.status(404).send(err.message);
                } else {
                    res.set('Content-Type', 'audio/x-wav');
                    res.send(body);
                    logger.info("Sending 2 ");
                    // Cache response - need to append original filename?
                    fs.writeFile(uploadDir + filename, body, function (err) {
                        if (err) {
                            logger.error(err);
                        } else {
                            latestAudio[req.params.dronename] = uploadDir + filename;
                        }
                    });
                }
            });
        }
    });
}
/**
 * Retrieve latest audio, from cache or Cloudant database
 * @param req {Object} The request from the client
 * @param res {Object} The response for the client
 * @param req.params.dronename {String} The drone from which image originated
 */
Retrieve latest audio, from cache or Cloudant database
@param req {Object} The request from the client
@param res {Object} The response for the client
@param req.params.dronename {String} The drone from which image originated
Retrieve latest audio from cache or Cloudant database @param req { Object } The request from the client @param res { Object } The response for the client @param req . params . dronename { String } The drone from which image originated
function retrieveLatestAudio(req, res) {
    if (latestAudio[req.params.dronename] != undefined) {
        try {
            var imageData = fs.readFileSync(latestAudio[req.params.dronename]);
            res.set('Content-Type', 'audio/x-wav');
            logger.info("Sending 1 ");
            res.send(imageData);
            return;
        } catch (e) {
            logger.error("Image Cache Error.");
            logger.error(e.message);
            delete latestAudio[req.params.dronename];
        }
    }
    var audio = cloudant.use(req.params.dronename + '_audio');
    audio.list({"descending": true, "include_docs": true, "limit": 2}, function (err, body) {
        if (err) {
            logger.error(err);
            res.status(404).send("Internal error");
        } else {
            if(body.rows.length == 0){
                res.status(404).send("No audio found");
                return;
            }
            if(body.rows[0].id.indexOf('_design') > -1){
                body.rows.splice(0, 1);
            }
            if(body.rows.length == 0){
                res.status(404).send("No audio found in database");
                return;
            }
            var filename = body.rows[0].id;
            audio.attachment.get(filename, "audio", function (err, body) {
                if (err) { 
                    logger.error(err);
                    res.status(404).send(err.message);
                } else {
                    res.set('Content-Type', 'audio/x-wav');
                    res.send(body);
                    logger.info("Sending 2 ");
                    fs.writeFile(uploadDir + filename, body, function (err) {
                        if (err) {
                            logger.error(err);
                        } else {
                            latestAudio[req.params.dronename] = uploadDir + filename;
                        }
                    });
                }
            });
        }
    });
}
function retrieveLatestAudio ( req , res ) { if ( latestAudio [ req . params . dronename ] != undefined ) { try { var imageData = fs . readFileSync ( latestAudio [ req . params . dronename ] ) ; res . set ( 'Content-Type' , 'audio/x-wav' ) ; logger . info ( "Sending 1 " ) ; res . send ( imageData ) ; return ; } catch ( e ) { logger . error ( "Image Cache Error." ) ; logger . error ( e . message ) ; delete latestAudio [ req . params . dronename ] ; } } var audio = cloudant . use ( req . params . dronename + '_audio' ) ; audio . list ( { "descending" : true , "include_docs" : true , "limit" : 2 } , function ( err , body ) { if ( err ) { logger . error ( err ) ; res . status ( 404 ) . send ( "Internal error" ) ; } else { if ( body . rows . length == 0 ) { res . status ( 404 ) . send ( "No audio found" ) ; return ; } if ( body . rows [ 0 ] . id . indexOf ( '_design' ) > - 1 ) { body . rows . splice ( 0 , 1 ) ; } if ( body . rows . length == 0 ) { res . status ( 404 ) . send ( "No audio found in database" ) ; return ; } var filename = body . rows [ 0 ] . id ; audio . attachment . get ( filename , "audio" , function ( err , body ) { if ( err ) { logger . error ( err ) ; res . status ( 404 ) . send ( err . message ) ; } else { res . set ( 'Content-Type' , 'audio/x-wav' ) ; res . send ( body ) ; logger . info ( "Sending 2 " ) ; fs . writeFile ( uploadDir + filename , body , function ( err ) { if ( err ) { logger . error ( err ) ; } else { latestAudio [ req . params . dronename ] = uploadDir + filename ; } } ) ; } } ) ; } } ) ; }
Retrieve latest audio, from cache or Cloudant database
 @param req {Object} The request from the client
 @param res {Object} The response for the client
 @param req.params.dronename {String} The drone from which image originated
Retrieve latest audio from cache or Cloudant database @param req { Object } The request from the client @param res { Object } The response for the client @param req . params . dronename { String } The drone from which image originated
// Would reflect data input error // Cache response - need to append original filename?