receiver: store CSVs by local date and keep UTC history fallback
This commit is contained in:
@@ -27,15 +27,15 @@ static bool ensure_dir(const String &path) {
|
|||||||
return SD.mkdir(path);
|
return SD.mkdir(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
static String format_date_utc(uint32_t ts_utc) {
|
static String format_date_local(uint32_t ts_utc) {
|
||||||
time_t t = static_cast<time_t>(ts_utc);
|
time_t t = static_cast<time_t>(ts_utc);
|
||||||
struct tm tm_utc;
|
struct tm tm_local;
|
||||||
gmtime_r(&t, &tm_utc);
|
localtime_r(&t, &tm_local);
|
||||||
char buf[16];
|
char buf[16];
|
||||||
snprintf(buf, sizeof(buf), "%04d-%02d-%02d",
|
snprintf(buf, sizeof(buf), "%04d-%02d-%02d",
|
||||||
tm_utc.tm_year + 1900,
|
tm_local.tm_year + 1900,
|
||||||
tm_utc.tm_mon + 1,
|
tm_local.tm_mon + 1,
|
||||||
tm_utc.tm_mday);
|
tm_local.tm_mday);
|
||||||
return String(buf);
|
return String(buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -94,7 +94,7 @@ void sd_logger_log_sample(const MeterData &data, bool include_error_text) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
String filename = sender_dir + "/" + format_date_utc(data.ts_utc) + ".csv";
|
String filename = sender_dir + "/" + format_date_local(data.ts_utc) + ".csv";
|
||||||
bool new_file = !SD.exists(filename);
|
bool new_file = !SD.exists(filename);
|
||||||
File f = SD.open(filename, FILE_APPEND);
|
File f = SD.open(filename, FILE_APPEND);
|
||||||
if (!f) {
|
if (!f) {
|
||||||
|
|||||||
@@ -243,7 +243,16 @@ static void history_reset() {
|
|||||||
g_history = {};
|
g_history = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
static String history_date_from_epoch(uint32_t ts_utc) {
|
static String history_date_from_epoch_local(uint32_t ts_utc) {
|
||||||
|
time_t t = static_cast<time_t>(ts_utc);
|
||||||
|
struct tm tm_local;
|
||||||
|
localtime_r(&t, &tm_local);
|
||||||
|
char buf[16];
|
||||||
|
snprintf(buf, sizeof(buf), "%04d-%02d-%02d", tm_local.tm_year + 1900, tm_local.tm_mon + 1, tm_local.tm_mday);
|
||||||
|
return String(buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
static String history_date_from_epoch_utc(uint32_t ts_utc) {
|
||||||
time_t t = static_cast<time_t>(ts_utc);
|
time_t t = static_cast<time_t>(ts_utc);
|
||||||
struct tm tm_utc;
|
struct tm tm_utc;
|
||||||
gmtime_r(&t, &tm_utc);
|
gmtime_r(&t, &tm_utc);
|
||||||
@@ -298,8 +307,17 @@ static bool history_open_next_file() {
|
|||||||
g_history.done = true;
|
g_history.done = true;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
String path = String("/dd3/") + g_history.device_id + "/" + history_date_from_epoch(day_ts) + ".csv";
|
String local_date = history_date_from_epoch_local(day_ts);
|
||||||
|
String path = String("/dd3/") + g_history.device_id + "/" + local_date + ".csv";
|
||||||
g_history.file = SD.open(path.c_str(), FILE_READ);
|
g_history.file = SD.open(path.c_str(), FILE_READ);
|
||||||
|
if (!g_history.file) {
|
||||||
|
// Compatibility fallback for files written before local-date partitioning.
|
||||||
|
String utc_date = history_date_from_epoch_utc(day_ts);
|
||||||
|
if (utc_date != local_date) {
|
||||||
|
String legacy_path = String("/dd3/") + g_history.device_id + "/" + utc_date + ".csv";
|
||||||
|
g_history.file = SD.open(legacy_path.c_str(), FILE_READ);
|
||||||
|
}
|
||||||
|
}
|
||||||
g_history.day_index++;
|
g_history.day_index++;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user