mirror of
https://github.com/Laxilef/OTGateway.git
synced 2025-12-10 18:24:27 +05:00
refactor: optimization
This commit is contained in:
@@ -73,7 +73,7 @@ public:
|
|||||||
this->webServer->sendContent((const char*)this->buffer, this->bufferPos);
|
this->webServer->sendContent((const char*)this->buffer, this->bufferPos);
|
||||||
this->bufferPos = 0;
|
this->bufferPos = 0;
|
||||||
#ifdef ARDUINO_ARCH_ESP8266
|
#ifdef ARDUINO_ARCH_ESP8266
|
||||||
::yield();
|
::delay(0);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -97,13 +97,13 @@ protected:
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (network->isConnected()) {
|
if (network->isConnected()) {
|
||||||
|
vars.sensors.rssi = WiFi.RSSI();
|
||||||
|
|
||||||
if (!this->telnetStarted && telnetStream != nullptr) {
|
if (!this->telnetStarted && telnetStream != nullptr) {
|
||||||
telnetStream->begin(23, false);
|
telnetStream->begin(23, false);
|
||||||
this->telnetStarted = true;
|
this->telnetStarted = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
vars.sensors.rssi = WiFi.RSSI();
|
|
||||||
|
|
||||||
if (!tMqtt->isEnabled() && strlen(settings.mqtt.server) > 0) {
|
if (!tMqtt->isEnabled() && strlen(settings.mqtt.server) > 0) {
|
||||||
tMqtt->enable();
|
tMqtt->enable();
|
||||||
}
|
}
|
||||||
@@ -161,6 +161,9 @@ protected:
|
|||||||
for (Stream* stream : Log.getStreams()) {
|
for (Stream* stream : Log.getStreams()) {
|
||||||
while (stream->available() > 0) {
|
while (stream->available() > 0) {
|
||||||
stream->read();
|
stream->read();
|
||||||
|
#ifdef ARDUINO_ARCH_ESP8266
|
||||||
|
::delay(0);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -179,8 +182,8 @@ protected:
|
|||||||
unsigned int freeHeap = getFreeHeap();
|
unsigned int freeHeap = getFreeHeap();
|
||||||
unsigned int maxFreeBlockHeap = getMaxFreeBlockHeap();
|
unsigned int maxFreeBlockHeap = getMaxFreeBlockHeap();
|
||||||
|
|
||||||
if (!vars.actions.restart && (freeHeap < 2048 || maxFreeBlockHeap < 2048)) {
|
if (!this->restartSignalTime && (freeHeap < 2048 || maxFreeBlockHeap < 2048)) {
|
||||||
vars.actions.restart = true;
|
this->restartSignalTime = millis();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!settings.system.debug) {
|
if (!settings.system.debug) {
|
||||||
@@ -299,7 +302,7 @@ protected:
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (vars.states.externalPump && !this->heatingEnabled) {
|
if (vars.states.externalPump && !this->heatingEnabled) {
|
||||||
if (this->externalPumpStartReason == MainTask::REASON_PUMP_START_HEATING && millis() - this->heatingDisabledTime > ((unsigned int) settings.externalPump.postCirculationTime * 1000)) {
|
if (this->externalPumpStartReason == MainTask::REASON_PUMP_START_HEATING && millis() - this->heatingDisabledTime > (settings.externalPump.postCirculationTime * 1000u)) {
|
||||||
digitalWrite(settings.externalPump.pin, false);
|
digitalWrite(settings.externalPump.pin, false);
|
||||||
|
|
||||||
vars.states.externalPump = false;
|
vars.states.externalPump = false;
|
||||||
@@ -307,7 +310,7 @@ protected:
|
|||||||
|
|
||||||
Log.sinfoln("EXTPUMP", F("Disabled: expired post circulation time"));
|
Log.sinfoln("EXTPUMP", F("Disabled: expired post circulation time"));
|
||||||
|
|
||||||
} else if (this->externalPumpStartReason == MainTask::REASON_PUMP_START_ANTISTUCK && millis() - this->externalPumpStartTime >= ((unsigned int) settings.externalPump.antiStuckTime * 1000)) {
|
} else if (this->externalPumpStartReason == MainTask::REASON_PUMP_START_ANTISTUCK && millis() - this->externalPumpStartTime >= (settings.externalPump.antiStuckTime * 1000u)) {
|
||||||
digitalWrite(settings.externalPump.pin, false);
|
digitalWrite(settings.externalPump.pin, false);
|
||||||
|
|
||||||
vars.states.externalPump = false;
|
vars.states.externalPump = false;
|
||||||
@@ -328,7 +331,7 @@ protected:
|
|||||||
|
|
||||||
Log.sinfoln("EXTPUMP", F("Enabled: heating on"));
|
Log.sinfoln("EXTPUMP", F("Enabled: heating on"));
|
||||||
|
|
||||||
} else if (!vars.states.externalPump && (vars.parameters.extPumpLastEnableTime == 0 || millis() - vars.parameters.extPumpLastEnableTime >= ((unsigned long) settings.externalPump.antiStuckInterval * 1000))) {
|
} else if (!vars.states.externalPump && (vars.parameters.extPumpLastEnableTime == 0 || millis() - vars.parameters.extPumpLastEnableTime >= (settings.externalPump.antiStuckInterval * 1000ul))) {
|
||||||
vars.states.externalPump = true;
|
vars.states.externalPump = true;
|
||||||
this->externalPumpStartTime = millis();
|
this->externalPumpStartTime = millis();
|
||||||
this->externalPumpStartReason = MainTask::REASON_PUMP_START_ANTISTUCK;
|
this->externalPumpStartReason = MainTask::REASON_PUMP_START_ANTISTUCK;
|
||||||
|
|||||||
@@ -38,7 +38,6 @@ public:
|
|||||||
|
|
||||||
void disable() {
|
void disable() {
|
||||||
this->client->stop();
|
this->client->stop();
|
||||||
this->wifiClient->stop();
|
|
||||||
Task::disable();
|
Task::disable();
|
||||||
|
|
||||||
Log.sinfoln(FPSTR(L_MQTT), F("Disabled"));
|
Log.sinfoln(FPSTR(L_MQTT), F("Disabled"));
|
||||||
@@ -90,11 +89,10 @@ protected:
|
|||||||
// wificlient settings
|
// wificlient settings
|
||||||
#ifdef ARDUINO_ARCH_ESP8266
|
#ifdef ARDUINO_ARCH_ESP8266
|
||||||
this->wifiClient->setSync(true);
|
this->wifiClient->setSync(true);
|
||||||
this->wifiClient->setTimeout(1000);
|
this->wifiClient->setNoDelay(true);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// client settings
|
// client settings
|
||||||
//this->client->setClient(*this->wifiClient);
|
|
||||||
this->client->setKeepAliveInterval(15000);
|
this->client->setKeepAliveInterval(15000);
|
||||||
this->client->setTxPayloadSize(256);
|
this->client->setTxPayloadSize(256);
|
||||||
#ifdef ARDUINO_ARCH_ESP8266
|
#ifdef ARDUINO_ARCH_ESP8266
|
||||||
@@ -128,7 +126,7 @@ protected:
|
|||||||
Log.straceln(FPSTR(L_MQTT), F("%s publish %u of %u bytes to topic: %s"), result ? F("Successfully") : F("Failed"), written, length, topic);
|
Log.straceln(FPSTR(L_MQTT), F("%s publish %u of %u bytes to topic: %s"), result ? F("Successfully") : F("Failed"), written, length, topic);
|
||||||
|
|
||||||
#ifdef ARDUINO_ARCH_ESP8266
|
#ifdef ARDUINO_ARCH_ESP8266
|
||||||
::yield();
|
::delay(0);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
//this->client->poll();
|
//this->client->poll();
|
||||||
@@ -137,9 +135,13 @@ protected:
|
|||||||
|
|
||||||
#ifdef ARDUINO_ARCH_ESP8266
|
#ifdef ARDUINO_ARCH_ESP8266
|
||||||
this->writer->setFlushEventCallback([this] (size_t, size_t) {
|
this->writer->setFlushEventCallback([this] (size_t, size_t) {
|
||||||
this->client->flush();
|
::delay(0);
|
||||||
this->wifiClient->flush();
|
|
||||||
::yield();
|
if (this->wifiClient->connected()) {
|
||||||
|
this->wifiClient->flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
::delay(0);
|
||||||
});
|
});
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -173,6 +175,7 @@ protected:
|
|||||||
this->client->setUsernamePassword(settings.mqtt.user, settings.mqtt.password);
|
this->client->setUsernamePassword(settings.mqtt.user, settings.mqtt.password);
|
||||||
this->client->connect(settings.mqtt.server, settings.mqtt.port);
|
this->client->connect(settings.mqtt.server, settings.mqtt.port);
|
||||||
this->lastReconnectTime = millis();
|
this->lastReconnectTime = millis();
|
||||||
|
this->yield();
|
||||||
|
|
||||||
} else if (!this->connected && this->client->connected()) {
|
} else if (!this->connected && this->client->connected()) {
|
||||||
this->connected = true;
|
this->connected = true;
|
||||||
@@ -192,10 +195,6 @@ protected:
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef ARDUINO_ARCH_ESP8266
|
|
||||||
::yield();
|
|
||||||
#endif
|
|
||||||
|
|
||||||
this->client->poll();
|
this->client->poll();
|
||||||
|
|
||||||
// delay for publish data
|
// delay for publish data
|
||||||
@@ -204,11 +203,11 @@ protected:
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef ARDUINO_ARCH_ESP8266
|
#ifdef ARDUINO_ARCH_ESP8266
|
||||||
::yield();
|
::delay(0);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// publish variables and status
|
// publish variables and status
|
||||||
if (this->newConnection || millis() - this->prevPubVarsTime > ((unsigned int) settings.mqtt.interval * 1000)) {
|
if (this->newConnection || millis() - this->prevPubVarsTime > (settings.mqtt.interval * 1000u)) {
|
||||||
this->writer->publish(
|
this->writer->publish(
|
||||||
this->haHelper->getDeviceTopic("status").c_str(),
|
this->haHelper->getDeviceTopic("status").c_str(),
|
||||||
!vars.states.otStatus ? "offline" : vars.states.fault ? "fault" : "online",
|
!vars.states.otStatus ? "offline" : vars.states.fault ? "fault" : "online",
|
||||||
@@ -220,7 +219,7 @@ protected:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// publish settings
|
// publish settings
|
||||||
if (this->newConnection || millis() - this->prevPubSettingsTime > ((unsigned int) settings.mqtt.interval * 10000)) {
|
if (this->newConnection || millis() - this->prevPubSettingsTime > (settings.mqtt.interval * 10000u)) {
|
||||||
this->publishSettings(this->haHelper->getDeviceTopic("settings").c_str());
|
this->publishSettings(this->haHelper->getDeviceTopic("settings").c_str());
|
||||||
this->prevPubSettingsTime = millis();
|
this->prevPubSettingsTime = millis();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
const unsigned int changeStateInterval = 1000;
|
const unsigned int changeStateInterval = 5000;
|
||||||
|
|
||||||
WebServer* webServer = nullptr;
|
WebServer* webServer = nullptr;
|
||||||
BufferedWebServer* bufferedWebServer = nullptr;
|
BufferedWebServer* bufferedWebServer = nullptr;
|
||||||
@@ -514,12 +514,20 @@ protected:
|
|||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
// web server
|
// web server
|
||||||
if (!this->stateWebServer() && (network->isApEnabled() || network->isStaEnabled())) {
|
if (!this->stateWebServer() && (network->isApEnabled() || network->isConnected()) && millis() - this->webServerChangeState >= this->changeStateInterval) {
|
||||||
this->startWebServer();
|
this->startWebServer();
|
||||||
Log.straceln(FPSTR(L_PORTAL_WEBSERVER), F("Started"));
|
Log.straceln(FPSTR(L_PORTAL_WEBSERVER), F("Started: AP up or STA connected"));
|
||||||
|
|
||||||
#ifdef ARDUINO_ARCH_ESP8266
|
#ifdef ARDUINO_ARCH_ESP8266
|
||||||
::esp_yield();
|
::delay(0);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
} else if (this->stateWebServer() && !network->isApEnabled() && !network->isStaEnabled()) {
|
||||||
|
this->stopWebServer();
|
||||||
|
Log.straceln(FPSTR(L_PORTAL_WEBSERVER), F("Stopped: AP and STA down"));
|
||||||
|
|
||||||
|
#ifdef ARDUINO_ARCH_ESP8266
|
||||||
|
::delay(0);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -529,22 +537,22 @@ protected:
|
|||||||
Log.straceln(FPSTR(L_PORTAL_DNSSERVER), F("Started: AP up"));
|
Log.straceln(FPSTR(L_PORTAL_DNSSERVER), F("Started: AP up"));
|
||||||
|
|
||||||
#ifdef ARDUINO_ARCH_ESP8266
|
#ifdef ARDUINO_ARCH_ESP8266
|
||||||
::esp_yield();
|
::delay(0);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
} else if (this->stateDnsServer() && (!network->isApEnabled() || !this->stateWebServer()) && millis() - this->dnsServerChangeState >= this->changeStateInterval) {
|
} else if (this->stateDnsServer() && (!network->isApEnabled() || !this->stateWebServer())) {
|
||||||
this->stopDnsServer();
|
this->stopDnsServer();
|
||||||
Log.straceln(FPSTR(L_PORTAL_DNSSERVER), F("Stopped: AP down"));
|
Log.straceln(FPSTR(L_PORTAL_DNSSERVER), F("Stopped: AP down"));
|
||||||
|
|
||||||
#ifdef ARDUINO_ARCH_ESP8266
|
#ifdef ARDUINO_ARCH_ESP8266
|
||||||
::esp_yield();
|
::delay(0);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this->stateDnsServer()) {
|
if (this->stateDnsServer()) {
|
||||||
this->dnsServer->processNextRequest();
|
this->dnsServer->processNextRequest();
|
||||||
#ifdef ARDUINO_ARCH_ESP8266
|
#ifdef ARDUINO_ARCH_ESP8266
|
||||||
::esp_yield();
|
::delay(0);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -309,7 +309,7 @@ protected:
|
|||||||
pidRegulator.Kd = settings.pid.d_factor;
|
pidRegulator.Kd = settings.pid.d_factor;
|
||||||
|
|
||||||
pidRegulator.setLimits(minTemp, maxTemp);
|
pidRegulator.setLimits(minTemp, maxTemp);
|
||||||
pidRegulator.setDt((unsigned long) settings.pid.dt * 1000);
|
pidRegulator.setDt(settings.pid.dt * 1000u);
|
||||||
pidRegulator.input = vars.temperatures.indoor;
|
pidRegulator.input = vars.temperatures.indoor;
|
||||||
pidRegulator.setpoint = settings.heating.target;
|
pidRegulator.setpoint = settings.heating.target;
|
||||||
|
|
||||||
|
|||||||
@@ -100,7 +100,7 @@ void setup() {
|
|||||||
if (strcmp(SETTINGS_VALID_VALUE, settings.validationValue) != 0) {
|
if (strcmp(SETTINGS_VALID_VALUE, settings.validationValue) != 0) {
|
||||||
Log.swarningln(FPSTR(L_SETTINGS), F("Not valid, set default and restart..."));
|
Log.swarningln(FPSTR(L_SETTINGS), F("Not valid, set default and restart..."));
|
||||||
fsSettings.reset();
|
fsSettings.reset();
|
||||||
delay(5000);
|
::delay(5000);
|
||||||
ESP.restart();
|
ESP.restart();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|||||||
Reference in New Issue
Block a user