From 6ccd53054b5d9acaaee5457a9a77519bc7631ed5 Mon Sep 17 00:00:00 2001 From: Scott Bender Date: Thu, 20 Jul 2023 09:32:18 -0400 Subject: [PATCH] fix: add ability to run tests without using canboatjs --- test/129026_cog_sog_rapid_update.js | 2 +- test/129029_position_data.js | 4 +--- test/129038_ais_class_a.js | 4 ++-- test/129040_class_b_extended.js | 6 +++--- test/129291_set_drift_rapid_update.js | 4 ++-- test/130306_wind_data.js | 6 +++--- test/maretron.js | 2 +- test/testMapper.js | 26 +++++++++++++++++++++----- 8 files changed, 34 insertions(+), 20 deletions(-) diff --git a/test/129026_cog_sog_rapid_update.js b/test/129026_cog_sog_rapid_update.js index eef9e47..01dc2a4 100644 --- a/test/129026_cog_sog_rapid_update.js +++ b/test/129026_cog_sog_rapid_update.js @@ -7,7 +7,7 @@ describe('129026 COG & SOG, Rapid Update', function () { it('complete sentence converts', function () { var tree = require('./testMapper').toNested( JSON.parse( - '{"timestamp":"2014-08-15-18:00:10.005Z","prio":"2","src":"160","dst":"255","pgn":"129026","description":"COG & SOG, Rapid Update","fields":{"COG Reference":"True","COG":206.1,"SOG":3.65}}' + '{"timestamp":"2014-08-15-18:00:10.005Z","prio":"2","src":"160","dst":"255","pgn":"129026","description":"COG & SOG, Rapid Update","fields":{"COG Reference":"True","COG":2.9384,"SOG":3.65}}' ) ) tree.should.have.nested.property('navigation.courseOverGroundTrue') diff --git a/test/129029_position_data.js b/test/129029_position_data.js index 7acc46b..db29756 100644 --- a/test/129029_position_data.js +++ b/test/129029_position_data.js @@ -4,9 +4,7 @@ chai.use(require('chai-things')) chai.use(require('@signalk/signalk-schema').chaiModule) var msg = JSON.parse( - '{"canId":234358051,"prio":3,"src":35,"dst":255,"pgn":129029,"fields":{"SID":126,"Date":"2020.03.09","Time":"17:47:47.80000","Latitude":42.4913166,"Longitude":-70.8850733,"Altitude":41.4,"GNSS type":"GPS","Method":"DGNSS fix","Integrity":"No integrity checking","Number of SVs":10,"HDOP":0.9,"PDOP":1.6,"Geoidal Separation":-30.9,"Reference Stations":1,"list":[{"Reference Station ID":15,"Age of DGNSS Corrections": 30}]},"description":"GNSS Position Data"}' - - //'{"timestamp":"2017-04-15T15:50:48.664Z","prio":3,"src":3,"dst":255,"pgn":129029,"description":"GNSS Position Data","fields":{"SID":22,"Date":"2017.04.15", "Time": "15:50:48.04950","Latitude":39.0536632,"Longitude":-76.3972731,"GNSS type":"GPS+GLONASS","Method":"GNSS Fix","Integrity":"No integrity checking","Number of SVs":18,"HDOP":0.73,"Geoidal Separation":-0.01, "Altitude": 1.0, "PDOP":1.20, "Age of DGNSS Corrections": 30, "Reference Station ID": 22, "list":[{}]}}' + '{"timestamp":"2017-04-15T15:50:48.664Z","prio":3,"src":35,"dst":255,"pgn":129029,"fields":{"SID":126,"Date":"2020.03.09","Time":"17:47:47.80000","Latitude":42.4913166,"Longitude":-70.8850733,"Altitude":41.4,"GNSS type":"GPS","Method":"DGNSS fix","Integrity":"No integrity checking","Number of SVs":10,"HDOP":0.9,"PDOP":1.6,"Geoidal Separation":-30.9,"Reference Stations":1,"list":[{"Reference Station ID":15,"Age of DGNSS Corrections": 30}]},"description":"GNSS Position Data"}' ) // 2017-07-01T13:02:15.120Z,3,129029,1,255,43,01,ff,ff,ff,ff,ff,ff,ff,ff,ff,ff,ff,ff,ff,7f,ff,ff,ff,ff,ff,ff,ff,7f,ff,ff,ff,ff,ff,ff,ff,7f,00,fc,08,ff,7f,ff,7f,ff,ff,ff,7f,ff diff --git a/test/129038_ais_class_a.js b/test/129038_ais_class_a.js index 7d8c0cc..c91d59e 100644 --- a/test/129038_ais_class_a.js +++ b/test/129038_ais_class_a.js @@ -9,13 +9,13 @@ var mapper = require('./testMapper') describe('129038 Class A Update', function () { it('complete sentence converts', function () { var msg = JSON.parse( - '{"timestamp":"2014-08-15-15:00:01.665Z","prio":"4","src":"43","dst":"255","pgn":"129038","description":"AIS Class A Position Report","fields":{"Message ID":"1","Repeat Indicator":"Initial","User ID":"230982000","Longitude":25.2026083,"Latitude":60.2176150,"Position Accuracy":"High","RAIM":"not in use","Time Stamp":"0","COG":154.0,"SOG":2.26,"Communication State":"2286","AIS Transceiver information":"Channel B VDL reception","Heading":153.0,"Rate of Turn":0.047,"Nav Status":"Under way using engine"}}' + '{"timestamp":"2014-08-15-15:00:01.665Z","prio":"4","src":"43","dst":"255","pgn":"129038","description":"AIS Class A Position Report","fields":{"Message ID":"1","Repeat Indicator":"Initial","User ID":"230982000","Longitude":25.2026083,"Latitude":60.2176150,"Position Accuracy":"High","RAIM":"not in use","Time Stamp":"0","COG":1.54,"SOG":2.26,"Communication State":"2286","AIS Transceiver information":"Channel B VDL reception","Heading":2.2672,"Rate of Turn":0.047,"Nav Status":"Under way using engine"}}' ) var tree = mapper.toNested(msg) tree.should.have.nested.property('navigation.courseOverGroundTrue') tree.should.have.nested.property( 'navigation.courseOverGroundTrue.value', - 3.2672 + 1.54 ) tree.should.have.nested.property('navigation.speedOverGround') tree.should.have.nested.property('navigation.speedOverGround.value', 2.26) diff --git a/test/129040_class_b_extended.js b/test/129040_class_b_extended.js index 6fcf69d..8d4a760 100644 --- a/test/129040_class_b_extended.js +++ b/test/129040_class_b_extended.js @@ -9,7 +9,7 @@ var mapper = require('./testMapper') describe('129040 AIS Class B Extended Position Repeat', function () { it('complete sentence converts', function () { var msg = JSON.parse( - '{"timestamp":"2014-08-15-15:01:01.881Z","prio":"6","src":"43","dst":"255","pgn":"129040","description":"AIS Class B Extended Position Report","fields":{"Message ID":"5","Repeat indicator":"Initial","User ID":"230939100","Name":"RESCUE RAUTAUOMA","Type of ship":"SAR","Length":16.0,"Beam":4.0,"Position reference from Starboard":2.0,"Position reference from Bow":9.0,"ETA Date":"2014.11.30", "ETA Time": "25:00:00","Draft":"1.00","AIS version indicator":"ITU-R M.1371-1","GNSS type":"GPS","DTE":"available","Reserved":"0","AIS Transceiver information":"Channel B VDL reception", "True Heading": 158.0,"Longitude":25.2026083,"Latitude":60.2176150,"COG":154.0,"SOG":2.26}}' + '{"timestamp":"2014-08-15-15:01:01.881Z","prio":"6","src":"43","dst":"255","pgn":"129040","description":"AIS Class B Extended Position Report","fields":{"Message ID":"5","Repeat indicator":"Initial","User ID":"230939100","Name":"RESCUE RAUTAUOMA","Type of ship":"SAR","Length":16.0,"Beam":4.0,"Position reference from Starboard":2.0,"Position reference from Bow":9.0,"ETA Date":"2014.11.30", "ETA Time": "25:00:00","Draft":"1.00","AIS version indicator":"ITU-R M.1371-1","GNSS type":"GPS","DTE":"available","Reserved":"0","AIS Transceiver information":"Channel B VDL reception", "True Heading": 1.58,"Longitude":25.2026083,"Latitude":60.2176150,"COG":1.54,"SOG":2.26}}' ) var delta = mapper.toDelta(msg) delta.updates.length.should.equal(1) @@ -28,11 +28,11 @@ describe('129040 AIS Class B Extended Position Repeat', function () { tree.should.have.nested.property('navigation.courseOverGroundTrue') tree.should.have.nested.property( 'navigation.courseOverGroundTrue.value', - 3.2672 + 1.54 ) tree.should.have.nested.property('navigation.speedOverGround') tree.should.have.nested.property('navigation.speedOverGround.value', 2.26) - tree.should.have.nested.property('navigation.headingTrue.value', 0.7136) + tree.should.have.nested.property('navigation.headingTrue.value', 1.58) tree.should.have.nested.property('sensors.ais.fromBow.value', 9.0) tree.should.have.nested.property('sensors.ais.fromCenter.value', 0) delete tree.design.aisShipType diff --git a/test/129291_set_drift_rapid_update.js b/test/129291_set_drift_rapid_update.js index 7bf6886..28aab1f 100644 --- a/test/129291_set_drift_rapid_update.js +++ b/test/129291_set_drift_rapid_update.js @@ -6,12 +6,12 @@ chai.use(require('@signalk/signalk-schema').chaiModule) describe('129291 set & drift rapid update complete sentence', function () { var tree = require('./testMapper').toNested( JSON.parse( - '{"timestamp":"2014-08-15-18:00:06.573Z","prio":"3","src":"160","dst":"255","pgn":"129291","description":"Set & Drift, Rapid Update","fields":{"Set Reference":"True","Set":212.6,"Drift":0.24}}' + '{"timestamp":"2014-08-15-18:00:06.573Z","prio":"3","src":"160","dst":"255","pgn":"129291","description":"Set & Drift, Rapid Update","fields":{"Set Reference":"True","Set":2.126,"Drift":0.24}}' ) ) it('result has correct values', function () { tree.should.have.nested.property('environment.current.value.setTrue') - tree.should.have.nested.property('environment.current.value.setTrue', 2.8848) + tree.should.have.nested.property('environment.current.value.setTrue', 2.126) tree.should.have.nested.property('environment.current.value.drift') tree.should.have.nested.property('environment.current.value.drift', 0.24) }) diff --git a/test/130306_wind_data.js b/test/130306_wind_data.js index 9ec06a9..5a7fca9 100644 --- a/test/130306_wind_data.js +++ b/test/130306_wind_data.js @@ -7,7 +7,7 @@ describe('130306 Wind Data', function () { it('Apparent sentence converts positive', function () { var tree = require('./testMapper').toNested( JSON.parse( - '{"timestamp":"2013-10-08-15:47:28.263Z","prio":"2","src":"1","dst":"255","pgn":"130306","description":"Wind Data","fields":{"SID":"67","Wind Speed":6.22,"Wind Angle":0.872665,"Reference":"Apparent"}}' + '{"timestamp":"2013-10-08-15:47:28.263Z","prio":"2","src":"1","dst":"255","pgn":"130306","description":"Wind Data","fields":{"SID":"67","Wind Speed":6.22,"Wind Angle":0.8727,"Reference":"Apparent"}}' ) ) tree.should.have.nested.property( @@ -24,7 +24,7 @@ describe('130306 Wind Data', function () { it('Apparent sentence converts positive gt 180', function () { var tree = require('./testMapper').toNested( JSON.parse( - '{"timestamp":"2013-10-08-15:47:28.263Z","prio":"2","src":"1","dst":"255","pgn":"130306","description":"Wind Data","fields":{"SID":"67","Wind Speed":6.22,"Wind Angle":3.31613,"Reference":"Apparent"}}' + '{"timestamp":"2013-10-08-15:47:28.263Z","prio":"2","src":"1","dst":"255","pgn":"130306","description":"Wind Data","fields":{"SID":"67","Wind Speed":6.22,"Wind Angle":3.3,"Reference":"Apparent"}}' ) ) tree.should.have.nested.property( @@ -33,7 +33,7 @@ describe('130306 Wind Data', function () { ) tree.should.have.nested.property( 'environment.wind.angleApparent.value', - -2.967085307179586 + -2.9831853071795864 ) tree.should.be.validSignalKVesselIgnoringIdentity }) diff --git a/test/maretron.js b/test/maretron.js index 7bf3a15..1d83e0f 100644 --- a/test/maretron.js +++ b/test/maretron.js @@ -82,7 +82,7 @@ describe('Maretron AC PGNs work', function () { it('65030 Generator Average Basic AC Quantities', function () { var tree = toNested( JSON.parse( - '{"canId":217974465,"prio":3,"src":193,"dst":255,"pgn":65030,"direction":"R","time":"18:58:11.835","fields":{"Line-Line AC RMS Voltage":1,"Line-Neutral AC RMS Voltage":1,"AC Frequency":60.0078125,"AC RMS Current":0},"description":"Generator Average Basic AC Quantities","timestamp":"2020-03-28T18:58:12.477Z"}' + '{"canId":217974465,"prio":3,"src":193,"dst":255,"pgn":65030,"direction":"R","time":"18:58:11.835","fields":{"Line-Line AC RMS Voltage":1,"Line-Neutral AC RMS Voltage":1,"AC Frequency":60.008,"AC RMS Current":0},"description":"Generator Average Basic AC Quantities","timestamp":"2020-03-28T18:58:12.477Z"}' ), n2kMapper.state ) diff --git a/test/testMapper.js b/test/testMapper.js index 3a4d606..be21486 100644 --- a/test/testMapper.js +++ b/test/testMapper.js @@ -6,7 +6,24 @@ const { FromPgn, pgnToActisenseSerialFormat } = canboatjs const Parser = canboatjs.FromPgn const parser = new FromPgn() -n2kMapper.toNested = function (n2k, state) { + +/* + By default we take the input canboat json and convert to actisense format, + back to conboat json and then do the n2k conversion. + This is done to expose any issues/changes with field names in canboat. + + Set the env variable NO_CANBOATJS=true to skip the back and forth conversion. +*/ + +n2kMapper.toNested = function(n2k, state) { + if ( !process.env.NO_CANBOATJS ) { + return n2kMapper.doubleConvertWithCanboat(n2k, state) + } else { + return n2kMapper.n2kToNested(n2k, state) + } +} + +n2kMapper.doubleConvertWithCanboat = function (n2k, state) { var actisense = pgnToActisenseSerialFormat(n2k) var parsed = parser.parseString(actisense) parsed.src = n2k.src @@ -20,15 +37,14 @@ n2kMapper.toNested = function (n2k, state) { var contextParts = delta.context.split('.') return signalkSchema.deltaToFull(delta)[contextParts[0]][contextParts[1]] } + n2kMapper.n2kToNested = function(n2k, state) { - var delta = n2kMapper.toDelta(parser.parseString(n2k), state) + var delta = n2kMapper.toDelta(n2k, state) if (!delta.context) { delta.context = 'vessels.' + signalkSchema.fakeMmsiId } var contextParts = delta.context.split('.') return signalkSchema.deltaToFull(delta)[contextParts[0]][contextParts[1]] } -n2kMapper.n2kToDelta = function(n2k, state) { - return n2kMapper.toDelta(parser.parseString(n2k), state) -} + module.exports = n2kMapper