fix scripter webeditor distortion

enable larger json hirarchie at mqtt event
This commit is contained in:
gemu2015 2020-12-21 15:55:01 +01:00
parent 6ea7359821
commit 24a80e36fa
1 changed files with 107 additions and 21 deletions

View File

@ -5196,8 +5196,9 @@ void HandleScriptConfiguration(void) {
WSContentSend_P(HTTP_FORM_SCRIPT1,1,1,bitRead(Settings.rule_enabled,0) ? " checked" : "",glob_script_mem.script_size);
#endif
// script is to larg for WSContentSend_P
// script is to large for WSContentSend_P
if (glob_script_mem.script_ram[0]) {
WSContentFlush();
_WSContentSend(glob_script_mem.script_ram);
}
WSContentSend_P(HTTP_FORM_SCRIPT1b);
@ -5902,9 +5903,13 @@ void execute_script(char *script) {
#define D_CMND_SUBSCRIBE "Subscribe"
#define D_CMND_UNSUBSCRIBE "Unsubscribe"
enum ScriptCommands { CMND_SCRIPT,CMND_SUBSCRIBE, CMND_UNSUBSCRIBE };
const char kScriptCommands[] PROGMEM = D_CMND_SCRIPT "|" D_CMND_SUBSCRIBE "|" D_CMND_UNSUBSCRIBE;
enum ScriptCommands { CMND_SCRIPT,CMND_SUBSCRIBE, CMND_UNSUBSCRIBE, CMND_SUBTEST};
const char kScriptCommands[] PROGMEM = D_CMND_SCRIPT "|" D_CMND_SUBSCRIBE "|" D_CMND_UNSUBSCRIBE
#ifdef DEBUG_MQTT_EVENT
"|" "SUBTEST"
#endif
;
bool ScriptCommand(void) {
char command[CMDSZ];
bool serviced = true;
@ -5971,6 +5976,12 @@ bool ScriptCommand(void) {
} else if (CMND_UNSUBSCRIBE == command_code) { //MQTT Un-subscribe command. UnSubscribe <Event>
String result = ScriptUnsubscribe(XdrvMailbox.data, XdrvMailbox.data_len);
Response_P(S_JSON_COMMAND_SVALUE, command, result.c_str());
#ifdef DEBUG_MQTT_EVENT
} else if (CMND_SUBTEST == command_code) {
XdrvMailbox.topic = (char*)"tele";
ScriptMqttData();
serviced = true;
#endif
#endif //SUPPORT_MQTT_EVENT
}
return serviced;
@ -5995,16 +6006,15 @@ void dateTime(uint16_t* date, uint16_t* time) {
#endif //USE_SCRIPT_FATFS
//#define DEBUG_MQTT_EVENT
#ifdef SUPPORT_MQTT_EVENT
#ifndef MQTT_EVENT_MSIZE
#define MQTT_EVENT_MSIZE 256
#endif
#ifndef MQTT_EVENT_JSIZE
#define MQTT_EVENT_JSIZE 400
#endif //SUPPORT_MQTT_EVENT
/********************************************************************************************/
/*
@ -6026,13 +6036,20 @@ bool ScriptMqttData(void)
}
String sTopic = XdrvMailbox.topic;
String sData = XdrvMailbox.data;
//AddLog_P(LOG_LEVEL_DEBUG, PSTR("Script: MQTT Topic %s, Event %s"), XdrvMailbox.topic, XdrvMailbox.data);
#ifdef DEBUG_MQTT_EVENT
AddLog_P(LOG_LEVEL_INFO, PSTR("Script: MQTT Topic %s, Event %s"), XdrvMailbox.topic, XdrvMailbox.data);
#endif
MQTT_Subscription event_item;
//Looking for matched topic
for (uint32_t index = 0; index < subscriptions.size(); index++) {
event_item = subscriptions.get(index);
uint8_t json_valid = 0;
//AddLog_P(LOG_LEVEL_DEBUG, PSTR("Script: Match MQTT message Topic %s with subscription topic %s"), sTopic.c_str(), event_item.Topic.c_str());
#ifdef DEBUG_MQTT_EVENT
AddLog_P(LOG_LEVEL_INFO, PSTR("Script: Match MQTT message Topic %s with subscription topic %s and key %s"), sTopic.c_str(), event_item.Topic.c_str(),event_item.Key.c_str());
#endif
if (sTopic.startsWith(event_item.Topic)) {
//This topic is subscribed by us, so serve it
serviced = true;
@ -6041,6 +6058,9 @@ bool ScriptMqttData(void)
if (event_item.Key.length() == 0) { //If did not specify Key
value = sData;
} else { //If specified Key, need to parse Key/Value from JSON data
#ifndef SUPPORT_MQTT_EVENT_MORE
JsonParser parser((char*)sData.c_str());
JsonParserObject jsonData = parser.getRootObject();
String key1 = event_item.Key;
@ -6054,13 +6074,75 @@ bool ScriptMqttData(void)
JsonParserToken val = jsonData[key1.c_str()].getObject()[key2.c_str()];
if (!val) break; //Failed to get the key/value, ignore this message.
value = val.getStr();
json_valid = 1;
} else {
JsonParserToken val = jsonData[key1.c_str()];
if (!val) break;
value = val.getStr();
lkey = key1;
json_valid = 1;
}
#else
JsonParser parser((char*)sData.c_str());
const char *cp = event_item.Key.c_str();
#ifdef DEBUG_MQTT_EVENT
AddLog_P(LOG_LEVEL_INFO, PSTR("Script: parsing json key: %s from json: %s"), cp, sData.c_str());
#endif
JsonParserObject obj = parser.getRootObject();
JsonParserObject lastobj = obj;
char selem[32];
uint8_t aindex = 0;
while (1) {
// read next element
for (uint32_t sp=0; sp<sizeof(selem)-1; sp++) {
if (!*cp || *cp=='.') {
selem[sp] = 0;
cp++;
index++;
break;
}
selem[sp] = *cp++;
}
#ifdef DEBUG_MQTT_EVENT
AddLog_P(LOG_LEVEL_INFO, PSTR("Script: cmp current key: %s"), selem);
#endif
// check for array
char *sp = strchr(selem,'[');
if (sp) {
*sp = 0;
aindex = atoi(sp+1);
}
// now check element
obj = obj[selem];
if (!obj.isValid()) {
#ifdef DEBUG_MQTT_EVENT
AddLog_P(LOG_LEVEL_INFO, PSTR("Script: obj invalid: %s"), selem);
#endif
JsonParserToken tok = lastobj[selem];
if (tok.isValid()) {
if (tok.isArray()) {
JsonParserArray array = JsonParserArray(tok);
value = array[aindex].getStr();
} else {
value = tok.getStr();
}
json_valid = 1;
}
#ifdef DEBUG_MQTT_EVENT
AddLog_P(LOG_LEVEL_INFO, PSTR("Script: token invalid: %s"), selem);
#endif
break;
}
if (obj.isObject()) {
lastobj = obj;
continue;
}
if (!*cp) break;
}
#endif
}
if (json_valid) {
value.trim();
char sbuffer[128];
@ -6070,10 +6152,14 @@ bool ScriptMqttData(void)
} else {
snprintf_P(sbuffer, sizeof(sbuffer), PSTR(">%s=\"%s\"\n"), event_item.Event.c_str(), value.c_str());
}
#ifdef DEBUG_MQTT_EVENT
AddLog_P(LOG_LEVEL_INFO, PSTR("Script: setting script var %s"), sbuffer);
#endif
//toLog(sbuffer);
execute_script(sbuffer);
}
}
}
return serviced;
}
@ -6115,7 +6201,7 @@ String ScriptSubscribe(const char *data, int data_len)
}
}
}
//AddLog_P(LOG_LEVEL_DEBUG, PSTR("Script: Subscribe command with parameters: %s, %s, %s."), event_name.c_str(), topic.c_str(), key.c_str());
AddLog_P(LOG_LEVEL_INFO, PSTR("Script: Subscribe command with parameters: %s, %s, %s."), event_name.c_str(), topic.c_str(), key.c_str());
//event_name.toUpperCase();
if (event_name.length() > 0 && topic.length() > 0) {
//Search all subscriptions
@ -6136,7 +6222,7 @@ String ScriptSubscribe(const char *data, int data_len)
topic.concat("/#");
}
}
//AddLog_P(LOG_LEVEL_DEBUG, PSTR("Script: New topic: %s."), topic.c_str());
AddLog_P(LOG_LEVEL_INFO, PSTR("Script: New topic: %s."), topic.c_str());
//MQTT Subscribe
subscription_item.Event = event_name;
subscription_item.Topic = topic.substring(0, topic.length() - 2); //Remove "/#" so easy to match