Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add RAW MAVLink support #1462

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"leaflet": "1.9.3",
"localforage": "^1.10.0",
"mathjs": "^13.0.3",
"mavlink-browser": "git+https://github.com/williangalvani/mavlink-browser.git#cockpit_updates",
"pinia": "^2.0.13",
"roboto-fontface": "*",
"semver": "^7.5.4",
Expand Down
26 changes: 23 additions & 3 deletions src/libs/connection/websocket-connection.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { MAVLink20Processor } from 'mavlink-browser'

import * as Protocol from '@/libs/vehicle/protocol/protocol'

import * as Connection from './connection'
Expand All @@ -9,15 +11,18 @@ export class WebSocketConnection extends Connection.Abstract {
_socket: WebSocket
private _textEncoder = new TextEncoder()
private _textDecoder = new TextDecoder()

private mavlinkParser = new MAVLink20Processor()
/**
* Websocket constructor
* @param {Connection.URI} uri
* @param {Protocol.Type} vehicleProtocol
*/
constructor(uri: Connection.URI, vehicleProtocol: Protocol.Type) {
super(uri, vehicleProtocol)

this.mavlinkParser.on('message', (message: any) => {
const data = this._textEncoder.encode(this.mavlink2restfy(message))
this.onRead.emit_value(data)
})
this._socket = this.createSocket(uri)
}

Expand Down Expand Up @@ -72,7 +77,13 @@ export class WebSocketConnection extends Connection.Abstract {
// We need to have the same abstraction for all onRead
socket.onmessage = (message: MessageEvent) => {
try {
this.onRead.emit_value(this._textEncoder.encode(message.data))
if (String.fromCharCode(message.data[0]) == '{') {
this.onRead.emit_value(this._textEncoder.encode(message.data))
} else {
new Response(message.data).arrayBuffer().then((buffer) => {
this.mavlinkParser.parseBuffer(new Uint8Array(buffer))
})
}
} catch (error) {
console.error('Error reading websocket message: ', error)
}
Expand All @@ -84,4 +95,13 @@ export class WebSocketConnection extends Connection.Abstract {
}
return socket
}

/**
*
* @param {any} message a "raw" MAVLink message as it comes from Mavlink20Processor
* @returns {string} a MAVLink2Rest-like stringified json object
*/
mavlink2restfy(message): any {
return JSON.stringify(this.mavlinkParser.toMavlink2RestV1Format(message))
}
}
Loading