diff --git a/src/libs/actions/data-lake.ts b/src/libs/actions/data-lake.ts index 57b2c09d3..ccaef4bbf 100644 --- a/src/libs/actions/data-lake.ts +++ b/src/libs/actions/data-lake.ts @@ -52,27 +52,102 @@ export const getDataLakeVariableData = (id: string): string | number | boolean | return dataLakeVariableData[id] } -export const setDataLakeVariableData = (id: string, data: object | string | number | boolean): void => { - const newData = data - if (data === null) { - return +/** + * The result of type-checking the incoming mavlink data + */ +interface TypeCheckResult { + /** + * + */ + type: 'string' | 'number' | 'boolean' | 'object' + /** + * + */ + value: string | number | boolean | object | Array +} + +/** + * + * @param data + * @param id + */ +function determineTypeAndValue( + data: object | string | number | boolean | Array, + id: string +): TypeCheckResult | null { + const typeOfData = typeof data + + if (typeOfData !== 'object') { + return { + type: typeOfData as 'string' | 'number' | 'boolean', + value: data, + } } - if (dataLakeVariableData[id] === undefined) { - console.trace(`Cockpit action variable with id '${id}' does not exist. Creating it.`) - const type_of_variable = typeof data - if (type_of_variable === 'object') { - // TODO: support strings + + // Handle arrays and objects + if (Array.isArray(data)) { + if (data.length === 0) return null + + if (typeof data[0] === 'string') { + return { + type: 'string', + value: data.join(''), + } } - if (type_of_variable !== 'string' && type_of_variable !== 'number') { - console.debug(`attempting to create a variable with type ${type_of_variable}. Skipping`) - return + + if (typeof data[0] === 'number') { + // Handle array of numbers by creating individual variables + data.forEach((value, index) => { + setDataLakeVariableData(`${id}/${index}`, value) + }) + return null } - createDataLakeVariable(new DataLakeVariable(id, id, typeof data)) } - if (newData === undefined || typeof newData === 'object') { + + // Handle objects with special properties + const objData = data as Record + + if ('type' in objData && 'value' in objData) { + return { + type: typeof objData.type as 'string' | 'number' | 'boolean', + value: objData.value as string | number | boolean, + } + } + + if ('bits' in objData) { + return { + type: typeof objData.bits as 'string' | 'number' | 'boolean', + value: objData.bits as string | number | boolean, + } + } + + return null +} + +export const setDataLakeVariableData = ( + id: string, + data: object | string | number | boolean | Array +): void => { + if (data === null) return + + const typeCheckResult = determineTypeAndValue(data, id) + if (!typeCheckResult) return + + const { type, value } = typeCheckResult + + // Only proceed with string or number types + if (type !== 'string' && type !== 'number') { + console.debug(`attempting to create a variable with type ${type}. Skipping`) return } - dataLakeVariableData[id] = newData + + // Create variable if it doesn't exist + if (dataLakeVariableData[id] === undefined) { + createDataLakeVariable(new DataLakeVariable(id, id, type)) + } + + // Update the value and notify listeners + dataLakeVariableData[id] = value notifyDataLakeVariableListeners(id) }