Skip to content

Commit a6d3e67

Browse files
committed
feat(cloudlog): add response parsing for cloud logging service
- Add _NAVCloudLogResponse struct to represent service responses - Add NAVCloudLogResponseInit() to initialize response structures - Add NAVCloudLogResponseParse() to parse JSON responses - Validate required status field to prevent silent failures
1 parent bc6ab1a commit a6d3e67

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

ErrorLogUtils/NAVFoundation.CloudLog.axi

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,4 +268,81 @@ define_function NAVCloudLog(dev device, long level, char message[]) {
268268
}
269269

270270

271+
/**
272+
* @function NAVCloudLogResponseInit
273+
* @public
274+
* @description Initializes a _NAVCloudLogResponse structure with empty values.
275+
* Useful for creating a clean response structure before parsing.
276+
*
277+
* @param {_NAVCloudLogResponse} response - The response structure to initialize
278+
*
279+
* @example
280+
* stack_var _NAVCloudLogResponse response
281+
* NAVCloudLogResponseInit(response)
282+
*/
283+
define_function NAVCloudLogResponseInit(_NAVCloudLogResponse response) {
284+
response.status = ''
285+
response.id = ''
286+
response.timestamp = ''
287+
response.type = ''
288+
response.message = ''
289+
}
290+
291+
292+
/**
293+
* @function NAVCloudLogResponseParse
294+
* @public
295+
* @description Parses a JSON response from the cloud logging service into a structured format.
296+
* Automatically initializes the response structure and extracts all fields
297+
* from the JSON payload (status, id, timestamp, type, message).
298+
*
299+
* @param {char[]} payload - JSON string containing the cloud logging service response
300+
* @param {_NAVCloudLogResponse} response - Output parameter to receive the parsed response data
301+
*
302+
* @returns {char} True if parsing succeeded, false if JSON parsing failed
303+
*
304+
* @example
305+
* stack_var _NAVCloudLogResponse response
306+
* stack_var char jsonPayload[1024]
307+
* jsonPayload = '{"status":"success","id":"abc-123","timestamp":"2026-01-29T10:30:00Z","type":"control_system"}'
308+
* if (NAVCloudLogResponseParse(jsonPayload, response)) {
309+
* // Response successfully parsed
310+
* if (response.status == 'success') {
311+
* send_string 0, "'Log submitted successfully: ', response.id"
312+
* }
313+
* }
314+
*/
315+
define_function char NAVCloudLogResponseParse(char payload[], _NAVCloudLogResponse response) {
316+
stack_var _NAVJson json
317+
318+
if (!NAVJsonParse(payload, json)) {
319+
NAVLibraryFunctionErrorLog(NAV_LOG_LEVEL_WARNING,
320+
__NAV_FOUNDATION_CLOUDLOG__,
321+
'NAVCloudLogResponseParse',
322+
'Failed to parse JSON response')
323+
return false
324+
}
325+
326+
NAVCloudLogResponseInit(response)
327+
328+
// Query all response fields
329+
NAVJsonQueryString(json, '.status', response.status)
330+
NAVJsonQueryString(json, '.id', response.id)
331+
NAVJsonQueryString(json, '.timestamp', response.timestamp)
332+
NAVJsonQueryString(json, '.type', response.type)
333+
NAVJsonQueryString(json, '.message', response.message)
334+
335+
// Validate that critical fields were found
336+
if (!length_array(response.status)) {
337+
NAVLibraryFunctionErrorLog(NAV_LOG_LEVEL_WARNING,
338+
__NAV_FOUNDATION_CLOUDLOG__,
339+
'NAVCloudLogResponseParse',
340+
'Response missing required field: status')
341+
return false
342+
}
343+
344+
return true
345+
}
346+
347+
271348
#END_IF // __NAV_FOUNDATION_CLOUDLOG__

ErrorLogUtils/NAVFoundation.CloudLog.h.axi

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,24 @@ struct _NAVCloudLog {
104104
}
105105

106106

107+
/**
108+
* @struct _NAVCloudLogResponse
109+
* @description Structure representing the response from the cloud logging service.
110+
* Indicates success or failure of log submission along with relevant details.
111+
*
112+
* @property {char[10]} status - Status of the log submission ("success" or "error")
113+
* @property {char[40]} id - Unique identifier for the log entry (UUID v4 format, 36 chars)
114+
* @property {char[32]} timestamp - ISO 8601 formatted timestamp of when the response was generated (~24 chars)
115+
* @property {char[15]} type - Type of log ("control_system" or "generic")
116+
* @property {char[512]} message - Error message if status is "error"
117+
*/
118+
struct _NAVCloudLogResponse {
119+
char status[10]
120+
char id[NAV_CLOUDLOG_SIZE_ID]
121+
char timestamp[NAV_CLOUDLOG_SIZE_TIMESTAMP]
122+
char type[15]
123+
char message[NAV_CLOUDLOG_SIZE_MESSAGE]
124+
}
125+
126+
107127
#END_IF // __NAV_FOUNDATION_CLOUDLOG_H__

0 commit comments

Comments
 (0)