read modem gnss state

This commit is contained in:
oarcher 2024-08-12 14:55:37 +02:00
parent e1af5bc767
commit 60a7ef7408
3 changed files with 32 additions and 7 deletions

View file

@ -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;

View file

@ -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<bool> 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<bool> modem_state_;
};
} // namespace modem

View file

@ -31,15 +31,39 @@ using namespace esp_modem;
static const char *const TAG = "modem.switch";
optional<bool> GnssSwitch::get_modem_gnss_state() {
optional<bool> 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);
}
}
}
}