@canboat/canboatjs 3.0.0-beta.1 → 3.0.0-beta.10

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/lib/candevice.js CHANGED
@@ -20,8 +20,9 @@ const _ = require('lodash')
20
20
  const Uint64LE = require('int64-buffer').Uint64LE
21
21
  const { defaultTransmitPGNs, getIndustryCode, getManufacturerCode, getDeviceClassCode } = require('./codes')
22
22
  const { toPgn } = require('./toPgn')
23
- let packageJson
23
+ const N2kDevice = require('./n2kDevice')
24
24
 
25
+ let packageJson
25
26
  try
26
27
  {
27
28
  packageJson = require('../' + 'package.json')
@@ -30,344 +31,21 @@ try
30
31
 
31
32
  const deviceTransmitPGNs = [ 60928, 59904, 126996, 126464 ]
32
33
 
33
- class CanDevice extends EventEmitter {
34
+ class CanDevice extends N2kDevice {
34
35
  constructor (canbus, options) {
35
- super()
36
-
37
- if ( options.addressClaim ) {
38
- this.addressClaim = options.addressClaim
39
- this.addressClaim.pgn = 60928
40
- this.addressClaim.dst = 255
41
- this.addressClaim.prio = 6
42
- } else {
43
- this.addressClaim = {
44
- pgn: 60928,
45
- dst: 255,
46
- prio:6,
47
- "Unique Number": 1263,
48
- "Manufacturer Code": 999,
49
- "Device Function": 130, // PC gateway
50
- "Device Class": 25, // Inter/Intranetwork Device
51
- "Device Instance Lower": 0,
52
- "Device Instance Upper": 0,
53
- "System Instance": 0,
54
- "Industry Group": 4, // Marine
55
- "Reserved1": 1,
56
- "Reserved2": 2
57
- }
58
- this.addressClaim["Unique Number"] = options.uniqueNumber || Math.floor(Math.random() * Math.floor(2097151))
59
- }
60
-
61
- let version = packageJson ? packageJson.version : "1.0"
62
-
63
- if ( options.productInfo ) {
64
- this.productInfo = options.productInfo
65
- this.productInfo.pgn = 126996
66
- this.productInfo.dst = 255
67
- } else {
68
- this.productInfo = {
69
- pgn: 126996,
70
- dst: 255,
71
- "NMEA 2000 Version": 1300,
72
- "Product Code": 667, // Just made up..
73
- "Model ID": "Signal K",
74
- "Model Version": "canboatjs",
75
- "Model Serial Code": options.uniqueNumber ? options.uniqueNumber.toString() : "000001",
76
- "Certification Level": 0,
77
- "Load Equivalency": 1
78
- }
79
- }
80
-
81
- this.productInfo["Software Version Code"] = version
82
-
83
- if ( options.serverVersion && options.serverUrl ) {
84
- this.configurationInfo = {
85
- pgn: 126998,
86
- dst: 255,
87
- "Installation Description #1": options.serverUrl,
88
- "Installation Description #2": options.serverDescription,
89
- "Manufacturer Information": options.serverVersion
90
- }
91
- }
92
-
36
+ super(options)
93
37
  this.canbus = canbus
94
- this.options = _.isUndefined(options) ? {} : options
95
-
96
- this.address = _.isUndefined(options.preferredAddress) ? 100 : options.preferredAddress
97
- this.cansend = false
98
- this.foundConflict = false
99
- this.heartbeatCounter = 0
100
- this.devices = {}
101
-
102
- if ( !options.disableDefaultTransmitPGNs ) {
103
- this.transmitPGNs = _.union(deviceTransmitPGNs, defaultTransmitPGNs)
104
- } else {
105
- this.transmitPGNs = [...deviceTransmitPGNs]
106
- }
107
-
108
- if ( this.options.transmitPGNs ) {
109
- this.transmitPGNs = _.union(this.transmitPGNs,
110
- this.options.transmitPGNs)
111
- }
112
38
 
113
39
  if ( options.app ) {
114
40
  options.app.on(options.analyzerOutEvent || 'N2KAnalyzerOut', this.n2kMessage.bind(this))
115
41
  }
116
42
  }
117
43
 
118
- start() {
119
- sendISORequest(this, 60928, 254)
120
- setTimeout(() => {
121
- sendAddressClaim(this)
122
- }, 1000)
123
- }
124
-
125
- n2kMessage(pgn) {
126
- if ( pgn.dst == 255 || pgn.dst == this.address ) {
127
- try {
128
- if ( pgn.pgn == 59904 ) {
129
- handleISORequest(this, pgn)
130
- } else if ( pgn.pgn == 126208 ) {
131
- handleGroupFunction(this, pgn)
132
- } else if ( pgn.pgn == 60928 ) {
133
- handleISOAddressClaim(this, pgn)
134
- } else if ( pgn.pgn == 126996 ) {
135
- handleProductInformation(this, pgn)
136
- }
137
- } catch ( err ) {
138
- console.error(err)
139
- console.error(err.stack)
140
- }
141
-
142
- /*
143
- var handler = this.handlers[pgn.pgn.toString()]
144
- if ( pgn.dst == this.address )
145
- debug(`handler ${handler}`)
146
- if ( _.isFunction(handler) ) {
147
- debug(`got handled PGN %j ${handled}`, pgn)
148
- handler(pgn)
149
- }
150
- */
151
- }
152
- }
153
- }
154
-
155
- function sendPGN(device, pgn, src, dest) {
156
- pgn.src = src || device.address
157
- debug('Sending PGN %j', pgn)
158
- device.canbus.sendPGN(pgn)
159
- }
160
-
161
- function handleISORequest(device, n2kMsg) {
162
- debug('handleISORequest %j', n2kMsg)
163
-
164
- switch (n2kMsg.fields.PGN) {
165
- case 126996: // Product Information request
166
- sendProductInformation(device)
167
- break;
168
- case 126998: // Config Information request
169
- sendConfigInformation(device)
170
- break;
171
- case 60928: // ISO address claim request
172
- sendPGN(device, device.addressClaim)
173
- break;
174
- case 126464:
175
- sendPGNList(device)
176
- break;
177
- default:
178
- if ( !device.options.disableNAKs ) {
179
- debug(`Got unsupported ISO request for PGN ${n2kMsg.fields.PGN}. Sending NAK.`)
180
- sendNAKAcknowledgement(device, n2kMsg.src, n2kMsg.fields.PGN)
181
- }
182
- }
183
- }
184
-
185
- function handleGroupFunction(device, n2kMsg) {
186
- debug('handleGroupFunction %j', n2kMsg)
187
- if(n2kMsg.fields.functionCodeRequest) {
188
- handleRequestGroupFunction(device, n2kMsg)
189
- } else if(n2kMsg.fields.functionCodeCommand) {
190
- handleCommandGroupFunction(device, n2kMsg)
191
- } else {
192
- debug('Got unsupported Group Function PGN: %j', n2kMsg)
193
- }
194
-
195
- function handleRequestGroupFunction(device, n2kMsg) {
196
- if ( !device.options.disableNAKs ) {
197
- // We really don't support group function requests for any PGNs yet -> always respond with pgnErrorCode 1 = "PGN not supported"
198
- debug("Sending 'PGN Not Supported' Group Function response for requested PGN", n2kMsg.fields.PGN)
199
-
200
- const acknowledgement = {
201
- pgn: 126208,
202
- dst: n2kMsg.src,
203
- "Function Code": 2,
204
- "PGN": n2kMsg.fields.PGN,
205
- "PGN error code": 4,
206
- "Transmission interval/Priority error code": 0,
207
- "# of Parameters": 0
208
- }
209
- sendPGN(device, acknowledgement)
210
- }
211
- }
212
-
213
- function handleCommandGroupFunction(device, n2kMsg) {
214
- if ( !device.options.disableNAKs ) {
215
- // We really don't support group function commands for any PGNs yet -> always respond with pgnErrorCode 1 = "PGN not supported"
216
- debug("Sending 'PGN Not Supported' Group Function response for commanded PGN", n2kMsg.fields.PGN)
217
-
218
- const acknowledgement = {
219
- pgn: 126208,
220
- dst: n2kMsg.src,
221
- "Function Code": 2,
222
- "PGN": n2kMsg.fields.PGN,
223
- "PGN error code": 4,
224
- "Transmission interval/Priority error code": 0,
225
- "# of Parameters": 0
226
- }
227
- sendPGN(device, acknowledgement)
228
- }
229
- }
230
- }
231
-
232
- function handleISOAddressClaim(device, n2kMsg) {
233
- if ( n2kMsg.src != device.address ) {
234
- if ( !device.devices[n2kMsg.src] ) {
235
- debug(`registering device ${n2kMsg.src}`)
236
- device.devices[n2kMsg.src] = { addressClaim: n2kMsg }
237
- if ( device.cansend ) {
238
- //sendISORequest(device, 126996, undefined, n2kMsg.src)
239
- }
240
- }
241
- return
242
- }
243
-
244
- debug('Checking ISO address claim. %j', n2kMsg)
245
-
246
- const uint64ValueFromReceivedClaim = getISOAddressClaimAsUint64(n2kMsg)
247
- const uint64ValueFromOurOwnClaim = getISOAddressClaimAsUint64(device.addressClaim)
248
-
249
- if(uint64ValueFromOurOwnClaim < uint64ValueFromReceivedClaim) {
250
- debug(`Address conflict detected! Kept our address as ${device.address}.`)
251
- sendAddressClaim(device) // We have smaller address claim data -> we can keep our address -> re-claim it
252
- } else if(uint64ValueFromOurOwnClaim > uint64ValueFromReceivedClaim) {
253
- this.foundConflict = true
254
- increaseOwnAddress(device) // We have bigger address claim data -> we have to change our address
255
- sendAddressClaim(device)
256
- debug(`Address conflict detected! Changed our address to ${device.address}.`)
257
- }
258
- }
259
-
260
- function increaseOwnAddress(device) {
261
- var start = device.address
262
- do {
263
- device.address = (device.address + 1) % 253
264
- } while ( device.address != start && device.devices[device.address] )
265
- }
266
-
267
- function handleProductInformation(device, n2kMsg) {
268
- if ( !device.devices[n2kMsg.src] ) {
269
- device.devices[n2kMsg.src] = {}
270
- }
271
- debug('got production information %j', n2kMsg)
272
- device.devices[n2kMsg.src].productInformation = n2kMsg
273
- }
274
-
275
- function sendHeartbeat(device)
276
- {
277
- device.heartbeatCounter = device.heartbeatCounter + 1
278
- if ( device.heartbeatCounter > 252 )
279
- {
280
- device.heartbeatCounter = 0
281
- }
282
- sendPGN(device,{
283
- pgn: 126993,
284
- dst: 255,
285
- prio:7,
286
- "Data transmit offset": "00:01:00",
287
- "Sequence Counter": device.heartbeatCounter,
288
- "Controller 1 State":"Error Active"
289
- })
290
- }
291
-
292
-
293
- function sendAddressClaim(device) {
294
- if ( device.devices[device.address] ) {
295
- //someone already has this address, so find a free one
296
- increaseOwnAddress(device)
297
- }
298
- debug(`Sending address claim ${device.address}`)
299
- sendPGN(device, device.addressClaim)
300
- setTimeout(() => {
301
- if ( !device.foundConflict ) {
302
- debug('no address conflics, enabling send')
303
- device.cansend = true
304
- if ( device.options.app ) {
305
- device.options.app.emit('nmea2000OutAvailable')
306
- }
307
- sendISORequest(device, 126996)
308
- device.heartbeatInterval = setInterval(() => {
309
- sendHeartbeat(device)
310
- }, 60*1000)
311
- /*
312
- _.keys(device.devices).forEach((address) => {
313
- sendISORequest(device, 126996, undefined, address)
314
- })
315
- */
316
-
317
- }
318
- }, 250)
319
- }
320
-
321
- function sendISORequest(device, pgn, src, dst=255) {
322
- debug(`Sending iso request for ${pgn} to ${dst}`)
323
-
324
- const isoRequest = {
325
- pgn: 59904,
326
- dst: dst,
327
- "PGN": pgn
328
- }
329
- sendPGN(device, isoRequest, src)
330
- }
331
-
332
-
333
- function sendProductInformation(device) {
334
- debug("Sending product info..")
335
-
336
- sendPGN(device, device.productInfo)
337
- }
338
-
339
- function sendConfigInformation(device) {
340
- if ( device.configurationInfo ) {
341
- debug("Sending config info..")
342
- sendPGN(device, device.configurationInfo)
44
+ sendPGN(pgn, src) {
45
+ pgn.src = src || this.address
46
+ debug('Sending PGN %j', pgn)
47
+ this.canbus.sendPGN(pgn, true)
343
48
  }
344
49
  }
345
50
 
346
- function sendNAKAcknowledgement(device, src, requestedPGN) {
347
- const acknowledgement = {
348
- pgn: 59392,
349
- dst: src,
350
- Control: 1,
351
- "Group Function": 255,
352
- PGN: requestedPGN
353
- }
354
- sendPGN(device, acknowledgement)
355
- }
356
-
357
- function sendPGNList(device, src) {
358
- //FIXME: for now, adding everything that signalk-to-nmea2000 supports
359
- //need a way for plugins, etc. to register the pgns they provide
360
- const pgnList = {
361
- pgn: 126464,
362
- dst: src,
363
- "Function Code": 0,
364
- list: device.transmitPGNs
365
- }
366
- sendPGN(device, pgnList)
367
- }
368
-
369
- function getISOAddressClaimAsUint64(pgn) {
370
- return new Uint64LE(toPgn(pgn))
371
- }
372
-
373
51
  module.exports = CanDevice
package/lib/fromPgn.js CHANGED
@@ -635,7 +635,7 @@ function readField(options, runPostProcessor, pgn, field, bs, fields) {
635
635
  }
636
636
  if ( options.checkForInvalidFields !== false && max !== 'undefined' &&
637
637
  field.FieldType !== 'LOOKUP' &&
638
- field.FieldType !== 'FIELDTYPE_LOOKUP' &&
638
+ field.FieldType !== 'DYNAMIC_FIELD_KEY' &&
639
639
  field.FieldType !== 'PGN' &&
640
640
  field.BitLength > 1 &&
641
641
  max - value < 0 ) {
@@ -660,7 +660,7 @@ function readField(options, runPostProcessor, pgn, field, bs, fields) {
660
660
  }
661
661
 
662
662
  if ((field.FieldType === 'LOOKUP' ||
663
- field.FieldType === 'FIELDTYPE_LOOKUP') &&
663
+ field.FieldType === 'DYNAMIC_FIELD_KEY') &&
664
664
  runPostProcessor &&
665
665
  (_.isUndefined(options.resolveEnums) ||
666
666
  options.resolveEnums)) {
@@ -691,7 +691,7 @@ function readField(options, runPostProcessor, pgn, field, bs, fields) {
691
691
  function readValue(options, pgn, field, bs, fields, bitLength) {
692
692
  var value
693
693
  if ( _.isUndefined(bitLength) ) {
694
- if ( field.BitLengthVariable && field.FieldType === "KEY_VALUE" ) {
694
+ if ( field.BitLengthVariable && field.FieldType === "DYNAMIC_FIELD_VALUE" ) {
695
695
  bitLength = lookupKeyBitLength(pgn.fields, fields)
696
696
  } else {
697
697
  bitLength = field.BitLength
@@ -2,6 +2,7 @@ const debug = require('debug')('canboatjs:w2k01')
2
2
  const debugData = require('debug')('canboatjs:w2k01-data')
3
3
  const { parseCanId, encodeCanId } = require('./canId')
4
4
  const BitStream = require('bit-buffer').BitStream
5
+ const { binToActisense } = require('./utilities')
5
6
 
6
7
  exports.readN2KActisense = function (data, plainText, context, cb) {
7
8
  const inBuf = Buffer.from(data)
@@ -105,20 +106,3 @@ exports.encodeN2KActisense = ({
105
106
 
106
107
  return bs.view.buffer
107
108
  }
108
-
109
-
110
- function binToActisense(pgn, data, length) {
111
- return (
112
- pgn.timestamp +
113
- `,${pgn.prio},${pgn.pgn},${pgn.src},${pgn.dst},${length},` +
114
- new Uint32Array(data)
115
- .reduce(function(acc, i) {
116
- acc.push(i.toString(16));
117
- return acc;
118
- }, [])
119
- .map(x => (x.length === 1 ? "0" + x : x))
120
- .join(",")
121
- );
122
- }
123
-
124
-