parseResponse method
Parse the response bytes from the device.
Implementation
@override
VtjCommandResult<AccelSelfTestResult> parseResponse(Uint8List response) {
try {
if (response.isEmpty) {
return const VtjCommandResult.failure('Response is empty', null);
}
if (response.length < 2) {
return const VtjCommandResult.failure('Response too short', null);
}
final callId = response[0];
final statusCode = response[1];
if (callId != commandId) {
return VtjCommandResult.failure(
'Invalid call ID in response: $callId',
null,
);
}
if (statusCode == 3) {
return const VtjCommandResult.failure('Device in incorrect state', 3);
} else if (statusCode != 0) {
return VtjCommandResult.failure(
'Command failed with status: $statusCode',
statusCode,
);
}
// For successful response, we need the accelerometer data
// Expected response structure: [Call ID: 1 byte][Status: 1 byte][Test Status: 1 byte][X: 2 bytes][Y: 2 bytes][Z: 2 bytes]
// Total minimum length: 1 + 1 + 1 + 6 = 9 bytes
if (response.length < 9) {
return const VtjCommandResult.failure(
'Insufficient data in response for accelerometer readings',
null,
);
}
// Read test status (UINT8) at index 2
final testStatus = response[2];
// Read the three INT16 axis difference values starting from index 3
final xAxisDifference = _readInt16(response, 3);
final yAxisDifference = _readInt16(response, 5);
final zAxisDifference = _readInt16(response, 7);
return VtjCommandResult.success(
AccelSelfTestResult(
testStatus: testStatus,
xAxisDifference: xAxisDifference,
yAxisDifference: yAxisDifference,
zAxisDifference: zAxisDifference,
),
);
} catch (e) {
return VtjCommandResult.failure('Failed to parse response: $e', null);
}
}