@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,169 @@
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 { debug } from 'debug'
18
+ import { CanID } from './canId'
19
+ import { map, padCharsStart, trimChars } from 'lodash/fp'
20
+ import fs from 'fs'
21
+
22
+ const getDataPath = (options: any) => {
23
+ if (options.app?.config?.configPath !== undefined) {
24
+ return `${options.app.config.configPath}/canboatjs-data.json`
25
+ }
26
+ }
27
+
28
+ export const getPersistedData = (options: any, id: string, key: string) => {
29
+ const path = getDataPath(options)
30
+ if (path !== undefined) {
31
+ const content = fs.readFileSync(path)
32
+ const data = JSON.parse(content.toString())
33
+ return data[id] !== undefined && data[id][key]
34
+ }
35
+ }
36
+
37
+ export const savePersistedData = (
38
+ options: any,
39
+ id: string,
40
+ key: string,
41
+ value: any
42
+ ) => {
43
+ const path = getDataPath(options)
44
+ if (path !== undefined) {
45
+ let content: string
46
+
47
+ try {
48
+ content = fs.readFileSync(path).toString()
49
+ } catch (err: any) {
50
+ if (err.code === 'ENOENT') {
51
+ content = '{}'
52
+ } else {
53
+ throw err
54
+ }
55
+ }
56
+ const data = JSON.parse(content.toString())
57
+ if (data[id] === undefined) {
58
+ data[id] = {}
59
+ }
60
+ data[id][key] = value
61
+ fs.writeFileSync(path, JSON.stringify(data, null, 2))
62
+ }
63
+ }
64
+
65
+ export const createDebug = (name: string, appOptions: any = undefined) => {
66
+ if (appOptions !== undefined && appOptions.createDebug !== undefined) {
67
+ return appOptions.createDebug(name)
68
+ } else {
69
+ return debug(name)
70
+ }
71
+ }
72
+
73
+ export function getPlainPGNs(buffer: Buffer) {
74
+ const res = []
75
+ let bucket = 0x40 // 64
76
+
77
+ const first = Buffer.alloc(8)
78
+ first.writeUInt8(bucket++, 0)
79
+ first.writeUInt8(buffer.length, 1)
80
+ buffer.copy(first, 2, 0, 6)
81
+ res.push(first)
82
+
83
+ for (let index = 6; index < buffer.length; index += 7) {
84
+ const next = Buffer.alloc(8)
85
+ next.writeUInt8(bucket++, 0)
86
+ let end = index + 7
87
+ let fill = 0
88
+ if (end > buffer.length) {
89
+ fill = end - buffer.length
90
+ end = buffer.length
91
+ }
92
+ buffer.copy(next, 1, index, end)
93
+ if (fill > 0) {
94
+ for (let i = end - index + 1; i < 8; i++) {
95
+ next.writeUInt8(0xff, i)
96
+ }
97
+ }
98
+ res.push(next)
99
+ }
100
+ return res
101
+ }
102
+
103
+ const m_hex = [
104
+ '0',
105
+ '1',
106
+ '2',
107
+ '3',
108
+ '4',
109
+ '5',
110
+ '6',
111
+ '7',
112
+ '8',
113
+ '9',
114
+ 'A',
115
+ 'B',
116
+ 'C',
117
+ 'D',
118
+ 'E',
119
+ 'F'
120
+ ]
121
+
122
+ function toHexString(v: number) {
123
+ const msn = (v >> 4) & 0x0f
124
+ const lsn = (v >> 0) & 0x0f
125
+ return m_hex[msn] + m_hex[lsn]
126
+ }
127
+
128
+ export function compute0183Checksum(sentence: string) {
129
+ // skip the $
130
+ let i = 1
131
+ // init to first character
132
+ let c1 = sentence.charCodeAt(i)
133
+ // process rest of characters, zero delimited
134
+ for (i = 2; i < sentence.length; ++i) {
135
+ c1 = c1 ^ sentence.charCodeAt(i)
136
+ }
137
+ return '*' + toHexString(c1)
138
+ }
139
+
140
+ export function binToActisense(
141
+ pgn: CanID,
142
+ timestamp: string,
143
+ data: Buffer,
144
+ length: number
145
+ ) {
146
+ const arr: string[] = []
147
+ return (
148
+ timestamp +
149
+ `,${pgn.prio},${pgn.pgn},${pgn.src},${pgn.dst},${length},` +
150
+ new Uint32Array(data)
151
+ .reduce(function (acc, i) {
152
+ acc.push(i.toString(16))
153
+ return acc
154
+ }, arr)
155
+ .map((x) => (x.length === 1 ? '0' + x : x))
156
+ .join(',')
157
+ )
158
+ }
159
+
160
+ export const trimWrap = trimChars('()<>[]')
161
+ export const rmChecksum = (str: string) =>
162
+ str.includes('*') ? str.split('*', 1)[0] : str
163
+ export const arrBuff = (arr: string[], encoding: BufferEncoding = 'hex') =>
164
+ Buffer.from(arr.join(''), encoding)
165
+ export const hexByte = (x: number) =>
166
+ padCharsStart('0', 2, Number(x).toString(16))
167
+ export const byteString = (data: Buffer, separator = ',') =>
168
+ // Uint32Array map method doesn't work as expect. _.map does.
169
+ map(exports.hexByte, new Uint32Array(data)).join(separator)
package/lib/venus-mqtt.js CHANGED
@@ -1,11 +1,10 @@
1
-
2
1
  const Transform = require('stream').Transform
3
- const mqtt = require('mqtt');
2
+ const mqtt = require('mqtt')
4
3
  const debug = require('debug')('venus-mqtt')
5
4
  //const pgns = require('./fromPgn').pgns
6
5
  const _ = require('lodash')
7
6
 
8
- function MQTTStream (options) {
7
+ function MQTTStream(options) {
9
8
  Transform.call(this, {
10
9
  objectMode: true
11
10
  })
@@ -14,109 +13,106 @@ function MQTTStream (options) {
14
13
 
15
14
  require('util').inherits(MQTTStream, Transform)
16
15
 
17
- MQTTStream.prototype.discoveryAvailable = function() {
18
- return moduleAvailable('md' + 'ns');
19
- };
20
-
21
- function mysleep(millis) {
22
- return new Promise(resolve => setTimeout(resolve, millis));
16
+ MQTTStream.prototype.discoveryAvailable = function () {
17
+ return moduleAvailable('md' + 'ns')
23
18
  }
24
19
 
25
20
  MQTTStream.prototype.pipe = function (pipeTo) {
26
21
  const that = this
27
- this.startDiscovery().then(service => {
28
- let url = service.txtRecord.mqtt_broker
29
- var client = mqtt.connect(url)
30
- client.on('connect', function () {
31
- debug(`connected to ${url}`)
32
-
33
- client.subscribe('n2k/mqtt_n2k/self')
34
- //client.subscribe('n2k/mqtt_n2k/listenpgns')
35
- client.publish('n2k/mqtt_n2k/getself')
36
- client.publish('n2k/mqtt_n2k/addpgn', '129026')
37
- client.publish('n2k/mqtt_n2k/addpgn', '129025')
38
- //client.publish('n2k/mqtt_n2k/getlistenpgns')
39
- client.subscribe('n2k/mqtt_n2k/bus/#')
40
-
41
- /*
22
+ this.startDiscovery()
23
+ .then((service) => {
24
+ let url = service.txtRecord.mqtt_broker
25
+ var client = mqtt.connect(url)
26
+ client.on('connect', function () {
27
+ debug(`connected to ${url}`)
28
+
29
+ client.subscribe('n2k/mqtt_n2k/self')
30
+ //client.subscribe('n2k/mqtt_n2k/listenpgns')
31
+ client.publish('n2k/mqtt_n2k/getself')
32
+ client.publish('n2k/mqtt_n2k/addpgn', '129026')
33
+ client.publish('n2k/mqtt_n2k/addpgn', '129025')
34
+ //client.publish('n2k/mqtt_n2k/getlistenpgns')
35
+ client.subscribe('n2k/mqtt_n2k/bus/#')
36
+
37
+ /*
42
38
  pgns.forEach(key => {
43
39
  debug(`adding pgn ${key}`)
44
40
  client.publish('n2k/mqtt_n2k/addpgn', key)
45
41
  //require('sleep').msleep(100)
46
42
  })
47
43
  */
48
-
49
- })
44
+ })
50
45
 
51
- client.on('error', error => {
52
- this.options.app.setProviderError(`error connecting to mqtt ${error}`)
53
- })
46
+ client.on('error', (error) => {
47
+ this.options.app.setProviderError(`error connecting to mqtt ${error}`)
48
+ })
54
49
 
55
- client.on('close', () => {
56
- this.options.app.setProviderError(`mqtt close`)
57
- });
50
+ client.on('close', () => {
51
+ this.options.app.setProviderError(`mqtt close`)
52
+ })
58
53
 
59
- client.on('reconnect', () => {
60
- //this.(`mqtt reconnect`)
61
- });
54
+ client.on('reconnect', () => {
55
+ //this.(`mqtt reconnect`)
56
+ })
62
57
 
63
- client.on('message', (topic, json) => {
64
- debug(`${topic}: '${json}'`)
65
- const pgn = JSON.parse(json);
66
- if ( topic.startsWith('n2k/mqtt_n2k/bus/') ) {
67
- that.push(pgn)
68
- }
58
+ client.on('message', (topic, json) => {
59
+ debug(`${topic}: '${json}'`)
60
+ const pgn = JSON.parse(json)
61
+ if (topic.startsWith('n2k/mqtt_n2k/bus/')) {
62
+ that.push(pgn)
63
+ }
64
+ })
69
65
  })
70
- }).catch(error => {
71
- debug(error)
72
- this.options.app.setProviderStatus(error.msg)
73
- })
74
-
66
+ .catch((error) => {
67
+ debug(error)
68
+ this.options.app.setProviderStatus(error.msg)
69
+ })
70
+
75
71
  Transform.prototype.pipe.call(this, pipeTo)
76
72
  }
77
73
 
78
- MQTTStream.prototype.startDiscovery = function() {
79
- var that = this;
80
- return new Promise(function(resolve, reject) {
74
+ MQTTStream.prototype.startDiscovery = function () {
75
+ var that = this
76
+ return new Promise(function (resolve, reject) {
81
77
  if (!that.discoveryAvailable()) {
82
- console.log(
83
- "Discovery requires mdns or specify hostname and port"
84
- );
85
- reject('Discovery requires mdns');
78
+ console.log('Discovery requires mdns or specify hostname and port')
79
+ reject('Discovery requires mdns')
86
80
  }
87
-
88
- var mdns = require('md' + 'ns');
89
-
81
+
82
+ var mdns = require('md' + 'ns')
83
+
90
84
  function doStart(serviceName) {
91
85
  let browser = mdns.createBrowser(mdns.tcp(serviceName), {
92
- resolverSequence: [mdns.rst.DNSServiceResolve()],
93
- });
94
- browser.on('serviceUp', function(service) {
95
- debug(`Discovered ${serviceName}:` + JSON.stringify(service, null, 2));
86
+ resolverSequence: [mdns.rst.DNSServiceResolve()]
87
+ })
88
+ browser.on('serviceUp', function (service) {
89
+ debug(`Discovered ${serviceName}:` + JSON.stringify(service, null, 2))
96
90
  //that.get('/signalk', service.host, service.port, isHttps ? 'https' : 'http').then(function(response) {
97
- debug(`Service at ${service.host}:${service.port} ${service.txtRecord.base_topic}`)
91
+ debug(
92
+ `Service at ${service.host}:${service.port} ${service.txtRecord.base_topic}`
93
+ )
98
94
  resolve(service)
99
- });
100
- debug(`Starting ${serviceName} discovery`);
101
- browser.start();
95
+ })
96
+ debug(`Starting ${serviceName} discovery`)
97
+ browser.start()
102
98
  return browser
103
99
  }
104
-
100
+
105
101
  this.browser = doStart('mqtt-n2k')
106
102
  })
107
103
  }
108
-
109
104
 
110
- MQTTStream.prototype._transform = function (data, encoding, callback) {}
105
+ MQTTStream.prototype._transform = function (_data, _encoding, _callback) {}
111
106
 
112
107
  function moduleAvailable(name) {
113
108
  try {
114
- require.resolve(name);
115
- return true;
116
- } catch (e) {}
117
- return false;
109
+ require.resolve(name)
110
+ return true
111
+ // eslint-disable-next-line no-empty
112
+ } catch (_e) {}
113
+ return false
118
114
  }
119
115
 
120
- module.exports = MQTTStream
116
+ module.exports.VenusMQTT = MQTTStream
121
117
 
122
- const pgns = [ '126992','127505','129029','129291','130312','127245','127506','129038','129793','130314','127250','127508','129039','129794','130577','127251','128259','129040','129809','130842','127257','128267','129041','129810','127258','128275','129283','130306','127488','129025','129284','130310','127489','129026','129285','130311']
118
+ //const pgns = [ '126992','127505','129029','129291','130312','127245','127506','129038','129793','130314','127250','127508','129039','129794','130577','127251','128259','129040','129809','130842','127257','128267','129041','129810','127258','128275','129283','130306','127488','129025','129284','130310','127489','129026','129285','130311']
package/lib/venus.js CHANGED
@@ -19,7 +19,7 @@ const Transform = require('stream').Transform
19
19
  const FromPgn = require('./fromPgn').Parser
20
20
  const _ = require('lodash')
21
21
 
22
- function VenusStream (options) {
22
+ function VenusStream(options) {
23
23
  if (!(this instanceof VenusStream)) {
24
24
  return new VenusStream(options)
25
25
  }
@@ -47,7 +47,6 @@ function VenusStream (options) {
47
47
  debug(`[error] ${pgn.pgn} ${error}`)
48
48
  })
49
49
 
50
-
51
50
  /*
52
51
  if ( this.options.app ) {
53
52
  options.app.on('nmea2000out', (msg) => {
@@ -67,22 +66,23 @@ function VenusStream (options) {
67
66
 
68
67
  require('util').inherits(VenusStream, Transform)
69
68
 
70
-
71
69
  VenusStream.prototype._transform = function (pgn, encoding, done) {
72
70
  //let line = chunk.toString().trim()
73
71
  //line = line.substring(0, line.length) // take off the \r
74
72
 
75
73
  this.fromPgn.parseVenusMQTT(pgn, (error, pgn) => {
76
- if ( !error ) {
74
+ if (!error) {
77
75
  this.push(pgn)
78
- this.options.app.emit(this.options.analyzerOutEvent || 'N2KAnalyzerOut', pgn)
76
+ this.options.app.emit(
77
+ this.options.analyzerOutEvent || 'N2KAnalyzerOut',
78
+ pgn
79
+ )
79
80
  }
80
81
 
81
82
  done()
82
83
  })
83
84
  }
84
85
 
85
- VenusStream.prototype.end = function () {
86
- }
86
+ VenusStream.prototype.end = function () {}
87
87
 
88
- module.exports = VenusStream
88
+ module.exports.VenusStream = VenusStream
package/lib/w2k01.ts ADDED
@@ -0,0 +1,142 @@
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 {
21
+ pgnToActisenseN2KAsciiFormat,
22
+ actisenseToN2KAsciiFormat,
23
+ pgnToN2KActisenseFormat,
24
+ actisenseToN2KActisenseFormat
25
+ } from './toPgn'
26
+ import { readN2KActisense } from './n2k-actisense'
27
+ import util from 'util'
28
+
29
+ //const pgnsSent = {}
30
+
31
+ const N2K_ASCII = 0
32
+ const N2K_ACTISENSE = 1
33
+
34
+ export function W2K01Stream(
35
+ this: any,
36
+ options: any,
37
+ type: string,
38
+ outEvent: string
39
+ ) {
40
+ if (this === undefined) {
41
+ return new (W2K01Stream as any)(options, type, outEvent)
42
+ }
43
+
44
+ Transform.call(this, {
45
+ objectMode: true
46
+ })
47
+
48
+ this.debug = createDebug('canboatjs:w2k01', options)
49
+ this.debugData = createDebug('canboatjs:w2k01-data', options)
50
+
51
+ this.sentAvailable = false
52
+ this.options = options
53
+ this.outEvent = outEvent || 'w2k-1-out'
54
+
55
+ this.format = type === 'ascii' ? N2K_ASCII : N2K_ACTISENSE
56
+
57
+ if (this.format === N2K_ASCII) {
58
+ if (options.app) {
59
+ options.app.on(this.options.outEevent || 'nmea2000out', (msg: string) => {
60
+ if (typeof msg === 'string') {
61
+ this.sendW2KPGN(msg)
62
+ } else {
63
+ this.sendPGN(msg)
64
+ }
65
+ options.app.emit('connectionwrite', { providerId: options.providerId })
66
+ })
67
+
68
+ options.app.on(options.jsonOutEvent || 'nmea2000JsonOut', (msg: PGN) => {
69
+ this.sendPGN(msg)
70
+ options.app.emit('connectionwrite', { providerId: options.providerId })
71
+ })
72
+ }
73
+ }
74
+
75
+ this.debug('started')
76
+ }
77
+
78
+ W2K01Stream.prototype.send = function (msg: string | Buffer) {
79
+ this.debug('sending %s', msg)
80
+ this.options.app.emit(this.outEvent, msg)
81
+ }
82
+
83
+ W2K01Stream.prototype.sendPGN = function (pgn: PGN) {
84
+ //const now = Date.now()
85
+ //let lastSent = pgnsSent[pgn.pgn]
86
+ if (this.format === N2K_ASCII) {
87
+ const ascii = pgnToActisenseN2KAsciiFormat(pgn)
88
+ this.send(ascii + '\r\n')
89
+ } else {
90
+ const buf = pgnToN2KActisenseFormat(pgn)
91
+ this.send(buf)
92
+ }
93
+ //pgnsSent[pgn.pgn] = now
94
+ }
95
+
96
+ W2K01Stream.prototype.sendW2KPGN = function (msg: string) {
97
+ if (this.format === N2K_ASCII) {
98
+ const ascii = actisenseToN2KAsciiFormat(msg)
99
+ this.send(ascii + '\r\n')
100
+ } else {
101
+ const buf = actisenseToN2KActisenseFormat
102
+ this.send(buf)
103
+ }
104
+ }
105
+
106
+ util.inherits(W2K01Stream, Transform)
107
+
108
+ W2K01Stream.prototype._transform = function (
109
+ chunk: any,
110
+ encoding: string,
111
+ done: any
112
+ ) {
113
+ if (!this.sentAvailable && this.format === N2K_ASCII) {
114
+ this.debug('emit nmea2000OutAvailable')
115
+ this.options.app.emit('nmea2000OutAvailable')
116
+ this.sentAvailable = true
117
+ }
118
+
119
+ if (this.format === N2K_ASCII) {
120
+ if (this.debugData.enabled) {
121
+ this.debugData('Received: ' + chunk)
122
+ }
123
+ this.push(chunk)
124
+ } else {
125
+ readN2KActisense(chunk, this.plainText, this, (data: any) => {
126
+ this.push(data)
127
+ })
128
+ }
129
+
130
+ done()
131
+ }
132
+
133
+ W2K01Stream.prototype.pipe = function (pipeTo: any) {
134
+ if (!pipeTo.fromPgn) {
135
+ this.plainText = true
136
+ } else {
137
+ this.plainText = false
138
+ }
139
+ return (W2K01Stream as any).super_.prototype.pipe.call(this, pipeTo)
140
+ }
141
+
142
+ W2K01Stream.prototype.end = function () {}
@@ -14,33 +14,35 @@
14
14
  * limitations under the License.
15
15
  */
16
16
 
17
+ import { PGN } from '@canboat/ts-pgns'
18
+ import { N2kDevice } from './n2kDevice'
19
+ import { actisenseToYdgwFullRawFormat } from './toPgn'
17
20
 
18
- const debug = require('debug')('canboatjs:n2kdevice')
19
- const N2kDevice = require('./n2kDevice')
21
+ export class YdDevice extends N2kDevice {
22
+ app: any
23
+ n2kOutEvent: string
20
24
 
21
- const { actisenseToYdgwFullRawFormat } = require('./toPgn')
22
-
23
- class YdDevice extends N2kDevice {
24
- constructor (options) {
25
- super(options)
25
+ constructor(options: any) {
26
+ super(options, 'canboatjs:yddevice')
26
27
  this.app = options.app
27
28
  this.n2kOutEvent = options.jsonOutEvent || 'nmea2000JsonOut'
28
-
29
+
29
30
  const analyzerOutEvent = options.analyzerOutEvent || 'N2KAnalyzerOut'
30
-
31
- this.app.on(options.analyzerOutEvent || 'N2KAnalyzerOut', this.n2kMessage.bind(this))
31
+
32
+ this.app.on(analyzerOutEvent, this.n2kMessage.bind(this))
32
33
  }
33
34
 
34
- sendPGN(pgn, src) {
35
+ sendPGN(pgn: PGN, src: number | undefined = undefined) {
35
36
  pgn.src = src || this.address
36
- pgn.ydFullFormat = true
37
- debug('Sending PGN %j', pgn)
37
+
38
+ const ppgn = pgn as any //FIXME??
39
+ ppgn.ydFullFormat = true
40
+
41
+ this.debug('Sending PGN %j', pgn)
38
42
  this.app.emit(this.n2kOutEvent, pgn)
39
43
  }
40
44
 
41
- sendActisenseFormat(msg) {
45
+ sendActisenseFormat(msg: string) {
42
46
  this.app.emit('ydFullRawOut', actisenseToYdgwFullRawFormat(msg))
43
47
  }
44
48
  }
45
-
46
- module.exports = YdDevice