From 60a7ef740865ec2d0043209cf3718f0e839a7cab Mon Sep 17 00:00:00 2001 From: oarcher Date: Mon, 12 Aug 2024 14:55:37 +0200 Subject: [PATCH] read modem gnss state --- esphome/components/modem/modem_component.cpp | 4 +-- esphome/components/modem/switch/gnns_switch.h | 3 +- .../components/modem/switch/gnss_switch.cpp | 32 ++++++++++++++++--- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/esphome/components/modem/modem_component.cpp b/esphome/components/modem/modem_component.cpp index 0dcc75836b..5e82527c0b 100644 --- a/esphome/components/modem/modem_component.cpp +++ b/esphome/components/modem/modem_component.cpp @@ -73,7 +73,7 @@ bool ModemComponent::get_imei(std::string &result) { if (this->dce) { command_result status; // status = this->dce->get_imei(result); - status = this->dce->at("AT+CGSN", result, 3000); + status = this->dce->at("AT+CGSN", result, 1000); success = true; if (status == command_result::OK && result.length() == 15) { for (char c : result) { @@ -122,7 +122,7 @@ bool ModemComponent::modem_ready(bool force_check) { #endif } std::string imei; - watchdog::WatchdogManager wdt(10000); + // watchdog::WatchdogManager wdt(10000); if (this->get_imei(imei)) { // we are sure that the modem is on this->internal_state_.powered_on = true; diff --git a/esphome/components/modem/switch/gnns_switch.h b/esphome/components/modem/switch/gnns_switch.h index 37fcd54daf..ee1ce0eef0 100644 --- a/esphome/components/modem/switch/gnns_switch.h +++ b/esphome/components/modem/switch/gnns_switch.h @@ -16,6 +16,7 @@ namespace modem { class GnssSwitch : public switch_::Switch, public Component { public: void set_command(const std::string &command) { this->command_ = command; } + optional get_modem_gnss_state(); // ========== INTERNAL METHODS ========== // (In most use cases you won't need these) @@ -26,7 +27,7 @@ class GnssSwitch : public switch_::Switch, public Component { protected: std::string command_; void write_state(bool state) override; - bool modem_state_{false}; + optional modem_state_; }; } // namespace modem diff --git a/esphome/components/modem/switch/gnss_switch.cpp b/esphome/components/modem/switch/gnss_switch.cpp index 74359650d4..cc98da4165 100644 --- a/esphome/components/modem/switch/gnss_switch.cpp +++ b/esphome/components/modem/switch/gnss_switch.cpp @@ -31,15 +31,39 @@ using namespace esp_modem; static const char *const TAG = "modem.switch"; +optional GnssSwitch::get_modem_gnss_state() { + optional gnss_state; + if (global_modem_component->modem_ready()) { + std::string modem_state = global_modem_component->send_at(this->command_ + "?"); + std::string delimiter = ": "; + std::size_t pos = modem_state.find(delimiter); + if (pos != std::string::npos) { + pos += delimiter.length(); + if (modem_state[pos] == '1') { + return true; + } else if (modem_state[pos] == '0') { + return false; + } + } + } + return gnss_state; +} + void GnssSwitch::dump_config() { LOG_SWITCH("", "Modem GNSS Switch", this); } void GnssSwitch::setup() { this->state = this->get_initial_state_with_restore_mode().value_or(false); } void GnssSwitch::loop() { - if ((this->state != this->modem_state_) && global_modem_component->modem_ready()) { - global_modem_component->send_at(this->command_ + (this->state ? "=1" : "=0")); - this->modem_state_ = this->state; - this->publish_state(this->modem_state_); + if (global_modem_component->modem_ready()) { + if (!this->modem_state_.has_value()) { + this->modem_state_ = this->get_modem_gnss_state(); + } else { + if ((this->state != this->modem_state_)) { + global_modem_component->send_at(this->command_ + (this->state ? "=1" : "=0")); + this->modem_state_ = this->state; + this->publish_state(this->state); + } + } } }