From 1341db1392debc43d7b936e352bdec935ebb9985 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Thu, 12 Sep 2019 14:19:44 +0200 Subject: [PATCH] Change JSON output format for some commands Change JSON output format for commands Adc, Adcs, Modules, Gpio and Gpios from list to dictionary (#6407) --- sonoff/_changelog.ino | 1 + sonoff/i18n.h | 6 +++--- sonoff/support.ino | 5 +++++ sonoff/support_command.ino | 16 ++++++++-------- sonoff/xdrv_05_irremote.ino | 2 +- sonoff/xdrv_05_irremote_full.ino | 2 +- sonoff/xdrv_09_timers.ino | 2 +- sonoff/xsns_02_analog.ino | 8 ++++---- sonoff/xsns_21_sgp30.ino | 2 +- sonoff/xsns_53_sml.ino | 4 ++-- 10 files changed, 27 insertions(+), 21 deletions(-) diff --git a/sonoff/_changelog.ino b/sonoff/_changelog.ino index e9050aafb..a02cc1636 100644 --- a/sonoff/_changelog.ino +++ b/sonoff/_changelog.ino @@ -4,6 +4,7 @@ * Commands Tariff1 22,23 = Tariff1 (Off-Peak) ST,DST Tariff2 (Standard) 6,7 = Tariff2 ST,DST Tariff9 0/1 = Weekend toggle (1 = Off-Peak during weekend) * Change rename "Data" to "Hash" and limit to 32 bits when receiving UNKNOWN IR protocol (see DECODE_HASH from IRremoteESP8266) * Add command Gpios 255/All to show all available GPIO components (#6407) + * Change JSON output format for commands Adc, Adcs, Modules, Gpio and Gpios from list to dictionary (#6407) * * 6.6.0.11 20190907 * Change Settings crc calculation allowing short term backward compatibility diff --git a/sonoff/i18n.h b/sonoff/i18n.h index a6695fb73..7659b9335 100644 --- a/sonoff/i18n.h +++ b/sonoff/i18n.h @@ -537,8 +537,8 @@ const char S_JSON_COMMAND_SVALUE_SPACE_UNIT[] PROGMEM = "{\"%s\":\"%s %s\" const char S_JSON_COMMAND_NVALUE_UNIT[] PROGMEM = "{\"%s\":\"%d%s\"}"; const char S_JSON_COMMAND_NVALUE_UNIT_NVALUE_UNIT[] PROGMEM = "{\"%s\":\"%d%s (%d%s)\"}"; -const char S_JSON_COMMAND_NVALUE_SVALUE[] PROGMEM = "{\"%s\":\"%d (%s)\"}"; -const char S_JSON_COMMAND_NVALUE_ACTIVE_NVALUE[] PROGMEM = "{\"%s\":\"%d (" D_JSON_ACTIVE " %d)\"}"; +const char S_JSON_COMMAND_NVALUE_SVALUE[] PROGMEM = "{\"%s\":{\"%d\":\"%s\"}}"; +const char S_JSON_COMMAND_NVALUE_ACTIVE_NVALUE[] PROGMEM = "{\"%s\":{\"%d\":{\"" D_JSON_ACTIVE "\":\"%d\"}}}"; const char S_JSON_COMMAND_NVALUE[] PROGMEM = "{\"%s\":%d}"; const char S_JSON_COMMAND_LVALUE[] PROGMEM = "{\"%s\":%lu}"; @@ -551,7 +551,7 @@ const char S_JSON_COMMAND_INDEX_LVALUE[] PROGMEM = "{\"%s%d\":%lu}"; const char S_JSON_COMMAND_INDEX_SVALUE[] PROGMEM = "{\"%s%d\":\"%s\"}"; const char S_JSON_COMMAND_INDEX_ASTERISK[] PROGMEM = "{\"%s%d\":\"" D_ASTERISK_PWD "\"}"; const char S_JSON_COMMAND_INDEX_SVALUE_SVALUE[] PROGMEM = "{\"%s%d\":\"%s%s\"}"; -const char S_JSON_COMMAND_INDEX_NVALUE_ACTIVE_NVALUE[] PROGMEM = "{\"%s%d\":\"%d (" D_JSON_ACTIVE " %d)\"}"; +const char S_JSON_COMMAND_INDEX_NVALUE_ACTIVE_NVALUE[] PROGMEM = "{\"%s%d\":{\"%d\":{\"" D_JSON_ACTIVE "\":\"%d\"}}}"; const char S_JSON_SENSOR_INDEX_NVALUE[] PROGMEM = "{\"" D_CMND_SENSOR "%d\":%d}"; const char S_JSON_SENSOR_INDEX_SVALUE[] PROGMEM = "{\"" D_CMND_SENSOR "%d\":\"%s\"}"; diff --git a/sonoff/support.ino b/sonoff/support.ino index cca75b377..1f9911e09 100644 --- a/sonoff/support.ino +++ b/sonoff/support.ino @@ -951,6 +951,11 @@ int ResponseJsonEnd(void) return ResponseAppend_P(PSTR("}")); } +int ResponseJsonEndEnd(void) +{ + return ResponseAppend_P(PSTR("}}")); +} + /*********************************************************************************************\ * GPIO Module and Template management \*********************************************************************************************/ diff --git a/sonoff/support_command.ino b/sonoff/support_command.ino index bd7107648..fff2831cb 100644 --- a/sonoff/support_command.ino +++ b/sonoff/support_command.ino @@ -366,7 +366,7 @@ void CmndStatus(void) XsnsDriverState(); ResponseAppend_P(PSTR(",\"Sensors\":")); XsnsSensorState(); - ResponseAppend_P(PSTR("}}")); + ResponseJsonEndEnd(); MqttPublishPrefixTopic_P(option, PSTR(D_CMND_STATUS "4")); } @@ -795,14 +795,14 @@ void CmndModules(void) for (uint32_t i = 0; i <= sizeof(kModuleNiceList); i++) { if (i > 0) { midx = pgm_read_byte(kModuleNiceList + i -1); } if (!jsflg) { - Response_P(PSTR("{\"" D_CMND_MODULES "%d\":["), lines); + Response_P(PSTR("{\"" D_CMND_MODULES "%d\":{"), lines); } else { ResponseAppend_P(PSTR(",")); } jsflg = true; uint32_t j = i ? midx +1 : 0; - if ((ResponseAppend_P(PSTR("\"%d (%s)\""), j, AnyModuleName(midx).c_str()) > (LOGSZ - TOPSZ)) || (i == sizeof(kModuleNiceList))) { - ResponseAppend_P(PSTR("]}")); + if ((ResponseAppend_P(PSTR("\"%d\":\"%s\""), j, AnyModuleName(midx).c_str()) > (LOGSZ - TOPSZ)) || (i == sizeof(kModuleNiceList))) { + ResponseJsonEndEnd(); MqttPublishPrefixTopic_P(RESULT_OR_STAT, UpperCase(XdrvMailbox.command, XdrvMailbox.command)); jsflg = false; lines++; @@ -839,7 +839,7 @@ void CmndGpio(void) if (jsflg) { ResponseAppend_P(PSTR(",")); } jsflg = true; char stemp1[TOPSZ]; - ResponseAppend_P(PSTR("\"" D_CMND_GPIO "%d\":\"%d (%s)\""), i, Settings.my_gp.io[i], GetTextIndexed(stemp1, sizeof(stemp1), Settings.my_gp.io[i], kSensorNames)); + ResponseAppend_P(PSTR("\"" D_CMND_GPIO "%d\":{\"%d\":\"%s\"}"), i, Settings.my_gp.io[i], GetTextIndexed(stemp1, sizeof(stemp1), Settings.my_gp.io[i], kSensorNames)); } } if (jsflg) { @@ -861,14 +861,14 @@ void CmndGpios(void) midx = pgm_read_byte(kGpioNiceList + i); if ((XdrvMailbox.payload != 255) && GetUsedInModule(midx, cmodule.io)) { continue; } if (!jsflg) { - Response_P(PSTR("{\"" D_CMND_GPIOS "%d\":["), lines); + Response_P(PSTR("{\"" D_CMND_GPIOS "%d\":{"), lines); } else { ResponseAppend_P(PSTR(",")); } jsflg = true; char stemp1[TOPSZ]; - if ((ResponseAppend_P(PSTR("\"%d (%s)\""), midx, GetTextIndexed(stemp1, sizeof(stemp1), midx, kSensorNames)) > (LOGSZ - TOPSZ)) || (i == sizeof(kGpioNiceList) -1)) { - ResponseAppend_P(PSTR("]}")); + if ((ResponseAppend_P(PSTR("\"%d\":\"%s\""), midx, GetTextIndexed(stemp1, sizeof(stemp1), midx, kSensorNames)) > (LOGSZ - TOPSZ)) || (i == sizeof(kGpioNiceList) -1)) { + ResponseJsonEndEnd(); MqttPublishPrefixTopic_P(RESULT_OR_STAT, UpperCase(XdrvMailbox.command, XdrvMailbox.command)); jsflg = false; lines++; diff --git a/sonoff/xdrv_05_irremote.ino b/sonoff/xdrv_05_irremote.ino index db5332081..0e8a00982 100644 --- a/sonoff/xdrv_05_irremote.ino +++ b/sonoff/xdrv_05_irremote.ino @@ -189,7 +189,7 @@ void IrReceiveCheck(void) ResponseAppend_P(PSTR("],\"" D_JSON_IR_RAWDATA "Info\":[%d,%d,%d]"), extended_length, i -1, results.overflow); } - ResponseAppend_P(PSTR("}}")); + ResponseJsonEndEnd(); MqttPublishPrefixTopic_P(RESULT_OR_TELE, PSTR(D_JSON_IRRECEIVED)); if (iridx) { diff --git a/sonoff/xdrv_05_irremote_full.ino b/sonoff/xdrv_05_irremote_full.ino index d35c5f4ba..85c81684b 100644 --- a/sonoff/xdrv_05_irremote_full.ino +++ b/sonoff/xdrv_05_irremote_full.ino @@ -254,7 +254,7 @@ void IrReceiveCheck(void) ResponseAppend_P(PSTR("],\"" D_JSON_IR_RAWDATA "Info\":[%d,%d,%d]"), extended_length, i -1, results.overflow); } - ResponseAppend_P(PSTR("}}")); + ResponseJsonEndEnd(); MqttPublishPrefixTopic_P(RESULT_OR_TELE, PSTR(D_JSON_IRRECEIVED)); if (iridx) { diff --git a/sonoff/xdrv_09_timers.ino b/sonoff/xdrv_09_timers.ino index 4b767e77c..130ef04c4 100644 --- a/sonoff/xdrv_09_timers.ino +++ b/sonoff/xdrv_09_timers.ino @@ -482,7 +482,7 @@ void CmndTimers(void) jsflg++; PrepShowTimer(i +1); if (jsflg > 3) { - ResponseAppend_P(PSTR("}}")); + ResponseJsonEndEnd(); MqttPublishPrefixTopic_P(RESULT_OR_STAT, PSTR(D_CMND_TIMERS)); jsflg = 0; } diff --git a/sonoff/xsns_02_analog.ino b/sonoff/xsns_02_analog.ino index b3c29e9d8..aebb12e4f 100644 --- a/sonoff/xsns_02_analog.ino +++ b/sonoff/xsns_02_analog.ino @@ -196,12 +196,12 @@ void CmndAdc(void) restart_flag = 2; } char stemp1[TOPSZ]; - Response_P(PSTR("{\"" D_CMND_ADC "0\":\"%d (%s)\"}"), Settings.my_adc0, GetTextIndexed(stemp1, sizeof(stemp1), Settings.my_adc0, kAdc0Names)); + Response_P(PSTR("{\"" D_CMND_ADC "0\":{\"%d\":\"%s\"}}"), Settings.my_adc0, GetTextIndexed(stemp1, sizeof(stemp1), Settings.my_adc0, kAdc0Names)); } void CmndAdcs(void) { - Response_P(PSTR("{\"" D_CMND_ADCS "\":[")); + Response_P(PSTR("{\"" D_CMND_ADCS "\":{")); bool jsflg = false; char stemp1[TOPSZ]; for (uint32_t i = 0; i < ADC0_END; i++) { @@ -209,9 +209,9 @@ void CmndAdcs(void) ResponseAppend_P(PSTR(",")); } jsflg = true; - ResponseAppend_P(PSTR("\"%d (%s)\""), i, GetTextIndexed(stemp1, sizeof(stemp1), i, kAdc0Names)); + ResponseAppend_P(PSTR("\"%d\":\"%s\""), i, GetTextIndexed(stemp1, sizeof(stemp1), i, kAdc0Names)); } - ResponseAppend_P(PSTR("]}")); + ResponseJsonEndEnd(); } void CmndAdcParam(void) diff --git a/sonoff/xsns_21_sgp30.ino b/sonoff/xsns_21_sgp30.ino index e9c097dc4..356c690b8 100644 --- a/sonoff/xsns_21_sgp30.ino +++ b/sonoff/xsns_21_sgp30.ino @@ -134,7 +134,7 @@ void Sgp30Show(bool json) if (global_update) { ResponseAppend_P(PSTR(",\"" D_JSON_AHUM "\":%s"),abs_hum); } - ResponseAppend_P(PSTR("}")); + ResponseJsonEnd(); #ifdef USE_DOMOTICZ if (0 == tele_period) DomoticzSensor(DZ_AIRQUALITY, sgp.eCO2); #endif // USE_DOMOTICZ diff --git a/sonoff/xsns_53_sml.ino b/sonoff/xsns_53_sml.ino index 9dabb29cd..bb8ba1e4c 100644 --- a/sonoff/xsns_53_sml.ino +++ b/sonoff/xsns_53_sml.ino @@ -1616,7 +1616,7 @@ void SML_Show(boolean json) { if (lastmind!=mindex) { // meter changed, close mqtt //snprintf_P(b_mqtt_data, sizeof(b_mqtt_data), "%s}", b_mqtt_data); - ResponseAppend_P(PSTR("}")); + ResponseJsonEnd(); // and open new //snprintf_P(b_mqtt_data, sizeof(b_mqtt_data), "%s,\"%s\":{\"%s\":%s", b_mqtt_data,meter_desc_p[mindex].prefix,jname,tpowstr); ResponseAppend_P(PSTR(",\"%s\":{\"%s\":%s"),meter_desc_p[mindex].prefix,jname,tpowstr); @@ -1643,7 +1643,7 @@ void SML_Show(boolean json) { if (json) { //snprintf_P(b_mqtt_data, sizeof(b_mqtt_data), "%s}", b_mqtt_data); //ResponseAppend_P(PSTR("%s"),b_mqtt_data); - ResponseAppend_P(PSTR("}")); + ResponseJsonEnd(); } else { //WSContentSend_PD(PSTR("%s"),b_mqtt_data); }