Skip to content

Commit

Permalink
Store: Main-vehicle: Update store with recent changes
Browse files Browse the repository at this point in the history
Signed-off-by: Arturo Manzoli <[email protected]>
  • Loading branch information
ArturoManzoli committed Jun 19, 2024
1 parent 1bffb11 commit 0557467
Showing 1 changed file with 67 additions and 63 deletions.
130 changes: 67 additions & 63 deletions src/stores/mainVehicle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,72 +38,48 @@ 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<T> {
private _customValue: T
private _defaultValue: () => T
isCustom = false

/**
* @param {Ref<T>} defaultVal The default parameter value
*/
constructor(defaultVal: () => T) {
this._defaultValue = defaultVal
this._customValue = this.defaultValue
}

interface CustomParameter<T> {
/**
* 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()
const ws_protocol = location?.protocol === 'https:' ? 'wss' : 'ws'

const cpuLoad = ref<number>()
const globalAddress = useStorage('cockpit-vehicle-address', defaultGlobalAddress)
const _mainConnectionURI = new CustomizableParameter<Connection.URI>(() => {
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<Connection.URI>(() => {
const queryWebRTCSignallingURI = new URLSearchParams(window.location.search).get('webRTCSignallingURI')
return new Connection.URI(queryWebRTCSignallingURI || `${ws_protocol}://${globalAddress.value}:6021`)

const defaultMainConnectionURI = ref<string>(`${ws_protocol}://${globalAddress.value}/mavlink2rest/ws/mavlink`)
const defaultWebRTCSignallingURI = ref<string>(`${ws_protocol}://${globalAddress.value}:6021/`)
const customMainConnectionURI = useStorage('cockpit-vehicle-custom-main-connection-uri', {
data: defaultMainConnectionURI.value,
enabled: false,
} as CustomParameter<string>)
const customWebRTCSignallingURI = useStorage('cockpit-vehicle-custom-webrtc-signalling-uri', {
data: defaultWebRTCSignallingURI.value,
enabled: false,
} as CustomParameter<string>)
const customWebRTCConfiguration = useStorage('cockpit-custom-rtc-config', {
data: defaultRtcConfiguration,
enabled: false,
})
const webRTCSignallingURI = ref(_webRTCSignallingURI)

const lastHeartbeat = ref<Date>()
const firmwareType = ref<MavAutopilot>()
const vehicleType = ref<MavType>()
Expand All @@ -128,6 +104,18 @@ export const useMainVehicleStore = defineStore('main-vehicle', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const modes = ref<Map<string, any>>()

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
Expand All @@ -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.
Expand All @@ -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()
}

/**
Expand All @@ -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()
}

/**
Expand All @@ -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.
Expand All @@ -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()
}

/**
Expand Down Expand Up @@ -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<void> {
const enumMode = modes.value?.get(modeName)
if (enumMode !== undefined) {
mainVehicle.value?.setMode(enumMode)
await mainVehicle.value?.setMode(enumMode)
}
}

Expand All @@ -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<Vehicle.Abstract>[]): ArduPilot => {
const vehicle = vehicles?.last()?.deref()
Expand Down Expand Up @@ -479,11 +483,6 @@ export const useMainVehicleStore = defineStore('main-vehicle', () => {
}
}, 10)

const rtcConfiguration = {
bundlePolicy: 'max-bundle',
iceServers: [],
} as RTCConfiguration

return {
arm,
takeoff,
Expand All @@ -501,7 +500,11 @@ export const useMainVehicleStore = defineStore('main-vehicle', () => {
startMission,
globalAddress,
mainConnectionURI,
customMainConnectionURI,
defaultMainConnectionURI,
webRTCSignallingURI,
customWebRTCSignallingURI,
defaultWebRTCSignallingURI,
cpuLoad,
lastHeartbeat,
firmwareType,
Expand All @@ -521,6 +524,7 @@ export const useMainVehicleStore = defineStore('main-vehicle', () => {
icon,
configurationPages,
rtcConfiguration,
customWebRTCConfiguration,
genericVariables,
availableGenericVariables,
registerUsageOfGenericVariable,
Expand Down

0 comments on commit 0557467

Please sign in to comment.