@@ -52,27 +52,102 @@ export const getDataLakeVariableData = (id: string): string | number | boolean |
5252 return dataLakeVariableData [ id ]
5353}
5454
55- export const setDataLakeVariableData = ( id : string , data : object | string | number | boolean ) : void => {
56- const newData = data
57- if ( data === null ) {
58- return
55+ /**
56+ * The result of type-checking the incoming mavlink data
57+ */
58+ interface TypeCheckResult {
59+ /**
60+ *
61+ */
62+ type : 'string' | 'number' | 'boolean' | 'object'
63+ /**
64+ *
65+ */
66+ value : string | number | boolean | object | Array < string | number >
67+ }
68+
69+ /**
70+ *
71+ * @param data
72+ * @param id
73+ */
74+ function determineTypeAndValue (
75+ data : object | string | number | boolean | Array < string | number > ,
76+ id : string
77+ ) : TypeCheckResult | null {
78+ const typeOfData = typeof data
79+
80+ if ( typeOfData !== 'object' ) {
81+ return {
82+ type : typeOfData as 'string' | 'number' | 'boolean' ,
83+ value : data ,
84+ }
5985 }
60- if ( dataLakeVariableData [ id ] === undefined ) {
61- console . trace ( `Cockpit action variable with id '${ id } ' does not exist. Creating it.` )
62- const type_of_variable = typeof data
63- if ( type_of_variable === 'object' ) {
64- // TODO: support strings
86+
87+ // Handle arrays and objects
88+ if ( Array . isArray ( data ) ) {
89+ if ( data . length === 0 ) return null
90+
91+ if ( typeof data [ 0 ] === 'string' ) {
92+ return {
93+ type : 'string' ,
94+ value : data . join ( '' ) ,
95+ }
6596 }
66- if ( type_of_variable !== 'string' && type_of_variable !== 'number' ) {
67- console . debug ( `attempting to create a variable with type ${ type_of_variable } . Skipping` )
68- return
97+
98+ if ( typeof data [ 0 ] === 'number' ) {
99+ // Handle array of numbers by creating individual variables
100+ data . forEach ( ( value , index ) => {
101+ setDataLakeVariableData ( `${ id } /${ index } ` , value )
102+ } )
103+ return null
69104 }
70- createDataLakeVariable ( new DataLakeVariable ( id , id , typeof data ) )
71105 }
72- if ( newData === undefined || typeof newData === 'object' ) {
106+
107+ // Handle objects with special properties
108+ const objData = data as Record < string , unknown >
109+
110+ if ( 'type' in objData && 'value' in objData ) {
111+ return {
112+ type : typeof objData . type as 'string' | 'number' | 'boolean' ,
113+ value : objData . value as string | number | boolean ,
114+ }
115+ }
116+
117+ if ( 'bits' in objData ) {
118+ return {
119+ type : typeof objData . bits as 'string' | 'number' | 'boolean' ,
120+ value : objData . bits as string | number | boolean ,
121+ }
122+ }
123+
124+ return null
125+ }
126+
127+ export const setDataLakeVariableData = (
128+ id : string ,
129+ data : object | string | number | boolean | Array < string | number >
130+ ) : void => {
131+ if ( data === null ) return
132+
133+ const typeCheckResult = determineTypeAndValue ( data , id )
134+ if ( ! typeCheckResult ) return
135+
136+ const { type, value } = typeCheckResult
137+
138+ // Only proceed with string or number types
139+ if ( type !== 'string' && type !== 'number' ) {
140+ console . debug ( `attempting to create a variable with type ${ type } . Skipping` )
73141 return
74142 }
75- dataLakeVariableData [ id ] = newData
143+
144+ // Create variable if it doesn't exist
145+ if ( dataLakeVariableData [ id ] === undefined ) {
146+ createDataLakeVariable ( new DataLakeVariable ( id , id , type ) )
147+ }
148+
149+ // Update the value and notify listeners
150+ dataLakeVariableData [ id ] = value
76151 notifyDataLakeVariableListeners ( id )
77152}
78153
0 commit comments