Skip to content

Commit

Permalink
Migrate MqttHandleInverter to TaskScheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
tbnobody committed Nov 23, 2023
1 parent 5c501f8 commit 5244834
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 65 deletions.
9 changes: 5 additions & 4 deletions include/MqttHandleInverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
119 changes: 61 additions & 58 deletions src/MqttHandleInverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

MqttHandleInverterClass MqttHandleInverter;

void MqttHandleInverterClass::init()
void MqttHandleInverterClass::init(Scheduler* scheduler)
{
using std::placeholders::_1;
using std::placeholders::_2;
Expand All @@ -34,90 +34,93 @@ 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);
// Loop all inverters
for (uint8_t i = 0; i < Hoymiles.getNumInverters(); i++) {
auto inv = Hoymiles.getInverterByPos(i);

String subtopic = inv->serialString();
String subtopic = inv->serialString();

// Name
MqttSettings.publish(subtopic + "/name", inv->name());
// Name
MqttSettings.publish(subtopic + "/name", inv->name());

if (inv->DevInfo()->getLastUpdate() > 0) {
// Bootloader Version
MqttSettings.publish(subtopic + "/device/bootloaderversion", String(inv->DevInfo()->getFwBootloaderVersion()));
if (inv->DevInfo()->getLastUpdate() > 0) {
// Bootloader Version
MqttSettings.publish(subtopic + "/device/bootloaderversion", String(inv->DevInfo()->getFwBootloaderVersion()));

// Firmware Version
MqttSettings.publish(subtopic + "/device/fwbuildversion", String(inv->DevInfo()->getFwBuildVersion()));
// Firmware Version
MqttSettings.publish(subtopic + "/device/fwbuildversion", String(inv->DevInfo()->getFwBuildVersion()));

// Firmware Build DateTime
char timebuffer[32];
const time_t t = inv->DevInfo()->getFwBuildDateTime();
std::strftime(timebuffer, sizeof(timebuffer), "%Y-%m-%d %H:%M:%S", gmtime(&t));
MqttSettings.publish(subtopic + "/device/fwbuilddatetime", String(timebuffer));
// Firmware Build DateTime
char timebuffer[32];
const time_t t = inv->DevInfo()->getFwBuildDateTime();
std::strftime(timebuffer, sizeof(timebuffer), "%Y-%m-%d %H:%M:%S", gmtime(&t));
MqttSettings.publish(subtopic + "/device/fwbuilddatetime", String(timebuffer));

// Hardware part number
MqttSettings.publish(subtopic + "/device/hwpartnumber", String(inv->DevInfo()->getHwPartNumber()));
// Hardware part number
MqttSettings.publish(subtopic + "/device/hwpartnumber", String(inv->DevInfo()->getHwPartNumber()));

// Hardware version
MqttSettings.publish(subtopic + "/device/hwversion", inv->DevInfo()->getHwVersion());
}
// Hardware version
MqttSettings.publish(subtopic + "/device/hwversion", inv->DevInfo()->getHwVersion());
}

if (inv->SystemConfigPara()->getLastUpdate() > 0) {
// Limit
MqttSettings.publish(subtopic + "/status/limit_relative", String(inv->SystemConfigPara()->getLimitPercent()));
if (inv->SystemConfigPara()->getLastUpdate() > 0) {
// Limit
MqttSettings.publish(subtopic + "/status/limit_relative", String(inv->SystemConfigPara()->getLimitPercent()));

uint16_t maxpower = inv->DevInfo()->getMaxPower();
if (maxpower > 0) {
MqttSettings.publish(subtopic + "/status/limit_absolute", String(inv->SystemConfigPara()->getLimitPercent() * maxpower / 100));
}
uint16_t maxpower = inv->DevInfo()->getMaxPower();
if (maxpower > 0) {
MqttSettings.publish(subtopic + "/status/limit_absolute", String(inv->SystemConfigPara()->getLimitPercent() * maxpower / 100));
}
}

MqttSettings.publish(subtopic + "/status/reachable", String(inv->isReachable()));
MqttSettings.publish(subtopic + "/status/producing", String(inv->isProducing()));
MqttSettings.publish(subtopic + "/status/reachable", String(inv->isReachable()));
MqttSettings.publish(subtopic + "/status/producing", String(inv->isProducing()));

if (inv->Statistics()->getLastUpdate() > 0) {
MqttSettings.publish(subtopic + "/status/last_update", String(std::time(0) - (millis() - inv->Statistics()->getLastUpdate()) / 1000));
} else {
MqttSettings.publish(subtopic + "/status/last_update", String(0));
}
if (inv->Statistics()->getLastUpdate() > 0) {
MqttSettings.publish(subtopic + "/status/last_update", String(std::time(0) - (millis() - inv->Statistics()->getLastUpdate()) / 1000));
} else {
MqttSettings.publish(subtopic + "/status/last_update", String(0));
}

uint32_t lastUpdateInternal = inv->Statistics()->getLastUpdateFromInternal();
if (inv->Statistics()->getLastUpdate() > 0 && (lastUpdateInternal != _lastPublishStats[i])) {
_lastPublishStats[i] = lastUpdateInternal;

// Loop all channels
for (auto& t : inv->Statistics()->getChannelTypes()) {
for (auto& c : inv->Statistics()->getChannelsByType(t)) {
if (t == TYPE_DC) {
INVERTER_CONFIG_T* inv_cfg = Configuration.getInverterConfig(inv->serial());
if (inv_cfg != nullptr) {
// TODO(tbnobody)
MqttSettings.publish(inv->serialString() + "/" + String(static_cast<uint8_t>(c) + 1) + "/name", inv_cfg->channel[c].Name);
}
}
for (uint8_t f = 0; f < sizeof(_publishFields) / sizeof(FieldId_t); f++) {
publishField(inv, t, c, _publishFields[f]);
uint32_t lastUpdateInternal = inv->Statistics()->getLastUpdateFromInternal();
if (inv->Statistics()->getLastUpdate() > 0 && (lastUpdateInternal != _lastPublishStats[i])) {
_lastPublishStats[i] = lastUpdateInternal;

// Loop all channels
for (auto& t : inv->Statistics()->getChannelTypes()) {
for (auto& c : inv->Statistics()->getChannelsByType(t)) {
if (t == TYPE_DC) {
INVERTER_CONFIG_T* inv_cfg = Configuration.getInverterConfig(inv->serial());
if (inv_cfg != nullptr) {
// TODO(tbnobody)
MqttSettings.publish(inv->serialString() + "/" + String(static_cast<uint8_t>(c) + 1) + "/name", inv_cfg->channel[c].Name);
}
}
for (uint8_t f = 0; f < sizeof(_publishFields) / sizeof(FieldId_t); f++) {
publishField(inv, t, c, _publishFields[f]);
}
}
}

yield();
}

_lastPublish = millis();
yield();
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -157,8 +157,6 @@ void loop()
yield();
InverterSettings.loop();
yield();
MqttHandleInverter.loop();
yield();
WebApi.loop();
yield();
Display.loop();
Expand Down

0 comments on commit 5244834

Please sign in to comment.