on_not_responding nw state cb

This commit is contained in:
oarcher 2024-08-13 17:24:31 +02:00
parent 3a289c8284
commit 00f54725e3
3 changed files with 11 additions and 13 deletions

View file

@ -10,11 +10,13 @@ namespace esphome {
namespace modem { namespace modem {
class ModemOnNotRespondingTrigger : public Trigger<> { class ModemOnNotRespondingTrigger : public Trigger<> {
// not managed by `add_on_state_callback`, because we want to execute the callback
// as a single mode script (we have to know when the callback has ended)
public: public:
explicit ModemOnNotRespondingTrigger(ModemComponent *parent) { explicit ModemOnNotRespondingTrigger(ModemComponent *parent) {
parent->set_not_responding_cb(static_cast<Trigger<> *>(this)); parent->add_on_state_callback([this, parent](ModemComponentState old_state, ModemComponentState state) {
if (!parent->is_failed() && state == ModemComponentState::NOT_RESPONDING) {
this->trigger();
}
});
} }
}; };

View file

@ -297,16 +297,13 @@ void ModemComponent::loop() {
this->status_clear_warning(); this->status_clear_warning();
this->component_state_ = ModemComponentState::DISCONNECTED; this->component_state_ = ModemComponentState::DISCONNECTED;
} else { } else {
if (!this->internal_state_.powered_on) { this->modem_lazy_init_();
this->poweron_(); if (!this->modem_sync_()) {
} else if (this->not_responding_cb_) { ESP_LOGE(TAG, "Unable to recover modem");
if (!this->not_responding_cb_->is_action_running()) {
ESP_LOGD(TAG, "Calling 'on_not_responding' callback");
this->not_responding_cb_->trigger();
}
} else {
ESP_LOGW(TAG, "Modem not responding, and no 'on_not_responding' action defined");
} }
// if (!this->internal_state_.powered_on) {
// this->poweron_();
// }
} }
} }
break; break;

View file

@ -64,7 +64,6 @@ class ModemComponent : public Component {
void set_status_pin(GPIOPin *status_pin) { this->status_pin_ = status_pin; } void set_status_pin(GPIOPin *status_pin) { this->status_pin_ = status_pin; }
void set_pin_code(const std::string &pin_code) { this->pin_code_ = pin_code; } void set_pin_code(const std::string &pin_code) { this->pin_code_ = pin_code; }
void set_apn(const std::string &apn) { this->apn_ = apn; } void set_apn(const std::string &apn) { this->apn_ = apn; }
void set_not_responding_cb(Trigger<> *not_responding_cb) { this->not_responding_cb_ = not_responding_cb; }
void enable_cmux() { this->cmux_ = true; } void enable_cmux() { this->cmux_ = true; }
void enable_debug(); void enable_debug();
void add_init_at_command(const std::string &cmd) { this->init_at_commands_.push_back(cmd); } void add_init_at_command(const std::string &cmd) { this->init_at_commands_.push_back(cmd); }