From 05574670bc23a1caa6993c5f3750768803f4f61d Mon Sep 17 00:00:00 2001 From: Arturo Manzoli Date: Wed, 19 Jun 2024 10:20:13 -0300 Subject: [PATCH] Store: Main-vehicle: Update store with recent changes Signed-off-by: Arturo Manzoli --- src/stores/mainVehicle.ts | 130 ++++++++++++++++++++------------------ 1 file changed, 67 insertions(+), 63 deletions(-) diff --git a/src/stores/mainVehicle.ts b/src/stores/mainVehicle.ts index 40e66d274..cf8b72b43 100644 --- a/src/stores/mainVehicle.ts +++ b/src/stores/mainVehicle.ts @@ -38,53 +38,25 @@ import { useControllerStore } from './controller' import { useWidgetManagerStore } from './widgetManager' /** - * This is an abstraction that holds a customizable parameter that can fallback to a default value - * @template T The customizable parameter type + * Custom parameter data description interface */ -class CustomizableParameter { - private _customValue: T - private _defaultValue: () => T - isCustom = false - - /** - * @param {Ref} defaultVal The default parameter value - */ - constructor(defaultVal: () => T) { - this._defaultValue = defaultVal - this._customValue = this.defaultValue - } - +interface CustomParameter { /** - * Sets the URI to a given custom one - * @param {T} val + * Real data associated with the parameter */ - set val(val: T) { - this._customValue = val - } + data: T /** - * @returns {T} The current configured parameter, whether default or custom + * Indicates if the custom parameter data is enabled */ - get val(): T { - return this.isCustom ? this._customValue : this.defaultValue - } - - /** - * @returns {T} The current configured parameter, whether default or custom - */ - get defaultValue(): T { - return this._defaultValue() - } - - /** - * Resets custom to the default value and disables custom - */ - public reset(): void { - this.isCustom = false - this._customValue = this.defaultValue - } + enabled: boolean } +const defaultRtcConfiguration = { + bundlePolicy: 'max-bundle', + iceServers: [], +} as RTCConfiguration + export const useMainVehicleStore = defineStore('main-vehicle', () => { const controllerStore = useControllerStore() const widgetStore = useWidgetManagerStore() @@ -92,18 +64,22 @@ export const useMainVehicleStore = defineStore('main-vehicle', () => { const cpuLoad = ref() const globalAddress = useStorage('cockpit-vehicle-address', defaultGlobalAddress) - const _mainConnectionURI = new CustomizableParameter(() => { - const queryMainConnectionURI = new URLSearchParams(window.location.search).get('mainConnectionURI') - return new Connection.URI( - queryMainConnectionURI || `${ws_protocol}://${globalAddress.value}/mavlink2rest/ws/mavlink` - ) - }) - const mainConnectionURI = ref(_mainConnectionURI) - const _webRTCSignallingURI = new CustomizableParameter(() => { - const queryWebRTCSignallingURI = new URLSearchParams(window.location.search).get('webRTCSignallingURI') - return new Connection.URI(queryWebRTCSignallingURI || `${ws_protocol}://${globalAddress.value}:6021`) + + const defaultMainConnectionURI = ref(`${ws_protocol}://${globalAddress.value}/mavlink2rest/ws/mavlink`) + const defaultWebRTCSignallingURI = ref(`${ws_protocol}://${globalAddress.value}:6021/`) + const customMainConnectionURI = useStorage('cockpit-vehicle-custom-main-connection-uri', { + data: defaultMainConnectionURI.value, + enabled: false, + } as CustomParameter) + const customWebRTCSignallingURI = useStorage('cockpit-vehicle-custom-webrtc-signalling-uri', { + data: defaultWebRTCSignallingURI.value, + enabled: false, + } as CustomParameter) + const customWebRTCConfiguration = useStorage('cockpit-custom-rtc-config', { + data: defaultRtcConfiguration, + enabled: false, }) - const webRTCSignallingURI = ref(_webRTCSignallingURI) + const lastHeartbeat = ref() const firmwareType = ref() const vehicleType = ref() @@ -128,6 +104,18 @@ export const useMainVehicleStore = defineStore('main-vehicle', () => { // eslint-disable-next-line @typescript-eslint/no-explicit-any const modes = ref>() + const mainConnectionURI = computed(() => { + const queryURI = new URLSearchParams(window.location.search).get('mainConnectionURI') + const customURI = customMainConnectionURI.value.enabled ? customMainConnectionURI.value.data : undefined + return new Connection.URI(queryURI ?? customURI ?? defaultMainConnectionURI.value) + }) + + const webRTCSignallingURI = computed(() => { + const queryWebRTCSignallingURI = new URLSearchParams(window.location.search).get('webRTCSignallingURI') + const customURI = customWebRTCSignallingURI.value.enabled ? customWebRTCSignallingURI.value.data : undefined + return new Connection.URI(queryWebRTCSignallingURI ?? customURI ?? defaultWebRTCSignallingURI.value) + }) + /** * Check if vehicle is online (no more than 5 seconds passed since last heartbeat) * @returns { boolean } True if vehicle is online @@ -136,6 +124,21 @@ export const useMainVehicleStore = defineStore('main-vehicle', () => { return lastHeartbeat.value !== undefined && new Date(timeNow.value).getTime() - lastHeartbeat.value.getTime() < 5000 }) + const rtcConfiguration = computed(() => { + const queryWebRtcConfiguration = new URLSearchParams(window.location.search).get('webRTCConfiguration') + if (queryWebRtcConfiguration) { + console.log('Using WebRTC configuration from query parameter') + console.log(queryWebRtcConfiguration) + try { + return JSON.parse(queryWebRtcConfiguration) + } catch (error) { + console.error('Failed to parse WebRTC configuration from query parameter.', error) + } + } + console.log('Using WebRTC configuration from storage.') + return customWebRTCConfiguration.value.enabled ? customWebRTCConfiguration.value.data : defaultRtcConfiguration + }) + /** * Arm the vehicle. * Awaits user confirmation before arming the vehicle. Resolves when arming is successful or rejects if the action is cancelled. @@ -146,7 +149,7 @@ export const useMainVehicleStore = defineStore('main-vehicle', () => { throw new Error('No vehicle available to arm.') } - mainVehicle.value.arm() + await mainVehicle.value.arm() } /** @@ -159,7 +162,7 @@ export const useMainVehicleStore = defineStore('main-vehicle', () => { throw new Error('No vehicle available to disarm.') } - mainVehicle.value.disarm() + await mainVehicle.value.disarm() } /** @@ -171,7 +174,7 @@ export const useMainVehicleStore = defineStore('main-vehicle', () => { throw new Error('No vehicle available for takeoff') } - mainVehicle.value.takeoff(altitude_setpoint.value) + await mainVehicle.value.takeoff(altitude_setpoint.value) } /** * Change the altitude of the vehicle. @@ -194,7 +197,7 @@ export const useMainVehicleStore = defineStore('main-vehicle', () => { throw new Error('No vehicle available to land.') } - mainVehicle.value.land() + await mainVehicle.value.land() } /** @@ -293,10 +296,10 @@ export const useMainVehicleStore = defineStore('main-vehicle', () => { * Set vehicle flight mode * @param {string} modeName */ - function setFlightMode(modeName: string): void { + async function setFlightMode(modeName: string): Promise { const enumMode = modes.value?.get(modeName) if (enumMode !== undefined) { - mainVehicle.value?.setMode(enumMode) + await mainVehicle.value?.setMode(enumMode) } } @@ -319,12 +322,13 @@ export const useMainVehicleStore = defineStore('main-vehicle', () => { ConnectionManager.onMainConnection.add(() => { const newMainConnection = ConnectionManager.mainConnection() + console.log('Main connection changed:', newMainConnection?.uri().toString()) if (newMainConnection !== undefined) { - mainConnectionURI.value.val = newMainConnection.uri() + customMainConnectionURI.value.data = newMainConnection.uri().toString() } }) - ConnectionManager.addConnection(mainConnectionURI.value.val, Protocol.Type.MAVLink) + ConnectionManager.addConnection(mainConnectionURI.value, Protocol.Type.MAVLink) const getAutoPilot = (vehicles: WeakRef[]): ArduPilot => { const vehicle = vehicles?.last()?.deref() @@ -479,11 +483,6 @@ export const useMainVehicleStore = defineStore('main-vehicle', () => { } }, 10) - const rtcConfiguration = { - bundlePolicy: 'max-bundle', - iceServers: [], - } as RTCConfiguration - return { arm, takeoff, @@ -501,7 +500,11 @@ export const useMainVehicleStore = defineStore('main-vehicle', () => { startMission, globalAddress, mainConnectionURI, + customMainConnectionURI, + defaultMainConnectionURI, webRTCSignallingURI, + customWebRTCSignallingURI, + defaultWebRTCSignallingURI, cpuLoad, lastHeartbeat, firmwareType, @@ -521,6 +524,7 @@ export const useMainVehicleStore = defineStore('main-vehicle', () => { icon, configurationPages, rtcConfiguration, + customWebRTCConfiguration, genericVariables, availableGenericVariables, registerUsageOfGenericVariable,