uploadScript method
Implementation
Future<void> uploadScript(String fileName, String filePath) async {
try {
_log.info("Uploading script: $fileName");
// TODO temporarily observe memory usage
await sendString(
'print("Frame Mem: " .. tostring(collectgarbage("count")))',
awaitResponse: true);
String file = await rootBundle.loadString(filePath);
file = file.replaceAll('\\', '\\\\');
file = file.replaceAll("\r\n", "\\n");
file = file.replaceAll("\n", "\\n");
file = file.replaceAll("'", "\\'");
file = file.replaceAll('"', '\\"');
var resp = await sendString(
"f=frame.file.open('$fileName', 'w');print('\x02')",
log: false);
if (resp != "\x02") {
throw ("Error opening file: $resp");
}
int index = 0;
int chunkSize = maxStringLength! - 22;
while (index < file.length) {
// Don't go over the end of the string
if (index + chunkSize > file.length) {
chunkSize = file.length - index;
}
// Don't split on an escape character
while (file[index + chunkSize - 1] == '\\') {
chunkSize -= 1;
}
String chunk = file.substring(index, index + chunkSize);
resp = await sendString("f:write('$chunk');print('\x02')", log: false);
if (resp != "\x02") {
throw ("Error writing file: $resp");
}
index += chunkSize;
}
resp = await sendString("f:close();print('\x02')", log: false);
if (resp != "\x02") {
throw ("Error closing file: $resp");
}
// TODO temporarily observe memory usage
await sendString(
'print("Frame Mem: " .. tostring(collectgarbage("count")))',
awaitResponse: true);
} catch (error) {
_log.warning("Couldn't upload script. $error");
return Future.error(BrilliantBluetoothException(error.toString()));
}
}