1
0
mirror of https://github.com/thooge/esp32-nmea2000-obp60.git synced 2026-03-28 18:06:37 +01:00

More work on formatter

This commit is contained in:
2025-08-22 15:17:54 +02:00
parent 672a3843d1
commit c932724473
5 changed files with 131 additions and 53 deletions

View File

@@ -20,6 +20,7 @@ Formatter::Formatter(GwConfigHandler *config) {
windspeedFormat = config->getString(config->windspeedFormat);
tempFormat = config->getString(config->tempFormat);
dateFormat = config->getString(config->dateFormat);
dateFmt = getDateFormat(dateFormat);
usesimudata = config->getBool(config->useSimuData);
precision = config->getString(config->valueprecision);
@@ -35,6 +36,37 @@ Formatter::Formatter(GwConfigHandler *config) {
}
fmtType Formatter::stringToFormat(const char* formatStr) {
auto it = formatMap.find(formatStr);
if (it != formatMap.end()) {
return it->second;
}
return fmtType::XDR_G; // generic as default
}
fmtDate Formatter::getDateFormat(String sformat) {
if (sformat == "DE") {
return fmtDate::DE;
}
if (sformat == "GB") {
return fmtDate::GB;
}
if (sformat == "US") {
return fmtDate::US;
}
return fmtDate::ISO; // default
}
fmtTime Formatter::getTimeFormat(String sformat) {
if (sformat == "MMHH") {
return fmtTime::MMHH;
}
if (sformat == "MMHHSS") {
return fmtTime::MMHHSS;
}
return fmtTime::MMHH; // default
}
FormattedData Formatter::formatValue(GwApi::BoatValue *value, CommonData &commondata){
GwLog *logger = commondata.logger;
FormattedData result;
@@ -782,32 +814,36 @@ FormattedData Formatter::formatValue(GwApi::BoatValue *value, CommonData &common
return result;
}
String formatDate(String fmttype, uint16_t year, uint8_t month, uint8_t day) {
String formatDate(fmtDate fmttype, uint16_t year, uint8_t month, uint8_t day) {
char buffer[12];
if (fmttype == "GB") {
if (fmttype == fmtDate::GB) {
snprintf(buffer, 12, "%02d/%02d/%04d", day , month, year);
}
else if (fmttype == "US") {
else if (fmttype == fmtDate::US) {
snprintf(buffer, 12, "%02d/%02d/%04d", month, day, year);
}
else if (fmttype == "ISO") {
else if (fmttype == fmtDate::ISO) {
snprintf(buffer, 12, "%04d-%02d-%02d", year, month, day);
}
else {
else if (fmttype == fmtDate::DE) {
snprintf(buffer, 12, "%02d.%02d.%04d", day, month, year);
} else {
snprintf(buffer, 12, "%04d-%02d-%02d", year, month, day);
}
return String(buffer);
}
String formatTime(char fmttype, uint8_t hour, uint8_t minute, uint8_t second) {
// fmttype: s: with seconds, m: only minutes
String formatTime(fmtTime fmttype, uint8_t hour, uint8_t minute, uint8_t second) {
char buffer[10];
if (fmttype == 'm') {
if (fmttype == fmtTime::MMHH) {
snprintf(buffer, 10, "%02d:%02d", hour , minute);
}
else {
else if (fmttype == fmtTime::MMHHSS) {
snprintf(buffer, 10, "%02d:%02d:%02d", hour, minute, second);
}
else {
snprintf(buffer, 10, "%02d%02d%02d", hour, minute, second);
}
return String(buffer);
}