Refactor command file

This commit is contained in:
Theo Arends 2021-02-16 15:54:53 +01:00
parent f39f29a316
commit 30a75edff5

View File

@ -354,7 +354,7 @@ bool TfsRenameFile(const char *fname1, const char *fname2) {
\*********************************************************************************************/ \*********************************************************************************************/
bool FileRunReady(void) { bool FileRunReady(void) {
return (UfsData.run_file_pos < 0); return (UfsData.run_file_pos < 0); // Check file ready to disable concurrency
} }
void FileRunLoop(void) { void FileRunLoop(void) {
@ -363,7 +363,7 @@ void FileRunLoop(void) {
if (strlen(UfsData.run_file) && !UfsData.run_file_mutex) { if (strlen(UfsData.run_file) && !UfsData.run_file_mutex) {
File file = ffsp->open(UfsData.run_file, "r"); File file = ffsp->open(UfsData.run_file, "r");
if (!file || !file.seek(UfsData.run_file_pos)) { if (!file || !file.seek(UfsData.run_file_pos)) {
UfsData.run_file_pos = -1; UfsData.run_file_pos = -1; // Signal file ready
return; return;
} }
@ -408,11 +408,14 @@ void FileRunLoop(void) {
} }
} }
void UfsAutoexec(void) { bool UfsExecuteCommandFile(const char *fname) {
if (TfsFileExists(TASM_FILE_AUTOEXEC)) { // Check for non-concurrency and file existance
snprintf(UfsData.run_file, sizeof(UfsData.run_file), TASM_FILE_AUTOEXEC); if (FileRunReady() && TfsFileExists(fname)) {
UfsData.run_file_pos = 0; snprintf(UfsData.run_file, sizeof(UfsData.run_file), fname);
UfsData.run_file_pos = 0; // Signal start of file
return true;
} }
return false;
} }
/*********************************************************************************************\ /*********************************************************************************************\
@ -499,9 +502,7 @@ void UFSRename(void) {
void UFSRun(void) { void UFSRun(void) {
if (XdrvMailbox.data_len > 0) { if (XdrvMailbox.data_len > 0) {
if (FileRunReady() && TfsFileExists(XdrvMailbox.data)) { if (UfsExecuteCommandFile(XdrvMailbox.data)) {
snprintf(UfsData.run_file, sizeof(UfsData.run_file), XdrvMailbox.data);
UfsData.run_file_pos = 0;
ResponseClear(); ResponseClear();
} else { } else {
ResponseCmndChar(PSTR(D_JSON_FAILED)); ResponseCmndChar(PSTR(D_JSON_FAILED));
@ -869,7 +870,9 @@ bool Xdrv50(uint8_t function) {
break; break;
#endif // USE_SDCARD #endif // USE_SDCARD
case FUNC_MQTT_INIT: case FUNC_MQTT_INIT:
if (!TasmotaGlobal.no_autoexec) { UfsAutoexec(); } if (!TasmotaGlobal.no_autoexec) {
UfsExecuteCommandFile(TASM_FILE_AUTOEXEC);
}
break; break;
case FUNC_COMMAND: case FUNC_COMMAND:
result = DecodeCommand(kUFSCommands, kUFSCommand); result = DecodeCommand(kUFSCommands, kUFSCommand);