我正在尝试在文本文件中为Samsung Gear S的Tizen可穿戴应用程序中的一些数据写

时间:2016-09-21 16:23:39

标签: javascript web-applications tizen

我对编码世界很陌生,而且我有点麻烦。我想从三星Gear S的Tizen网络应用程序中运行的Web应用程序获取心率数据。 Web应用程序是来自三星的样本,当然可以使用。我从Tizen的开发站点找到了一些用于在文件系统上编写的代码片段。事实上,我可以在usr的Documents文件夹下创建一个文件夹,其中包含一个文本文件,但我只能在文本文件中获得类似[Object object]的内容。所以,有些东西是写的,但我显然不是在写我想要的东西。一些帮助将不胜感激。提前谢谢!

/**
         * Value of current heart rate.
         *
         * @memberof models/heartRate
         * @private
         * @type {object}
         */
        heartRate = null,

        /**
         * Object represents Heart Rate Monitor data.
         *
         * @memberof models/heartRate
         * @private
         * @type {object}
         */
        heartRateData = {};



    /**
     * Sets heart rate and time values received from sensor.
     * Returns heart rate data.
     *
     * @memberof models/heartRate
     * @private
     * @param {object} heartRateInfo
     * @returns {object}
     */
    function setHeartRateData(heartRateInfo) {
        var pData = {
            rate: heartRateInfo.heartRate,
            rrinterval: heartRateInfo.rRInterval
        };

        heartRateData = pData;
        return pData;
    }

    /**
     * Returns last received motion data.
     *
     * @memberof models/heartRate
     * @private
     * @returns {object}
     */
    function getData() {
        return heartRateData;
    }

    /**
     * Resets heart rate data.
     *
     * @memberof models/heartRate
     * @private
     */
    var newDir, newFile;
    tizen.filesystem.resolve("documents", function(dir) 
        {
           newDir = dir.createDirectory("newDir");
           newFile = newDir.createFile("beats.txt");
           newFile.openStream(
            "w",
            function(fs) {
                 fs.write(getData());
                 fs.close();
            }, function(e) {
                 console.log("Error " + e.message);
            }, "UTF-8");
        });


    tizen.filesystem.resolve("documents", function(dir) 
            {
              var file = dir.resolve("newDir/beats.txt");
               file.openStream(
                    "r", 
                    function(fs) {
                        var text = fs.read(file.fileSize);
                        fs.close();
                        console.log(text);
                    }, function(e) {
                        console.log("Error " + e.message);
                    }, "UTF-8");
            });



    function resetData() {
        heartRateData = {
            rate: '-',
            rrinterval: '-'
        };
    }

    /**
     * Handles change event on current heart rate.
     *
     * @memberof models/heartRate
     * @private
     * @param {object} heartRateInfo
     * @fires models.heartRate.change
     */
    function handleHeartRateInfo(heartRateInfo) {
        setHeartRateData(heartRateInfo);
        event.fire('change', getData());
    }

    /**
     * Starts the sensor and registers a change listener.
     *
     * @memberof models/heartRate
     * @public
     */
    function start() {
        resetData();
        heartRate.start(
            CONTEXT_TYPE,
            function onSuccess(heartRateInfo) {
                handleHeartRateInfo(heartRateInfo);
            },
            function onError(error) {
                console.log('error: ', error.message);
            }
        );
    }

    /**
     * Stops the sensor and unregisters a previously registered listener.
     *
     * @memberof models/heartRate
     * @public
     */
    function stop() {
        heartRate.stop(CONTEXT_TYPE);
    }

2 个答案:

答案 0 :(得分:1)

您想以什么格式写入文件?

function setHeartRateData(heartRateInfo) {
    var pData = {
        rate: heartRateInfo.heartRate,
        rrinterval: heartRateInfo.rRInterval
    };

    heartRateData = pData;
    return pData;
}

此函数生成并返回具有两个属性的对象。 (raterrinterval)并将数据设置为全局heartRateData

function getData() {
    return heartRateData;
}

getData()返回全局hearRateData

        function(fs) {
             fs.write(getData());
             fs.close();
        }, function(e) {
             console.log("Error " + e.message);
        }, "UTF-8");

fs.write(getData());它尝试将object写入文件 因此,默认情况下toString()方法将对象转换为字符串 object.toString()返回[Object object],因此文件仅写入[Object object]

如果你想让它变得有意义。尝试从getdata()格式化对象和属性。

例如:

var data = getData();
fs.write("rate : " + data.rate);
fs.write("RR interval : " + data.rrinterval);

如果您对此代码重新fs.write(getData()); 文件将被写成。

rate : [some number of rate]
RR interval : [some number of RR interval]

答案 1 :(得分:1)

我设法写了一些东西,把你的代码直接放在函数(fs)中;但是,我只能看到一个心率和一个RR间隔。我怎样才能编写一系列数据?谢谢!

var newDir, newFile;
    tizen.filesystem.resolve("documents", function(dir) 
        {
           newDir = dir.createDirectory("newDir");
           newFile = newDir.createFile("beats.txt");
           newFile.openStream(
            "w",
            function(fs) {
                 var data = getData();
                 fs.write("rate : " + data.rate);
                 fs.write("RR interval : " + data.rrinterval);
                 fs.close();
            }, function(e) {
                 console.log("Error " + e.message);
            }, "UTF-8");
        });

    tizen.filesystem.resolve("documents", function(dir) 
            {
           var file = dir.resolve("newDir/beats.txt");
               file.openStream(
                    "r", 
                    function(fs) {
                        var text = fs.read(file.fileSize);
                        fs.close();
                        console.log(text);
                    }, function(e) {
                        console.log("Error " + e.message);
                    }, "UTF-8");
            });