Improve meter ingestion resilience under UART gaps

This commit is contained in:
2026-02-13 22:48:48 +01:00
parent 32358c3351
commit e83bc11dea
2 changed files with 30 additions and 12 deletions

View File

@@ -369,14 +369,18 @@ static void reset_build_counters() {
g_build_invalid = 0; g_build_invalid = 0;
} }
static bool append_meter_sample(const MeterData &data, bool meter_ok) { static bool append_meter_sample(const MeterData &data, bool meter_ok, bool has_snapshot) {
if (!meter_ok) { if (!has_snapshot) {
g_build_invalid++; g_build_invalid++;
return false; return false;
} }
g_last_sample_ts_utc = data.ts_utc; g_last_sample_ts_utc = data.ts_utc;
g_build_samples[g_build_count++] = data; g_build_samples[g_build_count++] = data;
if (meter_ok) {
g_build_valid++; g_build_valid++;
} else {
g_build_invalid++;
}
if (g_build_count >= METER_BATCH_MAX_SAMPLES) { if (g_build_count >= METER_BATCH_MAX_SAMPLES) {
batch_queue_enqueue(g_build_samples, g_build_count); batch_queue_enqueue(g_build_samples, g_build_count);
g_build_count = 0; g_build_count = 0;
@@ -1035,8 +1039,9 @@ static void sender_loop() {
g_build_attempts++; g_build_attempts++;
uint32_t meter_age_ms = g_last_meter_valid ? (now_ms - g_last_meter_rx_ms) : UINT32_MAX; uint32_t meter_age_ms = g_last_meter_valid ? (now_ms - g_last_meter_rx_ms) : UINT32_MAX;
// Reuse recent good samples to bridge short parser gaps without accepting stale data forever. // Reuse recent good samples to bridge short parser gaps without accepting stale data forever.
bool meter_ok = g_last_meter_valid && meter_age_ms <= METER_SAMPLE_MAX_AGE_MS; bool has_snapshot = g_last_meter_valid;
if (meter_ok) { bool meter_ok = has_snapshot && meter_age_ms <= METER_SAMPLE_MAX_AGE_MS;
if (has_snapshot) {
data.energy_total_kwh = g_last_meter_data.energy_total_kwh; data.energy_total_kwh = g_last_meter_data.energy_total_kwh;
data.total_power_w = g_last_meter_data.total_power_w; data.total_power_w = g_last_meter_data.total_power_w;
data.phase_power_w[0] = g_last_meter_data.phase_power_w[0]; data.phase_power_w[0] = g_last_meter_data.phase_power_w[0];
@@ -1064,9 +1069,9 @@ static void sender_loop() {
} }
} }
data.ts_utc = sample_ts_utc; data.ts_utc = sample_ts_utc;
data.valid = meter_ok; data.valid = has_snapshot;
bool appended = append_meter_sample(data, meter_ok); bool appended = append_meter_sample(data, meter_ok, has_snapshot);
if (SERIAL_DEBUG_MODE) { if (SERIAL_DEBUG_MODE) {
serial_debug_printf("sample: i=%lu ok=%u appended=%u e_kwh=%.3f p1=%.1f p2=%.1f p3=%.1f ms=%lu", serial_debug_printf("sample: i=%lu ok=%u appended=%u e_kwh=%.3f p1=%.1f p2=%.1f p3=%.1f ms=%lu",
static_cast<unsigned long>(g_build_attempts), static_cast<unsigned long>(g_build_attempts),
@@ -1233,8 +1238,9 @@ static void sender_loop() {
} }
if (!g_batch_ack_pending && next_due > now_ms) { if (!g_batch_ack_pending && next_due > now_ms) {
watchdog_kick(); watchdog_kick();
uint32_t idle_ms = next_due - now_ms;
if (SERIAL_DEBUG_MODE) { if (SERIAL_DEBUG_MODE) {
g_sender_sleep_ms += (next_due - now_ms); g_sender_sleep_ms += idle_ms;
if (now_ms - g_sender_power_log_ms >= 10000) { if (now_ms - g_sender_power_log_ms >= 10000) {
g_sender_power_log_ms = now_ms; g_sender_power_log_ms = now_ms;
serial_debug_printf("power: rx_ms=%lu sleep_ms=%lu", static_cast<unsigned long>(g_sender_rx_window_ms), serial_debug_printf("power: rx_ms=%lu sleep_ms=%lu", static_cast<unsigned long>(g_sender_rx_window_ms),
@@ -1242,7 +1248,12 @@ static void sender_loop() {
} }
} }
lora_sleep(); lora_sleep();
light_sleep_ms(next_due - now_ms); if (g_time_acquired) {
// Keep the meter reader task running while metering is active.
delay(idle_ms);
} else {
light_sleep_ms(idle_ms);
}
} }
} }

View File

@@ -4,9 +4,9 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
// LoRa TX/RX windows can block the main loop for several seconds at SF12. // Dedicated reader task pumps UART continuously; keep timeout short so parser can
// Keep partial frame state long enough so valid telegrams are not dropped. // recover quickly from broken frames.
static constexpr uint32_t METER_FRAME_TIMEOUT_MS = 20000; static constexpr uint32_t METER_FRAME_TIMEOUT_MS = 3000;
static constexpr size_t METER_FRAME_MAX = 512; static constexpr size_t METER_FRAME_MAX = 512;
enum class MeterRxState : uint8_t { enum class MeterRxState : uint8_t {
@@ -142,6 +142,13 @@ bool meter_poll_frame(const char *&frame, size_t &len) {
continue; continue;
} }
// Fast resync if a new telegram starts before current frame completed.
if (c == '/') {
g_frame_len = 0;
g_frame_buf[g_frame_len++] = c;
continue;
}
if (g_frame_len + 1 >= sizeof(g_frame_buf)) { if (g_frame_len + 1 >= sizeof(g_frame_buf)) {
g_rx_overflow++; g_rx_overflow++;
g_rx_state = MeterRxState::WaitStart; g_rx_state = MeterRxState::WaitStart;