Created
September 20, 2017 10:29
-
-
Save fabdrol/c786259a181dd7ce674fa02251d4b3df to your computer and use it in GitHub Desktop.
NMEA0183 Provider
This file contains hidden or 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
/* | |
* Copyright 2014-2015 Fabian Tollenaar <[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. | |
*/ | |
/* Usage: this is the pipeElement that transforms NMEA0183 input to Signal K deltas | |
* SelfId and selfType are fetched from app properties. Emits sentence data as "nmea0183" | |
* events on app.signalk by default. Furthermore you can use "sentenceEvent" option, | |
* that will cause sentence data to be emitted as events on app. sentenceEvent can | |
* be a string or an array of strings. | |
* | |
* Example: | |
{ | |
"type": "providers/nmea0183-signalk", | |
"options": { | |
"sentenceEvent": "nmea0183-1" | |
}, | |
"optionMappings": [ | |
{ | |
"fromAppProperty": "selfId", | |
"toOption": "selfId" | |
}, | |
{ | |
"fromAppProperty": "selfType", | |
"toOption": "selfType" | |
} | |
] | |
} | |
*/ | |
const Transform = require("stream").Transform; | |
const isArray = require("lodash").isArray; | |
const Parser = require('../../signalk-parser-nmea0183'); | |
const debug = require('debug')('signalk-provider-nmea0183'); | |
function ToSignalK(options) { | |
Transform.call(this, { | |
objectMode: true | |
}); | |
this.parser = new Parser(options); | |
var that = this; | |
this.parser.on("signalk:delta", function(delta) { | |
if (that.timestamp) { | |
delta.updates.forEach(update => { | |
update.timestamp = that.timestamp; | |
}); | |
} | |
// debug(`Got delta: "${JSON.stringify(delta)}"`) | |
that.push(delta); | |
}); | |
} | |
require("util").inherits(ToSignalK, Transform); | |
ToSignalK.prototype._transform = function(chunk, encoding, done) { | |
try { | |
if (typeof chunk === "object" && typeof chunk.line === "string") { | |
this.timestamp = new Date(Number(chunk.timestamp)); | |
this.parser.parse(chunk.line + "\n"); | |
} else { | |
try { | |
this.parser.parse(chunk + "\n") | |
.then(() => { | |
debug(`Parsed line: "${chunk.trim()}"`); | |
}) | |
.catch(err => { | |
debug(`Error parsing line: "${chunk.trim()}": ${err.message}`); | |
}) | |
} catch (e) {} | |
} | |
} catch (ex) { | |
console.error(ex); | |
} | |
done(); | |
}; | |
module.exports = ToSignalK; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment