mirror of
https://github.com/Laxilef/OTGateway.git
synced 2025-12-12 19:24:27 +05:00
added buffer for writing to mqtt, refactoring
This commit is contained in:
144
src/MqttTask.h
144
src/MqttTask.h
@@ -11,7 +11,14 @@ class MqttTask : public Task {
|
|||||||
public:
|
public:
|
||||||
MqttTask(bool _enabled = false, unsigned long _interval = 0) : Task(_enabled, _interval) {}
|
MqttTask(bool _enabled = false, unsigned long _interval = 0) : Task(_enabled, _interval) {}
|
||||||
|
|
||||||
|
~MqttTask() {
|
||||||
|
if (this->bClient != nullptr) {
|
||||||
|
delete this->bClient;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
BufferingPrint* bClient = nullptr;
|
||||||
unsigned long lastReconnectAttempt = 0;
|
unsigned long lastReconnectAttempt = 0;
|
||||||
unsigned long firstFailConnect = 0;
|
unsigned long firstFailConnect = 0;
|
||||||
|
|
||||||
@@ -26,8 +33,12 @@ protected:
|
|||||||
void setup() {
|
void setup() {
|
||||||
Log.sinfoln("MQTT", F("Started"));
|
Log.sinfoln("MQTT", F("Started"));
|
||||||
|
|
||||||
client.setCallback(__callback);
|
this->bClient = new BufferingPrint(client, 32);
|
||||||
|
|
||||||
|
client.setCallback(std::bind(&MqttTask::__callback, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
|
||||||
client.setBufferSize(1024);
|
client.setBufferSize(1024);
|
||||||
|
|
||||||
|
haHelper.setBufferedClient(this->bClient);
|
||||||
haHelper.setDevicePrefix(settings.mqtt.prefix);
|
haHelper.setDevicePrefix(settings.mqtt.prefix);
|
||||||
haHelper.setDeviceVersion(PROJECT_VERSION);
|
haHelper.setDeviceVersion(PROJECT_VERSION);
|
||||||
haHelper.setDeviceModel(PROJECT_NAME);
|
haHelper.setDeviceModel(PROJECT_NAME);
|
||||||
@@ -86,7 +97,7 @@ protected:
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static bool updateSettings(JsonDocument& doc) {
|
bool updateSettings(JsonDocument& doc) {
|
||||||
bool flag = false;
|
bool flag = false;
|
||||||
|
|
||||||
if (!doc["debug"].isNull() && doc["debug"].is<bool>()) {
|
if (!doc["debug"].isNull() && doc["debug"].is<bool>()) {
|
||||||
@@ -298,7 +309,7 @@ protected:
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool updateVariables(const JsonDocument& doc) {
|
bool updateVariables(const JsonDocument& doc) {
|
||||||
bool flag = false;
|
bool flag = false;
|
||||||
|
|
||||||
if (!doc["ping"].isNull() && doc["ping"]) {
|
if (!doc["ping"].isNull() && doc["ping"]) {
|
||||||
@@ -352,7 +363,7 @@ protected:
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void publish(bool force = false) {
|
void publish(bool force = false) {
|
||||||
static unsigned int prevPubVars = 0;
|
static unsigned int prevPubVars = 0;
|
||||||
static unsigned int prevPubSettings = 0;
|
static unsigned int prevPubSettings = 0;
|
||||||
|
|
||||||
@@ -376,74 +387,74 @@ protected:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void publishHaEntities() {
|
void publishHaEntities() {
|
||||||
// main
|
// main
|
||||||
haHelper.publishSelectOutdoorSensorType();
|
haHelper.publishSelectOutdoorSensorType(); yield();
|
||||||
haHelper.publishSelectIndoorSensorType();
|
haHelper.publishSelectIndoorSensorType(); yield();
|
||||||
haHelper.publishNumberOutdoorSensorOffset(false);
|
haHelper.publishNumberOutdoorSensorOffset(false); yield();
|
||||||
haHelper.publishNumberIndoorSensorOffset(false);
|
haHelper.publishNumberIndoorSensorOffset(false); yield();
|
||||||
haHelper.publishSwitchDebug(false);
|
haHelper.publishSwitchDebug(false); yield();
|
||||||
|
|
||||||
// emergency
|
// emergency
|
||||||
haHelper.publishSwitchEmergency();
|
haHelper.publishSwitchEmergency(); yield();
|
||||||
haHelper.publishNumberEmergencyTarget();
|
haHelper.publishNumberEmergencyTarget(); yield();
|
||||||
haHelper.publishSwitchEmergencyUseEquitherm();
|
haHelper.publishSwitchEmergencyUseEquitherm(); yield();
|
||||||
|
|
||||||
// heating
|
// heating
|
||||||
haHelper.publishSwitchHeating(false);
|
haHelper.publishSwitchHeating(false); yield();
|
||||||
haHelper.publishSwitchHeatingTurbo();
|
haHelper.publishSwitchHeatingTurbo(); yield();
|
||||||
haHelper.publishNumberHeatingHysteresis();
|
haHelper.publishNumberHeatingHysteresis(); yield();
|
||||||
haHelper.publishSensorHeatingSetpoint(false);
|
haHelper.publishSensorHeatingSetpoint(false); yield();
|
||||||
haHelper.publishSensorCurrentHeatingMinTemp(false);
|
haHelper.publishSensorCurrentHeatingMinTemp(false); yield();
|
||||||
haHelper.publishSensorCurrentHeatingMaxTemp(false);
|
haHelper.publishSensorCurrentHeatingMaxTemp(false); yield();
|
||||||
haHelper.publishNumberHeatingMinTemp(false);
|
haHelper.publishNumberHeatingMinTemp(false); yield();
|
||||||
haHelper.publishNumberHeatingMaxTemp(false);
|
haHelper.publishNumberHeatingMaxTemp(false); yield();
|
||||||
haHelper.publishNumberHeatingMaxModulation(false);
|
haHelper.publishNumberHeatingMaxModulation(false); yield();
|
||||||
|
|
||||||
// pid
|
// pid
|
||||||
haHelper.publishSwitchPID();
|
haHelper.publishSwitchPID(); yield();
|
||||||
haHelper.publishNumberPIDFactorP();
|
haHelper.publishNumberPIDFactorP(); yield();
|
||||||
haHelper.publishNumberPIDFactorI();
|
haHelper.publishNumberPIDFactorI(); yield();
|
||||||
haHelper.publishNumberPIDFactorD();
|
haHelper.publishNumberPIDFactorD(); yield();
|
||||||
haHelper.publishNumberPIDMinTemp(false);
|
haHelper.publishNumberPIDMinTemp(false); yield();
|
||||||
haHelper.publishNumberPIDMaxTemp(false);
|
haHelper.publishNumberPIDMaxTemp(false); yield();
|
||||||
|
|
||||||
// equitherm
|
// equitherm
|
||||||
haHelper.publishSwitchEquitherm();
|
haHelper.publishSwitchEquitherm(); yield();
|
||||||
haHelper.publishNumberEquithermFactorN();
|
haHelper.publishNumberEquithermFactorN(); yield();
|
||||||
haHelper.publishNumberEquithermFactorK();
|
haHelper.publishNumberEquithermFactorK(); yield();
|
||||||
haHelper.publishNumberEquithermFactorT();
|
haHelper.publishNumberEquithermFactorT(); yield();
|
||||||
|
|
||||||
// tuning
|
// tuning
|
||||||
haHelper.publishSwitchTuning();
|
haHelper.publishSwitchTuning(); yield();
|
||||||
haHelper.publishSelectTuningRegulator();
|
haHelper.publishSelectTuningRegulator(); yield();
|
||||||
|
|
||||||
// states
|
// states
|
||||||
haHelper.publishBinSensorStatus();
|
haHelper.publishBinSensorStatus(); yield();
|
||||||
haHelper.publishBinSensorOtStatus();
|
haHelper.publishBinSensorOtStatus(); yield();
|
||||||
haHelper.publishBinSensorHeating();
|
haHelper.publishBinSensorHeating(); yield();
|
||||||
haHelper.publishBinSensorFlame();
|
haHelper.publishBinSensorFlame(); yield();
|
||||||
haHelper.publishBinSensorFault();
|
haHelper.publishBinSensorFault(); yield();
|
||||||
haHelper.publishBinSensorDiagnostic();
|
haHelper.publishBinSensorDiagnostic(); yield();
|
||||||
|
|
||||||
// sensors
|
// sensors
|
||||||
haHelper.publishSensorModulation(false);
|
haHelper.publishSensorModulation(false); yield();
|
||||||
haHelper.publishSensorPressure(false);
|
haHelper.publishSensorPressure(false); yield();
|
||||||
haHelper.publishSensorFaultCode();
|
haHelper.publishSensorFaultCode(); yield();
|
||||||
haHelper.publishSensorRssi(false);
|
haHelper.publishSensorRssi(false); yield();
|
||||||
haHelper.publishSensorUptime(false);
|
haHelper.publishSensorUptime(false); yield();
|
||||||
|
|
||||||
// temperatures
|
// temperatures
|
||||||
haHelper.publishNumberIndoorTemp();
|
haHelper.publishNumberIndoorTemp(); yield();
|
||||||
haHelper.publishSensorHeatingTemp();
|
haHelper.publishSensorHeatingTemp(); yield();
|
||||||
|
|
||||||
// buttons
|
// buttons
|
||||||
haHelper.publishButtonRestart(false);
|
haHelper.publishButtonRestart(false); yield();
|
||||||
haHelper.publishButtonResetFault();
|
haHelper.publishButtonResetFault(); yield();
|
||||||
haHelper.publishButtonResetDiagnostic();
|
haHelper.publishButtonResetDiagnostic(); yield();
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool publishNonStaticHaEntities(bool force = false) {
|
bool publishNonStaticHaEntities(bool force = false) {
|
||||||
static byte _heatingMinTemp, _heatingMaxTemp, _dhwMinTemp, _dhwMaxTemp;
|
static byte _heatingMinTemp, _heatingMaxTemp, _dhwMinTemp, _dhwMaxTemp;
|
||||||
static bool _editableOutdoorTemp, _editableIndoorTemp, _dhwPresent;
|
static bool _editableOutdoorTemp, _editableIndoorTemp, _dhwPresent;
|
||||||
|
|
||||||
@@ -538,7 +549,7 @@ protected:
|
|||||||
return published;
|
return published;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool publishSettings(const char* topic) {
|
bool publishSettings(const char* topic) {
|
||||||
StaticJsonDocument<2048> doc;
|
StaticJsonDocument<2048> doc;
|
||||||
|
|
||||||
doc["debug"] = settings.debug;
|
doc["debug"] = settings.debug;
|
||||||
@@ -579,11 +590,12 @@ protected:
|
|||||||
doc["sensors"]["indoor"]["offset"] = settings.sensors.indoor.offset;
|
doc["sensors"]["indoor"]["offset"] = settings.sensors.indoor.offset;
|
||||||
|
|
||||||
client.beginPublish(topic, measureJson(doc), false);
|
client.beginPublish(topic, measureJson(doc), false);
|
||||||
serializeJson(doc, client);
|
serializeJson(doc, *this->bClient);
|
||||||
|
this->bClient->flush();
|
||||||
return client.endPublish();
|
return client.endPublish();
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool publishVariables(const char* topic) {
|
bool publishVariables(const char* topic) {
|
||||||
StaticJsonDocument<2048> doc;
|
StaticJsonDocument<2048> doc;
|
||||||
|
|
||||||
doc["tuning"]["enable"] = vars.tuning.enable;
|
doc["tuning"]["enable"] = vars.tuning.enable;
|
||||||
@@ -616,7 +628,8 @@ protected:
|
|||||||
doc["parameters"]["dhwMaxTemp"] = vars.parameters.dhwMaxTemp;
|
doc["parameters"]["dhwMaxTemp"] = vars.parameters.dhwMaxTemp;
|
||||||
|
|
||||||
client.beginPublish(topic, measureJson(doc), false);
|
client.beginPublish(topic, measureJson(doc), false);
|
||||||
serializeJson(doc, client);
|
serializeJson(doc, *this->bClient);
|
||||||
|
this->bClient->flush();
|
||||||
return client.endPublish();
|
return client.endPublish();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -624,25 +637,26 @@ protected:
|
|||||||
return std::string(settings.mqtt.prefix) + "/" + std::string(topic);
|
return std::string(settings.mqtt.prefix) + "/" + std::string(topic);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void __callback(char* topic, byte* payload, unsigned int length) {
|
void __callback(char* topic, byte* payload, unsigned int length) {
|
||||||
if (!length) {
|
if (!length) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (settings.debug) {
|
if (settings.debug) {
|
||||||
Log.strace("MQTT.MSG", F("Topic: %s\r\n> "), topic);
|
Log.strace("MQTT.MSG", F("Topic: %s\r\n> "), topic);
|
||||||
Log.lock();
|
if (Log.lock()) {
|
||||||
for (unsigned int i = 0; i < length; i++) {
|
for (unsigned int i = 0; i < length; i++) {
|
||||||
if ( payload[i] == 10 ) {
|
if ( payload[i] == 10 ) {
|
||||||
Log.print("\r\n> ");
|
Log.print("\r\n> ");
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
Log.print((char) payload[i]);
|
Log.print((char) payload[i]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Log.print("\r\n\n");
|
||||||
|
Log.flush();
|
||||||
|
Log.unlock();
|
||||||
}
|
}
|
||||||
Log.print("\r\n\n");
|
|
||||||
Log.unlock();
|
|
||||||
Log.flush();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
StaticJsonDocument<2048> doc;
|
StaticJsonDocument<2048> doc;
|
||||||
|
|||||||
Reference in New Issue
Block a user