@canboat/canboatjs 3.0.2 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/test.yml +2 -2
- package/.github/workflows/test_canboat_changes.yml +91 -0
- package/.husky/pre-commit +4 -0
- package/.mocharc.js +7 -0
- package/.prettierrc.json +5 -0
- package/README.md +2 -7
- package/dist/actisense-serial.d.ts +17 -0
- package/dist/actisense-serial.d.ts.map +1 -0
- package/dist/actisense-serial.js +569 -0
- package/dist/actisense-serial.js.map +1 -0
- package/dist/bin/actisense-file.d.ts +3 -0
- package/dist/bin/actisense-file.d.ts.map +1 -0
- package/dist/bin/actisense-file.js +49 -0
- package/dist/bin/actisense-file.js.map +1 -0
- package/dist/bin/actisense-n2k-tcp.d.ts +3 -0
- package/dist/bin/actisense-n2k-tcp.d.ts.map +1 -0
- package/dist/bin/actisense-n2k-tcp.js +46 -0
- package/dist/bin/actisense-n2k-tcp.js.map +1 -0
- package/dist/bin/actisense-serialjs.d.ts +3 -0
- package/dist/bin/actisense-serialjs.d.ts.map +1 -0
- package/dist/bin/actisense-serialjs.js +51 -0
- package/dist/bin/actisense-serialjs.js.map +1 -0
- package/dist/bin/analyzerjs.d.ts +3 -0
- package/dist/bin/analyzerjs.d.ts.map +1 -0
- package/dist/bin/analyzerjs.js +61 -0
- package/dist/bin/analyzerjs.js.map +1 -0
- package/dist/bin/candumpanalyzerjs.d.ts +3 -0
- package/dist/bin/candumpanalyzerjs.d.ts.map +1 -0
- package/dist/bin/candumpanalyzerjs.js +31 -0
- package/dist/bin/candumpanalyzerjs.js.map +1 -0
- package/dist/bin/candumpjs.d.ts +3 -0
- package/dist/bin/candumpjs.d.ts.map +1 -0
- package/dist/bin/candumpjs.js +73 -0
- package/dist/bin/candumpjs.js.map +1 -0
- package/dist/bin/cansend.d.ts +3 -0
- package/dist/bin/cansend.d.ts.map +1 -0
- package/dist/bin/cansend.js +123 -0
- package/dist/bin/cansend.js.map +1 -0
- package/dist/bin/ikonvert-serial.d.ts +3 -0
- package/dist/bin/ikonvert-serial.d.ts.map +1 -0
- package/dist/bin/ikonvert-serial.js +36 -0
- package/dist/bin/ikonvert-serial.js.map +1 -0
- package/dist/bin/to-pgn.d.ts +3 -0
- package/dist/bin/to-pgn.d.ts.map +1 -0
- package/dist/bin/to-pgn.js +57 -0
- package/dist/bin/to-pgn.js.map +1 -0
- package/dist/canId.d.ts +21 -0
- package/dist/canId.d.ts.map +1 -0
- package/dist/canId.js +66 -0
- package/dist/canId.js.map +1 -0
- package/dist/canId.test.d.ts +2 -0
- package/dist/canId.test.d.ts.map +1 -0
- package/dist/canId.test.js +58 -0
- package/dist/canId.test.js.map +1 -0
- package/dist/canbus.d.ts +17 -0
- package/dist/canbus.d.ts.map +1 -0
- package/dist/canbus.js +261 -0
- package/dist/canbus.js.map +1 -0
- package/dist/candevice.d.ts +23 -0
- package/dist/candevice.d.ts.map +1 -0
- package/dist/candevice.js +38 -0
- package/dist/candevice.js.map +1 -0
- package/dist/codes.d.ts +2 -0
- package/dist/codes.d.ts.map +1 -0
- package/dist/codes.js +24 -0
- package/dist/codes.js.map +1 -0
- package/dist/discovery.d.ts +17 -0
- package/dist/discovery.d.ts.map +1 -0
- package/dist/discovery.js +119 -0
- package/dist/discovery.js.map +1 -0
- package/dist/fromPgn.d.ts +51 -0
- package/dist/fromPgn.d.ts.map +1 -0
- package/dist/fromPgn.js +989 -0
- package/dist/fromPgn.js.map +1 -0
- package/dist/fromPgnStream.d.ts +17 -0
- package/dist/fromPgnStream.d.ts.map +1 -0
- package/dist/fromPgnStream.js +47 -0
- package/dist/fromPgnStream.js.map +1 -0
- package/dist/ikonvert.d.ts +17 -0
- package/dist/ikonvert.d.ts.map +1 -0
- package/dist/ikonvert.js +224 -0
- package/dist/ikonvert.js.map +1 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +60 -0
- package/dist/index.js.map +1 -0
- package/dist/n2k-actisense.d.ts +24 -0
- package/dist/n2k-actisense.d.ts.map +1 -0
- package/dist/n2k-actisense.js +126 -0
- package/dist/n2k-actisense.js.map +1 -0
- package/dist/n2k-actisense.test.d.ts +2 -0
- package/dist/n2k-actisense.test.d.ts.map +1 -0
- package/dist/n2k-actisense.test.js +49 -0
- package/dist/n2k-actisense.test.js.map +1 -0
- package/dist/n2kDevice.d.ts +40 -0
- package/dist/n2kDevice.d.ts.map +1 -0
- package/dist/n2kDevice.js +387 -0
- package/dist/n2kDevice.js.map +1 -0
- package/dist/ncanbus.d.ts +17 -0
- package/dist/ncanbus.d.ts.map +1 -0
- package/dist/ncanbus.js +92 -0
- package/dist/ncanbus.js.map +1 -0
- package/dist/pgns.d.ts +22 -0
- package/dist/pgns.d.ts.map +1 -0
- package/dist/pgns.js +157 -0
- package/dist/pgns.js.map +1 -0
- package/dist/pgns.test.d.ts +2 -0
- package/dist/pgns.test.d.ts.map +1 -0
- package/dist/pgns.test.js +14 -0
- package/dist/pgns.test.js.map +1 -0
- package/dist/simpleCan.d.ts +17 -0
- package/dist/simpleCan.d.ts.map +1 -0
- package/dist/simpleCan.js +125 -0
- package/dist/simpleCan.js.map +1 -0
- package/dist/stringMsg.d.ts +33 -0
- package/dist/stringMsg.d.ts.map +1 -0
- package/dist/stringMsg.js +359 -0
- package/dist/stringMsg.js.map +1 -0
- package/dist/stringMsg.test.d.ts +2 -0
- package/dist/stringMsg.test.d.ts.map +1 -0
- package/dist/stringMsg.test.js +244 -0
- package/dist/stringMsg.test.js.map +1 -0
- package/dist/toPgn.d.ts +34 -0
- package/dist/toPgn.d.ts.map +1 -0
- package/dist/toPgn.js +500 -0
- package/dist/toPgn.js.map +1 -0
- package/dist/utilities.d.ts +26 -0
- package/dist/utilities.d.ts.map +1 -0
- package/dist/utilities.js +112 -0
- package/dist/utilities.js.map +1 -0
- package/dist/utilities.test.d.ts +2 -0
- package/dist/utilities.test.d.ts.map +1 -0
- package/dist/utilities.test.js +9 -0
- package/dist/utilities.test.js.map +1 -0
- package/dist/venus-mqtt.d.ts +11 -0
- package/dist/venus-mqtt.d.ts.map +1 -0
- package/dist/venus-mqtt.js +100 -0
- package/dist/venus-mqtt.js.map +1 -0
- package/dist/venus.d.ts +11 -0
- package/dist/venus.d.ts.map +1 -0
- package/dist/venus.js +71 -0
- package/dist/venus.js.map +1 -0
- package/dist/w2k01.d.ts +17 -0
- package/dist/w2k01.d.ts.map +1 -0
- package/dist/w2k01.js +119 -0
- package/dist/w2k01.js.map +1 -0
- package/dist/yddevice.d.ts +25 -0
- package/dist/yddevice.d.ts.map +1 -0
- package/dist/yddevice.js +45 -0
- package/dist/yddevice.js.map +1 -0
- package/dist/ydgw02.d.ts +17 -0
- package/dist/ydgw02.d.ts.map +1 -0
- package/dist/ydgw02.js +163 -0
- package/dist/ydgw02.js.map +1 -0
- package/dist/ydvr.d.ts +17 -0
- package/dist/ydvr.d.ts.map +1 -0
- package/dist/ydvr.js +120 -0
- package/dist/ydvr.js.map +1 -0
- package/eslint.config.js +58 -0
- package/jest.config.js +5 -0
- package/lib/actisense-serial.ts +644 -0
- package/lib/bin/actisense-file.ts +53 -0
- package/lib/bin/actisense-n2k-tcp.ts +50 -0
- package/lib/bin/actisense-serialjs.ts +55 -0
- package/{bin/analyzerjs → lib/bin/analyzerjs.ts} +16 -15
- package/{bin/candumpjs → lib/bin/candumpjs.ts} +19 -20
- package/lib/bin/cansend.ts +131 -0
- package/lib/bin/ikonvert-serial.ts +44 -0
- package/{bin/to-pgn → lib/bin/to-pgn.ts} +23 -17
- package/lib/bin/ydvr-file +33 -0
- package/lib/canId.test.js +35 -15
- package/lib/canId.ts +84 -0
- package/lib/canbus.ts +293 -0
- package/lib/{candevice.js → candevice.ts} +14 -21
- package/lib/codes.ts +21 -0
- package/lib/{discovery.js → discovery.ts} +32 -30
- package/lib/fromPgn.ts +1207 -0
- package/lib/{fromPgnStream.js → fromPgnStream.ts} +17 -12
- package/lib/{ikonvert.js → ikonvert.ts} +90 -85
- package/lib/index.ts +48 -0
- package/lib/n2k-actisense.test.js +31 -32
- package/lib/n2k-actisense.ts +152 -0
- package/lib/n2kDevice.ts +458 -0
- package/lib/pgns.test.ts +12 -0
- package/lib/pgns.ts +191 -0
- package/lib/simpleCan.ts +140 -0
- package/lib/stringMsg.test.js +58 -41
- package/lib/stringMsg.ts +464 -0
- package/lib/toPgn.ts +597 -0
- package/lib/{utilities.js → utilities.ts} +48 -37
- package/lib/venus-mqtt.js +69 -73
- package/lib/venus.js +8 -8
- package/lib/{w2k01.js → w2k01.ts} +57 -44
- package/lib/{yddevice.js → yddevice.ts} +18 -13
- package/lib/{ydgw02.js → ydgw02.ts} +55 -47
- package/lib/ydvr.js +65 -147
- package/package.json +54 -18
- package/tsconfig.json +19 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/bin/actisense-file +0 -47
- package/bin/actisense-n2k-tcp +0 -45
- package/bin/actisense-serialjs +0 -52
- package/bin/candumpanalyzerjs +0 -29
- package/bin/cansend +0 -130
- package/bin/ikonvert-serial +0 -50
- package/bin/ydgw-analyzerjs +0 -36
- package/bin/ydvr-file +0 -33
- package/index.js +0 -53
- package/lib/canId.js +0 -64
- package/lib/canbus.js +0 -278
- package/lib/codes.js +0 -69
- package/lib/codes.test.js +0 -17
- package/lib/codesMfgs.json +0 -166
- package/lib/fromPgn.js +0 -1027
- package/lib/n2k-actisense.js +0 -108
- package/lib/n2kDevice.js +0 -390
- package/lib/pgns.js +0 -196
- package/lib/pgns.test.js +0 -13
- package/lib/serial.js +0 -647
- package/lib/simpleCan.js +0 -105
- package/lib/stringMsg.js +0 -339
- package/lib/toPgn.js +0 -518
package/lib/serial.js
DELETED
|
@@ -1,647 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright 2018 Scott Bender (scott@scottbender.net)
|
|
3
|
-
*
|
|
4
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
-
* you may not use this file except in compliance with the License.
|
|
6
|
-
* You may obtain a copy of the License at
|
|
7
|
-
*
|
|
8
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
-
*
|
|
10
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
-
* See the License for the specific language governing permissions and
|
|
14
|
-
* limitations under the License.
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
|
-
const debug = require('debug')('signalk:actisense-serial')
|
|
18
|
-
const debugOut = require('debug')('signalk:actisense-out')
|
|
19
|
-
const Transform = require('stream').Transform
|
|
20
|
-
const isArray = require('lodash').isArray
|
|
21
|
-
const BitStream = require('bit-buffer').BitStream
|
|
22
|
-
const BitView = require('bit-buffer').BitView
|
|
23
|
-
const { toPgn } = require('./toPgn')
|
|
24
|
-
const { encodeActisense } = require('./stringMsg')
|
|
25
|
-
const { defaultTransmitPGNs } = require('./codes')
|
|
26
|
-
const _ = require('lodash')
|
|
27
|
-
const FromPgn = require('./fromPgn').Parser
|
|
28
|
-
|
|
29
|
-
/* ASCII characters used to mark packet start/stop */
|
|
30
|
-
|
|
31
|
-
const STX = 0x02 /* Start packet */
|
|
32
|
-
const ETX = 0x03 /* End packet */
|
|
33
|
-
const DLE = 0x10 /* Start pto encode a STX or ETX send DLE+STX or DLE+ETX */
|
|
34
|
-
const ESC = 0x1B /* Escape */
|
|
35
|
-
|
|
36
|
-
/* Actisense message structure is:
|
|
37
|
-
|
|
38
|
-
DLE STX <command> <len> [<data> ...] <checksum> DLE ETX
|
|
39
|
-
|
|
40
|
-
<command> is a byte from the list below.
|
|
41
|
-
In <data> any DLE characters are double escaped (DLE DLE).
|
|
42
|
-
<len> encodes the unescaped length.
|
|
43
|
-
<checksum> is such that the sum of all unescaped data bytes plus the command
|
|
44
|
-
byte plus the length adds up to zero, modulo 256.
|
|
45
|
-
*/
|
|
46
|
-
|
|
47
|
-
const N2K_MSG_RECEIVED = 0x93 /* Receive standard N2K message */
|
|
48
|
-
const N2K_MSG_SEND = 0x94 /* Send N2K message */
|
|
49
|
-
const NGT_MSG_RECEIVED = 0xA0 /* Receive NGT specific message */
|
|
50
|
-
const NGT_MSG_SEND = 0xA1 /* Send NGT message */
|
|
51
|
-
|
|
52
|
-
const MSG_START = 1
|
|
53
|
-
const MSG_ESCAPE = 2
|
|
54
|
-
const MSG_MESSAGE = 3
|
|
55
|
-
|
|
56
|
-
const NGT_STARTUP_MSG = new Uint8Array([0x11, 0x02, 0x00])
|
|
57
|
-
|
|
58
|
-
function SerialStream (options) {
|
|
59
|
-
if (!(this instanceof SerialStream)) {
|
|
60
|
-
return new SerialStream(options)
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
Transform.call(this, {
|
|
64
|
-
objectMode: true
|
|
65
|
-
})
|
|
66
|
-
|
|
67
|
-
debug('options: %j', options)
|
|
68
|
-
|
|
69
|
-
this.reconnect = options.reconnect || true
|
|
70
|
-
this.serial = null
|
|
71
|
-
this.options = options
|
|
72
|
-
this.transmitPGNRetries = 2
|
|
73
|
-
|
|
74
|
-
this.transmitPGNs = defaultTransmitPGNs
|
|
75
|
-
if ( this.options.transmitPGNs ) {
|
|
76
|
-
this.transmitPGNs = _.union(this.transmitPGNs,
|
|
77
|
-
this.options.transmitPGNs)
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
this.options.disableSetTransmitPGNs = true
|
|
81
|
-
|
|
82
|
-
if ( process.env.DISABLESETTRANSMITPGNS ) {
|
|
83
|
-
this.options.disableSetTransmitPGNs = true
|
|
84
|
-
}
|
|
85
|
-
if ( process.env.ENABLESETTRANSMITPGNS ) {
|
|
86
|
-
this.options.disableSetTransmitPGNs = false
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
this.start()
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
require('util').inherits(SerialStream, Transform)
|
|
93
|
-
|
|
94
|
-
SerialStream.prototype.start = function () {
|
|
95
|
-
if (this.serial !== null) {
|
|
96
|
-
this.serial.unpipe(this)
|
|
97
|
-
this.serial.removeAllListeners()
|
|
98
|
-
this.serial = null
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
if (this.reconnect === false) {
|
|
102
|
-
return
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
this.buffer = Buffer.alloc(500)
|
|
106
|
-
this.bufferOffset = 0
|
|
107
|
-
this.isFile = false
|
|
108
|
-
this.state = MSG_START
|
|
109
|
-
|
|
110
|
-
if ( typeof this.reconnectDelay === 'undefined' ) {
|
|
111
|
-
this.reconnectDelay = 1000
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
if ( !this.options.fromFile ) {
|
|
115
|
-
const { SerialPort } = require('serialport')
|
|
116
|
-
this.serial = new SerialPort({
|
|
117
|
-
path: this.options.device,
|
|
118
|
-
baudRate: this.options.baudrate || 115200
|
|
119
|
-
})
|
|
120
|
-
|
|
121
|
-
const setProviderStatus = this.options.app && this.options.app.setProviderStatus
|
|
122
|
-
? (msg) => {
|
|
123
|
-
this.options.app.setProviderStatus(this.options.providerId, msg)
|
|
124
|
-
}
|
|
125
|
-
: () => {}
|
|
126
|
-
const setProviderError = this.options.app && this.options.app.setProviderError
|
|
127
|
-
? (msg) => {
|
|
128
|
-
this.options.app.setProviderError(this.options.providerId, msg)
|
|
129
|
-
}
|
|
130
|
-
: () => {}
|
|
131
|
-
this.setProviderStatus = setProviderStatus
|
|
132
|
-
|
|
133
|
-
var that = this
|
|
134
|
-
|
|
135
|
-
this.serial.on('data', (data) => {
|
|
136
|
-
try {
|
|
137
|
-
readData(this, data)
|
|
138
|
-
} catch ( err ) {
|
|
139
|
-
setProviderError(err.message)
|
|
140
|
-
console.error(err)
|
|
141
|
-
}
|
|
142
|
-
})
|
|
143
|
-
|
|
144
|
-
if ( this.options.app ) {
|
|
145
|
-
function writeString(msg) {
|
|
146
|
-
debugOut(`sending ${msg}`)
|
|
147
|
-
var buf = parseInput(msg)
|
|
148
|
-
buf = composeMessage(N2K_MSG_SEND, buf, buf.length)
|
|
149
|
-
debugOut(buf)
|
|
150
|
-
that.serial.write(buf)
|
|
151
|
-
that.options.app.emit('connectionwrite', { providerId: that.options.providerId })
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
function writeObject(msg) {
|
|
155
|
-
var data = toPgn(msg)
|
|
156
|
-
var actisense = encodeActisense({ pgn: msg.pgn, data, dst: msg.dst})
|
|
157
|
-
debugOut(`sending ${actisense}`)
|
|
158
|
-
var buf = parseInput(actisense)
|
|
159
|
-
buf = composeMessage(N2K_MSG_SEND, buf, buf.length)
|
|
160
|
-
debugOut(buf)
|
|
161
|
-
that.serial.write(buf)
|
|
162
|
-
that.options.app.emit('connectionwrite', { providerId: that.options.providerId })
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
this.options.app.on(this.options.outEevent || 'nmea2000out', msg => {
|
|
166
|
-
if ( this.outAvailable ) {
|
|
167
|
-
if ( typeof msg === 'string' ) {
|
|
168
|
-
writeString(msg)
|
|
169
|
-
} else {
|
|
170
|
-
writeObject(msg)
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
})
|
|
174
|
-
|
|
175
|
-
this.options.app.on(this.options.jsonOutEvent || 'nmea2000JsonOut', msg => {
|
|
176
|
-
if ( this.outAvailable ) {
|
|
177
|
-
writeObject(msg)
|
|
178
|
-
}
|
|
179
|
-
})
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
this.outAvailable = false
|
|
183
|
-
|
|
184
|
-
this.serial.on('error', function (x) {
|
|
185
|
-
setProviderError(x.message)
|
|
186
|
-
console.log(x)
|
|
187
|
-
that.scheduleReconnect()
|
|
188
|
-
})
|
|
189
|
-
this.serial.on('close', () => {
|
|
190
|
-
setProviderError('Closed, reconnecting...')
|
|
191
|
-
//this.start.bind(this)
|
|
192
|
-
that.scheduleReconnect()
|
|
193
|
-
})
|
|
194
|
-
this.serial.on(
|
|
195
|
-
'open',
|
|
196
|
-
function () {
|
|
197
|
-
try {
|
|
198
|
-
this.reconnectDelay = 1000
|
|
199
|
-
setProviderStatus(`Connected to ${that.options.device}`)
|
|
200
|
-
var buf = composeMessage(NGT_MSG_SEND, Buffer.from(NGT_STARTUP_MSG), NGT_STARTUP_MSG.length)
|
|
201
|
-
debugOut(buf)
|
|
202
|
-
that.serial.write(buf)
|
|
203
|
-
debug('sent startup message')
|
|
204
|
-
that.gotStartupResponse = false
|
|
205
|
-
if ( that.options.disableSetTransmitPGNs ) {
|
|
206
|
-
enableOutput(that)
|
|
207
|
-
} else {
|
|
208
|
-
setTimeout(() => {
|
|
209
|
-
if ( that.gotStartupResponse === false ) {
|
|
210
|
-
debug('retry startup message...')
|
|
211
|
-
debugOut(bug)
|
|
212
|
-
that.serial.write(buf)
|
|
213
|
-
}
|
|
214
|
-
}, 5000)
|
|
215
|
-
}
|
|
216
|
-
} catch ( err ) {
|
|
217
|
-
setProviderError(err.message)
|
|
218
|
-
console.error(err)
|
|
219
|
-
console.error(err.stack)
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
)
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
SerialStream.prototype.scheduleReconnect = function () {
|
|
227
|
-
this.reconnectDelay *= this.reconnectDelay < 60 * 1000 ? 1.5 : 1
|
|
228
|
-
const msg = `Not connected (retry delay ${(
|
|
229
|
-
this.reconnectDelay / 1000
|
|
230
|
-
).toFixed(0)} s)`
|
|
231
|
-
debug(msg)
|
|
232
|
-
this.setProviderStatus(msg)
|
|
233
|
-
setTimeout(this.start.bind(this), this.reconnectDelay)
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
function readData(that, data) {
|
|
237
|
-
for ( var i = 0; i < data.length; i++ ) {
|
|
238
|
-
//console.log(data[i])
|
|
239
|
-
read1Byte(that, data[i])
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
function read1Byte(that, c)
|
|
244
|
-
{
|
|
245
|
-
var startEscape = false;
|
|
246
|
-
var noEscape = false;
|
|
247
|
-
|
|
248
|
-
//debug("received byte %02x state=%d offset=%d\n", c, state, head - buf);
|
|
249
|
-
|
|
250
|
-
if (that.stat == MSG_START)
|
|
251
|
-
{
|
|
252
|
-
if ((c == ESC) && that.isFile)
|
|
253
|
-
{
|
|
254
|
-
noEscape = true;
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
if (that.stat == MSG_ESCAPE)
|
|
259
|
-
{
|
|
260
|
-
if (c == ETX)
|
|
261
|
-
{
|
|
262
|
-
if ( !that.options.outputOnly ) {
|
|
263
|
-
if ( that.buffer[0] == N2K_MSG_RECEIVED ) {
|
|
264
|
-
processN2KMessage(that, that.buffer, that.bufferOffset)
|
|
265
|
-
} else if ( that.buffer[0] == NGT_MSG_RECEIVED) {
|
|
266
|
-
processNTGMessage(that, that.buffer, that.bufferOffset)
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
that.bufferOffset = 0
|
|
270
|
-
that.stat = MSG_START;
|
|
271
|
-
}
|
|
272
|
-
else if (c == STX)
|
|
273
|
-
{
|
|
274
|
-
that.bufferOffset = 0
|
|
275
|
-
that.stat = MSG_MESSAGE;
|
|
276
|
-
}
|
|
277
|
-
else if ((c == DLE) || ((c == ESC) && that.isFile) || that.noEscape)
|
|
278
|
-
{
|
|
279
|
-
that.buffer.writeUInt8(c, that.bufferOffset)
|
|
280
|
-
that.bufferOffset++
|
|
281
|
-
that.stat = MSG_MESSAGE;
|
|
282
|
-
}
|
|
283
|
-
else
|
|
284
|
-
{
|
|
285
|
-
console.error("DLE followed by unexpected char , ignore message");
|
|
286
|
-
that.stat = MSG_START;
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
else if (that.stat == MSG_MESSAGE)
|
|
290
|
-
{
|
|
291
|
-
if (c == DLE)
|
|
292
|
-
{
|
|
293
|
-
that.stat = MSG_ESCAPE;
|
|
294
|
-
}
|
|
295
|
-
else if (that.isFile && (c == ESC) && !noEscape)
|
|
296
|
-
{
|
|
297
|
-
that.stat = MSG_ESCAPE;
|
|
298
|
-
}
|
|
299
|
-
else
|
|
300
|
-
{
|
|
301
|
-
that.buffer.writeUInt8(c, that.bufferOffset)
|
|
302
|
-
that.bufferOffset++
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
else
|
|
306
|
-
{
|
|
307
|
-
if (c == DLE)
|
|
308
|
-
{
|
|
309
|
-
that.stat = MSG_ESCAPE;
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
function enableTXPGN(serial, pgn) {
|
|
315
|
-
debug('enabling pgn %d', pgn)
|
|
316
|
-
const msg = composeEnablePGN(pgn)
|
|
317
|
-
debugOut(msg)
|
|
318
|
-
serial.write(msg)
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
function enableOutput(that) {
|
|
322
|
-
debug('outputEnabled')
|
|
323
|
-
that.outAvailable = true
|
|
324
|
-
if ( that.options.app ) {
|
|
325
|
-
that.options.app.emit('nmea2000OutAvailable')
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
function requestTransmitPGNList(that) {
|
|
330
|
-
debug('request tx pgns...')
|
|
331
|
-
const requestMsg = composeRequestTXPGNList()
|
|
332
|
-
debugOut(requestMsg)
|
|
333
|
-
that.serial.write(requestMsg)
|
|
334
|
-
setTimeout(() => {
|
|
335
|
-
if ( !that.gotTXPGNList ) {
|
|
336
|
-
if ( that.transmitPGNRetries-- > 0 ) {
|
|
337
|
-
debug('did not get tx pgn list, retrying...')
|
|
338
|
-
requestTransmitPGNList(that)
|
|
339
|
-
} else {
|
|
340
|
-
const msg = 'could not set transmit pgn list'
|
|
341
|
-
that.options.app.setProviderStatus(msg)
|
|
342
|
-
console.warn(msg)
|
|
343
|
-
enableOutput(that)
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
}, 10000)
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
function processNTGMessage(that, buffer, len)
|
|
350
|
-
{
|
|
351
|
-
var checksum = 0
|
|
352
|
-
|
|
353
|
-
for ( var i = 0; i < len; i++ ) {
|
|
354
|
-
checksum = addUInt8(checksum, buffer[i])
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
const command = buffer[2]
|
|
358
|
-
|
|
359
|
-
if ( checksum != 0 ) {
|
|
360
|
-
debug('received message with invalid checksum (%d,%d)', command, len)
|
|
361
|
-
return
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
if ( that.options.sendNetworkStats || debug.enabled ) {
|
|
365
|
-
let newbuf = new Buffer.alloc(len + 7 )
|
|
366
|
-
var bs = new BitStream(newbuf)
|
|
367
|
-
const pgn = 0x40000 + buffer[2]
|
|
368
|
-
bs.writeUint8(0) //prio
|
|
369
|
-
bs.writeUint8(pgn)
|
|
370
|
-
bs.writeUint8(pgn >> 8)
|
|
371
|
-
bs.writeUint8(pgn >> 16)
|
|
372
|
-
bs.writeUint8(0) //dst
|
|
373
|
-
bs.writeUint8(0) //src
|
|
374
|
-
bs.writeUint32(0) //timestamp
|
|
375
|
-
bs.writeUint8(len-4)
|
|
376
|
-
buffer.copy(bs.view.buffer, bs.byteIndex, 3)
|
|
377
|
-
|
|
378
|
-
if ( that.options.plainText ) {
|
|
379
|
-
that.push(binToActisense(bs.view.buffer, len+7))
|
|
380
|
-
} else {
|
|
381
|
-
that.push(bs.view.buffer, len+7)
|
|
382
|
-
}
|
|
383
|
-
if ( debug.enabled && command != 0xf2 ) { //don't log system status
|
|
384
|
-
if ( !that.parser ) {
|
|
385
|
-
that.parser = new FromPgn()
|
|
386
|
-
}
|
|
387
|
-
const js = that.parser.parseBuffer(bs.view.buffer)
|
|
388
|
-
if ( js ) {
|
|
389
|
-
debug('got ntg message: %j', js)
|
|
390
|
-
}
|
|
391
|
-
}
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
if ( command === 0x11 ) {
|
|
395
|
-
//confirm startup
|
|
396
|
-
that.gotStartupResponse = true
|
|
397
|
-
debug('got startup response')
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
if ( !that.outAvailable ) {
|
|
401
|
-
if ( command === 0x11 ) {
|
|
402
|
-
that.gotTXPGNList = false
|
|
403
|
-
setTimeout(() => {
|
|
404
|
-
requestTransmitPGNList(that)
|
|
405
|
-
}, 2000)
|
|
406
|
-
} else if ( command === 0x49 && buffer[3] === 1 ) {
|
|
407
|
-
that.gotTXPGNList = true
|
|
408
|
-
const pgnCount = buffer[14];
|
|
409
|
-
let bv = new BitView(buffer.slice(15, that.bufferOffset));
|
|
410
|
-
let bs = new BitStream(bv)
|
|
411
|
-
let pgns = []
|
|
412
|
-
for ( let i = 0; i < pgnCount; i++ ) {
|
|
413
|
-
pgns.push(bs.readUint32())
|
|
414
|
-
}
|
|
415
|
-
debug('tx pgns: %j', pgns)
|
|
416
|
-
|
|
417
|
-
that.neededTransmitPGNs = that.transmitPGNs.filter(pgn => {
|
|
418
|
-
return pgns.indexOf(pgn) == -1
|
|
419
|
-
})
|
|
420
|
-
debug('needed pgns: %j', that.neededTransmitPGNs)
|
|
421
|
-
} else if ( command === 0x49 && buffer[3] === 4 ) {
|
|
422
|
-
//I think this means done receiving the pgns list
|
|
423
|
-
if ( that.neededTransmitPGNs ) {
|
|
424
|
-
if ( that.neededTransmitPGNs.length ) {
|
|
425
|
-
enableTXPGN(that.serial, that.neededTransmitPGNs[0])
|
|
426
|
-
} else {
|
|
427
|
-
enableOutput(that)
|
|
428
|
-
}
|
|
429
|
-
}
|
|
430
|
-
} else if ( command === 0x47 ) {
|
|
431
|
-
//response from enable a pgn
|
|
432
|
-
if ( buffer[3] === 1 ) {
|
|
433
|
-
debug('enabled %d', that.neededTransmitPGNs[0])
|
|
434
|
-
that.neededTransmitPGNs = that.neededTransmitPGNs.slice(1)
|
|
435
|
-
if ( that.neededTransmitPGNs.length === 0 ) {
|
|
436
|
-
const commitMsg = composeCommitTXPGN()
|
|
437
|
-
debugOut(commitMsg)
|
|
438
|
-
that.serial.write(commitMsg)
|
|
439
|
-
} else {
|
|
440
|
-
enableTXPGN(that.serial, that.neededTransmitPGNs[0])
|
|
441
|
-
}
|
|
442
|
-
} else {
|
|
443
|
-
debug('bad response from Enable TX: %d', buffer[3])
|
|
444
|
-
}
|
|
445
|
-
} else if ( command === 0x01 ) {
|
|
446
|
-
debug('commited tx list')
|
|
447
|
-
const activateMsg = composeActivateTXPGN()
|
|
448
|
-
debugOut(activateMsg)
|
|
449
|
-
that.serial.write(activateMsg)
|
|
450
|
-
} else if ( command === 0x4b ) {
|
|
451
|
-
debug('activated tx list')
|
|
452
|
-
enableOutput(that)
|
|
453
|
-
}
|
|
454
|
-
}
|
|
455
|
-
}
|
|
456
|
-
|
|
457
|
-
function addUInt8(num, add) {
|
|
458
|
-
if ( num + add > 255 ) {
|
|
459
|
-
num = add - (256-num)
|
|
460
|
-
} else {
|
|
461
|
-
num += add
|
|
462
|
-
}
|
|
463
|
-
return num
|
|
464
|
-
}
|
|
465
|
-
|
|
466
|
-
function processN2KMessage(that, buffer, len)
|
|
467
|
-
{
|
|
468
|
-
|
|
469
|
-
var checksum = 0
|
|
470
|
-
|
|
471
|
-
for ( var i = 0; i < len; i++ ) {
|
|
472
|
-
checksum = addUInt8(checksum, buffer[i])
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
if ( checksum != 0 ) {
|
|
476
|
-
debug('received message with invalid checksum')
|
|
477
|
-
return
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
if ( that.options.plainText ) {
|
|
482
|
-
that.push(binToActisense(buffer.slice(2, len)))
|
|
483
|
-
} else {
|
|
484
|
-
that.push(buffer.slice(2, len))
|
|
485
|
-
}
|
|
486
|
-
}
|
|
487
|
-
|
|
488
|
-
function binToActisense(buffer) {
|
|
489
|
-
var bv = new BitView(buffer);
|
|
490
|
-
var bs = new BitStream(bv)
|
|
491
|
-
|
|
492
|
-
var pgn = {}
|
|
493
|
-
|
|
494
|
-
pgn.prio = bs.readUint8()
|
|
495
|
-
pgn.pgn = bs.readUint8() + 256 * (bs.readUint8() + 256 * bs.readUint8());
|
|
496
|
-
pgn.dst = bs.readUint8()
|
|
497
|
-
pgn.src = bs.readUint8()
|
|
498
|
-
pgn.timestamp = bs.readUint32()
|
|
499
|
-
var len = bs.readUint8()
|
|
500
|
-
return (
|
|
501
|
-
new Date().toISOString() +
|
|
502
|
-
`,${pgn.prio},${pgn.pgn},${pgn.src},${pgn.dst},${len},` +
|
|
503
|
-
new Uint32Array(buffer.slice(11, 11+len))
|
|
504
|
-
.reduce(function(acc, i) {
|
|
505
|
-
acc.push(i.toString(16));
|
|
506
|
-
return acc;
|
|
507
|
-
}, [])
|
|
508
|
-
.map(x => (x.length === 1 ? "0" + x : x))
|
|
509
|
-
.join(",")
|
|
510
|
-
);
|
|
511
|
-
}
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
function composeMessage(command, buffer, len)
|
|
515
|
-
{
|
|
516
|
-
var outBuf = Buffer.alloc(500);
|
|
517
|
-
var out = new BitStream(outBuf)
|
|
518
|
-
|
|
519
|
-
out.writeUint8(DLE)
|
|
520
|
-
out.writeUint8(STX)
|
|
521
|
-
out.writeUint8(command)
|
|
522
|
-
|
|
523
|
-
var lenPos = out.byteIndex
|
|
524
|
-
out.writeUint8(0) //length. will update later
|
|
525
|
-
var crc = command;
|
|
526
|
-
|
|
527
|
-
for (var i = 0; i < len; i++)
|
|
528
|
-
{
|
|
529
|
-
var c = buffer.readUInt8(i)
|
|
530
|
-
if (c == DLE)
|
|
531
|
-
{
|
|
532
|
-
out.writeUint8(DLE);
|
|
533
|
-
}
|
|
534
|
-
out.writeUint8(c)
|
|
535
|
-
crc = addUInt8(crc, c)
|
|
536
|
-
}
|
|
537
|
-
|
|
538
|
-
crc = addUInt8(crc, len)
|
|
539
|
-
|
|
540
|
-
out.writeUint8(256-crc)
|
|
541
|
-
out.writeUint8(DLE)
|
|
542
|
-
out.writeUint8(ETX)
|
|
543
|
-
|
|
544
|
-
out.view.buffer.writeUInt8(len, lenPos)
|
|
545
|
-
|
|
546
|
-
//debug(`command ${out.view.buffer[2]} ${lenPos} ${len} ${out.view.buffer[lenPos]} ${out.view.buffer.length} ${out.byteIndex}`)
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
return out.view.buffer.slice(0, out.byteIndex)
|
|
550
|
-
}
|
|
551
|
-
|
|
552
|
-
function parseInput(msg)
|
|
553
|
-
{
|
|
554
|
-
var split = msg.split(',')
|
|
555
|
-
var buffer = Buffer.alloc(500)
|
|
556
|
-
var bs = new BitStream(buffer)
|
|
557
|
-
|
|
558
|
-
var prio = Number(split[1])
|
|
559
|
-
var pgn = Number(split[2])
|
|
560
|
-
var dst = Number(split[4])
|
|
561
|
-
var bytes = Number(split[5])
|
|
562
|
-
|
|
563
|
-
bs.writeUint8(prio)
|
|
564
|
-
bs.writeUint8(pgn)
|
|
565
|
-
bs.writeUint8(pgn >> 8)
|
|
566
|
-
bs.writeUint8(pgn >> 16)
|
|
567
|
-
bs.writeUint8(dst)
|
|
568
|
-
|
|
569
|
-
/*
|
|
570
|
-
bs.writeUint8(split[3])
|
|
571
|
-
bs.writeUint32(0)
|
|
572
|
-
*/
|
|
573
|
-
|
|
574
|
-
bs.writeUint8(bytes)
|
|
575
|
-
|
|
576
|
-
for ( var i = 6; i < (bytes+6); i++ ) {
|
|
577
|
-
bs.writeUint8(parseInt('0x' + split[i], 16))
|
|
578
|
-
}
|
|
579
|
-
|
|
580
|
-
return bs.view.buffer.slice(0, bs.byteIndex)
|
|
581
|
-
}
|
|
582
|
-
|
|
583
|
-
function composeCommitTXPGN() {
|
|
584
|
-
let msg = new Uint32Array([0x01])
|
|
585
|
-
return composeMessage(NGT_MSG_SEND, Buffer.from(msg), msg.length)
|
|
586
|
-
}
|
|
587
|
-
|
|
588
|
-
function composeActivateTXPGN() {
|
|
589
|
-
let msg = new Uint32Array([0x4b])
|
|
590
|
-
return composeMessage(NGT_MSG_SEND, Buffer.from(msg), msg.length)
|
|
591
|
-
}
|
|
592
|
-
|
|
593
|
-
function composeRequestTXPGNList() {
|
|
594
|
-
let msg = new Uint32Array([0x49])
|
|
595
|
-
return composeMessage(NGT_MSG_SEND, Buffer.from(msg), msg.length)
|
|
596
|
-
}
|
|
597
|
-
|
|
598
|
-
function composeEnablePGN(pgn) {
|
|
599
|
-
var outBuf = Buffer.alloc(14);
|
|
600
|
-
let out = new BitStream(outBuf)
|
|
601
|
-
out.writeUint8(0x47)
|
|
602
|
-
out.writeUint32(pgn)
|
|
603
|
-
out.writeUint8(1) //enabled
|
|
604
|
-
|
|
605
|
-
out.writeUint32(0xfffffffe)
|
|
606
|
-
out.writeUint32(0xfffffffe)
|
|
607
|
-
|
|
608
|
-
let res = composeMessage(NGT_MSG_SEND, out.view.buffer.slice(0, out.byteIndex), out.byteIndex)
|
|
609
|
-
|
|
610
|
-
//debug('composeEnablePGN: %o', res)
|
|
611
|
-
|
|
612
|
-
return res;
|
|
613
|
-
}
|
|
614
|
-
|
|
615
|
-
function composeDisablePGN(pgn) {
|
|
616
|
-
var outBuf = Buffer.alloc(14);
|
|
617
|
-
let out = new BitStream(outBuf)
|
|
618
|
-
out.writeUint8(0x47)
|
|
619
|
-
out.writeUint32(pgn)
|
|
620
|
-
out.writeUint8(0) //disabled
|
|
621
|
-
|
|
622
|
-
//disbale system time
|
|
623
|
-
//10 02 a1 0e 47 10 10 f0 01 00 00 e8 03 00 00 00 00 00 00 1e 10 03
|
|
624
|
-
|
|
625
|
-
out.writeUint32(0x000003e8) //???
|
|
626
|
-
out.writeUint32(0x00)
|
|
627
|
-
|
|
628
|
-
let res = composeMessage(NGT_MSG_SEND, out.view.buffer.slice(0, out.byteIndex), out.byteIndex)
|
|
629
|
-
|
|
630
|
-
debug('composeDisablePGN: %o', res)
|
|
631
|
-
|
|
632
|
-
return res;
|
|
633
|
-
}
|
|
634
|
-
|
|
635
|
-
SerialStream.prototype.end = function () {
|
|
636
|
-
if ( this.serial ) {
|
|
637
|
-
this.serial.close()
|
|
638
|
-
}
|
|
639
|
-
}
|
|
640
|
-
|
|
641
|
-
SerialStream.prototype._transform = function (chunk, encoding, done) {
|
|
642
|
-
debug(`got data ${typeof chunk}`)
|
|
643
|
-
readData(this, chunk)
|
|
644
|
-
done()
|
|
645
|
-
}
|
|
646
|
-
|
|
647
|
-
module.exports = SerialStream
|