@canboat/canboatjs 3.0.2 → 3.2.2

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.
Files changed (222) hide show
  1. package/.github/workflows/publish.yml +18 -9
  2. package/.github/workflows/test.yml +2 -2
  3. package/.github/workflows/test_canboat_changes.yml +91 -0
  4. package/.husky/pre-commit +4 -0
  5. package/.mocharc.js +7 -0
  6. package/.prettierrc.json +5 -0
  7. package/README.md +5 -14
  8. package/dist/actisense-serial.d.ts +17 -0
  9. package/dist/actisense-serial.d.ts.map +1 -0
  10. package/dist/actisense-serial.js +569 -0
  11. package/dist/actisense-serial.js.map +1 -0
  12. package/dist/bin/actisense-file.d.ts +3 -0
  13. package/dist/bin/actisense-file.d.ts.map +1 -0
  14. package/dist/bin/actisense-file.js +49 -0
  15. package/dist/bin/actisense-file.js.map +1 -0
  16. package/dist/bin/actisense-n2k-tcp.d.ts +3 -0
  17. package/dist/bin/actisense-n2k-tcp.d.ts.map +1 -0
  18. package/dist/bin/actisense-n2k-tcp.js +46 -0
  19. package/dist/bin/actisense-n2k-tcp.js.map +1 -0
  20. package/dist/bin/actisense-serialjs.d.ts +3 -0
  21. package/dist/bin/actisense-serialjs.d.ts.map +1 -0
  22. package/dist/bin/actisense-serialjs.js +51 -0
  23. package/dist/bin/actisense-serialjs.js.map +1 -0
  24. package/dist/bin/analyzerjs.d.ts +3 -0
  25. package/dist/bin/analyzerjs.d.ts.map +1 -0
  26. package/dist/bin/analyzerjs.js +61 -0
  27. package/dist/bin/analyzerjs.js.map +1 -0
  28. package/dist/bin/candumpjs.d.ts +3 -0
  29. package/dist/bin/candumpjs.d.ts.map +1 -0
  30. package/dist/bin/candumpjs.js +73 -0
  31. package/dist/bin/candumpjs.js.map +1 -0
  32. package/dist/bin/cansend.d.ts +3 -0
  33. package/dist/bin/cansend.d.ts.map +1 -0
  34. package/dist/bin/cansend.js +123 -0
  35. package/dist/bin/cansend.js.map +1 -0
  36. package/dist/bin/ikonvert-serial.d.ts +3 -0
  37. package/dist/bin/ikonvert-serial.d.ts.map +1 -0
  38. package/dist/bin/ikonvert-serial.js +36 -0
  39. package/dist/bin/ikonvert-serial.js.map +1 -0
  40. package/dist/bin/to-pgn.d.ts +3 -0
  41. package/dist/bin/to-pgn.d.ts.map +1 -0
  42. package/dist/bin/to-pgn.js +57 -0
  43. package/dist/bin/to-pgn.js.map +1 -0
  44. package/dist/canId.d.ts +21 -0
  45. package/dist/canId.d.ts.map +1 -0
  46. package/dist/canId.js +66 -0
  47. package/dist/canId.js.map +1 -0
  48. package/dist/canId.test.d.ts +2 -0
  49. package/dist/canId.test.d.ts.map +1 -0
  50. package/dist/canId.test.js +58 -0
  51. package/dist/canId.test.js.map +1 -0
  52. package/dist/canbus.d.ts +17 -0
  53. package/dist/canbus.d.ts.map +1 -0
  54. package/dist/canbus.js +261 -0
  55. package/dist/canbus.js.map +1 -0
  56. package/dist/candevice.d.ts +23 -0
  57. package/dist/candevice.d.ts.map +1 -0
  58. package/dist/candevice.js +36 -0
  59. package/dist/candevice.js.map +1 -0
  60. package/dist/codes.d.ts +2 -0
  61. package/dist/codes.d.ts.map +1 -0
  62. package/dist/codes.js +24 -0
  63. package/dist/codes.js.map +1 -0
  64. package/dist/discovery.d.ts +17 -0
  65. package/dist/discovery.d.ts.map +1 -0
  66. package/dist/discovery.js +119 -0
  67. package/dist/discovery.js.map +1 -0
  68. package/dist/fromPgn.d.ts +51 -0
  69. package/dist/fromPgn.d.ts.map +1 -0
  70. package/dist/fromPgn.js +989 -0
  71. package/dist/fromPgn.js.map +1 -0
  72. package/dist/fromPgnStream.d.ts +17 -0
  73. package/dist/fromPgnStream.d.ts.map +1 -0
  74. package/dist/fromPgnStream.js +47 -0
  75. package/dist/fromPgnStream.js.map +1 -0
  76. package/dist/ikonvert.d.ts +17 -0
  77. package/dist/ikonvert.d.ts.map +1 -0
  78. package/dist/ikonvert.js +224 -0
  79. package/dist/ikonvert.js.map +1 -0
  80. package/dist/index.d.ts +31 -0
  81. package/dist/index.d.ts.map +1 -0
  82. package/dist/index.js +60 -0
  83. package/dist/index.js.map +1 -0
  84. package/dist/n2k-actisense.d.ts +24 -0
  85. package/dist/n2k-actisense.d.ts.map +1 -0
  86. package/dist/n2k-actisense.js +126 -0
  87. package/dist/n2k-actisense.js.map +1 -0
  88. package/dist/n2k-actisense.test.d.ts +2 -0
  89. package/dist/n2k-actisense.test.d.ts.map +1 -0
  90. package/dist/n2k-actisense.test.js +49 -0
  91. package/dist/n2k-actisense.test.js.map +1 -0
  92. package/dist/n2kDevice.d.ts +44 -0
  93. package/dist/n2kDevice.d.ts.map +1 -0
  94. package/dist/n2kDevice.js +436 -0
  95. package/dist/n2kDevice.js.map +1 -0
  96. package/dist/pgns.d.ts +22 -0
  97. package/dist/pgns.d.ts.map +1 -0
  98. package/dist/pgns.js +157 -0
  99. package/dist/pgns.js.map +1 -0
  100. package/dist/pgns.test.d.ts +2 -0
  101. package/dist/pgns.test.d.ts.map +1 -0
  102. package/dist/pgns.test.js +14 -0
  103. package/dist/pgns.test.js.map +1 -0
  104. package/dist/simpleCan.d.ts +17 -0
  105. package/dist/simpleCan.d.ts.map +1 -0
  106. package/dist/simpleCan.js +125 -0
  107. package/dist/simpleCan.js.map +1 -0
  108. package/dist/stringMsg.d.ts +33 -0
  109. package/dist/stringMsg.d.ts.map +1 -0
  110. package/dist/stringMsg.js +359 -0
  111. package/dist/stringMsg.js.map +1 -0
  112. package/dist/stringMsg.test.d.ts +2 -0
  113. package/dist/stringMsg.test.d.ts.map +1 -0
  114. package/dist/stringMsg.test.js +244 -0
  115. package/dist/stringMsg.test.js.map +1 -0
  116. package/dist/toPgn.d.ts +34 -0
  117. package/dist/toPgn.d.ts.map +1 -0
  118. package/dist/toPgn.js +500 -0
  119. package/dist/toPgn.js.map +1 -0
  120. package/dist/utilities.d.ts +28 -0
  121. package/dist/utilities.d.ts.map +1 -0
  122. package/dist/utilities.js +159 -0
  123. package/dist/utilities.js.map +1 -0
  124. package/dist/utilities.test.d.ts +2 -0
  125. package/dist/utilities.test.d.ts.map +1 -0
  126. package/dist/utilities.test.js +9 -0
  127. package/dist/utilities.test.js.map +1 -0
  128. package/dist/venus-mqtt.d.ts +11 -0
  129. package/dist/venus-mqtt.d.ts.map +1 -0
  130. package/dist/venus-mqtt.js +100 -0
  131. package/dist/venus-mqtt.js.map +1 -0
  132. package/dist/venus.d.ts +11 -0
  133. package/dist/venus.d.ts.map +1 -0
  134. package/dist/venus.js +71 -0
  135. package/dist/venus.js.map +1 -0
  136. package/dist/w2k01.d.ts +17 -0
  137. package/dist/w2k01.d.ts.map +1 -0
  138. package/dist/w2k01.js +119 -0
  139. package/dist/w2k01.js.map +1 -0
  140. package/dist/yddevice.d.ts +25 -0
  141. package/dist/yddevice.d.ts.map +1 -0
  142. package/dist/yddevice.js +43 -0
  143. package/dist/yddevice.js.map +1 -0
  144. package/dist/ydgw02.d.ts +17 -0
  145. package/dist/ydgw02.d.ts.map +1 -0
  146. package/dist/ydgw02.js +163 -0
  147. package/dist/ydgw02.js.map +1 -0
  148. package/dist/ydvr.d.ts +17 -0
  149. package/dist/ydvr.d.ts.map +1 -0
  150. package/dist/ydvr.js +120 -0
  151. package/dist/ydvr.js.map +1 -0
  152. package/eslint.config.js +58 -0
  153. package/jest.config.js +5 -0
  154. package/lib/actisense-serial.ts +644 -0
  155. package/lib/bin/actisense-file.ts +53 -0
  156. package/lib/bin/actisense-n2k-tcp.ts +50 -0
  157. package/lib/bin/actisense-serialjs.ts +55 -0
  158. package/{bin/analyzerjs → lib/bin/analyzerjs.ts} +16 -15
  159. package/{bin/candumpjs → lib/bin/candumpjs.ts} +19 -20
  160. package/lib/bin/cansend.ts +131 -0
  161. package/lib/bin/ikonvert-serial.ts +44 -0
  162. package/{bin/to-pgn → lib/bin/to-pgn.ts} +23 -17
  163. package/lib/bin/ydvr-file +33 -0
  164. package/lib/canId.test.js +35 -15
  165. package/lib/canId.ts +84 -0
  166. package/lib/canbus.ts +293 -0
  167. package/lib/candevice.ts +41 -0
  168. package/lib/codes.ts +21 -0
  169. package/lib/{discovery.js → discovery.ts} +32 -30
  170. package/lib/fromPgn.ts +1207 -0
  171. package/lib/{fromPgnStream.js → fromPgnStream.ts} +17 -12
  172. package/lib/{ikonvert.js → ikonvert.ts} +99 -94
  173. package/lib/index.ts +48 -0
  174. package/lib/n2k-actisense.test.js +31 -32
  175. package/lib/n2k-actisense.ts +152 -0
  176. package/lib/n2kDevice.ts +516 -0
  177. package/lib/pgns.test.ts +12 -0
  178. package/lib/pgns.ts +191 -0
  179. package/lib/simpleCan.ts +140 -0
  180. package/lib/stringMsg.test.js +58 -41
  181. package/lib/stringMsg.ts +464 -0
  182. package/lib/toPgn.ts +597 -0
  183. package/lib/utilities.ts +169 -0
  184. package/lib/venus-mqtt.js +69 -73
  185. package/lib/venus.js +8 -8
  186. package/lib/w2k01.ts +142 -0
  187. package/lib/{yddevice.js → yddevice.ts} +18 -16
  188. package/lib/{ydgw02.js → ydgw02.ts} +58 -51
  189. package/lib/ydvr.js +65 -147
  190. package/package.json +54 -18
  191. package/tsconfig.json +19 -0
  192. package/tsconfig.tsbuildinfo +1 -0
  193. package/CHANGELOG.md +0 -101
  194. package/bin/actisense-file +0 -47
  195. package/bin/actisense-n2k-tcp +0 -45
  196. package/bin/actisense-serialjs +0 -52
  197. package/bin/candumpanalyzerjs +0 -29
  198. package/bin/cansend +0 -130
  199. package/bin/ikonvert-serial +0 -50
  200. package/bin/ydgw-analyzerjs +0 -36
  201. package/bin/ydvr-file +0 -33
  202. package/index.js +0 -53
  203. package/ios_canboat.js/main.js +0 -2
  204. package/ios_canboat.js/main.js.LICENSE.txt +0 -17
  205. package/ios_canboat.js/main.js.map +0 -1
  206. package/lib/canId.js +0 -64
  207. package/lib/canbus.js +0 -278
  208. package/lib/candevice.js +0 -51
  209. package/lib/codes.js +0 -69
  210. package/lib/codes.test.js +0 -17
  211. package/lib/codesMfgs.json +0 -166
  212. package/lib/fromPgn.js +0 -1027
  213. package/lib/n2k-actisense.js +0 -108
  214. package/lib/n2kDevice.js +0 -390
  215. package/lib/pgns.js +0 -196
  216. package/lib/pgns.test.js +0 -13
  217. package/lib/serial.js +0 -647
  218. package/lib/simpleCan.js +0 -105
  219. package/lib/stringMsg.js +0 -339
  220. package/lib/toPgn.js +0 -518
  221. package/lib/utilities.js +0 -111
  222. package/lib/w2k01.js +0 -129
@@ -0,0 +1,50 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { createDebug } from '../utilities'
4
+ import net from 'net'
5
+ import { readN2KActisense } from '../n2k-actisense'
6
+ import minimist from 'minimist'
7
+
8
+ const debug = createDebug('canboatjs:w2k01')
9
+
10
+ const argv = minimist(process.argv.slice(2), {
11
+ alias: { h: 'help' }
12
+ })
13
+
14
+ function help() {
15
+ console.error(`Usage: ${process.argv[0]} [options] host port
16
+
17
+ Options:
18
+ -h, --help output usage information`)
19
+ process.exit(1)
20
+ }
21
+
22
+ if (argv['help']) {
23
+ help()
24
+ }
25
+
26
+ if (argv['_'].length < 2) {
27
+ console.error('Please specify a host and port')
28
+ help()
29
+ }
30
+
31
+ const client = new net.Socket()
32
+ client.connect(Number(argv['_'][1]), argv['_'][0], function () {
33
+ debug('Connected')
34
+ })
35
+
36
+ const context = {}
37
+ client.on('data', function (data) {
38
+ readN2KActisense(data, true, context, (result) => {
39
+ console.log(result)
40
+ })
41
+ })
42
+
43
+ client.on('close', function () {
44
+ debug('Connection closed')
45
+ })
46
+
47
+ process.on('SIGINT', () => {
48
+ debug('SIGINT signal received.')
49
+ client.destroy()
50
+ })
@@ -0,0 +1,55 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { EventEmitter } from 'node:events'
4
+ import minimist from 'minimist'
5
+ import readline from 'readline'
6
+ import { serial } from '../index'
7
+ import { Transform } from 'stream'
8
+
9
+ const argv = minimist(process.argv.slice(2), {
10
+ boolean: ['disable-output'],
11
+ alias: { h: 'help' }
12
+ })
13
+
14
+ if (argv['help']) {
15
+ console.error(`Usage: ${process.argv[0]} [options] device_path
16
+
17
+ Options:
18
+ --disable-output don't output pgns
19
+ -h, --help output usage information`)
20
+ process.exit(1)
21
+ }
22
+
23
+ if (argv['_'].length === 0) {
24
+ console.error('Please specify a device')
25
+ process.exit(1)
26
+ }
27
+
28
+ const app = new EventEmitter()
29
+
30
+ const actisense = new (serial as any)({
31
+ app: app,
32
+ device: argv['_'][0],
33
+ plainText: true,
34
+ disableSetTransmitPGNs: true,
35
+ outputOnly: argv['disable-output']
36
+ })
37
+ const toStringTr = new Transform({
38
+ objectMode: true,
39
+
40
+ transform(chunk: any, encoding: string, callback: any) {
41
+ this.push(chunk + '\n')
42
+ callback()
43
+ }
44
+ })
45
+
46
+ const rl = readline.createInterface({
47
+ input: process.stdin,
48
+ terminal: false
49
+ })
50
+
51
+ rl.on('line', (line) => {
52
+ app.emit('nmea2000out', line)
53
+ })
54
+
55
+ actisense.pipe(toStringTr).pipe(process.stdout)
@@ -1,11 +1,16 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- const argv = require('minimist')(process.argv.slice(2), {
3
+ import { PGN } from '@canboat/ts-pgns'
4
+ import { Parser } from '../fromPgn'
5
+ import minimist from 'minimist'
6
+ import readline from 'readline'
7
+
8
+ const argv = minimist(process.argv.slice(2), {
4
9
  alias: { h: 'help' },
5
10
  boolean: ['n', 'r', 'camel', 'camel-compat']
6
11
  })
7
12
 
8
- if ( argv['help'] ) {
13
+ if (argv['help']) {
9
14
  console.error(`Usage: ${process.argv[1]} [options]
10
15
 
11
16
  Options:
@@ -18,8 +23,7 @@ Options:
18
23
  process.exit(1)
19
24
  }
20
25
 
21
- const Parser = require('../index').FromPgn
22
- var parser = new Parser( {
26
+ const parser = new Parser({
23
27
  returnNulls: argv['n'] === true,
24
28
  littleEndianMXPGN: argv['r'] === true,
25
29
  checkForInvalidFields: argv['c'] !== true,
@@ -27,34 +31,31 @@ var parser = new Parser( {
27
31
  useCamelCompat: argv['camel-compat']
28
32
  })
29
33
 
30
- parser.on('error', (pgn, error) => {
34
+ parser.on('error', (pgn: PGN, error: any) => {
31
35
  console.error(`Error parsing ${pgn.pgn} ${error}`)
32
36
  console.error(error.stack)
33
37
  })
34
38
 
35
- parser.on('warning', (pgn, error) => {
39
+ parser.on('warning', (_pgn: PGN, _error: any) => {
36
40
  //console.error(`Warning parsing ${pgn.pgn} ${error}`)
37
41
  })
38
42
 
39
- parser.on('pgn', (pgn) => {
43
+ parser.on('pgn', (pgn: PGN) => {
40
44
  console.log(JSON.stringify(pgn))
41
45
  })
42
46
 
43
-
44
- var readline = require('readline')
45
- var rl = readline.createInterface({
47
+ const rl = readline.createInterface({
46
48
  input: process.stdin,
47
49
  output: process.stdout,
48
50
  terminal: false
49
51
  })
50
52
 
51
- var input = []
52
- rl.on('line', function (line) {
53
- if ( argv['log-input'] ) {
53
+ rl.on('line', (line: string) => {
54
+ if (argv['log-input']) {
54
55
  console.log(line)
55
56
  }
56
- if ( line.length > 13 && line.charAt(13) === ';' ) {
57
- if ( line.charAt(14) === 'A' ) {
57
+ if (line.length > 13 && line.charAt(13) === ';') {
58
+ if (line.charAt(14) === 'A') {
58
59
  parser.parseString(line.substring(16))
59
60
  }
60
61
  } else {
@@ -1,18 +1,20 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- const canboatjs = require('../index')
4
- const Parser = require('../index').FromPgn
5
- const { parseCanId } = require('../lib/canId')
3
+ import { FromPgn } from '../index'
4
+ import { parseCanId } from '../canId'
5
+ import minimist from 'minimist'
6
+ import { binToActisense } from '../utilities'
7
+
8
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
6
9
  const socketcan = require('socketcan')
7
- const { binToActisense } = require('../lib/utilities')
8
- var parser = new canboatjs.FromPgn()
9
10
 
11
+ const parser = new FromPgn()
10
12
 
11
- const argv = require('minimist')(process.argv.slice(2), {
13
+ const argv = minimist(process.argv.slice(2), {
12
14
  alias: { h: 'help' }
13
15
  })
14
16
 
15
- if ( argv['help'] ) {
17
+ if (argv['help']) {
16
18
  console.error(`Usage: ${process.argv[0]} [options] candevice
17
19
 
18
20
  Options:
@@ -21,7 +23,7 @@ Options:
21
23
  process.exit(1)
22
24
  }
23
25
 
24
- if ( argv['_'].length === 0 ) {
26
+ if (argv['_'].length === 0) {
25
27
  console.error('Please specify a device')
26
28
  process.exit(1)
27
29
  }
@@ -57,23 +59,22 @@ parser.on('pgn', (pgn) => {
57
59
  console.log(JSON.stringify(pgn))
58
60
  })
59
61
 
60
-
61
62
  const canDevice = argv['_'][0]
62
63
 
63
- const channel = socketcan.createRawChannel(canDevice);
64
+ const channel = socketcan.createRawChannel(canDevice)
64
65
 
65
- channel.addListener('onStopped', (msg) => {
66
- console.error('socketcan stopped')
66
+ channel.addListener('onStopped', (msg: any) => {
67
+ console.error(`socketcan stopped ${msg}`)
67
68
  })
68
69
 
69
- channel.addListener('onMessage', (msg) => {
70
- var pgn = parseCanId(msg.id)
71
-
72
- pgn.timestamp = new Date().toISOString()
70
+ channel.addListener('onMessage', (msg: any) => {
71
+ const pgn = parseCanId(msg.id)
72
+
73
+ const timestamp = new Date().toISOString()
73
74
 
74
- let sourceString = binToActisense(pgn, msg.data, msg.data.length)
75
+ const sourceString = binToActisense(pgn, timestamp, msg.data, msg.data.length)
75
76
 
76
- if ( format === 'json' ) {
77
+ if (format === 'json') {
77
78
  parser.parse({ pgn, length: msg.data.length, data: msg.data, sourceString })
78
79
  } else {
79
80
  console.log(sourceString)
@@ -81,5 +82,3 @@ channel.addListener('onMessage', (msg) => {
81
82
  })
82
83
 
83
84
  channel.start()
84
-
85
-
@@ -0,0 +1,131 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { parseActisense } from '../stringMsg'
4
+ import { toPgn } from '../toPgn'
5
+ import { getPlainPGNs, binToActisense } from '../utilities'
6
+ import { encodeCanId } from '../canId'
7
+ import readline from 'readline'
8
+ import minimist from 'minimist'
9
+
10
+ const argv = minimist(process.argv.slice(2), {
11
+ boolean: ['test', 'log-output'],
12
+ string: ['src'],
13
+ alias: { h: 'help' }
14
+ })
15
+
16
+ if (argv['help']) {
17
+ console.error(`Usage: ${process.argv[0]} [options] candevice
18
+
19
+ Options:
20
+ --src <src> use src for all messages
21
+ --log-output log messages sent
22
+ --test don't connect or send any data
23
+ -h, --help output usage information`)
24
+ process.exit(1)
25
+ }
26
+
27
+ if (argv['_'].length === 0) {
28
+ console.error('Please specify a device')
29
+ process.exit(1)
30
+ }
31
+
32
+ const canDevice = argv['_'][0]
33
+ const srcArg = argv.src
34
+ const logOut = argv['log-output']
35
+ const test = argv.test
36
+
37
+ let channel: any
38
+
39
+ if (!test) {
40
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
41
+ const socketcan = require('socketcan')
42
+ channel = socketcan.createRawChannel(canDevice)
43
+
44
+ channel.addListener('onStopped', (msg: any) => {
45
+ console.error(`socketcan stopped ${msg}`)
46
+ })
47
+
48
+ channel.start()
49
+ }
50
+
51
+ const rl = readline.createInterface({
52
+ input: process.stdin,
53
+ output: process.stdout,
54
+ terminal: false
55
+ })
56
+
57
+ rl.on('line', function (line) {
58
+ if (line.length === 0) {
59
+ return
60
+ }
61
+
62
+ let msg = line[0] === '{' ? JSON.parse(line) : line
63
+
64
+ if (typeof msg === 'string') {
65
+ const split = msg.split(',')
66
+ if (srcArg !== undefined) {
67
+ split[3] = srcArg
68
+ }
69
+ msg = split.join(',')
70
+ } else {
71
+ if (msg.prio === undefined) {
72
+ msg.prio = 3
73
+ }
74
+ if (msg.dst === undefined) {
75
+ msg.dst = 255
76
+ }
77
+ if (srcArg !== undefined) {
78
+ msg.src = srcArg
79
+ }
80
+ if (msg.src === undefined) {
81
+ msg.src = 100
82
+ }
83
+ }
84
+
85
+ let pgn: any, canid: number, buffer: Buffer | undefined
86
+ if (typeof msg === 'object') {
87
+ canid = encodeCanId(msg)
88
+ buffer = toPgn(msg)
89
+ if (buffer === undefined) {
90
+ console.error('invalid input: %s', line)
91
+ return
92
+ }
93
+ pgn = msg
94
+ } else {
95
+ pgn = parseActisense(msg)
96
+
97
+ if (isNaN(pgn.prio) || isNaN(pgn.pgn) || isNaN(pgn.dst) || isNaN(pgn.src)) {
98
+ console.error('invalid input: ' + line)
99
+ return
100
+ }
101
+
102
+ canid = encodeCanId(pgn)
103
+ buffer = pgn.data
104
+ }
105
+
106
+ const timestamp = new Date().toISOString()
107
+
108
+ if (buffer == undefined) {
109
+ console.error('unable to encode: %s', line)
110
+ return
111
+ } else {
112
+ if (buffer.length > 8 || pgn.pgn == 126720) {
113
+ const pgns = getPlainPGNs(buffer)
114
+ pgns.forEach((pbuffer) => {
115
+ if (!test) {
116
+ channel.send({ id: canid, ext: true, data: pbuffer })
117
+ }
118
+ if (logOut) {
119
+ console.log(binToActisense(pgn, timestamp, pbuffer, pbuffer.length))
120
+ }
121
+ })
122
+ } else {
123
+ if (!test) {
124
+ channel.send({ id: canid, ext: true, data: buffer })
125
+ }
126
+ if (logOut) {
127
+ console.log(binToActisense(pgn, timestamp, buffer, buffer.length))
128
+ }
129
+ }
130
+ }
131
+ })
@@ -0,0 +1,44 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { Transform } from 'stream'
4
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
5
+ const { SerialPort } = require('serialport')
6
+
7
+ if (process.argv.length < 3) {
8
+ console.error('Please specify a device')
9
+ console.error('usage: ikonvert-serial [device] [baud,default:230400]')
10
+ process.exit(1)
11
+ }
12
+
13
+ const device = process.argv[2]
14
+
15
+ const baud = process.argv.length > 3 ? Number(process.argv[3]) : 230400
16
+
17
+ const serial = new SerialPort({
18
+ path: device,
19
+ baudRate: baud
20
+ })
21
+
22
+ const toStringTr = new Transform({
23
+ objectMode: true,
24
+
25
+ transform(line, encoding, callback) {
26
+ //this.push(JSON.stringify(chunk) + "\n");
27
+ console.log(line)
28
+
29
+ if (line.startsWith('$PDGY,000000,,,,,')) {
30
+ serial.write('$PDGY,N2NET_INIT,ALL\r\n')
31
+ }
32
+
33
+ callback()
34
+ }
35
+ })
36
+
37
+ serial.on('open', function () {
38
+ const parser = new SerialPort.parsers.Readline()
39
+ serial.pipe(parser).pipe(toStringTr)
40
+ })
41
+
42
+ serial.on('error', (x: any) => {
43
+ console.log(x)
44
+ })
@@ -1,13 +1,23 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- const argv = require('minimist')(process.argv.slice(2), {
3
+ import minimist from 'minimist'
4
+ import readline from 'readline'
5
+ import {
6
+ pgnToActisenseSerialFormat,
7
+ pgnToActisenseN2KAsciiFormat,
8
+ pgnToiKonvertSerialFormat,
9
+ pgnToYdgwRawFormat,
10
+ pgnToYdgwFullRawFormat,
11
+ pgnToPCDIN,
12
+ pgnToMXPGN
13
+ } from '../index'
14
+
15
+ const argv = minimist(process.argv.slice(2), {
4
16
  string: ['format'],
5
17
  alias: { h: 'help' }
6
18
  })
7
- const { pgnToActisenseSerialFormat, pgnToActisenseN2KAsciiFormat, pgnToiKonvertSerialFormat, pgnToYdgwRawFormat, pgnToYdgwFullRawFormat, pgnToPCDIN, pgnToMXPGN } = require('../index')
8
- const { toActisenseSerialFormat } = require('../lib/stringMsg')
9
19
 
10
- if ( argv['help'] ) {
20
+ if (argv['help']) {
11
21
  console.error(`Usage: ${process.argv[0]} [options]
12
22
 
13
23
  Options:
@@ -16,38 +26,34 @@ Options:
16
26
  process.exit(1)
17
27
  }
18
28
 
19
-
20
- const formatters = {
29
+ const formatters: { [key: string]: any } = {
21
30
  actisense: pgnToActisenseSerialFormat,
22
- 'n2kascii': pgnToActisenseN2KAsciiFormat,
31
+ n2kascii: pgnToActisenseN2KAsciiFormat,
23
32
  ikconvert: pgnToiKonvertSerialFormat,
24
33
  ydgw: pgnToYdgwRawFormat,
25
- 'pcdin': pgnToPCDIN,
26
- 'mxpgn': pgnToMXPGN,
34
+ pcdin: pgnToPCDIN,
35
+ mxpgn: pgnToMXPGN,
27
36
  'yd-full': pgnToYdgwFullRawFormat
28
37
  }
29
38
 
30
39
  const format = argv['format'] || 'actisense'
31
40
  const formatter = formatters[format]
32
- if ( !formatter ) {
41
+ if (!formatter) {
33
42
  console.error(`unknown format: ${argv['format']}`)
34
43
  process.exit(1)
35
44
  }
36
-
37
45
 
38
- var readline = require('readline')
39
- var rl = readline.createInterface({
46
+ const rl = readline.createInterface({
40
47
  input: process.stdin,
41
48
  output: process.stdout,
42
49
  terminal: false
43
50
  })
44
51
 
45
- var input = []
46
52
  rl.on('line', function (line) {
47
- var msg = JSON.parse(line)
53
+ const msg = JSON.parse(line)
48
54
  const res = formatter(msg)
49
- if ( Array.isArray(res) ) {
50
- res.forEach(m => {
55
+ if (Array.isArray(res)) {
56
+ res.forEach((m) => {
51
57
  console.log(m)
52
58
  })
53
59
  } else {
@@ -0,0 +1,33 @@
1
+ #!/usr/bin/env node
2
+
3
+ const YdvrStream = require('../lib/ydvr.js')
4
+
5
+ const argv = require('minimist')(process.argv.slice(2), {
6
+ alias: { h: 'help' }
7
+ })
8
+
9
+ if (argv['help']) {
10
+ console.error(`Usage: ${process.argv[0]} file
11
+
12
+ Options:
13
+ -h, --help output usage information`)
14
+ process.exit(1)
15
+ }
16
+
17
+ if (argv['_'].length === 0) {
18
+ console.error('Please specify a file')
19
+ process.exit(1)
20
+ }
21
+
22
+ const serial = YdvrStream()
23
+
24
+ filestream = require('fs').createReadStream(argv['_'][0])
25
+ filestream.on('error', (err) => {
26
+ console.error(err.message)
27
+ })
28
+ filestream.on('end', () => {
29
+ process.exit(0)
30
+ })
31
+ filestream.pipe(serial).on('data', (chunk) => {
32
+ console.log(JSON.stringify(chunk))
33
+ })
package/lib/canId.test.js CHANGED
@@ -5,37 +5,57 @@ const { encodeCanId, parseCanId, parseEncode } = require('./canId')
5
5
  describe('parseCanId', () => {
6
6
  test('Return object with canId broken into properties', () => {
7
7
  expect(parseCanId(0x18eeff01)).toEqual({
8
- canId: 0x18eeff01, dst: 255, src: 1, pgn: 60928, prio: 6,
8
+ canId: 0x18eeff01,
9
+ dst: 255,
10
+ src: 1,
11
+ pgn: 60928,
12
+ prio: 6
9
13
  })
10
- expect(parseCanId(0xCF004EE)).toEqual({
11
- canId: 0xCF004EE, dst: 255, src: 0xEE, pgn: 0xF004, prio: (0xC >> 2),
14
+ expect(parseCanId(0xcf004ee)).toEqual({
15
+ canId: 0xcf004ee,
16
+ dst: 255,
17
+ src: 0xee,
18
+ pgn: 0xf004,
19
+ prio: 0xc >> 2
12
20
  })
13
21
  expect(parseCanId(0x18ea2301)).toEqual({
14
- canId: 0x18ea2301, dst: 35, src: 0x01, pgn: 0xEA00, prio: 6,
22
+ canId: 0x18ea2301,
23
+ dst: 35,
24
+ src: 0x01,
25
+ pgn: 0xea00,
26
+ prio: 6
15
27
  })
16
- expect(parseCanId(0x09F8017F)).toEqual({
17
- canId: 0x09F8017F, dst: 255, src: 127, pgn: 129025, prio: 2,
28
+ expect(parseCanId(0x09f8017f)).toEqual({
29
+ canId: 0x09f8017f,
30
+ dst: 255,
31
+ src: 127,
32
+ pgn: 129025,
33
+ prio: 2
18
34
  })
19
- expect(parseCanId(0x0DF8057F)).toEqual({
20
- canId: 0x0DF8057F, dst: 255, src: 127, pgn: 129029, prio: 3,
35
+ expect(parseCanId(0x0df8057f)).toEqual({
36
+ canId: 0x0df8057f,
37
+ dst: 255,
38
+ src: 127,
39
+ pgn: 129029,
40
+ prio: 3
21
41
  })
22
42
  })
23
43
  })
24
44
  describe('encodeCanId', () => {
25
45
  test('Return canId number from object', () => {
26
- expect(encodeCanId({ src: 1, pgn: 60928, prio: 6, dst: 255 }).toString(2))
27
- .toBe(0x18eeff01.toString(2))
28
- expect((encodeCanId({ src: 238, pgn: 61444, prio: 3 })))
29
- .toBe((0xCF004EE))
46
+ expect(
47
+ encodeCanId({ src: 1, pgn: 60928, prio: 6, dst: 255 }).toString(2)
48
+ ).toBe((0x18eeff01).toString(2))
49
+ expect(encodeCanId({ src: 238, pgn: 61444, prio: 3 })).toBe(0xcf004ee)
30
50
  })
31
51
  })
32
52
 
33
53
  describe('parseEncode', () => {
34
54
  test('Return exactly same number after parse and encode', () => {
35
55
  expect(parseEncode(0x18eeff01)).toBe(0x18eeff01)
36
- expect(parseEncode(0xCF004EE)).toBe(0xCF004EE)
56
+ expect(parseEncode(0xcf004ee)).toBe(0xcf004ee)
37
57
  expect(parseEncode(0x18ea2301)).toBe(0x18ea2301)
38
- expect(parseEncode(0x09F8017F)).toBe(0x09F8017F)
39
- expect(parseEncode(0x0DF8057F)).toBe(0x0DF8057F)
58
+ expect(parseEncode(0x09f8017f)).toBe(0x09f8017f)
59
+ expect(parseEncode(0x0df8057f)).toBe(0x0df8057f)
40
60
  })
41
61
  })
package/lib/canId.ts ADDED
@@ -0,0 +1,84 @@
1
+ import { flow } from 'lodash/fp'
2
+
3
+ export type ParsedCanID = {
4
+ canId: number
5
+ prio: number
6
+ src: number
7
+ pgn: number
8
+ dst: number
9
+ }
10
+
11
+ export type CanID = {
12
+ prio: number
13
+ src: number
14
+ pgn: number
15
+ dst: number
16
+ }
17
+
18
+ // Decode CAN Identifier (canId). ISO 11783 (CAN 2.0 B Extended Frame Format)
19
+ export const parseCanId = (id: number): ParsedCanID => {
20
+ const PF = (id >> 16) & 0xff // PDU Format
21
+ const PS = (id >> 8) & 0xff // PDU Specific
22
+ const DP = (id >> 24) & 1 // Data Page
23
+
24
+ let dst: number, pgn: number
25
+
26
+ if (PF < 240) {
27
+ /* PDU1 format, the PS contains the destination address */
28
+ dst = PS
29
+ pgn = (DP << 16) + (PF << 8)
30
+ } else {
31
+ /* PDU2 format, the destination is implied global and the PGN is extended */
32
+ dst = 0xff
33
+ pgn = (DP << 16) + (PF << 8) + PS
34
+ }
35
+ return {
36
+ canId: id, // Include original canId in return object.
37
+ prio: (id >> 26) & 0x7, // Priority
38
+ src: id & 0xff, // Source Address (SA),
39
+ pgn,
40
+ dst
41
+ }
42
+ }
43
+ // canId should be a hex encoded string without spaces or commas.
44
+ export const parseCanIdStr = (canId: string) => parseCanId(parseInt(canId, 16))
45
+
46
+ export const buildCanId = (
47
+ prio: string | number,
48
+ pgn: string | number,
49
+ dst: string | number,
50
+ src: string | number
51
+ ): CanID => ({
52
+ prio: Number(prio),
53
+ pgn: Number(pgn),
54
+ dst: Number(dst),
55
+ src: Number(src)
56
+ })
57
+
58
+ // Encode CAN Identifier (canId)
59
+ export const encodeCanId = (id: CanID) => {
60
+ let canId = id.src & 0xff
61
+
62
+ //I can't get this to work, but things seem ok??
63
+ //let canId = ((src & 0xff) | 0x80000000)) // src bits are the lowest ones of the CAN ID. Also set the highest bit to 1 as n2k uses
64
+ // only extended frames (EFF bit).
65
+
66
+ const PF = (id.pgn >> 8) & 0xff
67
+
68
+ if (PF < 240) {
69
+ // PDU 1
70
+ canId = canId | ((id.dst & 0xff) << 8)
71
+ canId = canId | (id.pgn << 8)
72
+ } else {
73
+ // PDU 2
74
+ canId = canId | (id.pgn << 8)
75
+ }
76
+ canId = canId | (id.prio << 26)
77
+
78
+ return canId
79
+ }
80
+ export const canIdString = (canId: number) =>
81
+ canId.toString(16).padStart(8, '0')
82
+ export const encodeCanIdString = flow(encodeCanId, canIdString)
83
+ // Utility function that parses and re-encodes. Compare result to original.
84
+ export const parseEncode = (x: number) => encodeCanId(parseCanId(x))