在OSC API

时间:2017-10-08 11:43:46

标签: javascript node.js express callback node-modules

我的想法是允许我按下HTML页面上的一个按钮来执行命令,复制和删除相机上的所有照片,并在执行的开始和结束时显示反馈。

目前,点击“从相机获取图片”后,textarea会显示以下文字:

  

执行命令:\ copyImages

     

结果如下:从中复制图像   两个相机...... \ n

它继续复制和删除我想要的所有图像。但是在此过程结束时,没有任何内容返回到屏幕,因此用户不知道会发生什么。 Node js中回调的性质让我很难弄清楚如何做到这一点。

P.S。在我来到这里得到你的帮助之前,我已经尝试了所有我知道的。所以要知道任何建议都非常感谢!

所以,我的问题是如何更改下面的代码以便我可以 显示一条消息,向用户显示复制已成功完成,如:

  

请等待复制完成...

     

完成!

以下是HTML标记

<button id="copyImages" type="button" class="button">Get Images From Camera</button>
<textarea id="output" readonly></textarea>

以下是Javascript事件处理:

copyImages.onclick = function() {
    dest = '/copyImages';
    writeToOutput(dest);
}
function writeToOutput(dest) {
        $.get(dest, null, function(data) {
            resultText += "Executed command: "+dest+"\n" 
                                    +"Result is as below: \n"+data;
            $("#output").val(resultText);
        }, "text");
        return true;
     } 

以下这些功能用于使用快速模块设置节点应用服务器,以收听HTML页面传递给它的任何内容。它们在不同的设备上运行。

expressServer.listen( expressPort, function() {
    console.log('expressServer listening at *:%d', expressPort );

});

// allow CORS on the express server
expressServer.use(function(req, res, next) {
    // enable cross original resource sharing to allow html page to access commands
    res.header("Access-Control-Allow-Origin", "*");

    // return to the console the URL that is being accesssed, leaving for clarity
    console.log("\n"+req.url);

    next();
});

expressServer.get('/copyImages', function (req, res) {
    // user accesses /copyImages and the copyImages function is called
    copyImages(function(result) {
        res.end(result + "\n");
    });
});

将图像从Theta S相机复制到Raspberry Pi并从相机中删除

var resultCopyImages = "";

copyImages = function (callback) {
    resultCopyImages = "Copying images from both cameras...\n";
    for (var i = 0; i < camArray.length; i++) {
        copyOneCamImages(i, callback);
    }
    return (callback(resultCopyImages));
//how to return multiple messages?
}

copyOneCamImages = function (camID, callback) {

    d.on('error', function(err){
        console.log('There was an error copying the images');
        return(callback('There was an error running a function, please make sure all cameras are connected and restart the server'));
    })

    d.run(function(){

        var imageFolder = baseImageFolder + camID;
        // if the directory does not exist, make it
        if (!fs.existsSync(imageFolder)) {
            fs.mkdirSync(imageFolder);
            console.log("no 'images' folder found, so a new one has been created!");
        }

        // initialise total images, approximate time
        var totalImages = 0;
        var approxTime = 0;

        // get the first image and do not include thumbnail
        var entryCount = 1;
        var includeThumb = false;
        var filename;
        var fileuri;

        // get the total amount of images
        camArray[camID].oscClient.listImages(entryCount, includeThumb)
            .then(function (res) {
            totalImages = res.results.totalEntries;
            approxTime = totalImages * 5;
            resultCopyImages = '';
            resultCopyImages = 'Camera ' + (camID + 1) + ': Copying a total of: ' + totalImages + ' images'
                + '\nTo folder: ' + imageFolder
                + '\nThis process will take approximately: ' + approxTime + ' seconds \n';
            console.log(resultCopyImages);
            callback(resultCopyImages);
            });

        // copy a single image, with the same name and put it in images folder
        camArray[camID].oscClient.listImages(entryCount, includeThumb)
            .then(function (res) {
            filename = imageFolder + '/' + res.results.entries[0].name;
            fileuri = res.results.entries[0].uri;
            imagesLeft = res.results.totalEntries;

            // gets the image data
            camArray[camID].oscClient.getImage(res.results.entries[0].uri)
                .then(function (res) {

                var imgData = res;
                fs.writeFile(filename, imgData);
                camArray[camID].oscClient.delete(fileuri).then(function () {
                    if (imagesLeft != 0) {
                        // callback to itself to continue copying if images are left
                        callback(copyOneCamImages(camID, callback));

        //????????????????????????????????????????????????????????????????????????????
        //if(imagesLeft==1) return(callback("Finished copying"));
                    }/* else {
        resultCopyImages = "Finshed copying image.\n";
        console.log(resultCopyImages);
        }
          else if   
                        return(callback(resultCopyImages));
                    }*/
                    });
                });
        });
    })

 }

1 个答案:

答案 0 :(得分:0)

到目前为止,我问的问题没有真正的答案,所以我们已经完成了项目并跳过了这个功能。但是,这只是掌握REST API和NodeJ中的异步函数的问题。该项目预计将在明年某个时候继续下一个版本。