-
Notifications
You must be signed in to change notification settings - Fork 41
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
- Loading branch information
Showing
7 changed files
with
260 additions
and
25 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
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,123 @@ | ||
/** | ||
* 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:canbus') | ||
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') | ||
|
||
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.sendPGN(msg) | ||
}) | ||
options.app.on('nmea2000JsonOut', (msg) => { | ||
that.sendPGN(msg) | ||
}) | ||
|
||
//this.options.app.emit('nmea2000OutAvailable') | ||
} | ||
} | ||
|
||
require('util').inherits(iKonvertStream, Transform) | ||
|
||
iKonvertStream.prototype.start = function () { | ||
} | ||
|
||
iKonvertStream.prototype.sendString = function (msg) { | ||
this.options.app.emit('ikonvertOut', msg + '\r\n') | ||
} | ||
|
||
iKonvertStream.prototype.sendPGN = function (pgn) { | ||
if ( this.cansend ) { | ||
let msg = toiKonvertSerialFormat(pgn) | ||
this.sendString(msg) | ||
} | ||
} | ||
|
||
iKonvertStream.prototype.setup = function () { | ||
let txPgns = '$PDGY,TX_LIST' | ||
defaultTransmitPGNs.forEach(pgn => { | ||
txPgns = txPgns + `,${pgn}` | ||
}) | ||
this.sendString(txPgns) | ||
this.sendString('$PDGY,N2NET_INIT,ALL') | ||
} | ||
|
||
iKonvertStream.prototype._transform = function (chunk, encoding, done) { | ||
let line = chunk.trim() | ||
|
||
if ( line.startsWith('$PDGY') ) { | ||
if ( line === '$PDGY,000000,,,,,,,' ) { | ||
//the iKonvert is not initialized | ||
this.setup() | ||
this.setProviderStatus('Initialized') | ||
} else if ( line.startsWith('$PDGY,000000') && !this.cansend ) { | ||
this.cansend = true; | ||
this.setProviderStatus('Connected') | ||
} 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 |
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,81 @@ | ||
debugger | ||
|
||
const chai = require('chai') | ||
chai.Should() | ||
chai.use(require('chai-things')) | ||
chai.use(require('chai-json-equal')); | ||
|
||
const { FromPgn, pgnToiKonvertSerialFormat } = require('../index') | ||
|
||
const pgns = [ | ||
{ | ||
pdgy: '!PDGY,126992,3,2,255,0.563,d2009e45b3b8821d', | ||
expected: { | ||
"pgn":126992, | ||
"src":2, | ||
"dst":255, | ||
"prio":3, | ||
"fields":{ | ||
"Date": "2141.11.12", | ||
"Reserved1": "6", | ||
"SID": 119, | ||
"Source": 13, | ||
"Time": "55:11:40.08140" | ||
}, | ||
"description":"System Time" | ||
} | ||
}, | ||
{ | ||
pdgy: '!PDGY,126996,6,0,255,4.19,3408276e4e4d4541203230303020504320496e7465726661636520284e47542d3129ffff312e3130302c20322e353630ffffffffffffffffffffffffffffffffffffffff4e47542d312d55534220206876312e3033ffffffffffffffffffffffffffffff313734323836ffffffffffffffffffffffffffffffffffffffffffffffffffff0201', | ||
expected: { | ||
pgn: 126996, | ||
src: 0, | ||
dst: 255, | ||
prio: 6, | ||
description: "Product Information" | ||
} | ||
} | ||
] | ||
|
||
const output = '!PDGY,126992,255,d2009e45b3Y=' | ||
|
||
describe('to ikconnect data converts', function () { | ||
it(`to 126992 converts`, function (done) { | ||
let msg = pgnToiKonvertSerialFormat(pgns[0].expected) | ||
msg.should.equal(output) | ||
done() | ||
}) | ||
}) | ||
|
||
describe('from ikconnect data converts', function () { | ||
|
||
pgns.forEach(info => { | ||
it(`from ${info.expected.pgn} converts`, function (done) { | ||
|
||
var fromPgn = new FromPgn() | ||
|
||
fromPgn.on('error', (pgn, error) => { | ||
console.error(`Error parsing ${pgn.pgn} ${error}`) | ||
console.error(error.stack) | ||
done(error) | ||
}) | ||
|
||
fromPgn.on('warning', (pgn, warning) => { | ||
done(new Error(`${pgn.pgn} ${warning}`)) | ||
}) | ||
|
||
fromPgn.on('pgn', (pgn) => { | ||
try { | ||
//console.log(JSON.stringify(pgn)) | ||
info.expected.timestamp = pgn.timestamp | ||
pgn.should.jsonEqual(info.expected) | ||
done() | ||
} catch ( e ) { | ||
done(e) | ||
} | ||
}) | ||
|
||
fromPgn.parseString(info.pdgy) | ||
}) | ||
}) | ||
}) |