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->bufferPos = 0;
|
||||
#ifdef ARDUINO_ARCH_ESP8266
|
||||
::yield();
|
||||
::delay(0);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@@ -97,13 +97,13 @@ protected:
|
||||
}
|
||||
|
||||
if (network->isConnected()) {
|
||||
vars.sensors.rssi = WiFi.RSSI();
|
||||
|
||||
if (!this->telnetStarted && telnetStream != nullptr) {
|
||||
telnetStream->begin(23, false);
|
||||
this->telnetStarted = true;
|
||||
}
|
||||
|
||||
vars.sensors.rssi = WiFi.RSSI();
|
||||
|
||||
if (!tMqtt->isEnabled() && strlen(settings.mqtt.server) > 0) {
|
||||
tMqtt->enable();
|
||||
}
|
||||
@@ -161,6 +161,9 @@ protected:
|
||||
for (Stream* stream : Log.getStreams()) {
|
||||
while (stream->available() > 0) {
|
||||
stream->read();
|
||||
#ifdef ARDUINO_ARCH_ESP8266
|
||||
::delay(0);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@@ -179,8 +182,8 @@ protected:
|
||||
unsigned int freeHeap = getFreeHeap();
|
||||
unsigned int maxFreeBlockHeap = getMaxFreeBlockHeap();
|
||||
|
||||
if (!vars.actions.restart && (freeHeap < 2048 || maxFreeBlockHeap < 2048)) {
|
||||
vars.actions.restart = true;
|
||||
if (!this->restartSignalTime && (freeHeap < 2048 || maxFreeBlockHeap < 2048)) {
|
||||
this->restartSignalTime = millis();
|
||||
}
|
||||
|
||||
if (!settings.system.debug) {
|
||||
@@ -299,7 +302,7 @@ protected:
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
vars.states.externalPump = false;
|
||||
@@ -307,7 +310,7 @@ protected:
|
||||
|
||||
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);
|
||||
|
||||
vars.states.externalPump = false;
|
||||
@@ -328,7 +331,7 @@ protected:
|
||||
|
||||
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;
|
||||
this->externalPumpStartTime = millis();
|
||||
this->externalPumpStartReason = MainTask::REASON_PUMP_START_ANTISTUCK;
|
||||
|
||||
@@ -38,7 +38,6 @@ public:
|
||||
|
||||
void disable() {
|
||||
this->client->stop();
|
||||
this->wifiClient->stop();
|
||||
Task::disable();
|
||||
|
||||
Log.sinfoln(FPSTR(L_MQTT), F("Disabled"));
|
||||
@@ -90,11 +89,10 @@ protected:
|
||||
// wificlient settings
|
||||
#ifdef ARDUINO_ARCH_ESP8266
|
||||
this->wifiClient->setSync(true);
|
||||
this->wifiClient->setTimeout(1000);
|
||||
this->wifiClient->setNoDelay(true);
|
||||
#endif
|
||||
|
||||
// client settings
|
||||
//this->client->setClient(*this->wifiClient);
|
||||
this->client->setKeepAliveInterval(15000);
|
||||
this->client->setTxPayloadSize(256);
|
||||
#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);
|
||||
|
||||
#ifdef ARDUINO_ARCH_ESP8266
|
||||
::yield();
|
||||
::delay(0);
|
||||
#endif
|
||||
|
||||
//this->client->poll();
|
||||
@@ -137,9 +135,13 @@ protected:
|
||||
|
||||
#ifdef ARDUINO_ARCH_ESP8266
|
||||
this->writer->setFlushEventCallback([this] (size_t, size_t) {
|
||||
this->client->flush();
|
||||
this->wifiClient->flush();
|
||||
::yield();
|
||||
::delay(0);
|
||||
|
||||
if (this->wifiClient->connected()) {
|
||||
this->wifiClient->flush();
|
||||
}
|
||||
|
||||
::delay(0);
|
||||
});
|
||||
#endif
|
||||
|
||||
@@ -173,6 +175,7 @@ protected:
|
||||
this->client->setUsernamePassword(settings.mqtt.user, settings.mqtt.password);
|
||||
this->client->connect(settings.mqtt.server, settings.mqtt.port);
|
||||
this->lastReconnectTime = millis();
|
||||
this->yield();
|
||||
|
||||
} else if (!this->connected && this->client->connected()) {
|
||||
this->connected = true;
|
||||
@@ -192,10 +195,6 @@ protected:
|
||||
return;
|
||||
}
|
||||
|
||||
#ifdef ARDUINO_ARCH_ESP8266
|
||||
::yield();
|
||||
#endif
|
||||
|
||||
this->client->poll();
|
||||
|
||||
// delay for publish data
|
||||
@@ -204,11 +203,11 @@ protected:
|
||||
}
|
||||
|
||||
#ifdef ARDUINO_ARCH_ESP8266
|
||||
::yield();
|
||||
::delay(0);
|
||||
#endif
|
||||
|
||||
// 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->haHelper->getDeviceTopic("status").c_str(),
|
||||
!vars.states.otStatus ? "offline" : vars.states.fault ? "fault" : "online",
|
||||
@@ -220,7 +219,7 @@ protected:
|
||||
}
|
||||
|
||||
// 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->prevPubSettingsTime = millis();
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ public:
|
||||
}
|
||||
|
||||
protected:
|
||||
const unsigned int changeStateInterval = 1000;
|
||||
const unsigned int changeStateInterval = 5000;
|
||||
|
||||
WebServer* webServer = nullptr;
|
||||
BufferedWebServer* bufferedWebServer = nullptr;
|
||||
@@ -514,12 +514,20 @@ protected:
|
||||
|
||||
void loop() {
|
||||
// web server
|
||||
if (!this->stateWebServer() && (network->isApEnabled() || network->isStaEnabled())) {
|
||||
if (!this->stateWebServer() && (network->isApEnabled() || network->isConnected()) && millis() - this->webServerChangeState >= this->changeStateInterval) {
|
||||
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
|
||||
::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
|
||||
}
|
||||
|
||||
@@ -529,22 +537,22 @@ protected:
|
||||
Log.straceln(FPSTR(L_PORTAL_DNSSERVER), F("Started: AP up"));
|
||||
|
||||
#ifdef ARDUINO_ARCH_ESP8266
|
||||
::esp_yield();
|
||||
::delay(0);
|
||||
#endif
|
||||
|
||||
} else if (this->stateDnsServer() && (!network->isApEnabled() || !this->stateWebServer()) && millis() - this->dnsServerChangeState >= this->changeStateInterval) {
|
||||
} else if (this->stateDnsServer() && (!network->isApEnabled() || !this->stateWebServer())) {
|
||||
this->stopDnsServer();
|
||||
Log.straceln(FPSTR(L_PORTAL_DNSSERVER), F("Stopped: AP down"));
|
||||
|
||||
#ifdef ARDUINO_ARCH_ESP8266
|
||||
::esp_yield();
|
||||
::delay(0);
|
||||
#endif
|
||||
}
|
||||
|
||||
if (this->stateDnsServer()) {
|
||||
this->dnsServer->processNextRequest();
|
||||
#ifdef ARDUINO_ARCH_ESP8266
|
||||
::esp_yield();
|
||||
::delay(0);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@@ -309,7 +309,7 @@ protected:
|
||||
pidRegulator.Kd = settings.pid.d_factor;
|
||||
|
||||
pidRegulator.setLimits(minTemp, maxTemp);
|
||||
pidRegulator.setDt((unsigned long) settings.pid.dt * 1000);
|
||||
pidRegulator.setDt(settings.pid.dt * 1000u);
|
||||
pidRegulator.input = vars.temperatures.indoor;
|
||||
pidRegulator.setpoint = settings.heating.target;
|
||||
|
||||
|
||||
@@ -100,7 +100,7 @@ void setup() {
|
||||
if (strcmp(SETTINGS_VALID_VALUE, settings.validationValue) != 0) {
|
||||
Log.swarningln(FPSTR(L_SETTINGS), F("Not valid, set default and restart..."));
|
||||
fsSettings.reset();
|
||||
delay(5000);
|
||||
::delay(5000);
|
||||
ESP.restart();
|
||||
}
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user