Changes in Wemo and HTTP response to JSON

* Change Wemo SetBinaryState to distinguish from GetBinaryState (#1357)

* Change output of HTTP command to valid JSON only (#1363)
This commit is contained in:
arendst 2017-12-17 16:01:30 +01:00
parent bd661fc263
commit 2e7248190c
4 changed files with 30 additions and 25 deletions

View File

@ -1,6 +1,8 @@
/* 5.10.0b /* 5.10.0b
* Change Sonoff Pow Energy MQTT data JSON message and consolidate Status 8 into Status 10 * Add optional support for PZEM004T energy sensor
* Add optional support for PZEM004T energy sensor to enable with define USE_PZEM004T in user_config.h (#614) * Change Sonoff Pow Enenrgy MQTT data message and consolidate Status 8 into Status 10
* Change Wemo SetBinaryState to distinguish from GetBinaryState (#1357)
* Change output of HTTP command to valid JSON only (#1363)
* *
* 5.10.0a * 5.10.0a
* Add (experimental) support for sensor SHT3x * Add (experimental) support for sensor SHT3x

View File

@ -177,7 +177,7 @@ power_t rel_inverted = 0; // Relay inverted flag (1 = (0 = On,
uint8_t led_inverted = 0; // LED inverted flag (1 = (0 = On, 1 = Off)) uint8_t led_inverted = 0; // LED inverted flag (1 = (0 = On, 1 = Off))
uint8_t pwm_inverted = 0; // PWM inverted flag (1 = inverted) uint8_t pwm_inverted = 0; // PWM inverted flag (1 = inverted)
uint8_t dht_flg = 0; // DHT configured uint8_t dht_flg = 0; // DHT configured
uint8_t energy_flg = 1; // Energy monitor configured bool energy_flg = true; // Energy monitor configured
uint8_t i2c_flg = 0; // I2C configured uint8_t i2c_flg = 0; // I2C configured
uint8_t spi_flg = 0; // SPI configured uint8_t spi_flg = 0; // SPI configured
uint8_t light_type = 0; // Light types uint8_t light_type = 0; // Light types
@ -189,7 +189,7 @@ boolean (*xsns_func_ptr[XSNS_MAX])(byte); // External Sensor Function Pointers
char my_hostname[33]; // Composed Wifi hostname char my_hostname[33]; // Composed Wifi hostname
char mqtt_client[33]; // Composed MQTT Clientname char mqtt_client[33]; // Composed MQTT Clientname
char serial_in_buffer[INPUT_BUFFER_SIZE + 2]; // Receive buffer char serial_in_buffer[INPUT_BUFFER_SIZE + 2]; // Receive buffer
char mqtt_data[TOPSZ + MESSZ]; // MQTT publish buffer char mqtt_data[MESSZ + TOPSZ]; // MQTT publish buffer (MESSZ) and web page ajax buffer (MESSZ + TOPSZ)
char log_data[TOPSZ + MESSZ]; // Logging char log_data[TOPSZ + MESSZ]; // Logging
String web_log[MAX_LOG_LINES]; // Web log buffer String web_log[MAX_LOG_LINES]; // Web log buffer
String backlog[MAX_BACKLOG]; // Command backlog String backlog[MAX_BACKLOG]; // Command backlog
@ -349,6 +349,7 @@ void MqttSubscribe(char *topic)
void MqttPublishDirect(const char* topic, boolean retained) void MqttPublishDirect(const char* topic, boolean retained)
{ {
if (Settings.flag.mqtt_enabled) { if (Settings.flag.mqtt_enabled) {
mqtt_data[MESSZ -1] = '\0';
if (MqttClient.publish(topic, mqtt_data, retained)) { if (MqttClient.publish(topic, mqtt_data, retained)) {
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_MQTT "%s = %s%s"), topic, mqtt_data, (retained) ? " (" D_RETAINED ")" : ""); snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_MQTT "%s = %s%s"), topic, mqtt_data, (retained) ? " (" D_RETAINED ")" : "");
// MqttClient.loop(); // Do not use here! Will block previous publishes // MqttClient.loop(); // Do not use here! Will block previous publishes
@ -2622,6 +2623,8 @@ void GpioInit()
} }
#endif // USE_IR_RECEIVE #endif // USE_IR_RECEIVE
#endif // USE_IR_REMOTE #endif // USE_IR_REMOTE
// energy_flg = (((pin[GPIO_HLW_SEL] < 99) && (pin[GPIO_HLW_CF1] < 99) && (pin[GPIO_HLW_CF] < 99)) || ((pin[GPIO_PZEM_RX] < 99) && (pin[GPIO_PZEM_TX])));
} }
extern "C" { extern "C" {

View File

@ -1337,7 +1337,7 @@ void HandleHttpCommand()
} }
} }
String message = ""; String message = F("{\"" D_RSLT_WARNING "\":\"");
if (valid) { if (valid) {
byte curridx = web_log_index; byte curridx = web_log_index;
if (strlen(WebServer->arg("cmnd").c_str())) { if (strlen(WebServer->arg("cmnd").c_str())) {
@ -1351,18 +1351,15 @@ void HandleHttpCommand()
if (web_log_index != curridx) { if (web_log_index != curridx) {
byte counter = curridx; byte counter = curridx;
message = F("{");
do { do {
if (web_log[counter].length()) { if (web_log[counter].length()) {
if (message.length()) { // [14:49:36 MQTT: stat/wemos5/RESULT = {"POWER":"OFF"}] > [{"POWER":"OFF"}]
message += F("\n"); if (web_log[counter].indexOf("{") > 0) { // Is it a JSON message (and not only [15:26:08 MQT: stat/wemos5/POWER = O])
if (message.length() > 1) {
message += F(",");
} }
if (Settings.flag.mqtt_enabled) { message += web_log[counter].substring(web_log[counter].indexOf("{")+1,web_log[counter].length()-1);
// [14:49:36 MQTT: stat/wemos5/RESULT = {"POWER":"OFF"}] > [RESULT = {"POWER":"OFF"}]
// message += web_log[counter].substring(17 + strlen(PUB_PREFIX) + strlen(Settings.mqtt_topic));
message += web_log[counter].substring(web_log[counter].lastIndexOf("/",web_log[counter].indexOf("="))+1);
} else {
// [14:49:36 RSLT: RESULT = {"POWER":"OFF"}] > [RESULT = {"POWER":"OFF"}]
message += web_log[counter].substring(web_log[counter].indexOf(": ")+2);
} }
} }
counter++; counter++;
@ -1370,13 +1367,14 @@ void HandleHttpCommand()
counter = 0; counter = 0;
} }
} while (counter != web_log_index); } while (counter != web_log_index);
message += F("}");
} else { } else {
message = F(D_ENABLE_WEBLOG_FOR_RESPONSE "\n"); message += F(D_ENABLE_WEBLOG_FOR_RESPONSE "\"}");
} }
} else { } else {
message = F(D_NEED_USER_AND_PASSWORD "\n"); message += F(D_NEED_USER_AND_PASSWORD "\"}");
} }
WebServer->send(200, FPSTR(HDR_CTYPE_PLAIN), message); WebServer->send(200, FPSTR(HDR_CTYPE_JSON), message);
} }
void HandleConsole() void HandleConsole()

View File

@ -299,14 +299,16 @@ void HandleUpnpEvent()
{ {
AddLog_P(LOG_LEVEL_DEBUG, S_LOG_HTTP, PSTR(D_WEMO_BASIC_EVENT)); AddLog_P(LOG_LEVEL_DEBUG, S_LOG_HTTP, PSTR(D_WEMO_BASIC_EVENT));
String request = WebServer->arg(0); String request = WebServer->arg(0);
if (request.indexOf(F("SetBinaryState")) > 0) {
if (request.indexOf(F("State>1</Binary")) > 0) { if (request.indexOf(F("State>1</Binary")) > 0) {
// ExecuteCommandPower(1, 1); // ExecuteCommandPower(1, 1);
ExecuteCommandPower(devices_present, 1); ExecuteCommandPower(devices_present, 1);
} }
if (request.indexOf(F("State>0</Binary")) > 0) { if (request.indexOf(F("State>0</Binary")) > 0) {
// ExecuteCommandPower(1, 0); // ExecuteCommandPower(1, 0);
ExecuteCommandPower(devices_present, 0); ExecuteCommandPower(devices_present, 0);
} }
}
WebServer->send(200, FPSTR(HDR_CTYPE_PLAIN), ""); WebServer->send(200, FPSTR(HDR_CTYPE_PLAIN), "");
} }