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.
package/lib/mavesp.ts DELETED
@@ -1,112 +0,0 @@
1
- import { EventEmitter } from 'events'
2
-
3
- import { Socket, createSocket } from 'dgram'
4
- import { Writable, PassThrough } from 'stream'
5
- import { MavLinkPacketSplitter, MavLinkPacketParser, MavLinkPacketSignature } from './mavlink'
6
- import { MavLinkProtocol, MavLinkProtocolV2 } from './mavlink'
7
- import { waitFor } from './utils'
8
- import { uint8_t, MavLinkData } from 'mavlink-mappings'
9
-
10
- /**
11
- * Encapsulation of communication with MavEsp8266
12
- */
13
- export class MavEsp8266 extends EventEmitter {
14
- private input: Writable
15
- private socket: Socket
16
- private ip: string = ''
17
- private sendPort: number = 14555
18
- private seq: number = 0
19
-
20
- constructor() {
21
- super()
22
-
23
- this.input = new PassThrough()
24
-
25
- this.processIncommingUDPData = this.processIncommingUDPData.bind(this)
26
- this.processIncommingPacket = this.processIncommingPacket.bind(this)
27
-
28
- // Create the reader as usual by piping the source stream through the splitter
29
- // and packet parser
30
- const reader = this.input
31
- .pipe(new MavLinkPacketSplitter())
32
- .pipe(new MavLinkPacketParser())
33
-
34
- reader.on('data', this.processIncommingPacket)
35
- }
36
-
37
- /**
38
- * Start communication with the controller via MAVESP2866
39
- *
40
- * @param receivePort port to receive messages on (default: 14550)
41
- * @param sendPort port to send messages to (default: 14555)
42
- */
43
- async start(receivePort: number = 14550, sendPort: number = 14555) {
44
- this.sendPort = sendPort
45
-
46
- // Create a UDP socket
47
- this.socket = createSocket({ type: 'udp4', reuseAddr: true })
48
- this.socket.on('message', this.processIncommingUDPData)
49
-
50
- // Start listening on the socket
51
- return new Promise((resolve, reject) => {
52
- this.socket.bind(receivePort, () => {
53
- // Wait for the first package to be returned to read the ip address
54
- // of the controller
55
- waitFor(() => this.ip !== '')
56
- .then(() => { resolve(this.ip) })
57
- .catch(e => { reject(e) })
58
- })
59
- })
60
- }
61
-
62
- /**
63
- * Send a packet
64
- *
65
- * @param msg message to send
66
- * @param sysid system id
67
- * @param compid component id
68
- */
69
- send(msg: MavLinkData, sysid: uint8_t = MavLinkProtocol.SYS_ID, compid: uint8_t = MavLinkProtocol.COMP_ID) {
70
- const protocol = new MavLinkProtocolV2(sysid, compid)
71
- const buffer = protocol.serialize(msg, this.seq++)
72
- this.seq &= 255
73
- this.sendBuffer(buffer)
74
- }
75
-
76
- /**
77
- * Send a signed packet
78
- *
79
- * @param msg message to send
80
- * @param sysid system id
81
- * @param compid component id
82
- * @param linkId link id for the signature
83
- */
84
- sendSigned(msg: MavLinkData, key: Buffer, linkId: uint8_t = 1, sysid: uint8_t = MavLinkProtocol.SYS_ID, compid: uint8_t = MavLinkProtocol.COMP_ID) {
85
- const protocol = new MavLinkProtocolV2(sysid, compid, MavLinkProtocolV2.IFLAG_SIGNED)
86
- const b1 = protocol.serialize(msg, this.seq++)
87
- this.seq &= 255
88
- const b2 = protocol.sign(b1, linkId, key)
89
- this.sendBuffer(b2)
90
- }
91
-
92
- /**
93
- * Send raw data over the socket. Useful for custom implementation of data sending
94
- *
95
- * @param buffer buffer to send
96
- */
97
- sendBuffer(buffer: Buffer) {
98
- this.socket.send(buffer, this.sendPort, this.ip)
99
- }
100
-
101
- private processIncommingUDPData(buffer, metadata) {
102
- // store the remote ip address
103
- if (this.ip === '') this.ip = metadata.address
104
- // pass on the data to the input stream
105
- this.input.write(buffer)
106
- }
107
-
108
- private processIncommingPacket(packet) {
109
- // let the user know we received the packet
110
- this.emit('data', packet)
111
- }
112
- }