mirror of
https://github.com/esphome/esphome.git
synced 2024-12-24 14:34:54 +01:00
read gnns state
This commit is contained in:
parent
352111b556
commit
6cbd2af6b4
1 changed files with 18 additions and 5 deletions
|
@ -35,15 +35,17 @@ optional<bool> GnssSwitch::get_modem_gnss_state() {
|
|||
optional<bool> gnss_state = nullopt;
|
||||
auto at_command_result = global_modem_component->send_at(this->command_ + "?");
|
||||
if (at_command_result) {
|
||||
std::string modem_state = at_command_result.result;
|
||||
std::string modem_state = at_command_result.output;
|
||||
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;
|
||||
gnss_state = true;
|
||||
} 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::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()) {
|
||||
this->modem_state_ = this->get_modem_gnss_state();
|
||||
next_loop_millis = millis() + 5000; // soft delay
|
||||
} 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"))) {
|
||||
this->modem_state_ = this->state;
|
||||
this->modem_state_ = nullopt;
|
||||
this->publish_state(this->state);
|
||||
next_loop_millis = millis() + 5000; // soft delay
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue