Skip to content

Commit

Permalink
this feels wrong
Browse files Browse the repository at this point in the history
  • Loading branch information
Williangalvani committed Dec 17, 2024
1 parent 6b192f2 commit c2a75e3
Showing 1 changed file with 90 additions and 15 deletions.
105 changes: 90 additions & 15 deletions src/libs/actions/data-lake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string | number>
}

/**
*
* @param data
* @param id
*/
function determineTypeAndValue(
data: object | string | number | boolean | Array<string | number>,
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<string, unknown>

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<string | number>
): 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)
}

Expand Down

0 comments on commit c2a75e3

Please sign in to comment.