Migrate MqttHandleInverter to TaskScheduler
This commit is contained in:
parent
5c501f879f
commit
524483451f
@ -3,22 +3,23 @@
|
|||||||
|
|
||||||
#include "Configuration.h"
|
#include "Configuration.h"
|
||||||
#include <Hoymiles.h>
|
#include <Hoymiles.h>
|
||||||
#include <TimeoutHelper.h>
|
#include <TaskSchedulerDeclarations.h>
|
||||||
#include <espMqttClient.h>
|
#include <espMqttClient.h>
|
||||||
|
|
||||||
class MqttHandleInverterClass {
|
class MqttHandleInverterClass {
|
||||||
public:
|
public:
|
||||||
void init();
|
void init(Scheduler* scheduler);
|
||||||
void loop();
|
|
||||||
|
|
||||||
static String getTopic(std::shared_ptr<InverterAbstract> inv, ChannelType_t type, ChannelNum_t channel, FieldId_t fieldId);
|
static String getTopic(std::shared_ptr<InverterAbstract> inv, ChannelType_t type, ChannelNum_t channel, FieldId_t fieldId);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void loop();
|
||||||
void publishField(std::shared_ptr<InverterAbstract> inv, ChannelType_t type, ChannelNum_t channel, FieldId_t fieldId);
|
void publishField(std::shared_ptr<InverterAbstract> inv, ChannelType_t type, ChannelNum_t channel, FieldId_t fieldId);
|
||||||
void onMqttMessage(const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, size_t len, size_t index, size_t total);
|
void onMqttMessage(const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, size_t len, size_t index, size_t total);
|
||||||
|
|
||||||
|
Task _loopTask;
|
||||||
|
|
||||||
uint32_t _lastPublishStats[INV_MAX_COUNT] = { 0 };
|
uint32_t _lastPublishStats[INV_MAX_COUNT] = { 0 };
|
||||||
uint32_t _lastPublish = 0;
|
|
||||||
|
|
||||||
FieldId_t _publishFields[14] = {
|
FieldId_t _publishFields[14] = {
|
||||||
FLD_UDC,
|
FLD_UDC,
|
||||||
|
|||||||
@ -18,7 +18,7 @@
|
|||||||
|
|
||||||
MqttHandleInverterClass MqttHandleInverter;
|
MqttHandleInverterClass MqttHandleInverter;
|
||||||
|
|
||||||
void MqttHandleInverterClass::init()
|
void MqttHandleInverterClass::init(Scheduler* scheduler)
|
||||||
{
|
{
|
||||||
using std::placeholders::_1;
|
using std::placeholders::_1;
|
||||||
using std::placeholders::_2;
|
using std::placeholders::_2;
|
||||||
@ -34,17 +34,23 @@ void MqttHandleInverterClass::init()
|
|||||||
MqttSettings.subscribe(String(topic + "+/cmd/" + TOPIC_SUB_LIMIT_NONPERSISTENT_ABSOLUTE).c_str(), 0, std::bind(&MqttHandleInverterClass::onMqttMessage, this, _1, _2, _3, _4, _5, _6));
|
MqttSettings.subscribe(String(topic + "+/cmd/" + TOPIC_SUB_LIMIT_NONPERSISTENT_ABSOLUTE).c_str(), 0, std::bind(&MqttHandleInverterClass::onMqttMessage, this, _1, _2, _3, _4, _5, _6));
|
||||||
MqttSettings.subscribe(String(topic + "+/cmd/" + TOPIC_SUB_POWER).c_str(), 0, std::bind(&MqttHandleInverterClass::onMqttMessage, this, _1, _2, _3, _4, _5, _6));
|
MqttSettings.subscribe(String(topic + "+/cmd/" + TOPIC_SUB_POWER).c_str(), 0, std::bind(&MqttHandleInverterClass::onMqttMessage, this, _1, _2, _3, _4, _5, _6));
|
||||||
MqttSettings.subscribe(String(topic + "+/cmd/" + TOPIC_SUB_RESTART).c_str(), 0, std::bind(&MqttHandleInverterClass::onMqttMessage, this, _1, _2, _3, _4, _5, _6));
|
MqttSettings.subscribe(String(topic + "+/cmd/" + TOPIC_SUB_RESTART).c_str(), 0, std::bind(&MqttHandleInverterClass::onMqttMessage, this, _1, _2, _3, _4, _5, _6));
|
||||||
|
|
||||||
|
scheduler->addTask(_loopTask);
|
||||||
|
_loopTask.setCallback(std::bind(&MqttHandleInverterClass::loop, this));
|
||||||
|
_loopTask.setIterations(TASK_FOREVER);
|
||||||
|
_loopTask.setInterval(Configuration.get().Mqtt.PublishInterval * TASK_SECOND);
|
||||||
|
_loopTask.enable();
|
||||||
}
|
}
|
||||||
|
|
||||||
void MqttHandleInverterClass::loop()
|
void MqttHandleInverterClass::loop()
|
||||||
{
|
{
|
||||||
|
_loopTask.setInterval(Configuration.get().Mqtt.PublishInterval * TASK_SECOND);
|
||||||
|
|
||||||
if (!MqttSettings.getConnected() || !Hoymiles.isAllRadioIdle()) {
|
if (!MqttSettings.getConnected() || !Hoymiles.isAllRadioIdle()) {
|
||||||
|
_loopTask.forceNextIteration();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const CONFIG_T& config = Configuration.get();
|
|
||||||
|
|
||||||
if (millis() - _lastPublish > (config.Mqtt.PublishInterval * 1000)) {
|
|
||||||
// Loop all inverters
|
// Loop all inverters
|
||||||
for (uint8_t i = 0; i < Hoymiles.getNumInverters(); i++) {
|
for (uint8_t i = 0; i < Hoymiles.getNumInverters(); i++) {
|
||||||
auto inv = Hoymiles.getInverterByPos(i);
|
auto inv = Hoymiles.getInverterByPos(i);
|
||||||
@ -116,9 +122,6 @@ void MqttHandleInverterClass::loop()
|
|||||||
|
|
||||||
yield();
|
yield();
|
||||||
}
|
}
|
||||||
|
|
||||||
_lastPublish = millis();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void MqttHandleInverterClass::publishField(std::shared_ptr<InverterAbstract> inv, ChannelType_t type, ChannelNum_t channel, FieldId_t fieldId)
|
void MqttHandleInverterClass::publishField(std::shared_ptr<InverterAbstract> inv, ChannelType_t type, ChannelNum_t channel, FieldId_t fieldId)
|
||||||
|
|||||||
@ -100,7 +100,7 @@ void setup()
|
|||||||
MessageOutput.print("Initialize MqTT... ");
|
MessageOutput.print("Initialize MqTT... ");
|
||||||
MqttSettings.init();
|
MqttSettings.init();
|
||||||
MqttHandleDtu.init(&scheduler);
|
MqttHandleDtu.init(&scheduler);
|
||||||
MqttHandleInverter.init();
|
MqttHandleInverter.init(&scheduler);
|
||||||
MqttHandleInverterTotal.init(&scheduler);
|
MqttHandleInverterTotal.init(&scheduler);
|
||||||
MqttHandleHass.init(&scheduler);
|
MqttHandleHass.init(&scheduler);
|
||||||
MessageOutput.println("done");
|
MessageOutput.println("done");
|
||||||
@ -157,8 +157,6 @@ void loop()
|
|||||||
yield();
|
yield();
|
||||||
InverterSettings.loop();
|
InverterSettings.loop();
|
||||||
yield();
|
yield();
|
||||||
MqttHandleInverter.loop();
|
|
||||||
yield();
|
|
||||||
WebApi.loop();
|
WebApi.loop();
|
||||||
yield();
|
yield();
|
||||||
Display.loop();
|
Display.loop();
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user