node-mavlink 1.2.1 → 1.3.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,256 +0,0 @@
1
- import { SERIALIZERS, DESERIALIZERS } from './serialization'
2
-
3
- describe('serialization', () => {
4
- describe('char', () => {
5
- it('will serialize char', () => {
6
- const b = Buffer.from([0])
7
- SERIALIZERS['char'](42, b, 0)
8
- expect(b).toStrictEqual(Buffer.from([42]))
9
- })
10
- it('will deserialize char', () => {
11
- const b = Buffer.from([42])
12
- const result = DESERIALIZERS['char'](b, 0)
13
- expect(result).toBe('*')
14
- })
15
- it('will serialize char[]', () => {
16
- const b = Buffer.from(new Array(3))
17
- SERIALIZERS['char[]']('LOL', b, 0, 5)
18
- expect(b).toStrictEqual(Buffer.from([76, 79, 76]))
19
- })
20
- it('will deserialize char[]', () => {
21
- const b = Buffer.from([76, 79, 76, 0, 0, 0, 0, 0])
22
- const result = DESERIALIZERS['char[]'](b, 0, 5)
23
- expect(result).toBe('LOL')
24
- })
25
- })
26
-
27
- describe('uint8_t', () => {
28
- it('will serialize 8-bit unsigned int', () => {
29
- const b = Buffer.from([0])
30
- SERIALIZERS['uint8_t'](2, b, 0)
31
- expect(b).toStrictEqual(Buffer.from([2]))
32
- })
33
- it('will deserialize 8-bit unsigned int', () => {
34
- const b = Buffer.from([2])
35
- const result = DESERIALIZERS['uint8_t'](b, 0)
36
- expect(result).toBe(2)
37
- })
38
- it('will serialize array of 8-bit unsigned int', () => {
39
- const b = Buffer.from(new Array(3))
40
- SERIALIZERS['uint8_t[]']([ 1, 2, 4 ], b, 0, 3)
41
- expect(b).toStrictEqual(Buffer.from([1, 2, 4]))
42
- })
43
- it('will deserialize array of 8-bit unsigned int', () => {
44
- const b = Buffer.from([1, 2, 4])
45
- const result = DESERIALIZERS['uint8_t[]'](b, 0, 3)
46
- expect(result).toStrictEqual([1, 2, 4])
47
- })
48
- })
49
-
50
- describe('int8_t', () => {
51
- it('will serialize 8-bit signed int', () => {
52
- const b = Buffer.from([0])
53
- SERIALIZERS['int8_t'](-2, b, 0)
54
- expect(b).toStrictEqual(Buffer.from([-2]))
55
- })
56
- it('will deserialize 8-bit signed int', () => {
57
- const b = Buffer.from([-2])
58
- const result = DESERIALIZERS['int8_t'](b, 0)
59
- expect(result).toBe(-2)
60
- })
61
- it('will serialize array of 8-bit signed int', () => {
62
- const b = Buffer.from(new Array(3))
63
- SERIALIZERS['int8_t[]']([ 1, -2, -5 ], b, 0, 3)
64
- expect(b).toStrictEqual(Buffer.from([1, -2, -5]))
65
- })
66
- it('will deserialize array of 8-bit signed int', () => {
67
- const b = Buffer.from([255, 254, 252])
68
- const result = DESERIALIZERS['int8_t[]'](b, 0, 3)
69
- expect(result).toStrictEqual([-1, -2, -4])
70
- })
71
- })
72
-
73
- describe('uint16_t', () => {
74
- it('will serialize 16-bit unsigned int', () => {
75
- const b = Buffer.from([0, 0])
76
- SERIALIZERS['uint16_t'](2, b, 0)
77
- expect(b).toStrictEqual(Buffer.from([2, 0]))
78
- })
79
- it('will deserialize 16-bit unsigned int', () => {
80
- const b = Buffer.from([2, 0])
81
- const result = DESERIALIZERS['uint16_t'](b, 0)
82
- expect(result).toBe(2)
83
- })
84
- it('will serialize array of 16-bit unsigned int', () => {
85
- const b = Buffer.from(new Array(6))
86
- SERIALIZERS['uint16_t[]']([ 1, 2, 4 ], b, 0, 3)
87
- expect(b).toStrictEqual(Buffer.from([1, 0, 2, 0, 4, 0]))
88
- })
89
- it('will deserialize array of 16-bit unsigned int', () => {
90
- const b = Buffer.from([1, 0, 2, 0, 4, 0])
91
- const result = DESERIALIZERS['uint16_t[]'](b, 0, 3)
92
- expect(result).toStrictEqual([1, 2, 4])
93
- })
94
- })
95
-
96
- describe('int16_t', () => {
97
- it('will serialize 16-bit signed int', () => {
98
- const b = Buffer.from([0, 0])
99
- SERIALIZERS['int16_t'](-2, b, 0)
100
- expect(b).toStrictEqual(Buffer.from([254, 255]))
101
- })
102
- it('will deserialize 16-bit signed int', () => {
103
- const b = Buffer.from([254, 255])
104
- const result = DESERIALIZERS['int16_t'](b, 0)
105
- expect(result).toBe(-2)
106
- })
107
- it('will serialize array of 16-bit signed int', () => {
108
- const b = Buffer.from(new Array(6))
109
- SERIALIZERS['int16_t[]']([1, -2, -5], b, 0, 3)
110
- expect(b).toStrictEqual(Buffer.from([1, 0, 254, 255, 251, 255]))
111
- })
112
- it('will deserialize array of 16-bit signed int', () => {
113
- const b = Buffer.from([1, 0, 254, 255, 251, 255])
114
- const result = DESERIALIZERS['int16_t[]'](b, 0, 3)
115
- expect(result).toStrictEqual([1, -2, -5])
116
- })
117
- })
118
-
119
- describe('uint32_t', () => {
120
- it('will serialize 32-bit unsigned int', () => {
121
- const b = Buffer.from([0, -1, -2, -3])
122
- SERIALIZERS['uint32_t'](2, b, 0)
123
- expect(b).toStrictEqual(Buffer.from([2, 0, 0, 0]))
124
- })
125
- it('will deserialize 32-bit unsigned int', () => {
126
- const b = Buffer.from([2, 0, 0, 0])
127
- const result = DESERIALIZERS['uint32_t'](b, 0)
128
- expect(result).toBe(2)
129
- })
130
- it('will serialize array of 32-bit unsigned int', () => {
131
- const b = Buffer.from(new Array(12))
132
- SERIALIZERS['uint32_t[]']([ 1, 2, 4 ], b, 0, 3)
133
- expect(b).toStrictEqual(Buffer.from([1, 0, 0, 0, 2, 0, 0, 0, 4, 0, 0, 0]))
134
- })
135
- it('will deserialize array of 32-bit unsigned int', () => {
136
- const b = Buffer.from([1, 0, 0, 0, 2, 0, 0, 0, 5, 0, 0, 0])
137
- const result = DESERIALIZERS['uint32_t[]'](b, 0, 3)
138
- expect(result).toStrictEqual([1, 2, 5])
139
- })
140
- })
141
-
142
- describe('int32_t', () => {
143
- it('will serialize 32-bit signed int', () => {
144
- const b = Buffer.from([0, 0, 0, 0])
145
- SERIALIZERS['int32_t'](-2, b, 0)
146
- expect(b).toStrictEqual(Buffer.from([254, 255, 255, 255]))
147
- })
148
- it('will deserialize 32-bit signed int', () => {
149
- const b = Buffer.from([254, 255, 255, 255])
150
- const result = DESERIALIZERS['int32_t'](b, 0)
151
- expect(result).toBe(-2)
152
- })
153
- it('will serialize array of 32-bit signed int', () => {
154
- const b = Buffer.from(new Array(12))
155
- SERIALIZERS['int32_t[]']([ 1, -2, -5 ], b, 0, 3)
156
- expect(b).toStrictEqual(Buffer.from([1, 0, 0, 0, 254, 255, 255, 255, 251, 255, 255, 255]))
157
- })
158
- it('will deserialize array of 32-bit signed int', () => {
159
- const b = Buffer.from([1, 0, 0, 0, 254, 255, 255, 255, 250, 255, 255, 255])
160
- const result = DESERIALIZERS['int32_t[]'](b, 0, 3)
161
- expect(result).toStrictEqual([1, -2, -6])
162
- })
163
- })
164
-
165
- describe('uint64_t', () => {
166
- it('will serialize 64-bit unsigned int', () => {
167
- const b = Buffer.from([0, -1, -2, -3, -4, -5, -6, -7])
168
- SERIALIZERS['uint64_t'](2n, b, 0)
169
- expect(b).toStrictEqual(Buffer.from([2, 0, 0, 0, 0, 0, 0, 0]))
170
- })
171
- it('will deserialize 64-bit unsigned int', () => {
172
- const b = Buffer.from([2, 0, 0, 0, 0, 0, 0, 0])
173
- const result = DESERIALIZERS['uint64_t'](b, 0)
174
- expect(result).toBe(2n)
175
- })
176
- it('will serialize array of 64-bit unsigned int', () => {
177
- const b = Buffer.from(new Array(24))
178
- SERIALIZERS['uint64_t[]']([ 1n, 2n, 4n ], b, 0, 3)
179
- expect(b).toStrictEqual(Buffer.from([1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0]))
180
- })
181
- it('will deserialize array of 64-bit unsigned int', () => {
182
- const b = Buffer.from([1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0])
183
- const result = DESERIALIZERS['uint64_t[]'](b, 0, 3)
184
- expect(result).toEqual([1n, 2n, 5n])
185
- })
186
- })
187
-
188
- describe('int64_t', () => {
189
- it('will serialize 64-bit signed int', () => {
190
- const b = Buffer.from([0, 0, 0, 0, 0, 0, 0, 0])
191
- SERIALIZERS['int64_t'](-2n, b, 0)
192
- expect(b).toStrictEqual(Buffer.from([254, 255, 255, 255, 255, 255, 255, 255]))
193
- })
194
- it('will deserialize 64-bit signed int', () => {
195
- const b = Buffer.from([254, 255, 255, 255, 255, 255, 255, 255])
196
- const result = DESERIALIZERS['int64_t'](b, 0)
197
- expect(result).toBe(-2n)
198
- })
199
- it('will serialize array of 64-bit signed int', () => {
200
- const b = Buffer.from(new Array(24))
201
- SERIALIZERS['int64_t[]']([ 1n, -2n, -5n ], b, 0, 3)
202
- expect(b).toStrictEqual(Buffer.from([1, 0, 0, 0, 0, 0, 0, 0, 254, 255, 255, 255, 255, 255, 255, 255, 251, 255, 255, 255, 255, 255, 255, 255]))
203
- })
204
- it('will deserialize array of 64-bit signed int', () => {
205
- const b = Buffer.from([1, 0, 0, 0, 0, 0, 0, 0, 254, 255, 255, 255, 255, 255, 255, 255, 250, 255, 255, 255, 255, 255, 255, 255])
206
- const result = DESERIALIZERS['int64_t[]'](b, 0, 3)
207
- expect(result).toEqual([1n, -2n, -6n])
208
- })
209
- })
210
-
211
- describe('float', () => {
212
- it('will serialize float', () => {
213
- const b = Buffer.from([0, -1, -2, -3])
214
- SERIALIZERS['float'](2.5, b, 0)
215
- expect(b).toStrictEqual(Buffer.from([0, 0, 32, 64]))
216
- })
217
- it('will deserialize float', () => {
218
- const b = Buffer.from([0, 0, 32, 192])
219
- const result = DESERIALIZERS['float'](b, 0)
220
- expect(result).toBe(-2.5)
221
- })
222
- it('will serialize array of floats', () => {
223
- const b = Buffer.from(new Array(12))
224
- SERIALIZERS['float[]']([ 1, 2, 4 ], b, 0, 3)
225
- expect(b).toStrictEqual(Buffer.from([0, 0, 128, 63, 0, 0, 0, 64, 0, 0, 128, 64]))
226
- })
227
- it('will deserialize array of floats', () => {
228
- const b = Buffer.from([0, 0, 192, 63, 0, 0, 32, 192, 0, 0, 128, 64])
229
- const result = DESERIALIZERS['float[]'](b, 0, 3)
230
- expect(result).toStrictEqual([1.5, -2.5, 4])
231
- })
232
- })
233
-
234
- describe('double', () => {
235
- it('will serialize double', () => {
236
- const b = Buffer.from([0, -1, -2, -3, -4, -5, -6, -7])
237
- SERIALIZERS['double'](2.34, b, 0)
238
- expect(b).toStrictEqual(Buffer.from([184, 30, 133, 235, 81, 184, 2, 64]))
239
- })
240
- it('will deserialize double', () => {
241
- const b = Buffer.from([184, 30, 133, 235, 81, 184, 2, 64])
242
- const result = DESERIALIZERS['double'](b, 0)
243
- expect(result).toBe(2.34)
244
- })
245
- it('will serialize array of doubles', () => {
246
- const b = Buffer.from(new Array(24))
247
- SERIALIZERS['double[]']([ 1.5, -2.5, 4 ], b, 0, 3)
248
- expect(b).toStrictEqual(Buffer.from([0, 0, 0, 0, 0, 0, 248, 63, 0, 0, 0, 0, 0, 0, 4, 192, 0, 0, 0, 0, 0, 0, 16, 64]))
249
- })
250
- it('will deserialize array of doubles', () => {
251
- const b = Buffer.from([0, 0, 0, 0, 0, 0, 248, 63, 0, 0, 0, 0, 0, 0, 4, 192, 0, 0, 0, 0, 0, 0, 16, 64])
252
- const result = DESERIALIZERS['double[]'](b, 0, 3)
253
- expect(result).toStrictEqual([1.5, -2.5, 4])
254
- })
255
- })
256
- })
@@ -1,176 +0,0 @@
1
- import {
2
- int8_t,
3
- uint8_t,
4
- int16_t,
5
- uint16_t,
6
- int32_t,
7
- uint32_t,
8
- int64_t,
9
- uint64_t,
10
- float,
11
- double,
12
- } from 'mavlink-mappings'
13
-
14
- /**
15
- * A dictionary containing functions that serialize a certain value based on the field type
16
- */
17
- export const SERIALIZERS = {
18
- // special types
19
- 'uint8_t_mavlink_version': (value: uint8_t, buffer: Buffer, offset: number) => buffer.writeUInt8(value, offset),
20
-
21
- // singular types
22
- 'char' : (value: int8_t, buffer: Buffer, offset: number) => buffer.writeUInt8(value, offset),
23
- 'int8_t' : (value: int8_t, buffer: Buffer, offset: number) => buffer.writeInt8(value, offset),
24
- 'uint8_t' : (value: uint8_t, buffer: Buffer, offset: number) => buffer.writeUInt8(value, offset),
25
- 'int16_t' : (value: int16_t, buffer: Buffer, offset: number) => buffer.writeInt16LE(value, offset),
26
- 'uint16_t': (value: uint16_t, buffer: Buffer, offset: number) => buffer.writeUInt16LE(value, offset),
27
- 'int32_t' : (value: int32_t, buffer: Buffer, offset: number) => buffer.writeInt32LE(value, offset),
28
- 'uint32_t': (value: uint32_t, buffer: Buffer, offset: number) => buffer.writeUInt32LE(value, offset),
29
- 'int64_t' : (value: int64_t, buffer: Buffer, offset: number) => buffer.writeBigInt64LE(value, offset),
30
- 'uint64_t': (value: uint64_t, buffer: Buffer, offset: number) => buffer.writeBigUInt64LE(value, offset),
31
- 'float' : (value: float, buffer: Buffer, offset: number) => buffer.writeFloatLE(value, offset),
32
- 'double' : (value: double, buffer: Buffer, offset: number) => buffer.writeDoubleLE(value, offset),
33
-
34
- // array types
35
- 'char[]': (value: string, buffer: Buffer, offset: number, maxLen: number) => {
36
- for (let i = 0; i < value.length && i < maxLen; i++) {
37
- const code = value.charCodeAt(i)
38
- buffer.writeUInt8(code, offset + i)
39
- }
40
- },
41
- 'int8_t[]': (value: uint8_t[], buffer: Buffer, offset: number, maxLen: number) => {
42
- for (let i = 0; i < value.length && i < maxLen; i++) {
43
- buffer.writeInt8(value[i], offset + i)
44
- }
45
- },
46
- 'uint8_t[]': (value: uint8_t[], buffer: Buffer, offset: number, maxLen: number) => {
47
- for (let i = 0; i < value.length && i < maxLen; i++) {
48
- buffer.writeUInt8(value[i], offset + i)
49
- }
50
- },
51
- 'int16_t[]': (value: uint16_t[], buffer: Buffer, offset: number, maxLen: number) => {
52
- for (let i = 0; i < value.length && i < maxLen; i++) {
53
- buffer.writeInt16LE(value[i], offset + i * 2)
54
- }
55
- },
56
- 'uint16_t[]': (value: uint16_t[], buffer: Buffer, offset: number, maxLen: number) => {
57
- for (let i = 0; i < value.length && i < maxLen; i++) {
58
- buffer.writeUInt16LE(value[i], offset + i * 2)
59
- }
60
- },
61
- 'int32_t[]': (value: uint32_t[], buffer: Buffer, offset: number, maxLen: number) => {
62
- for (let i = 0; i < value.length && i < maxLen; i++) {
63
- buffer.writeInt32LE(value[i], offset + i * 4)
64
- }
65
- },
66
- 'uint32_t[]': (value: uint32_t[], buffer: Buffer, offset: number, maxLen: number) => {
67
- for (let i = 0; i < value.length && i < maxLen; i++) {
68
- buffer.writeUInt32LE(value[i], offset + i * 4)
69
- }
70
- },
71
- 'int64_t[]': (value: uint64_t[], buffer: Buffer, offset: number, maxLen: number) => {
72
- for (let i = 0; i < value.length && i < maxLen; i++) {
73
- buffer.writeBigInt64LE(value[i], offset + i * 8)
74
- }
75
- },
76
- 'uint64_t[]': (value: uint64_t[], buffer: Buffer, offset: number, maxLen: number) => {
77
- for (let i = 0; i < value.length && i < maxLen; i++) {
78
- buffer.writeBigUInt64LE(value[i], offset + i * 8)
79
- }
80
- },
81
- 'float[]': (value: float[], buffer: Buffer, offset: number, maxLen: number) => {
82
- for (let i = 0; i < value.length && i < maxLen; i++) {
83
- buffer.writeFloatLE(value[i], offset + i * 4)
84
- }
85
- },
86
- 'double[]': (value: double[], buffer: Buffer, offset: number, maxLen: number) => {
87
- for (let i = 0; i < value.length && i < maxLen; i++) {
88
- buffer.writeDoubleLE(value[i], offset + i * 8)
89
- }
90
- },
91
- }
92
-
93
- /**
94
- * A dictionary containing functions that deserialize a certain value based on the field type
95
- */
96
- export const DESERIALIZERS = {
97
- // special types
98
- 'uint8_t_mavlink_version': (buffer: Buffer, offset: number) => buffer.readUInt8(offset),
99
-
100
- // singular types
101
- 'char' : (buffer: Buffer, offset: number) => String.fromCharCode(buffer.readUInt8(offset)),
102
- 'int8_t' : (buffer: Buffer, offset: number) => buffer.readInt8(offset),
103
- 'uint8_t' : (buffer: Buffer, offset: number) => buffer.readUInt8(offset),
104
- 'int16_t' : (buffer: Buffer, offset: number) => buffer.readInt16LE(offset),
105
- 'uint16_t': (buffer: Buffer, offset: number) => buffer.readUInt16LE(offset),
106
- 'int32_t' : (buffer: Buffer, offset: number) => buffer.readInt32LE(offset),
107
- 'uint32_t': (buffer: Buffer, offset: number) => buffer.readUInt32LE(offset),
108
- 'int64_t' : (buffer: Buffer, offset: number) => buffer.readBigInt64LE(offset),
109
- 'uint64_t': (buffer: Buffer, offset: number) => buffer.readBigUInt64LE(offset),
110
- 'float' : (buffer: Buffer, offset: number) => buffer.readFloatLE(offset),
111
- 'double' : (buffer: Buffer, offset: number) => buffer.readDoubleLE(offset),
112
-
113
- // array types
114
- 'char[]': (buffer: Buffer, offset: number, length: number) => {
115
- let result = ''
116
- for (let i = 0; i < length; i++) {
117
- const charCode = buffer.readUInt8(offset + i)
118
- if (charCode !== 0) {
119
- result += String.fromCharCode(charCode)
120
- } else {
121
- break
122
- }
123
- }
124
- return result
125
- },
126
- 'int8_t[]': (buffer: Buffer, offset: number, length: number) => {
127
- const result = new Array<number>(length)
128
- for (let i = 0; i < length; i++) result[i] = buffer.readInt8(offset + i)
129
- return result
130
- },
131
- 'uint8_t[]': (buffer: Buffer, offset: number, length: number) => {
132
- const result = new Array<number>(length)
133
- for (let i = 0; i < length; i++) result[i] = buffer.readUInt8(offset + i)
134
- return result
135
- },
136
- 'int16_t[]': (buffer: Buffer, offset: number, length: number) => {
137
- const result = new Array<number>(length)
138
- for (let i = 0; i < length; i++) result[i] = buffer.readInt16LE(offset + i * 2)
139
- return result
140
- },
141
- 'uint16_t[]': (buffer: Buffer, offset: number, length: number) => {
142
- const result = new Array<number>(length)
143
- for (let i = 0; i < length; i++) result[i] = buffer.readUInt16LE(offset + i * 2)
144
- return result
145
- },
146
- 'int32_t[]': (buffer: Buffer, offset: number, length: number) => {
147
- const result = new Array<number>(length)
148
- for (let i = 0; i < length; i++) result[i] = buffer.readInt32LE(offset + i * 4)
149
- return result
150
- },
151
- 'uint32_t[]': (buffer: Buffer, offset: number, length: number) => {
152
- const result = new Array<number>(length)
153
- for (let i = 0; i < length; i++) result[i] = buffer.readUInt32LE(offset + i * 4)
154
- return result
155
- },
156
- 'int64_t[]': (buffer: Buffer, offset: number, length: number) => {
157
- const result = new Array<BigInt>(length)
158
- for (let i = 0; i < length; i++) result[i] = buffer.readBigInt64LE(offset + i * 8)
159
- return result
160
- },
161
- 'uint64_t[]': (buffer: Buffer, offset: number, length: number) => {
162
- const result = new Array<BigInt>(length)
163
- for (let i = 0; i < length; i++) result[i] = buffer.readBigUInt64LE(offset + i * 8)
164
- return result
165
- },
166
- 'float[]': (buffer: Buffer, offset: number, length: number) => {
167
- const result = new Array<number>(length)
168
- for (let i = 0; i < length; i++) result[i] = buffer.readFloatLE(offset + i * 4)
169
- return result
170
- },
171
- 'double[]': (buffer: Buffer, offset: number, length: number) => {
172
- const result = new Array<number>(length)
173
- for (let i = 0; i < length; i++) result[i] = buffer.readDoubleLE(offset + i * 8)
174
- return result
175
- },
176
- }
package/lib/utils.ts DELETED
@@ -1,75 +0,0 @@
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
- }
Binary file
package/tests/main.ts DELETED
@@ -1,59 +0,0 @@
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, MavLinkPacketRegistry } from '..'
7
- import { 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: MavLinkPacketRegistry = {
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
- packet.protocol.data(packet.payload, clazz)
52
- } else {
53
- console.log('< (unknown)', packet.debug())
54
- }
55
- })
56
- }
57
- }
58
-
59
- main()
package/tsconfig.json DELETED
@@ -1,16 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "module": "commonjs",
4
- "lib": ["ES2020", "DOM"],
5
- "target": "es2020",
6
- "declaration": true,
7
- "outDir": "./dist",
8
- "esModuleInterop": true
9
- },
10
- "include": [
11
- "index.ts",
12
- ],
13
- "exclude": [
14
- "node_modules"
15
- ]
16
- }