@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/canbus.ts
ADDED
|
@@ -0,0 +1,293 @@
|
|
|
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
|
+
import { PGN } from '@canboat/ts-pgns'
|
|
18
|
+
import { createDebug } from './utilities'
|
|
19
|
+
import { Transform } from 'stream'
|
|
20
|
+
import { toPgn } from './toPgn'
|
|
21
|
+
import _ from 'lodash'
|
|
22
|
+
import { CanDevice } from './candevice'
|
|
23
|
+
import { getPlainPGNs, binToActisense } from './utilities'
|
|
24
|
+
import { CanID, encodeCanId, parseCanId } from './canId'
|
|
25
|
+
import { toActisenseSerialFormat, parseActisense } from './stringMsg'
|
|
26
|
+
import util from 'util'
|
|
27
|
+
|
|
28
|
+
const debug = createDebug('canboatjs:canbus')
|
|
29
|
+
|
|
30
|
+
export function CanbusStream(this: any, options: any) {
|
|
31
|
+
if (this === undefined) {
|
|
32
|
+
return new (CanbusStream as any)(options)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
Transform.call(this, {
|
|
36
|
+
objectMode: true
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
this.plainText = false
|
|
40
|
+
this.options = options
|
|
41
|
+
this.start()
|
|
42
|
+
|
|
43
|
+
this.setProviderStatus =
|
|
44
|
+
options.app && options.app.setProviderStatus
|
|
45
|
+
? (msg: string) => {
|
|
46
|
+
options.app.setProviderStatus(options.providerId, msg)
|
|
47
|
+
}
|
|
48
|
+
: () => {}
|
|
49
|
+
this.setProviderError =
|
|
50
|
+
options.app && options.app.setProviderError
|
|
51
|
+
? (msg: string) => {
|
|
52
|
+
options.app.setProviderError(options.providerId, msg)
|
|
53
|
+
}
|
|
54
|
+
: () => {}
|
|
55
|
+
|
|
56
|
+
if (options.fromStdIn) {
|
|
57
|
+
return
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
try {
|
|
61
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
62
|
+
this.socketcan = require('socketcan')
|
|
63
|
+
} catch (err) {
|
|
64
|
+
console.error(err)
|
|
65
|
+
const msg = 'unable to load native socketcan interface'
|
|
66
|
+
console.error(msg)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
70
|
+
const that = this
|
|
71
|
+
|
|
72
|
+
if (options.app) {
|
|
73
|
+
options.app.on(options.outEvent || 'nmea2000out', (msg: string) => {
|
|
74
|
+
that.sendPGN(msg)
|
|
75
|
+
})
|
|
76
|
+
options.app.on(options.jsonOutEvent || 'nmea2000JsonOut', (msg: PGN) => {
|
|
77
|
+
that.sendPGN(msg)
|
|
78
|
+
})
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (this.connect() == false) {
|
|
82
|
+
return
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const noDataReceivedTimeout =
|
|
86
|
+
typeof options.noDataReceivedTimeout !== 'undefined'
|
|
87
|
+
? options.noDataReceivedTimeout
|
|
88
|
+
: -1
|
|
89
|
+
if (noDataReceivedTimeout > 0) {
|
|
90
|
+
this.noDataInterval = setInterval(() => {
|
|
91
|
+
if (
|
|
92
|
+
this.channel &&
|
|
93
|
+
this.lastDataReceived &&
|
|
94
|
+
Date.now() - this.lastDataReceived > noDataReceivedTimeout * 1000
|
|
95
|
+
) {
|
|
96
|
+
const channel = this.channel
|
|
97
|
+
delete this.channel
|
|
98
|
+
try {
|
|
99
|
+
channel.stop()
|
|
100
|
+
} catch (_error) {}
|
|
101
|
+
this.setProviderError('No data received, retrying...')
|
|
102
|
+
if (this.options.app) {
|
|
103
|
+
console.error('No data received, retrying...')
|
|
104
|
+
}
|
|
105
|
+
this.connect()
|
|
106
|
+
}
|
|
107
|
+
}, noDataReceivedTimeout * 1000)
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
CanbusStream.prototype.connect = function () {
|
|
112
|
+
const canDevice = this.options.canDevice || 'can0'
|
|
113
|
+
|
|
114
|
+
try {
|
|
115
|
+
if (this.socketcan === undefined) {
|
|
116
|
+
this.setProviderError('unable to load native socketcan interface')
|
|
117
|
+
return false
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
121
|
+
const that = this
|
|
122
|
+
this.channel = this.socketcan.createRawChannelWithOptions(canDevice, {
|
|
123
|
+
non_block_send: true
|
|
124
|
+
})
|
|
125
|
+
this.channel.addListener('onStopped', () => {
|
|
126
|
+
if (this.channel) {
|
|
127
|
+
// stoped by us?
|
|
128
|
+
delete this.channel
|
|
129
|
+
this.setProviderError('Stopped, Retrying...')
|
|
130
|
+
if (this.options.app) {
|
|
131
|
+
console.error('socketcan stopped, retrying...')
|
|
132
|
+
}
|
|
133
|
+
setTimeout(() => {
|
|
134
|
+
this.setProviderError('Reconnecting...')
|
|
135
|
+
this.connect()
|
|
136
|
+
}, 2000)
|
|
137
|
+
}
|
|
138
|
+
})
|
|
139
|
+
this.channel.addListener('onMessage', (msg: any) => {
|
|
140
|
+
const pgn = parseCanId(msg.id)
|
|
141
|
+
|
|
142
|
+
if (this.noDataInterval) {
|
|
143
|
+
this.lastDataReceived = Date.now()
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
//always send address claims through
|
|
147
|
+
if (
|
|
148
|
+
pgn.pgn != 60928 &&
|
|
149
|
+
that.candevice &&
|
|
150
|
+
that.candevice.cansend &&
|
|
151
|
+
pgn.src == that.candevice.address
|
|
152
|
+
) {
|
|
153
|
+
return
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const timestamp = new Date().toISOString()
|
|
157
|
+
if (that.plainText) {
|
|
158
|
+
this.push(binToActisense(pgn, timestamp, msg.data, msg.data.length))
|
|
159
|
+
} else {
|
|
160
|
+
that.push({ pgn, length: msg.data.length, data: msg.data })
|
|
161
|
+
}
|
|
162
|
+
})
|
|
163
|
+
this.channel.start()
|
|
164
|
+
this.setProviderStatus('Connected to socketcan')
|
|
165
|
+
this.candevice = new CanDevice(this, this.options)
|
|
166
|
+
this.candevice.start()
|
|
167
|
+
return true
|
|
168
|
+
} catch (e: any) {
|
|
169
|
+
console.error(`unable to open canbus ${canDevice}: ${e}`)
|
|
170
|
+
console.error(e.stack)
|
|
171
|
+
this.setProviderError(e.message)
|
|
172
|
+
return false
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
util.inherits(CanbusStream, Transform)
|
|
177
|
+
|
|
178
|
+
CanbusStream.prototype.start = function () {}
|
|
179
|
+
|
|
180
|
+
CanbusStream.prototype.sendPGN = function (msg: any, force: boolean) {
|
|
181
|
+
if (this.candevice) {
|
|
182
|
+
//if ( !this.candevice.cansend && (_.isString(msg) || msg.pgn !== 59904) ) {
|
|
183
|
+
if (!this.candevice.cansend && force !== true) {
|
|
184
|
+
//we have not completed address claim yet
|
|
185
|
+
return
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
debug('sending %j', msg)
|
|
189
|
+
|
|
190
|
+
if (this.options.app) {
|
|
191
|
+
this.options.app.emit('connectionwrite', {
|
|
192
|
+
providerId: this.options.providerId
|
|
193
|
+
})
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const src =
|
|
197
|
+
msg.pgn === 59904 || msg.forceSrc ? msg.src : this.candevice.address
|
|
198
|
+
if (_.isString(msg)) {
|
|
199
|
+
const split = msg.split(',')
|
|
200
|
+
split[3] = src
|
|
201
|
+
msg = split.join(',')
|
|
202
|
+
} else {
|
|
203
|
+
msg.src = src
|
|
204
|
+
if (_.isUndefined(msg.prio)) {
|
|
205
|
+
msg.prio = 3
|
|
206
|
+
}
|
|
207
|
+
if (_.isUndefined(msg.dst)) {
|
|
208
|
+
msg.dst = 255
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
if (this.socketCanWriter) {
|
|
213
|
+
if (_.isString(msg)) {
|
|
214
|
+
this.socketCanWriter.stdin.write(msg + '\n')
|
|
215
|
+
} else {
|
|
216
|
+
const str = toActisenseSerialFormat(
|
|
217
|
+
msg.pgn,
|
|
218
|
+
toPgn(msg),
|
|
219
|
+
msg.dst,
|
|
220
|
+
msg.src
|
|
221
|
+
)
|
|
222
|
+
this.socketCanWriter.stdin.write(str + '\n')
|
|
223
|
+
}
|
|
224
|
+
} else if (this.channel) {
|
|
225
|
+
let canid: number
|
|
226
|
+
let buffer: Buffer | undefined
|
|
227
|
+
let pgn: any
|
|
228
|
+
|
|
229
|
+
if (_.isObject(msg)) {
|
|
230
|
+
canid = encodeCanId(msg as CanID)
|
|
231
|
+
buffer = toPgn(msg)
|
|
232
|
+
pgn = msg
|
|
233
|
+
} else {
|
|
234
|
+
pgn = parseActisense(msg)
|
|
235
|
+
canid = encodeCanId(pgn)
|
|
236
|
+
buffer = pgn.data
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
if (debug.enabled) {
|
|
240
|
+
const str = toActisenseSerialFormat(pgn.pgn, buffer, pgn.dst, pgn.src)
|
|
241
|
+
debug(str)
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
if (buffer === undefined) {
|
|
245
|
+
debug("can't convert %j", msg)
|
|
246
|
+
return
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
//seems as though 126720 should always be encoded this way
|
|
250
|
+
if (buffer.length > 8 || pgn.pgn == 126720) {
|
|
251
|
+
const pgns = getPlainPGNs(buffer)
|
|
252
|
+
pgns.forEach((pbuffer) => {
|
|
253
|
+
this.channel.send({ id: canid, ext: true, data: pbuffer })
|
|
254
|
+
})
|
|
255
|
+
} else {
|
|
256
|
+
this.channel.send({ id: canid, ext: true, data: buffer })
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
CanbusStream.prototype._transform = function (
|
|
263
|
+
chunk: any,
|
|
264
|
+
encoding: any,
|
|
265
|
+
done: any
|
|
266
|
+
) {
|
|
267
|
+
done()
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
CanbusStream.prototype.end = function () {
|
|
271
|
+
if (this.channel) {
|
|
272
|
+
const channel = this.channel
|
|
273
|
+
delete this.channel
|
|
274
|
+
channel.stop()
|
|
275
|
+
}
|
|
276
|
+
if (this.noDataInterval) {
|
|
277
|
+
clearInterval(this.noDataInterval)
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
CanbusStream.prototype.pipe = function (pipeTo: any) {
|
|
282
|
+
if (!pipeTo.fromPgn) {
|
|
283
|
+
this.plainText = true
|
|
284
|
+
}
|
|
285
|
+
/*
|
|
286
|
+
pipeTo.fromPgn.on('pgn', (pgn) => {
|
|
287
|
+
if ( this.candevice ) {
|
|
288
|
+
this.candevice.n2kMessage(pgn)
|
|
289
|
+
}
|
|
290
|
+
})
|
|
291
|
+
*/
|
|
292
|
+
return (CanbusStream as any).super_.prototype.pipe.call(this, pipeTo)
|
|
293
|
+
}
|
|
@@ -14,38 +14,31 @@
|
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
const { defaultTransmitPGNs, getIndustryCode, getManufacturerCode, getDeviceClassCode } = require('./codes')
|
|
22
|
-
const { toPgn } = require('./toPgn')
|
|
23
|
-
const N2kDevice = require('./n2kDevice')
|
|
17
|
+
import { PGN } from '@canboat/ts-pgns'
|
|
18
|
+
import { N2kDevice } from './n2kDevice'
|
|
19
|
+
import { createDebug } from './utilities'
|
|
20
|
+
import _ from 'lodash'
|
|
24
21
|
|
|
25
|
-
|
|
26
|
-
try
|
|
27
|
-
{
|
|
28
|
-
packageJson = require('../' + 'package.json')
|
|
29
|
-
} catch (ex) {
|
|
30
|
-
}
|
|
22
|
+
const debug = createDebug('canboatjs:candevice')
|
|
31
23
|
|
|
32
|
-
|
|
24
|
+
export class CanDevice extends N2kDevice {
|
|
25
|
+
canbus: any
|
|
33
26
|
|
|
34
|
-
|
|
35
|
-
constructor (canbus, options) {
|
|
27
|
+
constructor(canbus: any, options: any) {
|
|
36
28
|
super(options)
|
|
37
29
|
this.canbus = canbus
|
|
38
30
|
|
|
39
|
-
if (
|
|
40
|
-
options.app.on(
|
|
31
|
+
if (options.app) {
|
|
32
|
+
options.app.on(
|
|
33
|
+
options.analyzerOutEvent || 'N2KAnalyzerOut',
|
|
34
|
+
this.n2kMessage.bind(this)
|
|
35
|
+
)
|
|
41
36
|
}
|
|
42
37
|
}
|
|
43
38
|
|
|
44
|
-
sendPGN(pgn, src) {
|
|
39
|
+
sendPGN(pgn: PGN, src: number | undefined = undefined) {
|
|
45
40
|
pgn.src = src || this.address
|
|
46
41
|
debug('Sending PGN %j', pgn)
|
|
47
42
|
this.canbus.sendPGN(pgn, true)
|
|
48
43
|
}
|
|
49
44
|
}
|
|
50
|
-
|
|
51
|
-
module.exports = CanDevice
|
package/lib/codes.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2025 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
|
+
export const defaultTransmitPGNs = [
|
|
18
|
+
126992, 128267, 129794, 129038, 129041, 127505, 127506, 127508, 129026,
|
|
19
|
+
129025, 129029, 127250, 130306, 126720, 127489, 127488, 130311, 130312,
|
|
20
|
+
127257, 128259, 127502
|
|
21
|
+
]
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
|
|
2
1
|
/**
|
|
3
2
|
* Copyright 2019 Scott Bender <scott@scottbender.net>
|
|
4
3
|
*
|
|
@@ -15,41 +14,44 @@
|
|
|
15
14
|
* limitations under the License.
|
|
16
15
|
*/
|
|
17
16
|
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
import { createDebug } from './utilities'
|
|
18
|
+
import { isYDRAW } from './stringMsg'
|
|
19
|
+
import dgram from 'dgram'
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
const debug = createDebug('canboatjs:discovery')
|
|
22
22
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
23
|
+
export function discover(app: any) {
|
|
24
|
+
if (app.config.settings.pipedProviders) {
|
|
25
|
+
const exists = app.config.settings.pipedProviders.find((provider: any) => {
|
|
26
|
+
return (
|
|
27
|
+
provider.pipeElements &&
|
|
28
|
+
provider.pipeElements.length === 1 &&
|
|
29
|
+
provider.pipeElements[0].type == 'providers/simple' &&
|
|
30
|
+
provider.pipeElements[0].options &&
|
|
31
|
+
provider.pipeElements[0].options.type === 'NMEA2000' &&
|
|
32
|
+
provider.pipeElements[0].options.subOptions.type ===
|
|
33
|
+
'ydwg02-udp-canboatjs' &&
|
|
34
|
+
provider.pipeElements[0].options.subOptions.port === '2002'
|
|
35
|
+
)
|
|
32
36
|
})
|
|
33
37
|
|
|
34
|
-
if (
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
socket.on('message', function (buffer, remote) {
|
|
38
|
+
if (!exists) {
|
|
39
|
+
const socket = dgram.createSocket('udp4')
|
|
40
|
+
socket.on('message', (buffer: Buffer, _remote: any) => {
|
|
38
41
|
const msg = buffer.toString('utf8')
|
|
39
|
-
if (
|
|
42
|
+
if (isYDRAW(msg)) {
|
|
40
43
|
socket.close()
|
|
41
|
-
socket = undefined
|
|
42
44
|
app.emit('discovered', {
|
|
43
45
|
id: 'YDGW-02-UDP',
|
|
44
46
|
pipeElements: [
|
|
45
47
|
{
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
48
|
+
type: 'providers/simple',
|
|
49
|
+
options: {
|
|
50
|
+
logging: false,
|
|
51
|
+
type: 'NMEA2000',
|
|
52
|
+
subOptions: {
|
|
53
|
+
type: 'ydwg02-udp-canboatjs',
|
|
54
|
+
port: '2002'
|
|
53
55
|
}
|
|
54
56
|
}
|
|
55
57
|
}
|
|
@@ -57,7 +59,7 @@ module.exports = function discover(app) {
|
|
|
57
59
|
})
|
|
58
60
|
}
|
|
59
61
|
})
|
|
60
|
-
socket.on('error', error => {
|
|
62
|
+
socket.on('error', (error: any) => {
|
|
61
63
|
debug(error)
|
|
62
64
|
})
|
|
63
65
|
socket.on('close', () => {
|
|
@@ -66,16 +68,16 @@ module.exports = function discover(app) {
|
|
|
66
68
|
debug('looking for YDGW over UDP')
|
|
67
69
|
try {
|
|
68
70
|
socket.bind(2002)
|
|
69
|
-
} catch (
|
|
71
|
+
} catch (ex) {
|
|
70
72
|
debug(ex)
|
|
71
73
|
}
|
|
72
74
|
setTimeout(() => {
|
|
73
|
-
if (
|
|
75
|
+
if (socket) {
|
|
74
76
|
socket.close()
|
|
75
77
|
}
|
|
76
78
|
}, 5000)
|
|
77
79
|
}
|
|
78
|
-
}
|
|
80
|
+
}
|
|
79
81
|
|
|
80
82
|
/*
|
|
81
83
|
if ( app.config.settings.pipedProviders ) {
|