This commit is contained in:
J. Nick Koston
2025-11-28 13:52:58 -06:00
parent d9701af9c1
commit ab6b4c77d2
+24 -8
View File
@@ -9,20 +9,30 @@ namespace ota {
class OTAStateChangeTrigger final : public Trigger<OTAState>, public OTAStateListener {
public:
explicit OTAStateChangeTrigger(OTAComponent *parent) { parent->add_state_listener(this); }
explicit OTAStateChangeTrigger(OTAComponent *parent) : parent_(parent) { parent->add_state_listener(this); }
void on_ota_state(OTAState state, float progress, uint8_t error) override { this->trigger(state); }
void on_ota_state(OTAState state, float progress, uint8_t error) override {
if (!this->parent_->is_failed()) {
this->trigger(state);
}
}
protected:
OTAComponent *parent_;
};
template<OTAState State> class OTAStateTrigger final : public Trigger<>, public OTAStateListener {
public:
explicit OTAStateTrigger(OTAComponent *parent) { parent->add_state_listener(this); }
explicit OTAStateTrigger(OTAComponent *parent) : parent_(parent) { parent->add_state_listener(this); }
void on_ota_state(OTAState state, float progress, uint8_t error) override {
if (state == State) {
if (state == State && !this->parent_->is_failed()) {
this->trigger();
}
}
protected:
OTAComponent *parent_;
};
using OTAStartTrigger = OTAStateTrigger<OTA_STARTED>;
@@ -31,24 +41,30 @@ using OTAAbortTrigger = OTAStateTrigger<OTA_ABORT>;
class OTAProgressTrigger final : public Trigger<float>, public OTAStateListener {
public:
explicit OTAProgressTrigger(OTAComponent *parent) { parent->add_state_listener(this); }
explicit OTAProgressTrigger(OTAComponent *parent) : parent_(parent) { parent->add_state_listener(this); }
void on_ota_state(OTAState state, float progress, uint8_t error) override {
if (state == OTA_IN_PROGRESS) {
if (state == OTA_IN_PROGRESS && !this->parent_->is_failed()) {
this->trigger(progress);
}
}
protected:
OTAComponent *parent_;
};
class OTAErrorTrigger final : public Trigger<uint8_t>, public OTAStateListener {
public:
explicit OTAErrorTrigger(OTAComponent *parent) { parent->add_state_listener(this); }
explicit OTAErrorTrigger(OTAComponent *parent) : parent_(parent) { parent->add_state_listener(this); }
void on_ota_state(OTAState state, float progress, uint8_t error) override {
if (state == OTA_ERROR) {
if (state == OTA_ERROR && !this->parent_->is_failed()) {
this->trigger(error);
}
}
protected:
OTAComponent *parent_;
};
} // namespace ota