mirror of
https://github.com/esphome/esphome.git
synced 2024-11-23 15:38:11 +01:00
send_at return AtCommandResult
This commit is contained in:
parent
60a7ef7408
commit
307880bc05
4 changed files with 53 additions and 52 deletions
|
@ -50,47 +50,43 @@ ModemComponent::ModemComponent() {
|
|||
|
||||
void ModemComponent::enable_debug() { esp_log_level_set("command_lib", ESP_LOG_VERBOSE); }
|
||||
|
||||
std::string ModemComponent::send_at(const std::string &cmd) {
|
||||
std::string result = "";
|
||||
AtCommandResult ModemComponent::send_at(const std::string &cmd, uint32_t timeout) {
|
||||
AtCommandResult at_command_result;
|
||||
at_command_result.success = false;
|
||||
command_result status = command_result::FAIL;
|
||||
ESP_LOGV(TAG, "Sending command: %s", cmd.c_str());
|
||||
if (this->modem_ready()) {
|
||||
status = this->dce->at(cmd, result, this->command_delay_);
|
||||
ESP_LOGV(TAG, "Result for command %s: %s (status %s)", cmd.c_str(), result.c_str(),
|
||||
ESP_LOGV(TAG, "Sending command: %s", cmd.c_str());
|
||||
status = this->dce->at(cmd, at_command_result.result, timeout);
|
||||
ESP_LOGV(TAG, "Result for command %s: %s (status %s)", cmd.c_str(), at_command_result.result.c_str(),
|
||||
command_result_to_string(status).c_str());
|
||||
}
|
||||
if (status != command_result::OK) {
|
||||
result = "ERROR";
|
||||
if (status == command_result::OK) {
|
||||
at_command_result.success = true;
|
||||
}
|
||||
return result;
|
||||
return at_command_result;
|
||||
}
|
||||
|
||||
bool ModemComponent::get_imei(std::string &result) {
|
||||
// wrapper around this->dce->get_imei() that check that the result is valid
|
||||
AtCommandResult ModemComponent::send_at(const std::string &cmd) { return this->send_at(cmd, this->command_delay_); }
|
||||
|
||||
AtCommandResult ModemComponent::get_imei() {
|
||||
// get the imei, and check the result is a valid imei string
|
||||
// (so it can be used to check if the modem is responding correctly (a simple 'AT' cmd is sometime not enough))
|
||||
|
||||
bool success = false;
|
||||
if (this->dce) {
|
||||
command_result status;
|
||||
// status = this->dce->get_imei(result);
|
||||
status = this->dce->at("AT+CGSN", result, 1000);
|
||||
success = true;
|
||||
if (status == command_result::OK && result.length() == 15) {
|
||||
for (char c : result) {
|
||||
if (!isdigit(static_cast<unsigned char>(c))) {
|
||||
success = false;
|
||||
break;
|
||||
}
|
||||
AtCommandResult at_command_result;
|
||||
at_command_result.success = false;
|
||||
command_result status = command_result::FAIL;
|
||||
status = this->dce->at("AT+CGSN", at_command_result.result, 1000);
|
||||
if ((status == command_result::OK) && at_command_result.result.length() == 15) {
|
||||
at_command_result.success = true;
|
||||
for (char c : at_command_result.result) {
|
||||
if (!isdigit(static_cast<unsigned char>(c))) {
|
||||
at_command_result.success = false;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
success = false;
|
||||
}
|
||||
} else {
|
||||
at_command_result.success = false;
|
||||
}
|
||||
|
||||
if (!success) {
|
||||
result = "UNAVAILABLE";
|
||||
}
|
||||
return success;
|
||||
return at_command_result;
|
||||
}
|
||||
|
||||
bool ModemComponent::get_power_status() {
|
||||
|
@ -121,9 +117,7 @@ bool ModemComponent::modem_ready(bool force_check) {
|
|||
return false;
|
||||
#endif
|
||||
}
|
||||
std::string imei;
|
||||
// watchdog::WatchdogManager wdt(10000);
|
||||
if (this->get_imei(imei)) {
|
||||
if (this->get_imei()) {
|
||||
// we are sure that the modem is on
|
||||
this->internal_state_.powered_on = true;
|
||||
return true;
|
||||
|
@ -603,14 +597,13 @@ bool ModemComponent::prepare_sim_() {
|
|||
|
||||
void ModemComponent::send_init_at_() {
|
||||
// send initial AT commands from yaml
|
||||
// watchdog::WatchdogManager wdt(20000);
|
||||
for (const auto &cmd : this->init_at_commands_) {
|
||||
App.feed_wdt();
|
||||
std::string result = this->send_at(cmd);
|
||||
if (result == "ERROR") {
|
||||
auto at_command_result = this->send_at(cmd);
|
||||
if (!at_command_result) {
|
||||
ESP_LOGE(TAG, "Error while executing 'init_at' '%s' command", cmd.c_str());
|
||||
} else {
|
||||
ESP_LOGI(TAG, "'init_at' '%s' result: %s", cmd.c_str(), result.c_str());
|
||||
ESP_LOGI(TAG, "'init_at' '%s' result: %s", cmd.c_str(), at_command_result.result.c_str());
|
||||
}
|
||||
delay(200); // NOLINT
|
||||
}
|
||||
|
|
|
@ -42,6 +42,14 @@ enum class ModemPowerState {
|
|||
TOFFUART,
|
||||
};
|
||||
|
||||
struct AtCommandResult {
|
||||
std::string result;
|
||||
bool success;
|
||||
|
||||
// Conversion to bool, allowing you to do things like `if (commandResult) {...}`
|
||||
operator bool() const { return success; }
|
||||
};
|
||||
|
||||
class ModemComponent : public Component {
|
||||
public:
|
||||
void set_use_address(const std::string &use_address) { this->use_address_ = use_address; }
|
||||
|
@ -58,16 +66,15 @@ class ModemComponent : public Component {
|
|||
void set_password(const std::string &password) { this->password_ = password; }
|
||||
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_gnss_power_command(const std::string &at_command) { this->gnss_power_command_ = at_command; }
|
||||
// std::string get_gnss_power_command() { return this->gnss_power_command_; }
|
||||
void set_not_responding_cb(Trigger<> *not_responding_cb) { this->not_responding_cb_ = not_responding_cb; }
|
||||
void enable_cmux() { this->cmux_ = true; }
|
||||
void enable_debug();
|
||||
void add_init_at_command(const std::string &cmd) { this->init_at_commands_.push_back(cmd); }
|
||||
bool is_connected() { return this->component_state_ == ModemComponentState::CONNECTED; }
|
||||
bool is_disabled() { return this->component_state_ == ModemComponentState::DISABLED; }
|
||||
std::string send_at(const std::string &cmd);
|
||||
bool get_imei(std::string &result);
|
||||
AtCommandResult send_at(const std::string &cmd, uint32_t timeout);
|
||||
AtCommandResult send_at(const std::string &cmd);
|
||||
AtCommandResult get_imei();
|
||||
bool get_power_status();
|
||||
bool modem_ready();
|
||||
bool modem_ready(bool force_check);
|
||||
|
|
|
@ -153,9 +153,9 @@ std::map<std::string, std::string> get_gnssinfo_tokens(const std::string &gnss_i
|
|||
|
||||
void ModemSensor::update_gnss_sensors_() {
|
||||
if (this->gnss_latitude_sensor_ || this->gnss_longitude_sensor_ || this->gnss_altitude_sensor_) {
|
||||
std::string gnss_info = modem::global_modem_component->send_at("AT+CGNSSINFO");
|
||||
|
||||
if (gnss_info != "ERROR") {
|
||||
auto at_command_result = modem::global_modem_component->send_at("AT+CGNSSINFO");
|
||||
if (at_command_result) {
|
||||
std::string gnss_info = at_command_result.result;
|
||||
std::map<std::string, std::string> parts = get_gnssinfo_tokens(gnss_info, "SIM7600");
|
||||
|
||||
float lat = NAN;
|
||||
|
@ -220,6 +220,7 @@ void ModemSensor::update_gnss_sensors_() {
|
|||
ESP_LOGV(TAG, "Time: %02d:%02d:%02d", hour, minute, second);
|
||||
|
||||
// Sensors update
|
||||
App.feed_wdt();
|
||||
if (this->gnss_latitude_sensor_)
|
||||
this->gnss_latitude_sensor_->publish_state(lat);
|
||||
if (this->gnss_longitude_sensor_)
|
||||
|
|
|
@ -33,8 +33,9 @@ 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_ + "?");
|
||||
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 delimiter = ": ";
|
||||
std::size_t pos = modem_state.find(delimiter);
|
||||
if (pos != std::string::npos) {
|
||||
|
@ -54,12 +55,11 @@ 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 (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"));
|
||||
if (!this->modem_state_.has_value()) {
|
||||
this->modem_state_ = this->get_modem_gnss_state();
|
||||
} else {
|
||||
if ((this->state != this->modem_state_)) {
|
||||
if (global_modem_component->send_at(this->command_ + (this->state ? "=1" : "=0"))) {
|
||||
this->modem_state_ = this->state;
|
||||
this->publish_state(this->state);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue