From 0519a8c663b1387b9e8ab647e0dfb5184d076739 Mon Sep 17 00:00:00 2001 From: Teppo Kurki Date: Sun, 16 Oct 2022 20:17:25 +0300 Subject: [PATCH] =?UTF-8?q?[WIP]=C2=A0fix:=20output=20null=20when=20depth?= =?UTF-8?q?=20not=20available=20(#230)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: DPT null output DPT, DBT, DBS, DBK: Output null when depth is not available. DPT: Output depth and offset independently. --- hooks/DBK.js | 16 +++------------- hooks/DBS.js | 16 +++------------- hooks/DBT.js | 9 ++++++--- hooks/DPT.js | 29 +++++++++++++---------------- test/DBK.js | 9 +++++++-- test/DBS.js | 8 ++++++-- test/DBT.js | 8 ++++++-- test/DPT.js | 22 ++++++++++++++++++++-- 8 files changed, 64 insertions(+), 53 deletions(-) diff --git a/hooks/DBK.js b/hooks/DBK.js index b49998bd..59bd4dc4 100644 --- a/hooks/DBK.js +++ b/hooks/DBK.js @@ -37,16 +37,8 @@ Field Number: */ module.exports = function (input) { - const { id, sentence, parts, tags } = input - - if ( - (typeof parts[2] !== 'string' && typeof parts[2] !== 'number') || - (typeof parts[2] === 'string' && parts[2].trim() === '') - ) { - return null - } - - const delta = { + const { parts, tags } = input + return { updates: [ { source: tags.source, @@ -54,12 +46,10 @@ module.exports = function (input) { values: [ { path: 'environment.depth.belowKeel', - value: utils.float(parts[2]), + value: parts[2].length > 0 ? utils.float(parts[2]) : null, }, ], }, ], } - - return delta } diff --git a/hooks/DBS.js b/hooks/DBS.js index ad0c9771..fc83a87f 100644 --- a/hooks/DBS.js +++ b/hooks/DBS.js @@ -37,16 +37,8 @@ Field Number: */ module.exports = function (input) { - const { id, sentence, parts, tags } = input - - if ( - (typeof parts[2] !== 'string' && typeof parts[2] !== 'number') || - (typeof parts[2] === 'string' && parts[2].trim() === '') - ) { - return null - } - - const delta = { + const { parts, tags } = input + return { updates: [ { source: tags.source, @@ -54,12 +46,10 @@ module.exports = function (input) { values: [ { path: 'environment.depth.belowSurface', - value: utils.float(parts[2]), + value: parts[2].length > 0 ? utils.float(parts[2]) : null, }, ], }, ], } - - return delta } diff --git a/hooks/DBT.js b/hooks/DBT.js index 745111de..04461ce7 100644 --- a/hooks/DBT.js +++ b/hooks/DBT.js @@ -46,9 +46,12 @@ module.exports = function (input) { if (hasNoValue(meterValue)) { const feetValue = parts[0] if (hasNoValue(feetValue)) { - return null + meterValue = null + } else { + meterValue = utils.float(feetValue) * FEET_TO_METERS } - meterValue = utils.float(feetValue) * FEET_TO_METERS + } else { + meterValue = utils.float(meterValue) } const delta = { @@ -59,7 +62,7 @@ module.exports = function (input) { values: [ { path: 'environment.depth.belowTransducer', - value: utils.float(meterValue), + value: meterValue, }, ], }, diff --git a/hooks/DPT.js b/hooks/DPT.js index c4be5af1..ba1570d4 100644 --- a/hooks/DPT.js +++ b/hooks/DPT.js @@ -35,14 +35,7 @@ Field Number: module.exports = function (input) { const { id, sentence, parts, tags } = input - if ( - ((typeof parts[0] !== 'string' || parts[0].trim() == '') && - typeof parts[0] !== 'number') || - (typeof parts[1] !== 'string' && typeof parts[1] !== 'number') - ) { - return null - } - var depth = utils.float(parts[0]) + var depth = parts[0].trim() == '' ? null : utils.float(parts[0]) const delta = { updates: [ @@ -66,19 +59,23 @@ module.exports = function (input) { path: 'environment.depth.surfaceToTransducer', value: offset, }) - delta.updates[0].values.push({ - path: 'environment.depth.belowSurface', - value: depth + offset, - }) + if (depth !== null) { + delta.updates[0].values.push({ + path: 'environment.depth.belowSurface', + value: depth + offset, + }) + } } else if (offset < 0) { delta.updates[0].values.push({ path: 'environment.depth.transducerToKeel', value: offset * -1, }) - delta.updates[0].values.push({ - path: 'environment.depth.belowKeel', - value: depth + offset, - }) + if (depth !== null) { + delta.updates[0].values.push({ + path: 'environment.depth.belowKeel', + value: depth + offset, + }) + } } return delta diff --git a/test/DBK.js b/test/DBK.js index 235cf3f5..6c396aea 100644 --- a/test/DBK.js +++ b/test/DBK.js @@ -32,8 +32,13 @@ describe('DBK', () => { delta.updates[0].values.should.contain.an.item.with.property('value', 10.83) }) - it("Doesn't choke on empty sentences", () => { + it('Converts empty value to null', () => { const delta = new Parser().parse('$IIDBK,,,,,,*4D') - should.equal(delta, null) + delta.updates[0].values.length.should.equal(1) + delta.updates[0].values[0].path.should.equal( + 'environment.depth.belowKeel' + ) + should.equal(delta.updates[0].values[0].value, null) }) + }) diff --git a/test/DBS.js b/test/DBS.js index f49c1937..108483b6 100644 --- a/test/DBS.js +++ b/test/DBS.js @@ -32,8 +32,12 @@ describe('DBS', () => { delta.updates[0].values.should.contain.an.item.with.property('value', 10.83) }) - it("Doesn't choke on empty sentences", () => { + it('Converts empty value to null', () => { const delta = new Parser().parse('$IIDBS,,,,,,*55') - should.equal(delta, null) + delta.updates[0].values.length.should.equal(1) + delta.updates[0].values[0].path.should.equal( + 'environment.depth.belowSurface' + ) + should.equal(delta.updates[0].values[0].value, null) }) }) diff --git a/test/DBT.js b/test/DBT.js index 99520825..d346f288 100644 --- a/test/DBT.js +++ b/test/DBT.js @@ -46,8 +46,12 @@ describe('DBT', () => { ) }) - it("Doesn't choke on empty sentences", () => { + it("Converts empty value to null", () => { const delta = new Parser().parse('$IIDBT,,,,,,*52') - should.equal(delta, null) + delta.updates[0].values.length.should.equal(1) + delta.updates[0].values[0].path.should.equal( + 'environment.depth.belowTransducer' + ) + should.equal(delta.updates[0].values[0].value, null) }) }) diff --git a/test/DPT.js b/test/DPT.js index d0c4be41..5254a9cf 100644 --- a/test/DPT.js +++ b/test/DPT.js @@ -77,8 +77,26 @@ describe('DPT', () => { delta.updates[0].values[2].value.should.be.closeTo(3.1, 0.1) }) - it("Doesn't choke on empty sentences", () => { + it("Converts empty depth to null", () => { const delta = new Parser().parse('$IIDPT,,,*6C') - should.equal(delta, null) + delta.updates[0].values[0].path.should.equal( + 'environment.depth.belowTransducer' + ) + delta.updates[0].values.length.should.equal(1) + should.equal(delta.updates[0].values[0].value, null) + }) + + it("Converts empty depth to null and still outputs offset", () => { + const delta = new Parser().parse('$IIDPT,,0.1*6F') + delta.updates[0].values.length.should.equal(2) + delta.updates[0].values[0].path.should.equal( + 'environment.depth.belowTransducer' + ) + should.equal(delta.updates[0].values[0].value, null) + delta.updates[0].values[1].path.should.equal( + 'environment.depth.surfaceToTransducer' + ) + should.equal(delta.updates[0].values[1].value, 0.1) }) + })