Migrate MqttHandleInverter to TaskScheduler
This commit is contained in:
parent
5c501f879f
commit
524483451f
@ -3,22 +3,23 @@
|
||||
|
||||
#include "Configuration.h"
|
||||
#include <Hoymiles.h>
|
||||
#include <TimeoutHelper.h>
|
||||
#include <TaskSchedulerDeclarations.h>
|
||||
#include <espMqttClient.h>
|
||||
|
||||
class MqttHandleInverterClass {
|
||||
public:
|
||||
void init();
|
||||
void loop();
|
||||
void init(Scheduler* scheduler);
|
||||
|
||||
static String getTopic(std::shared_ptr<InverterAbstract> inv, ChannelType_t type, ChannelNum_t channel, FieldId_t fieldId);
|
||||
|
||||
private:
|
||||
void loop();
|
||||
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);
|
||||
|
||||
Task _loopTask;
|
||||
|
||||
uint32_t _lastPublishStats[INV_MAX_COUNT] = { 0 };
|
||||
uint32_t _lastPublish = 0;
|
||||
|
||||
FieldId_t _publishFields[14] = {
|
||||
FLD_UDC,
|
||||
|
||||
@ -18,7 +18,7 @@
|
||||
|
||||
MqttHandleInverterClass MqttHandleInverter;
|
||||
|
||||
void MqttHandleInverterClass::init()
|
||||
void MqttHandleInverterClass::init(Scheduler* scheduler)
|
||||
{
|
||||
using std::placeholders::_1;
|
||||
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_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));
|
||||
|
||||
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()
|
||||
{
|
||||
_loopTask.setInterval(Configuration.get().Mqtt.PublishInterval * TASK_SECOND);
|
||||
|
||||
if (!MqttSettings.getConnected() || !Hoymiles.isAllRadioIdle()) {
|
||||
_loopTask.forceNextIteration();
|
||||
return;
|
||||
}
|
||||
|
||||
const CONFIG_T& config = Configuration.get();
|
||||
|
||||
if (millis() - _lastPublish > (config.Mqtt.PublishInterval * 1000)) {
|
||||
// Loop all inverters
|
||||
for (uint8_t i = 0; i < Hoymiles.getNumInverters(); i++) {
|
||||
auto inv = Hoymiles.getInverterByPos(i);
|
||||
@ -116,9 +122,6 @@ void MqttHandleInverterClass::loop()
|
||||
|
||||
yield();
|
||||
}
|
||||
|
||||
_lastPublish = millis();
|
||||
}
|
||||
}
|
||||
|
||||
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... ");
|
||||
MqttSettings.init();
|
||||
MqttHandleDtu.init(&scheduler);
|
||||
MqttHandleInverter.init();
|
||||
MqttHandleInverter.init(&scheduler);
|
||||
MqttHandleInverterTotal.init(&scheduler);
|
||||
MqttHandleHass.init(&scheduler);
|
||||
MessageOutput.println("done");
|
||||
@ -157,8 +157,6 @@ void loop()
|
||||
yield();
|
||||
InverterSettings.loop();
|
||||
yield();
|
||||
MqttHandleInverter.loop();
|
||||
yield();
|
||||
WebApi.loop();
|
||||
yield();
|
||||
Display.loop();
|
||||
|
||||
Loading…
Reference in New Issue
Block a user