Skip to content

Commit

Permalink
Merge pull request #15 from oyve/dev
Browse files Browse the repository at this point in the history
fix parse date in json deserialize
  • Loading branch information
oyve authored Sep 9, 2021
2 parents d1e75c8 + f6bc6ca commit 64cd4fa
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 19 deletions.
15 changes: 13 additions & 2 deletions barometer.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,21 @@ function populate(populateCallback) {

if (barometerData) {
barometerData.forEach((bd) => {
addPressure(bd.datetime, bd.meta.value, bd.meta.altitude, bd.meta.temperature, bd.meta.twd);
addPressure(bd.datetime, bd.meta.value, bd.meta.altitude, bd.meta.temperature, bd.meta.twd);
});
}
}

function JSONParser(content) {
return JSON.parse(content, (key, value) => {
if (key == "datetime") {
return new Date(value);
} else {
return value;
}
})
};

module.exports = {
SUBSCRIPTIONS,
hasTWDWithinOneMinute,
Expand All @@ -187,5 +197,6 @@ module.exports = {
setAltitudeCorrection,
persist,
populate,
getAll
getAll,
JSONParser
}
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = function (app) {
var unsubscribes = [];
plugin.start = function (options, restartPlugin) {
app.debug('Plugin started');

barometer.setSampleRate(options.rate);
app.debug('Sample rate set to ' + options.rate + " seconds");
barometer.setAltitudeCorrection(options.altitude);
Expand Down Expand Up @@ -94,7 +94,7 @@ module.exports = function (app) {
const content = fs.readFileSync(offlineFilePath(), 'utf-8');

try {
return JSON.parse(content);
return barometer.JSONParser(content);
} catch (err) {
app.error("Could not parse JSON : " + content);
app.error(err.stack);
Expand Down
50 changes: 35 additions & 15 deletions test/barometer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,15 @@ describe("Barometer Tests", function () {
let actual = barometer.onDeltasUpdate(createDeltaMockPressure(101500));
//assert
assert.strictEqual(actual.find((f) => f.path === getPath("system")).value, expected);

});
});

describe("Set Sample Rate", function () {
it("It should equal", function () {
//arrange
barometer.clear();
let expected = 80 * 1000;
let expected = 80 * 1000;
//act
var actual = barometer.setSampleRate(80);
//assert
Expand All @@ -196,7 +196,7 @@ describe("Barometer Tests", function () {
it("It should equal under threshold", function () {
//arrange
barometer.clear();
let expected = 1200 * 1000;
let expected = 1200 * 1000;
//act
var actual = barometer.setSampleRate(1201);
//assert
Expand All @@ -205,7 +205,7 @@ describe("Barometer Tests", function () {
it("It should equal above threshold", function () {
//arrange
barometer.clear();
let expected = 60 * 1000;
let expected = 60 * 1000;
//act
var actual = barometer.setSampleRate(59);
//assert
Expand All @@ -217,7 +217,7 @@ describe("Barometer Tests", function () {
it("It should equal positive", function () {
//arrange
barometer.clear();
let expected = 104;
let expected = 104;
//act
barometer.setAltitudeCorrection(4);
barometer.onDeltasUpdate(createDeltaMockAltitude(100));
Expand All @@ -229,7 +229,7 @@ describe("Barometer Tests", function () {
it("It should equal negative", function () {
//arrange
barometer.clear();
let expected = 96;
let expected = 96;
//act
barometer.setAltitudeCorrection(-4);
barometer.onDeltasUpdate(createDeltaMockAltitude(100));
Expand All @@ -241,7 +241,7 @@ describe("Barometer Tests", function () {
it("It should not equal", function () {
//arrange
barometer.clear();
let expected = 100;
let expected = 100;
//act
barometer.setAltitudeCorrection(null);
barometer.setAltitudeCorrection(undefined);
Expand All @@ -250,7 +250,7 @@ describe("Barometer Tests", function () {
//assert
assert.strictEqual(actual, expected);
});
});
});

describe("persist", function () {
it("Persist should persist", function () {
Expand Down Expand Up @@ -288,11 +288,11 @@ describe("Barometer Tests", function () {
}

barometer.clear();

//act
barometer.populate(populateCallback)
const actual = barometer.getAll();

//assert
assert.deepEqual(actual, all);
});
Expand All @@ -304,10 +304,10 @@ describe("Barometer Tests", function () {
const populateCallback = () => {
return [];
}

//act
barometer.populate(populateCallback)

//assert
//assert.strictEqual(actual, all);
});
Expand All @@ -319,12 +319,32 @@ describe("Barometer Tests", function () {
const populateCallback = () => {
return null;
}

//act
barometer.populate(populateCallback)

//assert
//assert.strictEqual(actual, all);
assert.ok(true);
});

it("Parse date into date objecft", function () {
//arrange
barometer.clear();
barometer.onDeltasUpdate(createDeltaMockPressure(101500));
barometer.onDeltasUpdate(createDeltaMockPressure(101600));
barometer.onDeltasUpdate(createDeltaMockPressure(101700));

const content = JSON.stringify(barometer.getAll());

const populateCallback = () => {
barometer.JSONParser(content);
}

//act
barometer.populate(populateCallback)

//assert
assert.ok(true);
});
});
});
Expand Down

0 comments on commit 64cd4fa

Please sign in to comment.