Skip to content

Commit

Permalink
fix: add ability to run tests without using canboatjs
Browse files Browse the repository at this point in the history
  • Loading branch information
sbender9 committed Jul 20, 2023
1 parent eb40d45 commit 6ccd530
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 20 deletions.
2 changes: 1 addition & 1 deletion test/129026_cog_sog_rapid_update.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
4 changes: 1 addition & 3 deletions test/129029_position_data.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/129038_ais_class_a.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions test/129040_class_b_extended.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/129291_set_drift_rapid_update.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down
6 changes: 3 additions & 3 deletions test/130306_wind_data.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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
})
Expand Down
2 changes: 1 addition & 1 deletion test/maretron.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
26 changes: 21 additions & 5 deletions test/testMapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit 6ccd530

Please sign in to comment.