error handling in mqtt to prevent bogus commands crashing the system - #298

This commit is contained in:
Paul
2020-02-02 12:22:14 +01:00
parent d70b101973
commit a57c26fdbe

View File

@@ -1456,6 +1456,9 @@ void MQTTCallback(unsigned int type, const char * topic, const char * message) {
strlcat(topic_s, itoa(hc, buffer, 10), sizeof(topic_s));
myESP.mqttSubscribe(topic_s);
}
// also subscribe without the HC appended to the end of the topic
myESP.mqttSubscribe(TOPIC_THERMOSTAT_CMD_TEMP_HA);
myESP.mqttSubscribe(TOPIC_THERMOSTAT_CMD_MODE_HA);
// generic incoming MQTT command for Thermostat
// this is used for example for setting daytemp, nighttemp, holidaytemp
@@ -1552,10 +1555,16 @@ void MQTTCallback(unsigned int type, const char * topic, const char * message) {
return;
}
const char * command = doc["cmd"];
if (command == nullptr) {
return;
}
// boiler ww comfort setting
if (strcmp(command, TOPIC_BOILER_CMD_COMFORT) == 0) {
const char * data = doc["data"];
if (data == nullptr) {
return;
}
if (strcmp((char *)data, "hot") == 0) {
ems_setWarmWaterModeComfort(1);
} else if (strcmp((char *)data, "comfort") == 0) {
@@ -1569,7 +1578,9 @@ void MQTTCallback(unsigned int type, const char * topic, const char * message) {
// boiler flowtemp setting
if (strcmp(command, TOPIC_BOILER_CMD_FLOWTEMP) == 0) {
uint8_t t = doc["data"];
if (t) {
ems_setFlowTemp(t);
}
return;
}
@@ -1611,24 +1622,31 @@ void MQTTCallback(unsigned int type, const char * topic, const char * message) {
// boiler wwtemp changes
if (strcmp(topic, TOPIC_BOILER_CMD_WWTEMP) == 0) {
uint8_t t = atoi((char *)message);
if (t) {
ems_setWarmWaterTemp(t);
publishEMSValues(true);
}
return;
}
uint8_t hc;
// thermostat temp changes
hc = _hasHCspecified(TOPIC_THERMOSTAT_CMD_TEMP_HA, topic);
if (hc && EMS_Thermostat.hc[hc - 1].active) {
if (hc) {
if (EMS_Thermostat.hc[hc - 1].active) {
float f = strtof((char *)message, 0);
if (f) {
ems_setThermostatTemp(f, hc);
publishEMSValues(true); // publish back immediately
publishEMSValues(true); // publish back immediately }
return;
}
}
}
// thermostat mode changes
hc = _hasHCspecified(TOPIC_THERMOSTAT_CMD_MODE_HA, topic);
if (hc && EMS_Thermostat.hc[hc - 1].active) {
if (hc) {
if (EMS_Thermostat.hc[hc - 1].active) {
if (strncmp(message, "auto", 4) == 0) {
ems_setThermostatMode(2, hc);
} else if ((strncmp(message, "day", 4) == 0) || (strncmp(message, "manual", 6) == 0) || (strncmp(message, "heat", 4) == 0)) {
@@ -1638,6 +1656,7 @@ void MQTTCallback(unsigned int type, const char * topic, const char * message) {
}
return;
}
}
// check for generic thermostat commands
if (strcmp(topic, TOPIC_THERMOSTAT_CMD) == 0) {
@@ -1649,20 +1668,31 @@ void MQTTCallback(unsigned int type, const char * topic, const char * message) {
return;
}
const char * command = doc["cmd"];
if (command == nullptr) {
return;
}
// thermostat temp changes
hc = _hasHCspecified(TOPIC_THERMOSTAT_CMD_TEMP, command);
if (hc && EMS_Thermostat.hc[hc - 1].active) {
if (hc) {
if (EMS_Thermostat.hc[hc - 1].active) {
float f = doc["data"];
if (f) {
ems_setThermostatTemp(f, hc);
publishEMSValues(true); // publish back immediately
publishEMSValues(true); // publish back immediately }
return;
}
}
}
// thermostat mode changes
hc = _hasHCspecified(TOPIC_THERMOSTAT_CMD_MODE, command);
if (hc && EMS_Thermostat.hc[hc - 1].active) {
if (hc) {
if (EMS_Thermostat.hc[hc - 1].active) {
const char * data_cmd = doc["data"];
if (data_cmd == nullptr) {
return;
}
if (strncmp(data_cmd, "auto", 4) == 0) {
ems_setThermostatMode(2, hc);
} else if ((strncmp(data_cmd, "day", 4) == 0) || (strncmp(data_cmd, "manual", 6) == 0) || (strncmp(data_cmd, "heat", 4) == 0)) {
@@ -1672,32 +1702,45 @@ void MQTTCallback(unsigned int type, const char * topic, const char * message) {
}
return;
}
}
// set night temp value
hc = _hasHCspecified(TOPIC_THERMOSTAT_CMD_NIGHTTEMP, command);
if (hc && EMS_Thermostat.hc[hc - 1].active) {
if (hc) {
if (EMS_Thermostat.hc[hc - 1].active) {
float f = doc["data"];
ems_setThermostatTemp(f, hc, 1); // night
if (f) {
ems_setThermostatTemp(f, hc, 1); // night }
return;
}
}
}
// set daytemp value
hc = _hasHCspecified(TOPIC_THERMOSTAT_CMD_DAYTEMP, command);
if (hc && EMS_Thermostat.hc[hc - 1].active) {
if (hc) {
if (EMS_Thermostat.hc[hc - 1].active) {
float f = doc["data"];
if (f) {
ems_setThermostatTemp(f, hc, 2); // day
}
return;
}
}
// set holiday value
hc = _hasHCspecified(TOPIC_THERMOSTAT_CMD_HOLIDAYTEMP, command);
if (hc && EMS_Thermostat.hc[hc - 1].active) {
if (hc) {
if (EMS_Thermostat.hc[hc - 1].active) {
float f = doc["data"];
if (f) {
ems_setThermostatTemp(f, hc, 3); // holiday
}
return;
}
}
}
}
// Init callback, which is used to set functions and call methods after a wifi connection has been established
void WIFICallback() {