mirror of https://github.com/arendst/Tasmota.git
commit
845b89d260
|
@ -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);
|
WSContentSend_P(HTTP_FORM_SCRIPT1,1,1,bitRead(Settings.rule_enabled,0) ? " checked" : "",glob_script_mem.script_size);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// script is to larg for WSContentSend_P
|
// script is to large for WSContentSend_P
|
||||||
if (glob_script_mem.script_ram[0]) {
|
if (glob_script_mem.script_ram[0]) {
|
||||||
|
WSContentFlush();
|
||||||
_WSContentSend(glob_script_mem.script_ram);
|
_WSContentSend(glob_script_mem.script_ram);
|
||||||
}
|
}
|
||||||
WSContentSend_P(HTTP_FORM_SCRIPT1b);
|
WSContentSend_P(HTTP_FORM_SCRIPT1b);
|
||||||
|
@ -5902,9 +5903,13 @@ void execute_script(char *script) {
|
||||||
#define D_CMND_SUBSCRIBE "Subscribe"
|
#define D_CMND_SUBSCRIBE "Subscribe"
|
||||||
#define D_CMND_UNSUBSCRIBE "Unsubscribe"
|
#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) {
|
bool ScriptCommand(void) {
|
||||||
char command[CMDSZ];
|
char command[CMDSZ];
|
||||||
bool serviced = true;
|
bool serviced = true;
|
||||||
|
@ -5971,8 +5976,14 @@ bool ScriptCommand(void) {
|
||||||
} else if (CMND_UNSUBSCRIBE == command_code) { //MQTT Un-subscribe command. UnSubscribe <Event>
|
} else if (CMND_UNSUBSCRIBE == command_code) { //MQTT Un-subscribe command. UnSubscribe <Event>
|
||||||
String result = ScriptUnsubscribe(XdrvMailbox.data, XdrvMailbox.data_len);
|
String result = ScriptUnsubscribe(XdrvMailbox.data, XdrvMailbox.data_len);
|
||||||
Response_P(S_JSON_COMMAND_SVALUE, command, result.c_str());
|
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
|
#endif //SUPPORT_MQTT_EVENT
|
||||||
}
|
}
|
||||||
return serviced;
|
return serviced;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5995,16 +6006,15 @@ void dateTime(uint16_t* date, uint16_t* time) {
|
||||||
|
|
||||||
#endif //USE_SCRIPT_FATFS
|
#endif //USE_SCRIPT_FATFS
|
||||||
|
|
||||||
|
//#define DEBUG_MQTT_EVENT
|
||||||
|
|
||||||
#ifdef SUPPORT_MQTT_EVENT
|
#ifdef SUPPORT_MQTT_EVENT
|
||||||
|
|
||||||
#ifndef MQTT_EVENT_MSIZE
|
#ifndef MQTT_EVENT_MSIZE
|
||||||
#define MQTT_EVENT_MSIZE 256
|
#define MQTT_EVENT_MSIZE 256
|
||||||
#endif
|
#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 sTopic = XdrvMailbox.topic;
|
||||||
String sData = XdrvMailbox.data;
|
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;
|
MQTT_Subscription event_item;
|
||||||
//Looking for matched topic
|
//Looking for matched topic
|
||||||
for (uint32_t index = 0; index < subscriptions.size(); index++) {
|
for (uint32_t index = 0; index < subscriptions.size(); index++) {
|
||||||
event_item = subscriptions.get(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)) {
|
if (sTopic.startsWith(event_item.Topic)) {
|
||||||
//This topic is subscribed by us, so serve it
|
//This topic is subscribed by us, so serve it
|
||||||
serviced = true;
|
serviced = true;
|
||||||
|
@ -6041,6 +6058,9 @@ bool ScriptMqttData(void)
|
||||||
if (event_item.Key.length() == 0) { //If did not specify Key
|
if (event_item.Key.length() == 0) { //If did not specify Key
|
||||||
value = sData;
|
value = sData;
|
||||||
} else { //If specified Key, need to parse Key/Value from JSON data
|
} else { //If specified Key, need to parse Key/Value from JSON data
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef SUPPORT_MQTT_EVENT_MORE
|
||||||
JsonParser parser((char*)sData.c_str());
|
JsonParser parser((char*)sData.c_str());
|
||||||
JsonParserObject jsonData = parser.getRootObject();
|
JsonParserObject jsonData = parser.getRootObject();
|
||||||
String key1 = event_item.Key;
|
String key1 = event_item.Key;
|
||||||
|
@ -6054,24 +6074,90 @@ bool ScriptMqttData(void)
|
||||||
JsonParserToken val = jsonData[key1.c_str()].getObject()[key2.c_str()];
|
JsonParserToken val = jsonData[key1.c_str()].getObject()[key2.c_str()];
|
||||||
if (!val) break; //Failed to get the key/value, ignore this message.
|
if (!val) break; //Failed to get the key/value, ignore this message.
|
||||||
value = val.getStr();
|
value = val.getStr();
|
||||||
|
json_valid = 1;
|
||||||
} else {
|
} else {
|
||||||
JsonParserToken val = jsonData[key1.c_str()];
|
JsonParserToken val = jsonData[key1.c_str()];
|
||||||
if (!val) break;
|
if (!val) break;
|
||||||
value = val.getStr();
|
value = val.getStr();
|
||||||
lkey = key1;
|
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
|
||||||
}
|
}
|
||||||
value.trim();
|
|
||||||
char sbuffer[128];
|
|
||||||
|
|
||||||
if (!strncmp(lkey.c_str(), "Epoch", 5)) {
|
if (json_valid) {
|
||||||
uint32_t ep = atoi(value.c_str()) - (uint32_t)EPOCH_OFFSET;
|
value.trim();
|
||||||
snprintf_P(sbuffer, sizeof(sbuffer), PSTR(">%s=%d\n"), event_item.Event.c_str(), ep);
|
char sbuffer[128];
|
||||||
} else {
|
|
||||||
snprintf_P(sbuffer, sizeof(sbuffer), PSTR(">%s=\"%s\"\n"), event_item.Event.c_str(), value.c_str());
|
if (!strncmp(lkey.c_str(), "Epoch", 5)) {
|
||||||
|
uint32_t ep = atoi(value.c_str()) - (uint32_t)EPOCH_OFFSET;
|
||||||
|
snprintf_P(sbuffer, sizeof(sbuffer), PSTR(">%s=%d\n"), event_item.Event.c_str(), ep);
|
||||||
|
} 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);
|
||||||
}
|
}
|
||||||
//toLog(sbuffer);
|
|
||||||
execute_script(sbuffer);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return serviced;
|
return serviced;
|
||||||
|
@ -6136,7 +6222,7 @@ String ScriptSubscribe(const char *data, int data_len)
|
||||||
topic.concat("/#");
|
topic.concat("/#");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//AddLog_P(LOG_LEVEL_DEBUG, PSTR("Script: New topic: %s."), topic.c_str());
|
// AddLog_P(LOG_LEVEL_DEBUG, PSTR("Script: New topic: %s."), topic.c_str());
|
||||||
//MQTT Subscribe
|
//MQTT Subscribe
|
||||||
subscription_item.Event = event_name;
|
subscription_item.Event = event_name;
|
||||||
subscription_item.Topic = topic.substring(0, topic.length() - 2); //Remove "/#" so easy to match
|
subscription_item.Topic = topic.substring(0, topic.length() - 2); //Remove "/#" so easy to match
|
||||||
|
|
Loading…
Reference in New Issue