-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Custom sentence parser mechanism (#193)
* fix: do not require source to be there There is no need to require that individual sentence parsers set source in their output. This change allows source to not be there. This makes adding custom sentence parsers a little more robust, as the exception from a missing source was silently ignored. * feature: add custom sentence parser mechanism
- Loading branch information
Showing
8 changed files
with
155 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,4 +50,5 @@ package-lock.json | |
.DS_Store | ||
*.db | ||
|
||
.vscode | ||
.vscode | ||
custom-sentence-plugin/package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* To test the plugin | ||
* - install the plugin (for example with npm link) | ||
* - activate the plugin | ||
* - add an UDP NMEA0183 connection to the server | ||
* - send data via udp | ||
* echo '$IIXXX,1,2,3,foobar,D*17' | nc -u -w 0 127.0.0.1 7777 | ||
*/ | ||
|
||
module.exports = function (app) { | ||
const plugin = {} | ||
plugin.id = plugin.name = plugin.description = 'signalk-nmea0183-custom-sentence-plugin' | ||
|
||
plugin.start = function () { | ||
app.emitPropertyValue('nmea0183sentenceParser', { | ||
sentence: 'XXX', | ||
parser: ({ id, sentence, parts, tags }, session) => { | ||
return { | ||
updates: [ | ||
{ | ||
values: [ | ||
{ path: 'navigation.speedOverGround', value: Number(parts[0]) } | ||
] | ||
} | ||
] | ||
} | ||
} | ||
}) | ||
} | ||
|
||
plugin.stop = function () { } | ||
plugin.schema = {} | ||
return plugin | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "signalk-nmea0183-custom-sentence-plugin", | ||
"version": "1.0.0", | ||
"description": "Example of a plugin for Signal K Server that adds a custom sentence to the NMEA0183 parser", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"keywords": [ | ||
"signalk-node-server-plugin" | ||
], | ||
"author": "[email protected]", | ||
"license": "Apache-2.0" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* Copyright 2021 Signal K and Teppo Kurki <[email protected]> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
const Parser = require('../lib') | ||
const chai = require('chai') | ||
const { expect } = require('chai') | ||
const should = chai.Should() | ||
chai.use(require('chai-things')) | ||
|
||
|
||
describe('Custom Sentence Parser', () => { | ||
it('works', () => { | ||
const TEST_SENTENCE_PARTS = ['1', '2', '3', 'foobar', 'D'] | ||
const TEST_CUSTOM_SENTENCE = `$IIXXX,${TEST_SENTENCE_PARTS.join(',')}*17` | ||
const DELTA = { | ||
updates: [ | ||
{ | ||
values: [ | ||
{ path: 'a.b.c', value: 3.14 } | ||
] | ||
} | ||
] | ||
} | ||
let onPropValuesCallCount = 0 | ||
const options = { | ||
onPropertyValues: (propertyName, cb) => { | ||
onPropValuesCallCount++ | ||
cb(undefined) | ||
cb([{ | ||
value: { | ||
sentence: 'XXX', | ||
parser: ({ id, sentence, parts, tags }, session) => { | ||
id.should.equal('XXX') | ||
sentence.should.equal(TEST_CUSTOM_SENTENCE) | ||
parts.should.have.members(TEST_SENTENCE_PARTS) | ||
expect(typeof session).to.equal('object') | ||
return DELTA | ||
} | ||
} | ||
}]) | ||
} | ||
} | ||
const parser = new Parser(options) | ||
onPropValuesCallCount.should.equal(1) | ||
const delta = parser.parse(TEST_CUSTOM_SENTENCE) | ||
delta.should.deep.equal(DELTA) | ||
}) | ||
}) |