This commit is contained in:
Tester23
2025-12-02 18:29:04 +01:00
parent ed35203ca4
commit afdc0a48c9
5 changed files with 43 additions and 4 deletions

View File

@@ -662,7 +662,7 @@ void SIM_GenerateChannelStatesDesc(char *o, int outLen) {
}
}
}
#endif
const char *CMD_ExpandConstantString(const char *s, const char *stop, char *out, int outLen) {
int idx;
const char *ret;
@@ -750,7 +750,6 @@ const char *CMD_ExpandConstantString(const char *s, const char *stop, char *out,
}
return false;
}
#endif
const char* CMD_ExpandConstantToString(const char* constant, char* out, char* stop)
{

View File

@@ -330,6 +330,7 @@ void SVM_RunThreads(int deltaMS);
void CMD_InitScripting();
void SVM_RunStartupCommandAsScript();
byte* LFS_ReadFile(const char* fname);
byte* LFS_ReadFileExpanding(const char* fname);
int LFS_WriteFile(const char *fname, const byte *data, int len, bool bAppend);
commandResult_t CMD_ClearAllHandlers(const void* context, const char* cmd, const char* args, int cmdFlags);

View File

@@ -270,6 +270,19 @@ byte *LFS_ReadFile(const char *fname) {
#endif
return 0;
}
byte* LFS_ReadFileExpanding(const char* fname) {
byte *d = LFS_ReadFile(fname);
if (d == 0)
return;
const char *s = (const char *)d;
int vars = CMD_CountVarsInString(s);
if (vars == 0) {
return d;
}
char *r = CMD_ExpandingStrdup(s);
free(d);
return (byte*)r;
}
int LFS_WriteFile(const char *fname, const byte *data, int len, bool bAppend) {
#if ENABLE_LITTLEFS
init_lfs(1);

View File

@@ -1555,7 +1555,7 @@ commandResult_t MQTT_PublishFile(const void* context, const char* cmd, const cha
int flags = 0;
byte*data;
Tokenizer_TokenizeString(args, TOKENIZER_ALLOW_QUOTES | TOKENIZER_ALLOW_ESCAPING_QUOTATIONS);
Tokenizer_TokenizeString(args, TOKENIZER_ALLOW_QUOTES | TOKENIZER_ALLOW_ESCAPING_QUOTATIONS | TOKENIZER_EXPAND_EARLY);
if (Tokenizer_GetArgsCount() < 2) {
addLogAdv(LOG_INFO, LOG_FEATURE_MQTT, "Publish command requires two arguments (topic and value)");
@@ -1567,7 +1567,7 @@ commandResult_t MQTT_PublishFile(const void* context, const char* cmd, const cha
if (Tokenizer_GetArgIntegerDefault(2, 0) != 0) {
flags = OBK_PUBLISH_FLAG_RAW_TOPIC_NAME;
}
data = LFS_ReadFile(fname);
data = LFS_ReadFileExpanding(fname);
if (data) {
ret = MQTT_PublishMain_StringString(topic, (const char*)data, flags);
free(data);

View File

@@ -544,6 +544,32 @@ void Test_MQTT_Misc() {
SELFTEST_ASSERT_HAD_MQTT_PUBLISH_STR("homeassistant/cover/miscDevice/config", "{\"name\":\"miscDevice\",\"uniq_id\":\"miscDevice\",\"~\":\"miscDevice\",\"dev_cla\":\"shutter\",\"cmd_t\":\"~/backlog\",\"stat_t\":\"~/shutterState/get\",\"pl_open\":\"OPEN\",\"pl_cls\":\"CLOSE\",\"pl_stop\":\"STOP\",\"stat_o\":\"open\",\"stat_c\":\"closed\"}", false);
SIM_ClearMQTTHistory();
CMD_ExecuteCommand("lfs_format", 0);
// send file content as POST to REST interface
Test_FakeHTTPClientPacket_POST("api/lfs/unitTestFile.txt", "filetext");
// get this file
CMD_ExecuteCommand("publishFile homeassistant/cover/$mqtt_client/config unitTestFile.txt 1", 0);
SELFTEST_ASSERT_HAD_MQTT_PUBLISH_STR("homeassistant/cover/miscDevice/config", "filetext", false);
SIM_ClearMQTTHistory();
CMD_ExecuteCommand("lfs_format", 0);
// send file content as POST to REST interface
Test_FakeHTTPClientPacket_POST("api/lfs/unitTestFile.txt", "My name is $mqtt_client");
// get this file
CMD_ExecuteCommand("publishFile homeassistant/cover/$mqtt_client/config unitTestFile.txt 1", 0);
SELFTEST_ASSERT_HAD_MQTT_PUBLISH_STR("homeassistant/cover/miscDevice/config", "My name is miscDevice", false);
SIM_ClearMQTTHistory();
CMD_ExecuteCommand("lfs_format", 0);
CMD_ExecuteCommand("SetChannel 5 123", 0);
// send file content as POST to REST interface
Test_FakeHTTPClientPacket_POST("api/lfs/unitTestFile.txt", "My name is $mqtt_client $CH5");
// get this file
CMD_ExecuteCommand("publishFile homeassistant/cover/$mqtt_client/config unitTestFile.txt 1", 0);
SELFTEST_ASSERT_HAD_MQTT_PUBLISH_STR("homeassistant/cover/miscDevice/config", "My name is miscDevice 123", false);
SIM_ClearMQTTHistory();
CMD_ExecuteCommand("addEventHandler OnChannelChange 5 publish myMagicResult $CH5", 0);
// set channel 5 to 50 and see what we get
SIM_SendFakeMQTTAndRunSimFrame_CMND("setChannel", "5 50");