Skip to content

Commit

Permalink
Migrate MqttHandleHass to TaskScheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
tbnobody committed Nov 23, 2023
1 parent 98c30d1 commit 48a27fb
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
7 changes: 5 additions & 2 deletions include/MqttHandleHass.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <ArduinoJson.h>
#include <Hoymiles.h>
#include <TaskSchedulerDeclarations.h>

// mqtt discovery device classes
enum {
Expand Down Expand Up @@ -50,19 +51,21 @@ const byteAssign_fieldDeviceClass_t deviceFieldAssignment[] = {

class MqttHandleHassClass {
public:
void init();
void loop();
void init(Scheduler* scheduler);
void publishConfig();
void forceUpdate();

private:
void loop();
void publish(const String& subtopic, const String& payload);
void publishField(std::shared_ptr<InverterAbstract> inv, ChannelType_t type, ChannelNum_t channel, byteAssign_fieldDeviceClass_t fieldType, bool clear = false);
void publishInverterButton(std::shared_ptr<InverterAbstract> inv, const char* caption, const char* icon, const char* category, const char* deviceClass, const char* subTopic, const char* payload);
void publishInverterNumber(std::shared_ptr<InverterAbstract> inv, const char* caption, const char* icon, const char* category, const char* commandTopic, const char* stateTopic, const char* unitOfMeasure, int16_t min = 1, int16_t max = 100);
void publishInverterBinarySensor(std::shared_ptr<InverterAbstract> inv, const char* caption, const char* subTopic, const char* payload_on, const char* payload_off);
void createDeviceInfo(JsonObject& object, std::shared_ptr<InverterAbstract> inv);

Task _loopTask;

bool _wasConnected = false;
bool _updateForced = false;
};
Expand Down
6 changes: 5 additions & 1 deletion src/MqttHandleHass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@

MqttHandleHassClass MqttHandleHass;

void MqttHandleHassClass::init()
void MqttHandleHassClass::init(Scheduler* scheduler)
{
scheduler->addTask(_loopTask);
_loopTask.setCallback(std::bind(&MqttHandleHassClass::loop, this));
_loopTask.setIterations(TASK_FOREVER);
_loopTask.enable();
}

void MqttHandleHassClass::loop()
Expand Down
4 changes: 1 addition & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ void setup()
MqttHandleDtu.init();
MqttHandleInverter.init();
MqttHandleInverterTotal.init(&scheduler);
MqttHandleHass.init();
MqttHandleHass.init(&scheduler);
MessageOutput.println("done");

// Initialize WebApi
Expand Down Expand Up @@ -161,8 +161,6 @@ void loop()
yield();
MqttHandleInverter.loop();
yield();
MqttHandleHass.loop();
yield();
WebApi.loop();
yield();
Display.loop();
Expand Down

0 comments on commit 48a27fb

Please sign in to comment.