Skip to content

Commit

Permalink
Convert celcius and hPa values
Browse files Browse the repository at this point in the history
  • Loading branch information
oyve committed May 5, 2021
1 parent fa4885c commit c574615
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 16 deletions.
44 changes: 37 additions & 7 deletions barometer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict'
const barometer = require('barometer-trend');
const barometerTrend = require('barometer-trend');

const ENVIRONMENT_OUTSIDE_PRESSURE = 'environment.outside.pressure';
const ENVIRONMENT_OUTSIDE_TEMPERATURE = 'environment.outside.temperature';
Expand All @@ -10,6 +10,8 @@ const NAVIGATION_ALTITUDE = 'navigation.gnss.antennaAltitude';
const ONE_MINUTE_MILLISECONDS = 60 * 1000;
const TEN_SECONDS_MILLISECONDS = 10 * 1000;

const KELVIN = 273.15;

const SUBSCRIPTIONS = [
{ path: ENVIRONMENT_WIND_TWD, period: TEN_SECONDS_MILLISECONDS, policy: "instant", minPeriod: ONE_MINUTE_MILLISECONDS },
{ path: NAVIGATION_POSITION, period: ONE_MINUTE_MILLISECONDS, policy: "instant", minPeriod: ONE_MINUTE_MILLISECONDS },
Expand Down Expand Up @@ -100,7 +102,7 @@ function onPositionUpdated(value) {
}

function onTemperatureUpdated(value) {
latest.temperature.value = value;
latest.temperature.value = toKelvinIfCelcius(value);
}

function onAltitudeUpdated(value) {
Expand All @@ -112,6 +114,32 @@ function onTrueWindUpdated(value) {
latest.twd.value = value;
}

/**
*
* @param {number} pressure Pressure
* @returns returns Pascal if pressure is recieved in hPa
*/
function toPaIfHpa(pressure) {
if (Math.trunc(pressure).toString().length <= 4) {
pressure *= 100;
}

return pressure;
}

/**
*
* @param {number} temperature Temperature
* @returns returns Kelvin if temperature is recieved in Celcius
*/
function toKelvinIfCelcius(temperature) {
if (Math.trunc(temperature).toString().length <= 2) {
temperature += KELVIN;
}

return temperature;
}

/**
*
* @param {number} value Pressure value in (Pa) Pascal.
Expand All @@ -120,14 +148,14 @@ function onTrueWindUpdated(value) {
function onPressureUpdated(value) {
if (value == null) throw new Error("Cannot add null value");

barometer.addPressure(
barometerTrend.addPressure(
new Date(),
value,
toPaIfHpa(value),
latest.altitude.value,
latest.temperature.value,
hasTWDWithinOneMinute() ? latest.twd.value : null);

let forecast = barometer.getPredictions(isNortherHemisphere());
let forecast = barometerTrend.getPredictions(isNortherHemisphere());

return forecast !== null ? prepareUpdate(forecast) : null;
}
Expand Down Expand Up @@ -164,7 +192,7 @@ function buildDeltaUpdate(path, value) {
}

function clear() {
barometer.clear();
barometerTrend.clear();

latest.twd.time = null;
latest.twd.value = null;
Expand Down Expand Up @@ -202,5 +230,7 @@ module.exports = {
onDeltasUpdate,
clear,
preLoad,
latest
latest,
toKelvinIfCelcius,
toPaIfHpa
}
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
},
"homepage": "https://github.com/oyve/signalk-barometer-trend#readme",
"dependencies": {
"barometer-trend": "^2.1.3"
"barometer-trend": "^2.1.4"
},
"devDependencies": {
"mocha": "^8.2.1"
Expand Down
50 changes: 46 additions & 4 deletions test/index-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'
const assert = require('assert');
const barometer = require('../barometer');
const KELVIN = 273.15;

describe("Barometer Tests", function () {
describe("onDeltasUpdated", function () {
Expand All @@ -19,10 +20,9 @@ describe("Barometer Tests", function () {
barometer.clear();
const expectedTendency = "RISING";
const expectedTrend = "STEADY";
barometer.onDeltasUpdate(createDeltaMockPressure(1015));
barometer.onDeltasUpdate(createDeltaMockPressure(1016));
barometer.onDeltasUpdate(createDeltaMockPressure(101500));
//act
let actual = barometer.onDeltasUpdate(createDeltaMockPressure(1017));
let actual = barometer.onDeltasUpdate(createDeltaMockPressure(101500 + 3));
//assert
assert.strictEqual(actual.find((f) => f.path === barometer.OUTPUT_PATHS.TREND_TENDENCY).value, expectedTendency);
assert.strictEqual(actual.find((f) => f.path === barometer.OUTPUT_PATHS.TREND_TREND).value, expectedTrend);
Expand Down Expand Up @@ -150,7 +150,7 @@ describe("Barometer Tests", function () {

it("Has temperature", function () {
//arrange
const expected = 30;
const expected = 30 + KELVIN;
barometer.clear();
//act
barometer.onDeltasUpdate(createDeltaMockTemperature(expected));
Expand All @@ -177,7 +177,49 @@ describe("Barometer Tests", function () {
//assert
assert.strictEqual(barometer.latest.altitude.value, expected);
});

it("To Kelvin if Celcius", function () {
//arrange
const expected = 30 + KELVIN;
barometer.clear();
//act
let actual = barometer.toKelvinIfCelcius(30);
//assert
assert.strictEqual(actual, expected);
});

it("Not to Kelvin if Kelvin", function () {
//arrange
const expected = KELVIN;
barometer.clear();
//act
let actual = barometer.toKelvinIfCelcius(expected);
//assert
assert.strictEqual(actual, expected);
});

it("To Pa if hPa", function () {
//arrange
const expected = 101513;
barometer.clear();
//act
let actual = barometer.toPaIfHpa(1015.13);
//assert
assert.strictEqual(actual, expected);
});

it("Not to Pa if Pa", function () {
//arrange
const expected = 101513;
barometer.clear();
//act
let actual = barometer.toPaIfHpa(expected);
//assert
assert.strictEqual(actual, expected);
});
});


});

function createDeltaMockPressure(value) {
Expand Down

0 comments on commit c574615

Please sign in to comment.