Enhance timestamp parsing and arrival data handling in MainUIHandler
- Introduced a new helper function to parse ISO 8601-like timestamps into epoch seconds. - Improved the handling of timezones, allowing for better accuracy in arrival time calculations. - Refactored arrival data fetching to ensure UI updates only when data changes. - Enhanced error handling for arrival data retrieval, providing clearer messages for various error states. - Updated formatting functions for arrival times to handle both relative and absolute formats more robustly.
This commit is contained in:
@@ -4,6 +4,7 @@
|
|||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
|
|
||||||
static const char* TAG = "TravelMainUI";
|
static const char* TAG = "TravelMainUI";
|
||||||
|
#define LVGL_PORT_LOCK_TIMEOUT_MS 6000
|
||||||
|
|
||||||
namespace travel {
|
namespace travel {
|
||||||
|
|
||||||
@@ -21,7 +22,7 @@ esp_err_t MainUI::init(lv_obj_t* parent) {
|
|||||||
|
|
||||||
parent_ = parent;
|
parent_ = parent;
|
||||||
|
|
||||||
if (!lvgl_port_lock(pdMS_TO_TICKS(1000))) {
|
if (!lvgl_port_lock(pdMS_TO_TICKS(LVGL_PORT_LOCK_TIMEOUT_MS))) {
|
||||||
ESP_LOGE(TAG, "Failed to acquire LVGL lock");
|
ESP_LOGE(TAG, "Failed to acquire LVGL lock");
|
||||||
return ESP_ERR_TIMEOUT;
|
return ESP_ERR_TIMEOUT;
|
||||||
}
|
}
|
||||||
@@ -53,7 +54,7 @@ esp_err_t MainUI::init(lv_obj_t* parent) {
|
|||||||
lv_obj_set_width(refresh_time_label_, LV_PCT(100));
|
lv_obj_set_width(refresh_time_label_, LV_PCT(100));
|
||||||
lv_label_set_text(refresh_time_label_, "");
|
lv_label_set_text(refresh_time_label_, "");
|
||||||
lv_obj_set_style_text_font(refresh_time_label_, ¬o_sans_tc_14, 0);
|
lv_obj_set_style_text_font(refresh_time_label_, ¬o_sans_tc_14, 0);
|
||||||
lv_obj_set_style_text_color(refresh_time_label_, lv_color_hex(0x808080), 0);
|
lv_obj_set_style_text_color(refresh_time_label_, lv_color_black(), 0);
|
||||||
|
|
||||||
lvgl_port_unlock();
|
lvgl_port_unlock();
|
||||||
|
|
||||||
@@ -61,7 +62,7 @@ esp_err_t MainUI::init(lv_obj_t* parent) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t MainUI::deinit() {
|
esp_err_t MainUI::deinit() {
|
||||||
if (!lvgl_port_lock(pdMS_TO_TICKS(1000))) {
|
if (!lvgl_port_lock(pdMS_TO_TICKS(LVGL_PORT_LOCK_TIMEOUT_MS))) {
|
||||||
ESP_LOGE(TAG, "Failed to acquire LVGL lock");
|
ESP_LOGE(TAG, "Failed to acquire LVGL lock");
|
||||||
return ESP_ERR_TIMEOUT;
|
return ESP_ERR_TIMEOUT;
|
||||||
}
|
}
|
||||||
@@ -101,7 +102,7 @@ void MainUI::create_header_() {
|
|||||||
lv_obj_set_style_border_width(header, 0, 0);
|
lv_obj_set_style_border_width(header, 0, 0);
|
||||||
lv_obj_set_style_border_width(header, 1, 0);
|
lv_obj_set_style_border_width(header, 1, 0);
|
||||||
lv_obj_set_style_border_side(header, LV_BORDER_SIDE_BOTTOM, 0);
|
lv_obj_set_style_border_side(header, LV_BORDER_SIDE_BOTTOM, 0);
|
||||||
lv_obj_set_style_border_color(header, lv_color_hex(0x808080), 0);
|
lv_obj_set_style_border_color(header, lv_color_black(), 0);
|
||||||
lv_obj_set_style_anim_time(header, 0, 0);
|
lv_obj_set_style_anim_time(header, 0, 0);
|
||||||
lv_obj_clear_flag(header, LV_OBJ_FLAG_SCROLLABLE);
|
lv_obj_clear_flag(header, LV_OBJ_FLAG_SCROLLABLE);
|
||||||
|
|
||||||
@@ -132,7 +133,7 @@ void MainUI::create_route_displays_() {
|
|||||||
lv_obj_set_style_border_width(display.container, 0, 0);
|
lv_obj_set_style_border_width(display.container, 0, 0);
|
||||||
lv_obj_set_style_border_width(display.container, 1, 0);
|
lv_obj_set_style_border_width(display.container, 1, 0);
|
||||||
lv_obj_set_style_border_side(display.container, LV_BORDER_SIDE_BOTTOM, 0);
|
lv_obj_set_style_border_side(display.container, LV_BORDER_SIDE_BOTTOM, 0);
|
||||||
lv_obj_set_style_border_color(display.container, lv_color_hex(0xC0C0C0), 0);
|
lv_obj_set_style_border_color(display.container, lv_color_black(), 0);
|
||||||
lv_obj_set_style_anim_time(display.container, 0, 0);
|
lv_obj_set_style_anim_time(display.container, 0, 0);
|
||||||
lv_obj_clear_flag(display.container, LV_OBJ_FLAG_SCROLLABLE);
|
lv_obj_clear_flag(display.container, LV_OBJ_FLAG_SCROLLABLE);
|
||||||
lv_obj_add_flag(display.container, LV_OBJ_FLAG_HIDDEN); // Hidden by default
|
lv_obj_add_flag(display.container, LV_OBJ_FLAG_HIDDEN); // Hidden by default
|
||||||
@@ -155,7 +156,7 @@ void MainUI::create_route_displays_() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void MainUI::update_arrivals(const std::vector<RouteArrivalData>& arrival_data) {
|
void MainUI::update_arrivals(const std::vector<RouteArrivalData>& arrival_data) {
|
||||||
if (!lvgl_port_lock(pdMS_TO_TICKS(1000))) {
|
if (!lvgl_port_lock(pdMS_TO_TICKS(LVGL_PORT_LOCK_TIMEOUT_MS))) {
|
||||||
ESP_LOGE(TAG, "Failed to acquire LVGL lock");
|
ESP_LOGE(TAG, "Failed to acquire LVGL lock");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -249,7 +250,7 @@ void MainUI::update_route_display_(RouteDisplay& display, const RouteArrivalData
|
|||||||
}
|
}
|
||||||
|
|
||||||
void MainUI::update_last_refresh_time(const std::string& time_str) {
|
void MainUI::update_last_refresh_time(const std::string& time_str) {
|
||||||
if (!lvgl_port_lock(pdMS_TO_TICKS(1000))) {
|
if (!lvgl_port_lock(pdMS_TO_TICKS(LVGL_PORT_LOCK_TIMEOUT_MS))) {
|
||||||
ESP_LOGE(TAG, "Failed to acquire LVGL lock");
|
ESP_LOGE(TAG, "Failed to acquire LVGL lock");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -264,7 +265,7 @@ void MainUI::update_last_refresh_time(const std::string& time_str) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void MainUI::show_no_routes_message() {
|
void MainUI::show_no_routes_message() {
|
||||||
if (!lvgl_port_lock(pdMS_TO_TICKS(1000))) {
|
if (!lvgl_port_lock(pdMS_TO_TICKS(LVGL_PORT_LOCK_TIMEOUT_MS))) {
|
||||||
ESP_LOGE(TAG, "Failed to acquire LVGL lock");
|
ESP_LOGE(TAG, "Failed to acquire LVGL lock");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -282,7 +283,7 @@ void MainUI::show_no_routes_message() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void MainUI::show_error_message(const std::string& message) {
|
void MainUI::show_error_message(const std::string& message) {
|
||||||
if (!lvgl_port_lock(pdMS_TO_TICKS(1000))) {
|
if (!lvgl_port_lock(pdMS_TO_TICKS(LVGL_PORT_LOCK_TIMEOUT_MS))) {
|
||||||
ESP_LOGE(TAG, "Failed to acquire LVGL lock");
|
ESP_LOGE(TAG, "Failed to acquire LVGL lock");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,11 +5,141 @@
|
|||||||
#include <ctime>
|
#include <ctime>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <cctype>
|
||||||
|
|
||||||
static const char* TAG = "TravelMainHandler";
|
static const char* TAG = "TravelMainHandler";
|
||||||
|
|
||||||
namespace travel {
|
namespace travel {
|
||||||
|
|
||||||
|
// Helper functions shared by formatting routines
|
||||||
|
namespace {
|
||||||
|
// Parse an ISO 8601-like timestamp into epoch seconds (UTC).
|
||||||
|
// Supports formats like: 2024-01-15T14:30:00+08:00, 2026-02-03 23:08:22, or ...Z
|
||||||
|
bool parse_iso_to_epoch(const std::string& s, time_t& out_epoch) {
|
||||||
|
auto trim_copy = [](const std::string& in) {
|
||||||
|
size_t a = 0;
|
||||||
|
size_t b = in.size();
|
||||||
|
while (a < b && std::isspace((unsigned char)in[a])) ++a;
|
||||||
|
while (b > a && std::isspace((unsigned char)in[b - 1])) --b;
|
||||||
|
return in.substr(a, b - a);
|
||||||
|
};
|
||||||
|
|
||||||
|
std::string s_trim = trim_copy(s);
|
||||||
|
// Accept either 'T' or space (also lowercase 't') as date/time separator
|
||||||
|
size_t t_pos = s_trim.find_first_of("Tt ");
|
||||||
|
if (t_pos == std::string::npos) return false;
|
||||||
|
|
||||||
|
std::string date = trim_copy(s_trim.substr(0, t_pos));
|
||||||
|
std::string tzpart;
|
||||||
|
std::string timepart = trim_copy(s_trim.substr(t_pos + 1));
|
||||||
|
|
||||||
|
// Extract timezone (Z or +HH:MM or +HHMM or +HH)
|
||||||
|
size_t zpos = timepart.find_first_of("Zz");
|
||||||
|
if (zpos != std::string::npos) {
|
||||||
|
tzpart = "Z";
|
||||||
|
timepart = trim_copy(timepart.substr(0, zpos));
|
||||||
|
} else {
|
||||||
|
// Find first '+' or '-' AFTER the numeric time portion
|
||||||
|
size_t plus = std::string::npos;
|
||||||
|
for (size_t i = 0; i < timepart.size(); ++i) {
|
||||||
|
if (timepart[i] == '+' || timepart[i] == '-') { plus = i; break; }
|
||||||
|
}
|
||||||
|
if (plus != std::string::npos) {
|
||||||
|
tzpart = trim_copy(timepart.substr(plus));
|
||||||
|
timepart = trim_copy(timepart.substr(0, plus));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int year = 0, month = 0, day = 0;
|
||||||
|
if (sscanf(date.c_str(), "%d-%d-%d", &year, &month, &day) != 3) {
|
||||||
|
// Try alternative separators like '/'
|
||||||
|
if (sscanf(date.c_str(), "%d/%d/%d", &year, &month, &day) != 3) return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
int hour = 0, min = 0, sec = 0;
|
||||||
|
// Remove fractional seconds if present (e.g., 10:34:52.123)
|
||||||
|
size_t dot = timepart.find('.');
|
||||||
|
if (dot != std::string::npos) timepart = timepart.substr(0, dot);
|
||||||
|
|
||||||
|
int time_parsed = sscanf(timepart.c_str(), "%d:%d:%d", &hour, &min, &sec);
|
||||||
|
if (time_parsed < 2) {
|
||||||
|
// Try hour only or hour:minute
|
||||||
|
if (sscanf(timepart.c_str(), "%d:%d", &hour, &min) == 2) {
|
||||||
|
sec = 0;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int parsed_offset_seconds = 0; // seconds east of UTC
|
||||||
|
bool has_tz = false;
|
||||||
|
if (!tzpart.empty()) {
|
||||||
|
has_tz = true;
|
||||||
|
if (tzpart == "Z" || tzpart == "z") {
|
||||||
|
parsed_offset_seconds = 0;
|
||||||
|
} else {
|
||||||
|
char sign = tzpart[0];
|
||||||
|
int oh = 0, om = 0;
|
||||||
|
std::string tznum = tzpart.substr(1);
|
||||||
|
// Accept +HH:MM, +HHMM, or +HH
|
||||||
|
if (sscanf(tznum.c_str(), "%d:%d", &oh, &om) == 2) {
|
||||||
|
parsed_offset_seconds = oh * 3600 + om * 60;
|
||||||
|
} else if (sscanf(tznum.c_str(), "%d", &oh) == 1) {
|
||||||
|
// If tz like 0800, interpret as HHMM when length>=3
|
||||||
|
if (tznum.size() >= 3 && tznum.find(':') == std::string::npos && tznum.size() <= 4) {
|
||||||
|
if (tznum.size() == 4) {
|
||||||
|
int hh = 0, mm = 0;
|
||||||
|
if (sscanf(tznum.c_str(), "%2d%2d", &hh, &mm) == 2) {
|
||||||
|
oh = hh; om = mm;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
parsed_offset_seconds = oh * 3600 + om * 60;
|
||||||
|
} else {
|
||||||
|
parsed_offset_seconds = oh * 3600;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (sign == '-') parsed_offset_seconds = -parsed_offset_seconds;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::tm tm = {};
|
||||||
|
tm.tm_year = year - 1900;
|
||||||
|
tm.tm_mon = month - 1;
|
||||||
|
tm.tm_mday = day;
|
||||||
|
tm.tm_hour = hour;
|
||||||
|
tm.tm_min = min;
|
||||||
|
tm.tm_sec = sec;
|
||||||
|
tm.tm_isdst = -1;
|
||||||
|
|
||||||
|
time_t now = time(nullptr);
|
||||||
|
std::tm local_tm = *std::localtime(&now);
|
||||||
|
std::tm gm_tm = *std::gmtime(&now);
|
||||||
|
time_t local_epoch = mktime(&local_tm);
|
||||||
|
time_t gm_epoch = mktime(&gm_tm);
|
||||||
|
int local_offset = static_cast<int>(difftime(local_epoch, gm_epoch));
|
||||||
|
|
||||||
|
time_t epoch_assuming_local = mktime(&tm);
|
||||||
|
if (epoch_assuming_local == (time_t)-1) return false;
|
||||||
|
|
||||||
|
if (!has_tz) {
|
||||||
|
// No timezone provided: assume local time
|
||||||
|
out_epoch = epoch_assuming_local;
|
||||||
|
} else {
|
||||||
|
// Adjust when parsed time had a specific timezone
|
||||||
|
out_epoch = epoch_assuming_local + (local_offset - parsed_offset_seconds);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string format_epoch_HHMM(time_t epoch) {
|
||||||
|
std::tm at = *std::localtime(&epoch);
|
||||||
|
char buf[6];
|
||||||
|
strftime(buf, sizeof(buf), "%H:%M", &at);
|
||||||
|
return std::string(buf);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
MainUIHandler::MainUIHandler()
|
MainUIHandler::MainUIHandler()
|
||||||
: main_ui_(std::make_unique<MainUI>())
|
: main_ui_(std::make_unique<MainUI>())
|
||||||
, mtr_handler_(std::make_unique<MTRNextTrainHandler>()) {
|
, mtr_handler_(std::make_unique<MTRNextTrainHandler>()) {
|
||||||
@@ -247,55 +377,58 @@ void MainUIHandler::fetch_and_update_arrivals_() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::string MainUIHandler::format_arrival_time_(const std::string& api_time) {
|
std::string MainUIHandler::format_arrival_time_(const std::string& api_time) {
|
||||||
// API returns time in format like "2024-01-15T14:30:00+08:00" or "2"
|
// Keep fallback for numeric minute strings (e.g., "0", "6", "15")
|
||||||
// Check if it's a simple minute count
|
if (!api_time.empty() && std::all_of(api_time.begin(), api_time.end(), [](char c) { return std::isdigit((unsigned char)c); })) {
|
||||||
if (api_time.length() <= 2) {
|
|
||||||
return api_time + "分鐘";
|
return api_time + "分鐘";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try to parse ISO format time
|
time_t arrival_epoch = 0;
|
||||||
// Extract time part (HH:MM)
|
if (parse_iso_to_epoch(api_time, arrival_epoch)) {
|
||||||
size_t t_pos = api_time.find('T');
|
time_t now = time(nullptr);
|
||||||
if (t_pos != std::string::npos && api_time.length() > t_pos + 5) {
|
double diff_seconds = difftime(arrival_epoch, now);
|
||||||
std::string time_part = api_time.substr(t_pos + 1, 5);
|
if (diff_seconds < 0) diff_seconds = 0; // already arrived -> show 0
|
||||||
return time_part;
|
int minutes = static_cast<int>((diff_seconds + 59) / 60); // round up
|
||||||
}
|
|
||||||
|
|
||||||
// Try to find HH:MM pattern in the string
|
if (minutes < 60) {
|
||||||
for (size_t i = 0; i < api_time.length() - 5; i++) {
|
return std::to_string(minutes) + "分鐘";
|
||||||
if (api_time[i] == ':' && i > 0 && i < api_time.length() - 3) {
|
|
||||||
std::string candidate = api_time.substr(i - 2, 5);
|
|
||||||
if (isdigit(candidate[0]) && isdigit(candidate[1]) && candidate[2] == ':' &&
|
|
||||||
isdigit(candidate[3]) && isdigit(candidate[4])) {
|
|
||||||
return candidate;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ""; // Return empty instead of raw input
|
// Only relative minutes are returned from this function.
|
||||||
|
ESP_LOGW(TAG, "Unable to parse arrival time for relative format: %s", api_time.c_str());
|
||||||
|
return std::string();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string MainUIHandler::format_arrival_time_full_(const std::string& api_time) {
|
std::string MainUIHandler::format_arrival_time_full_(const std::string& api_time) {
|
||||||
// Returns absolute time for display (e.g., "14:32")
|
// Returns absolute time for display (e.g., "14:32")
|
||||||
// Returns empty string for relative times
|
// Returns empty string for relative times
|
||||||
if (api_time.length() <= 2) {
|
if (api_time.length() <= 2) {
|
||||||
|
ESP_LOGW(TAG, "Arrival time appears to be relative, no full time available: %s", api_time.c_str());
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
time_t arrival_epoch = 0;
|
||||||
|
if (parse_iso_to_epoch(api_time, arrival_epoch)) {
|
||||||
|
return format_epoch_HHMM(arrival_epoch);
|
||||||
|
}
|
||||||
|
|
||||||
|
// fallback: extract HH:MM
|
||||||
size_t t_pos = api_time.find('T');
|
size_t t_pos = api_time.find('T');
|
||||||
if (t_pos != std::string::npos && api_time.length() > t_pos + 5) {
|
if (t_pos != std::string::npos && api_time.length() > t_pos + 5) {
|
||||||
return api_time.substr(t_pos + 1, 5);
|
return api_time.substr(t_pos + 1, 5);
|
||||||
}
|
}
|
||||||
|
|
||||||
return "";
|
ESP_LOGW(TAG, "Unable to parse arrival time for full format: %s", api_time.c_str());
|
||||||
|
return std::string();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string MainUIHandler::get_current_time_string_() {
|
std::string MainUIHandler::get_current_time_string_() {
|
||||||
auto now = std::time(nullptr);
|
time_t now = time(nullptr);
|
||||||
auto tm = *std::localtime(&now);
|
std::tm tm = *std::localtime(&now);
|
||||||
|
|
||||||
char buffer[6]; // HH:MM\0
|
char buffer[32];
|
||||||
strftime(buffer, sizeof(buffer), "%H:%M", &tm);
|
// Return absolute local date and time: YYYY-MM-DD HH:MM
|
||||||
|
strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M", &tm);
|
||||||
return std::string(buffer);
|
return std::string(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user