Idea of how typescript should decode Protobuffer
This commit is contained in:
+16
-14
@@ -1,5 +1,6 @@
|
||||
import { writable } from 'svelte/store'
|
||||
import type { IMUMsg } from '$lib/types/models'
|
||||
import type { IMUReport } from '$lib/platform_shared/imu_report'
|
||||
|
||||
const maxIMUData = 100
|
||||
|
||||
@@ -14,23 +15,24 @@ export const imu = (() => {
|
||||
bmp_temp: [] as number[]
|
||||
})
|
||||
|
||||
const addData = (content: IMUMsg) => {
|
||||
const addData = (content: IMUReport) => {
|
||||
update(data => {
|
||||
if (content.imu && content.imu[4]) {
|
||||
data.x = [...data.x, content.imu[0]].slice(-maxIMUData)
|
||||
data.y = [...data.y, content.imu[1]].slice(-maxIMUData)
|
||||
data.z = [...data.z, content.imu[2]].slice(-maxIMUData)
|
||||
}
|
||||
if (content.success) {
|
||||
data.x = [...data.x, content.x].slice(-maxIMUData)
|
||||
data.y = [...data.y, content.y].slice(-maxIMUData)
|
||||
data.z = [...data.z, content.z].slice(-maxIMUData)
|
||||
}
|
||||
|
||||
if (content.mag && content.mag[4]) {
|
||||
data.heading = [...data.heading, content.mag[3]].slice(-maxIMUData)
|
||||
}
|
||||
// TODO: Temporarily disabled
|
||||
// if (content.mag && content.mag[4]) {
|
||||
// data.heading = [...data.heading, content.mag[3]].slice(-maxIMUData)
|
||||
// }
|
||||
|
||||
if (content.bmp && content.bmp[3]) {
|
||||
data.pressure = [...data.pressure, content.bmp[0]].slice(-maxIMUData)
|
||||
data.altitude = [...data.altitude, content.bmp[1]].slice(-maxIMUData)
|
||||
data.bmp_temp = [...data.bmp_temp, content.bmp[2]].slice(-maxIMUData)
|
||||
}
|
||||
// if (content.bmp && content.bmp[3]) {
|
||||
// data.pressure = [...data.pressure, content.bmp[0]].slice(-maxIMUData)
|
||||
// data.altitude = [...data.altitude, content.bmp[1]].slice(-maxIMUData)
|
||||
// data.bmp_temp = [...data.bmp_temp, content.bmp[2]].slice(-maxIMUData)
|
||||
// }
|
||||
|
||||
return data
|
||||
})
|
||||
|
||||
@@ -4,18 +4,24 @@ import { encode, decode } from '@msgpack/msgpack'
|
||||
const socketEvents = ['open', 'close', 'error', 'message', 'unresponsive'] as const
|
||||
type SocketEvent = (typeof socketEvents)[number]
|
||||
|
||||
type SocketMessage = [number, string?, unknown?]
|
||||
type SocketMessage = [string, ArrayBuffer]
|
||||
|
||||
let useBinary = false
|
||||
|
||||
const decodeMessage = (data: string | ArrayBuffer): SocketMessage | null => {
|
||||
useBinary = data instanceof ArrayBuffer
|
||||
|
||||
|
||||
const decodeMessage = (data: ArrayBuffer): SocketMessage | null => {
|
||||
|
||||
try {
|
||||
if (useBinary) {
|
||||
return decode(new Uint8Array(data as ArrayBuffer)) as SocketMessage
|
||||
const view = new Uint8Array(data);
|
||||
let comma_index: number = 0;
|
||||
let tag: string = "";
|
||||
for (comma_index = 0; view[comma_index] != 0x2c; comma_index++) { // 0x2c is the ascii code for a comma!
|
||||
tag += String.fromCharCode(view[comma_index]);
|
||||
if (comma_index >= data.byteLength) { throw new RangeError("Comma index exceeded")}
|
||||
}
|
||||
return JSON.parse(data as string)
|
||||
|
||||
return [ tag, data.slice(comma_index+1) ]
|
||||
} catch (error) {
|
||||
console.error(`Could not decode data: ${new Uint8Array(data as ArrayBuffer)} - ${error}`)
|
||||
}
|
||||
@@ -72,7 +78,7 @@ function createWebSocket() {
|
||||
resetUnresponsiveCheck()
|
||||
const message = decodeMessage(frame.data)
|
||||
if (!message) return
|
||||
const [, event, payload = undefined] = message
|
||||
const [event, payload] = message
|
||||
if (event) listeners.get(event)?.forEach(listener => listener(payload))
|
||||
}
|
||||
ws.onerror = ev => disconnect('error', ev)
|
||||
|
||||
Reference in New Issue
Block a user