|
| 1 | +#include "gpstime.h" |
| 2 | +#include "esphome/core/log.h" |
| 3 | + |
| 4 | +namespace esphome { |
| 5 | +namespace gpstime { |
| 6 | + |
| 7 | +static const char *TAG = "GPS.time"; |
| 8 | + |
| 9 | +void GPSTime::loop() { |
| 10 | + while (this->available()) { |
| 11 | + uint8_t c; |
| 12 | + this->read_byte(&c); |
| 13 | + if (this->have_time_) |
| 14 | + continue; |
| 15 | + if (c == '$') { |
| 16 | + this->receiving_ = true; |
| 17 | + this->ending_ = false; |
| 18 | + this->rx_buffer_.clear(); |
| 19 | + continue; |
| 20 | + } |
| 21 | + if (!this->receiving_) |
| 22 | + continue; |
| 23 | + if (c == '\r') { |
| 24 | + if (this->ending_) { |
| 25 | + ESP_LOGW(TAG, "unexpected return"); |
| 26 | + this->receiving_ = false; |
| 27 | + } else { |
| 28 | + this->ending_ = true; |
| 29 | + } |
| 30 | + continue; |
| 31 | + } |
| 32 | + if (c == '\n') { |
| 33 | + this->receiving_ = false; |
| 34 | + if (!this->ending_) { |
| 35 | + ESP_LOGW(TAG, "unexpected newline"); |
| 36 | + continue; |
| 37 | + } |
| 38 | + this->handle_message_(); |
| 39 | + continue; |
| 40 | + } |
| 41 | + if (this->ending_) { |
| 42 | + ESP_LOGW(TAG, "unexpected character"); |
| 43 | + this->receiving_ = false; |
| 44 | + continue; |
| 45 | + } |
| 46 | + if (this->rx_buffer_.size() < 90) |
| 47 | + this->rx_buffer_.push_back(c); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +void GPSTime::handle_message_() { |
| 52 | + std::vector<std::string> terms; |
| 53 | + std::string data(this->rx_buffer_.begin(), this->rx_buffer_.end()); |
| 54 | + ESP_LOGD(TAG, "received: %s", data.c_str()); |
| 55 | + int start = 0; |
| 56 | + int end; |
| 57 | + int csum_pos = data.find('*'); |
| 58 | + if (csum_pos != std::string::npos) { |
| 59 | + if (data.size() - csum_pos != 3) { |
| 60 | + ESP_LOGE(TAG, "invalid checksum data"); |
| 61 | + return; |
| 62 | + } |
| 63 | + uint8_t csum = 0; |
| 64 | + parse_hex(data.c_str() + csum_pos + 1, &csum, 1); |
| 65 | + data.resize(csum_pos); |
| 66 | + uint8_t ccsum = 0; |
| 67 | + for (auto c : data) |
| 68 | + ccsum ^= c; |
| 69 | + if (csum != ccsum) { |
| 70 | + ESP_LOGE(TAG, "checksum failed: %02x != %02x", csum, ccsum); |
| 71 | + return; |
| 72 | + } |
| 73 | + } |
| 74 | + do { |
| 75 | + end = data.find(',', start); |
| 76 | + int len = end != std::string::npos ? end - start : end; |
| 77 | + terms.push_back(data.substr(start, len)); |
| 78 | + start = end + 1; |
| 79 | + } while (end != std::string::npos); |
| 80 | + ESP_LOGD(TAG, "message: %s", terms[0].c_str()); |
| 81 | + if (terms[0] != "GPRMC") |
| 82 | + return; |
| 83 | + ESP_LOGD(TAG, "time: '%s', date: '%s'", terms[1].c_str(), terms[9].c_str()); |
| 84 | + if ((terms[1].size() < 6) || (terms[9].size() < 6)) |
| 85 | + return; |
| 86 | + uint32_t timeinfo = int(parse_number<float>(terms[1]).value()); |
| 87 | + uint32_t dateinfo = int(parse_number<uint32_t>(terms[9]).value()); |
| 88 | + ESP_LOGD(TAG, "processing %d %d", timeinfo, dateinfo); |
| 89 | + ESPTime now{}; |
| 90 | + now.year = dateinfo % 100 + 2000; |
| 91 | + now.month = dateinfo / 100 % 100; |
| 92 | + now.day_of_month = dateinfo / 10000; |
| 93 | + // Set these to valid value for recalc_timestamp_utc - it's not used for calculation |
| 94 | + now.day_of_week = 1; |
| 95 | + now.day_of_year = 1; |
| 96 | + |
| 97 | + now.hour = timeinfo / 10000; |
| 98 | + now.minute = timeinfo / 100 % 100; |
| 99 | + now.second = timeinfo % 100; |
| 100 | + now.recalc_timestamp_utc(false); |
| 101 | + this->synchronize_epoch_(now.timestamp); |
| 102 | + this->have_time_ = true; |
| 103 | +} |
| 104 | + |
| 105 | +void GPSTime::update() { this->have_time_ = false; } |
| 106 | + |
| 107 | +void GPSTime::dump_config() { ESP_LOGCONFIG("", "GPS time", this); } |
| 108 | + |
| 109 | +} // namespace gpstime |
| 110 | +} // namespace esphome |
0 commit comments