read gnns state

This commit is contained in:
oarcher 2024-08-16 17:43:26 +02:00
parent 352111b556
commit 6cbd2af6b4

View file

@ -35,15 +35,17 @@ optional<bool> GnssSwitch::get_modem_gnss_state() {
optional<bool> gnss_state = nullopt; optional<bool> gnss_state = nullopt;
auto at_command_result = global_modem_component->send_at(this->command_ + "?"); auto at_command_result = global_modem_component->send_at(this->command_ + "?");
if (at_command_result) { if (at_command_result) {
std::string modem_state = at_command_result.result; std::string modem_state = at_command_result.output;
std::string delimiter = ": "; std::string delimiter = ": ";
std::size_t pos = modem_state.find(delimiter); std::size_t pos = modem_state.find(delimiter);
if (pos != std::string::npos) { if (pos != std::string::npos) {
pos += delimiter.length(); pos += delimiter.length();
if (modem_state[pos] == '1') { if (modem_state[pos] == '1') {
return true; gnss_state = true;
} else if (modem_state[pos] == '0') { } else if (modem_state[pos] == '0') {
return false; gnss_state = false;
if (!gnss_state.value()) {
}
} }
} }
} }
@ -55,13 +57,24 @@ 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::setup() { this->state = this->get_initial_state_with_restore_mode().value_or(false); }
void GnssSwitch::loop() { void GnssSwitch::loop() {
static uint32_t next_loop_millis = millis();
if ((millis() < next_loop_millis)) {
// some commands need some delay
yield();
return;
}
if (!this->modem_state_.has_value()) { if (!this->modem_state_.has_value()) {
this->modem_state_ = this->get_modem_gnss_state(); this->modem_state_ = this->get_modem_gnss_state();
next_loop_millis = millis() + 5000; // soft delay
} else { } else {
if ((this->state != this->modem_state_)) { if ((this->state != this->modem_state_.value())) {
ESP_LOGI(TAG, "gnss switch state: %d, modem state: %d", this->state, this->modem_state_.value());
if (global_modem_component->send_at(this->command_ + (this->state ? "=1" : "=0"))) { if (global_modem_component->send_at(this->command_ + (this->state ? "=1" : "=0"))) {
this->modem_state_ = this->state; this->modem_state_ = nullopt;
this->publish_state(this->state); this->publish_state(this->state);
next_loop_millis = millis() + 5000; // soft delay
} }
} }
} }