将文件写入Tizen文件系统

时间:2015-06-05 11:43:53

标签: javascript tizen

我正在尝试使用Samsung Gear 2设备将一些数据写入文本。我正在使用Tizen sdk。我有以下代码,我是从Tizen turorial for filesystem找到的。我可以使用我的代码在文件夹中正常创建一个文件,但似乎我无法写入该文件。我的档案是空的。 编辑:我的代码可以写出心率值,但似乎每次重写时都在文件的相同位置。最后,我只获得了一个心率值。如何在应用程序运行期间写出所有心率值。

window.onload = function () {
var initial = new Date().getTime();
var dataLength = 500;
var dps = []; // dataPoints
var historyDataLength = 5;
var history = [];
var temp = "nada";

function onsuccess(files) {
       var testFile = null;
       try{
          testFile = documentsDir.createFile("test.txt");
       if (testFile !== null) {
         testFile.openStream(
             "w",
             function(fs){
               fs.write(temp);
               fs.close();
             }, function(e){
               console.log("Error " + e.message);
             }, "UTF-8"
         );
       }
       }
       catch (e) { // file already exist -> append content
           testFile = documentsDir.resolve('test.txt');
            if(testFile !== null)
            {
                testFile.openStream(
                     "w",
                     function(fs){
                       fs.write(temp);
                       fs.close();
                     }, function(e){
                       console.log("Error " + e.message);
                     }, "UTF-8"
                 );
            }
        }
     }
     function onerror(error) {
       console.log("The error " + error.message + " occurred when listing the files in the selected folder");
     }




var chart = new CanvasJS.Chart("chartContainer",{
    title :{
        fontColor: "#ccc",
        text: "Heart Rate"
    },
    backgroundColor: "#222",
    data: [{
        color: "#CD5C5C",
        type: "line",
        dataPoints: dps 
    }]
});
var lastSecond = -1;
var updateChart = function (heartrate) {
    time = new Date().getTime() - initial;
    console.log("[" + time + ", " + heartrate + "]");
    temp = heartrate;
    console.log("tempVar"+ temp);

     tizen.filesystem.resolve(
             'documents',
             function(dir){
               documentsDir = dir; dir.listFiles(onsuccess,onerror);
             }, function(e) {
               console.log("Error" + e.message);
             }, "a"
         );

    dps.push({
        x: time / 1000.0,
        y: heartrate
    });
    if (dps.length > dataLength)
    {
        dps.shift();                
    }
    var second = Math.round(time / 1000.0);
    console.log(history.length);
    if(lastSecond != second) {
        // TODO use avg heart rate instead of smapshot.
        history.push({
            x: second,
            y: heartrate
        });
        if(history.length > historyDataLength) {
            history.shift();
        }
        lastSecond = second;
    }

    if(dps.length >= dataLength) {
        chart.render();
    }
    var hrchart = "<center>" + heartrate + "bps</center><table width='100%' cellpadding=4px>";
    for(var i = history.length - historyDataLength; i >= 0 && i < history.length; i++) {
        hrchart += "<tr><td align='right' width='50%'>" + history[i].x + "s</td><td width='50%'>" + history[i].y + "bps</td></tr>";
    }
    hrchart += "</table>";
    $('#textbox').html(hrchart);
};

updateChart(0);
updateChart(250);
for(var i = 0; i < dataLength; i++) {
    updateChart(0);
}

document.addEventListener('tizenhwkey', function(e) {
    if(e.keyName == "back")
        tizen.application.getCurrentApplication().exit();
});

window.webapis.motion.start("HRM", onchangedCB);


function onchangedCB(hrmInfo) 
{
   if(hrmInfo.heartRate > 0) {


            // add eventListener for tizenhwkey
            document.addEventListener('tizenhwkey', function(e) {
                if(e.keyName == "back")

                    tizen.application.getCurrentApplication().exit();
            });

       updateChart(hrmInfo.heartRate);
   } else {
       $('#textbox').html("No heart rate detected.");
   }
}

}

在控制台中,将打印所有console.log消息。其次,如何使用按钮关闭写入过程?哪个是停止写入过程的命令。

1 个答案:

答案 0 :(得分:1)

我认为你有一个可变范围问题。这条线 var documentsDir; 在window.onload函数中定义一个局部变量(它将为空)。相反,您希望此函数使用您在tizen.filesystem.resolve()函数中创建的documentsDir变量。注释掉你的onload中的var行(或者将它移到你的tizen.filesystem.resolve(行之前,以便在onload函数的内部和外部使用documentsDir。