Skip to content

Commit

Permalink
[WIP] fix: output null when depth not available (#230)
Browse files Browse the repository at this point in the history
* fix: DPT null output

DPT, DBT, DBS, DBK: Output null when depth is not available.
DPT: Output depth and offset independently.
  • Loading branch information
tkurki authored Oct 16, 2022
1 parent 9e288ca commit 0519a8c
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 53 deletions.
16 changes: 3 additions & 13 deletions hooks/DBK.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,19 @@ 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,
timestamp: tags.timestamp,
values: [
{
path: 'environment.depth.belowKeel',
value: utils.float(parts[2]),
value: parts[2].length > 0 ? utils.float(parts[2]) : null,
},
],
},
],
}

return delta
}
16 changes: 3 additions & 13 deletions hooks/DBS.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,19 @@ 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,
timestamp: tags.timestamp,
values: [
{
path: 'environment.depth.belowSurface',
value: utils.float(parts[2]),
value: parts[2].length > 0 ? utils.float(parts[2]) : null,
},
],
},
],
}

return delta
}
9 changes: 6 additions & 3 deletions hooks/DBT.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -59,7 +62,7 @@ module.exports = function (input) {
values: [
{
path: 'environment.depth.belowTransducer',
value: utils.float(meterValue),
value: meterValue,
},
],
},
Expand Down
29 changes: 13 additions & 16 deletions hooks/DPT.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -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
Expand Down
9 changes: 7 additions & 2 deletions test/DBK.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

})
8 changes: 6 additions & 2 deletions test/DBS.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
8 changes: 6 additions & 2 deletions test/DBT.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
22 changes: 20 additions & 2 deletions test/DPT.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

})

0 comments on commit 0519a8c

Please sign in to comment.