parseResponse method
Parse the response bytes from the device.
Implementation
@override
VtjCommandResult<GetVersionResult> 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 != 0) {
return VtjCommandResult.failure(
'Command failed with status: $statusCode',
statusCode,
);
}
// For successful response, we need the version string data
// Expected response structure: [Call ID: 1 byte][Status: 1 byte][Version String: 1-30 bytes, null terminated]
// Total minimum length: 1 + 1 + 1 = 3 bytes (at least one character + null terminator)
if (response.length < 3) {
return const VtjCommandResult.failure(
'Insufficient data in response for version string',
null,
);
}
// Extract version string starting from index 2
final versionBytes = response.sublist(2);
// Find null terminator or use all bytes if no null terminator found
int nullIndex = versionBytes.indexOf(0);
final actualVersionBytes =
nullIndex >= 0 ? versionBytes.sublist(0, nullIndex) : versionBytes;
// Convert bytes to ASCII string
final versionString = ascii.decode(actualVersionBytes);
return VtjCommandResult.success(GetVersionResult(version: versionString));
} catch (e) {
return VtjCommandResult.failure('Failed to parse response: $e', null);
}
}