-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: add support for Digital Yacht’s iKonvert (#28)
- Loading branch information
Showing
13 changed files
with
413 additions
and
26,444 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
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,49 @@ | ||
#!/usr/bin/env node | ||
|
||
const { Transform } = require('stream'); | ||
const SerialPort = require('serialport') | ||
|
||
if ( process.argv.length < 3 ) { | ||
console.error('Please specify a device') | ||
console.error('usage: ikonvert-serial [device] [baud,default:230400]') | ||
process.exit(1) | ||
} | ||
|
||
let device = process.argv[2] | ||
|
||
let baud = process.argv.length > 3 ? Number(process.argv[3]) : 230400 | ||
|
||
let serial = new SerialPort(device, { | ||
baudRate: baud | ||
}) | ||
|
||
const toStringTr = new Transform({ | ||
objectMode: true, | ||
|
||
transform(line, encoding, callback) { | ||
//this.push(JSON.stringify(chunk) + "\n"); | ||
console.log(line) | ||
|
||
if ( line.startsWith('$PDGY,000000,,,,,') ) { | ||
serial.write('$PDGY,N2NET_INIT,ALL\r\n') | ||
} | ||
|
||
callback(); | ||
} | ||
}); | ||
|
||
serial.on( | ||
'open', | ||
function () { | ||
const parser = new SerialPort.parsers.Readline() | ||
serial.pipe(parser).pipe(toStringTr) | ||
} | ||
) | ||
|
||
serial.on( | ||
'error', | ||
function (x) { | ||
console.log(x) | ||
} | ||
) | ||
|
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
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,175 @@ | ||
/** | ||
* Copyright 2018 Scott Bender (scott@scottbender.net) | ||
* | ||
* 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 debug = require('debug')('canboatjs:ikonvert') | ||
const Transform = require('stream').Transform | ||
const isArray = require('lodash').isArray | ||
const BitStream = require('bit-buffer').BitStream | ||
const BitView = require('bit-buffer').BitView | ||
const {toPgn, pgnToiKonvertSerialFormat} = require('./toPgn') | ||
const Parser = require('./fromPgn').Parser | ||
const _ = require('lodash') | ||
const CanDevice = require('./candevice') | ||
const spawn = require('child_process').spawn | ||
const { getPGNFromCanId, getCanIdFromPGN, actisenseSerialToBuffer, defaultTransmitPGNs } = require('./utilities') | ||
|
||
const pgnsSent = {} | ||
const rateLimit = 200 | ||
|
||
function iKonvertStream (options) { | ||
if (!(this instanceof iKonvertStream)) { | ||
return new iKonvertStream(options) | ||
} | ||
|
||
Transform.call(this, { | ||
objectMode: true | ||
}) | ||
|
||
this.plainText = false | ||
this.reconnect = options.reconnect || true | ||
this.options = options | ||
this.cansend = false | ||
this.buffer = new Buffer(500) | ||
this.bufferOffset = 0 | ||
this.start() | ||
|
||
this.setProviderStatus = options.app && options.app.setProviderStatus | ||
? (msg) => { | ||
options.app.setProviderStatus(options.providerId, msg) | ||
} | ||
: () => {} | ||
this.setProviderError = options.app && options.app.setProviderError | ||
? (msg) => { | ||
options.app.setProviderError(options.providerId, msg) | ||
} | ||
: () => {} | ||
|
||
var that = this | ||
|
||
if ( this.options.app ) { | ||
options.app.on('nmea2000out', (msg) => { | ||
that.sendActisensePGN(msg) | ||
}) | ||
options.app.on('nmea2000JsonOut', (msg) => { | ||
that.sendPGN(msg) | ||
}) | ||
|
||
this.sendString('$PDGY,N2NET_OFFLINE') | ||
|
||
debug('started') | ||
//this.options.app.emit('nmea2000OutAvailable') | ||
} | ||
} | ||
|
||
require('util').inherits(iKonvertStream, Transform) | ||
|
||
iKonvertStream.prototype.start = function () { | ||
} | ||
|
||
iKonvertStream.prototype.sendString = function (msg) { | ||
debug('sending %s', msg) | ||
this.options.app.emit('ikonvertOut', msg) | ||
} | ||
|
||
iKonvertStream.prototype.sendPGN = function (pgn) { | ||
if ( this.cansend ) { | ||
let now = Date.now() | ||
let lastSent = pgnsSent[pgn.pgn] | ||
if ( !lastSent || now - lastSent > rateLimit ) { | ||
let msg = pgnToiKonvertSerialFormat(pgn) | ||
this.sendString(msg) | ||
pgnsSent[pgn.pgn] = now | ||
} | ||
} | ||
} | ||
|
||
iKonvertStream.prototype.sendActisensePGN = function (msg) { | ||
if ( this.cansend ) { | ||
if ( !this.parser ) { | ||
this.parser = new Parser() | ||
|
||
let that = this | ||
this.parser.on('error', (pgn, error) => { | ||
console.error(`Error parsing ${pgn.pgn} ${error}`) | ||
console.error(error.stack) | ||
}) | ||
|
||
this.parser.on('pgn', (pgn) => { | ||
let now = Date.now() | ||
let lastSent = pgnsSent[pgn.pgn] | ||
if ( !lastSent || now - lastSent > rateLimit ) { | ||
let msg = pgnToiKonvertSerialFormat(pgn) | ||
that.sendString(msg) | ||
pgnsSent[pgn.pgn] = now | ||
} | ||
}) | ||
} | ||
this.parser.parseString(msg) | ||
} | ||
} | ||
|
||
iKonvertStream.prototype.setup = function () { | ||
let txPgns = '$PDGY,TX_LIST' | ||
defaultTransmitPGNs.forEach(pgn => { | ||
txPgns = txPgns + `,${pgn}` | ||
}) | ||
debug('sending pgn tx list') | ||
this.sendString(txPgns) | ||
} | ||
|
||
iKonvertStream.prototype._transform = function (chunk, encoding, done) { | ||
let line = chunk.toString().trim() | ||
line = line.substring(0, line.length) // take off the \r | ||
|
||
if ( line.startsWith('$PDGY') ) { | ||
if ( line === '$PDGY,000000,,,,,,' ) { | ||
//the iKonvert is not initialized | ||
if ( !this.didSetup ) { | ||
this.setup() | ||
this.setProviderStatus('Initializing...') | ||
this.didSetup = true | ||
} | ||
} else if ( line === '$PDGY,ACK,TX_LIST' ) { | ||
debug('sending net init') | ||
this.sendString('$PDGY,N2NET_INIT,ALL') | ||
this.setProviderStatus('Initialized...') | ||
} else if ( line === '$PDGY,ACK,N2NET_INIT,ALL' && !this.cansend ) { | ||
this.cansend = true; | ||
this.setProviderStatus('Connected') | ||
//this.sendString('$PDGY,SHOW_LISTS') | ||
} else if ( line.startsWith('$PDGY,TEXT') ) { | ||
debug(line) | ||
} else if ( line.startsWith('$PDGY,000000') ) { | ||
let parts = line.split(',') | ||
debug('ikonvert can address: %s', parts[6]) | ||
debug(line) | ||
} else if ( line.startsWith('$PDGY,NAK') ) { | ||
let parts = line.split(',') | ||
let msg = `iKonvert error ${parts[2]}: ${parts[3]}` | ||
console.error(msg) | ||
this.setProviderError(msg) | ||
} | ||
} else { | ||
this.push(line) | ||
} | ||
|
||
done() | ||
} | ||
|
||
iKonvertStream.prototype.end = function () { | ||
} | ||
|
||
module.exports = iKonvertStream |
Oops, something went wrong.