node-mavlink 1.0.8 → 1.0.9

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/utils.ts ADDED
@@ -0,0 +1,75 @@
1
+ /**
2
+ * Convert a number to hexadecimal representation with a minumum
3
+ * number of characters and optional prefix (0x by default)
4
+ *
5
+ * @param n value to convert
6
+ * @param len length of the converted string (without prefix)
7
+ * @param prefix prefix to prepend the generated string with
8
+ */
9
+ export function hex(n: number, len: number = 2, prefix = '0x') {
10
+ return `${prefix}${n.toString(16).padStart(len, '0')}`
11
+ }
12
+
13
+ /**
14
+ * Dump a buffer in a readable form
15
+ *
16
+ * @param buffer buffer to dump
17
+ * @param lineWidth width of the line, in bytes of buffer
18
+ */
19
+ export function dump(buffer: Buffer, lineWidth = 16) {
20
+ const line = []
21
+ let address = 0
22
+ for (let i = 0; i < buffer.length; i++) {
23
+ line.push(hex(buffer[i], 2, '0x'))
24
+ if (line.length === lineWidth) {
25
+ console.log(hex(address, 4), '|', line.join(' '))
26
+ address += lineWidth
27
+ line.length = 0
28
+ }
29
+ }
30
+ if (line.length > 0) {
31
+ console.log(hex(address, 4), '|', line.join(' '))
32
+ }
33
+ }
34
+
35
+ /**
36
+ * Sleep for a given number of miliseconds
37
+ *
38
+ * @param ms number of miliseconds to sleep
39
+ */
40
+ export function sleep(ms) {
41
+ return new Promise(resolve => setTimeout(resolve, ms))
42
+ }
43
+
44
+ /**
45
+ * Execute a callback every <code>interval</code>ms and if it will not return
46
+ * a truthy value in the <code>timeout<code>ms then throw a Timeout exception.
47
+ * This is a very useful utility that will allow you to specify how often
48
+ * a particular expression should be evaluated and how long will it take to end
49
+ * the execution without success. Great for time-sensitive operations.
50
+ *
51
+ * @param cb callback to call every <code>interval</code>ms
52
+ * @param timeout number of miliseconds that need to pass before the Timeout exception is thrown
53
+ * @param interval number of miliseconds before re-running the callback
54
+ */
55
+ export async function waitFor(cb, timeout = 10000, interval = 100) {
56
+ return new Promise((resolve, reject) => {
57
+ const timeoutTimer = setTimeout(() => {
58
+ cleanup()
59
+ reject('Timeout')
60
+ }, timeout)
61
+
62
+ const intervalTimer = setInterval(() => {
63
+ const result = cb()
64
+ if (result) {
65
+ cleanup()
66
+ resolve(result)
67
+ }
68
+ })
69
+
70
+ const cleanup = () => {
71
+ clearTimeout(timeoutTimer)
72
+ clearTimeout(intervalTimer)
73
+ }
74
+ })
75
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-mavlink",
3
- "version": "1.0.8",
3
+ "version": "1.0.9",
4
4
  "author": "Matthias Hryniszak <padcom@gmail.com>",
5
5
  "license": "LGPL",
6
6
  "description": "MavLink definitions and parsing library",
@@ -22,19 +22,23 @@
22
22
  "main": "dist/index.js",
23
23
  "types": "dist/index.d.ts",
24
24
  "dependencies": {
25
- "mavlink-mappings": "^1.0.6-20220104"
25
+ "mavlink-mappings": "^1.0.8-20220318"
26
26
  },
27
27
  "scripts": {
28
- "build": "tsc"
28
+ "build": "tsc",
29
+ "test": "cd tests && ./main.ts e2e --input data.mavlink",
30
+ "prepublishOnly": "rm -rf dist && npm install && npm test && npm run build"
29
31
  },
30
32
  "devDependencies": {
31
33
  "@types/node": "^15.14.9",
32
34
  "@types/serialport": "^8.0.1",
33
35
  "@types/xml2js": "^0.4.8",
36
+ "@types/yargs": "^17.0.8",
34
37
  "serialport": "^9.0.7",
35
38
  "ts-node": "^9.1.1",
36
39
  "typescript": "^4.4.3",
37
40
  "wget-improved": "^3.2.1",
38
- "xml2js": "^0.4.23"
41
+ "xml2js": "^0.4.23",
42
+ "yargs": "^17.3.1"
39
43
  }
40
44
  }
Binary file
package/tests/main.ts ADDED
@@ -0,0 +1,63 @@
1
+ #!/usr/bin/env ts-node
2
+
3
+ import yargs from 'yargs'
4
+ import { existsSync, createReadStream } from 'fs'
5
+ import { minimal, common, ardupilotmega } from 'mavlink-mappings'
6
+ import { createMavLinkStream, MavLinkPacket, Logger, LogLevel } from '..'
7
+ import { hex, dump } from '..'
8
+
9
+ Logger.on('log', ({ context, level, message }) => {
10
+ if (level <= LogLevel.error) {
11
+ console.log(`${new Date().toISOString()} ${context} [${LogLevel[level]}]`, ...message)
12
+ }
13
+ })
14
+
15
+ async function configure() {
16
+ return yargs(process.argv.slice(2))
17
+ .command('e2e', 'Execute end to end serialization/deserialization verification',
18
+ yargs => yargs.positional('input', {
19
+ description: 'Input file (- for stdin)',
20
+ default: '-'
21
+ }),
22
+ argv => {
23
+ if (argv.input !== '-' && !existsSync(argv.input)) {
24
+ console.error(`error: ${argv.input} not found`)
25
+ process.exit(1)
26
+ }
27
+ }
28
+ )
29
+ .help()
30
+ .alias('help', 'h')
31
+ .parse()
32
+ }
33
+
34
+ async function main() {
35
+ const config = await configure()
36
+
37
+ const command = config._[0]
38
+ if (command === 'e2e') {
39
+ const REGISTRY = {
40
+ ...minimal.REGISTRY,
41
+ ...common.REGISTRY,
42
+ ...ardupilotmega.REGISTRY,
43
+ }
44
+
45
+ const input = config.input === '-' ? process.stdin : createReadStream(config.input)
46
+ const reader = createMavLinkStream(input, dump)
47
+
48
+ reader.on('data', (packet: MavLinkPacket) => {
49
+ const clazz = REGISTRY[packet.header.msgid]
50
+ if (clazz) {
51
+ const message = packet.protocol.data(packet.payload, clazz)
52
+ console.log('<', packet.debug())
53
+ clazz.FIELDS.forEach(field => {
54
+ console.log(clazz.MSG_NAME + '.' + field.source + ' = ' + message[field.name])
55
+ })
56
+ } else {
57
+ console.log('< (unknown)', packet.debug())
58
+ }
59
+ })
60
+ }
61
+ }
62
+
63
+ main()
package/tsconfig.json CHANGED
@@ -4,7 +4,8 @@
4
4
  "lib": ["ES2020", "DOM"],
5
5
  "target": "es2015",
6
6
  "declaration": true,
7
- "outDir": "./dist"
7
+ "outDir": "./dist",
8
+ "esModuleInterop": true
8
9
  },
9
10
  "include": [
10
11
  "index.ts",