Write file to Tizen filesystem

1.1k views Asked by At

I am trying to write some data to text using Samsung Gear 2 device. I am using Tizen sdk. I have the following code which I found it from Tizen turorial for filesystem. I can create normally a file with my code in document folder however it seems that I couldnt write something to that file. My file is empty. EDIT: My code could write the heartrate value but it seems that every time it re-write in the same position of the file. Finally I got only one value for heartrate. How is it possible to write all the values of heartrate during app running.

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.");
   }
}

}

In the console, all the console.log messages are printed. Secondly how can I close the writing process using a button? Which is the command which stops the writing process.

1

There are 1 answers

3
Wayne Whitaker On BEST ANSWER

I think you have a variable scope issue. The line var documentsDir; in your window.onload function is defining a local variable (which will be empty). Instead, you want this function to use the documentsDir variable you create in your tizen.filesystem.resolve() function. Comment out that var line in your onload (or move it to just before your tizen.filesystem.resolve( line, so that the documentsDir is the same inside and outside the onload function.