@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
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
+ export function CanbusStream(this: any, options: any) {
29
+ if (this === undefined) {
30
+ return new (CanbusStream as any)(options)
31
+ }
32
+
33
+ this.debug = createDebug('canboatjs:canbus', options)
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
+ this.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 (this.debug.enabled) {
240
+ const str = toActisenseSerialFormat(pgn.pgn, buffer, pgn.dst, pgn.src)
241
+ this.debug(str)
242
+ }
243
+
244
+ if (buffer === undefined) {
245
+ this.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
+ }
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Copyright 2018 Scott Bender (scott@scottbender.net) and Jouni Hartikainen (jouni.hartikainen@iki.fi)
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 { N2kDevice } from './n2kDevice'
19
+ import _ from 'lodash'
20
+
21
+ export class CanDevice extends N2kDevice {
22
+ canbus: any
23
+
24
+ constructor(canbus: any, options: any) {
25
+ super(options, 'canboatjs:candevice')
26
+ this.canbus = canbus
27
+
28
+ if (options.app) {
29
+ options.app.on(
30
+ options.analyzerOutEvent || 'N2KAnalyzerOut',
31
+ this.n2kMessage.bind(this)
32
+ )
33
+ }
34
+ }
35
+
36
+ sendPGN(pgn: PGN, src: number | undefined = undefined) {
37
+ pgn.src = src || this.address
38
+ this.debug('Sending PGN %j', pgn)
39
+ this.canbus.sendPGN(pgn, true)
40
+ }
41
+ }
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
- const debug = require('debug')('canboatjs:discovery')
19
- const { isYDRAW } = require('./stringMsg')
17
+ import { createDebug } from './utilities'
18
+ import { isYDRAW } from './stringMsg'
19
+ import dgram from 'dgram'
20
20
 
21
- module.exports = function discover(app) {
21
+ const debug = createDebug('canboatjs:discovery')
22
22
 
23
- if ( app.config.settings.pipedProviders ) {
24
- const exists = app.config.settings.pipedProviders.find(provider => {
25
- return provider.pipeElements
26
- && provider.pipeElements.length === 1
27
- && provider.pipeElements[0].type == 'providers/simple'
28
- && provider.pipeElements[0].options
29
- && provider.pipeElements[0].options.type === 'NMEA2000'
30
- && provider.pipeElements[0].options.subOptions.type === 'ydwg02-udp-canboatjs'
31
- && provider.pipeElements[0].options.subOptions.port === '2002'
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 ( !exists ) {
35
- const dgram = require('dgram')
36
- let socket = dgram.createSocket('udp4')
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 ( isYDRAW(msg) ) {
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
- "type": "providers/simple",
47
- "options": {
48
- "logging": false,
49
- "type": "NMEA2000",
50
- "subOptions": {
51
- "type": "ydwg02-udp-canboatjs",
52
- "port": "2002"
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 ( ex ) {
71
+ } catch (ex) {
70
72
  debug(ex)
71
73
  }
72
74
  setTimeout(() => {
73
- if ( socket ) {
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 ) {