From eb007fe381da678546015e555999908ff9f8ba2e Mon Sep 17 00:00:00 2001 From: John Date: Mon, 2 Jan 2023 20:00:58 +0100 Subject: [PATCH] use preferred language for config file retrieval --- src/ebusd/bushandler.cpp | 3 +++ src/ebusd/main.cpp | 16 +++++++++++++--- src/lib/ebus/filereader.h | 5 +++++ src/lib/ebus/message.h | 2 +- 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/ebusd/bushandler.cpp b/src/ebusd/bushandler.cpp index 47bb5547..9f12edab 100644 --- a/src/ebusd/bushandler.cpp +++ b/src/ebusd/bushandler.cpp @@ -1530,6 +1530,9 @@ void BusHandler::formatUpdateInfo(ostringstream* output) const { } *output << "],\"gu\":" << unknownCnt; } + if (!m_messages->getPreferLanguage().empty()) { + *output << ",\"lc\":\"" << m_messages->getPreferLanguage() << "\""; + } unsigned char address = 0; for (int index = 0; index < 256; index++, address++) { bool ownAddress = !m_device->isReadOnly() && (address == m_ownMasterAddress || address == m_ownSlaveAddress); diff --git a/src/ebusd/main.cpp b/src/ebusd/main.cpp index 1a6b9e32..9824c877 100644 --- a/src/ebusd/main.cpp +++ b/src/ebusd/main.cpp @@ -151,6 +151,9 @@ static string s_configLocalPrefix = ""; /** the URI prefix (including trailing "/") for retrieving configuration files from HTTPS (empty for local files). */ static string s_configUriPrefix = ""; +/** the optional language query part for retrieving configuration files from HTTPS (empty for local files). */ +static string s_configLangQuery = ""; + /** the @a HttpClient for retrieving configuration files from HTTPS. */ static HttpClient* s_configHttpClient = nullptr; @@ -858,7 +861,8 @@ static result_t collectConfigFiles(const string& relPath, const string& prefix, vector* dirs = nullptr, bool* hasTemplates = nullptr) { const string relPathWithSlash = relPath.empty() ? "" : relPath + "/"; if (!s_configUriPrefix.empty()) { - string uri = s_configUriPrefix + relPathWithSlash + "?t=" + extension.substr(1) + query; + string uri = s_configUriPrefix + relPathWithSlash + s_configLangQuery + (s_configLangQuery.empty() ? "?" : "&") + + "t=" + extension.substr(1) + query; string names; if (!lazyHttpClient() || !s_configHttpClient->get(uri, "", &names)) { return RESULT_ERR_NOTFOUND; @@ -1074,7 +1078,7 @@ result_t loadDefinitionsFromConfigPath(FileReader* reader, const string& filenam stream = FileReader::openFile(s_configLocalPrefix + filename, errorDescription, &mtime); } else { string content; - if (lazyHttpClient() && s_configHttpClient->get(s_configUriPrefix + filename, "", &content, &mtime)) { + if (lazyHttpClient() && s_configHttpClient->get(s_configUriPrefix + filename + s_configLangQuery, "", &content, &mtime)) { stream = new istringstream(content); } } @@ -1423,6 +1427,12 @@ int main(int argc, char* argv[]) { } s_configHttpClient->disconnect(); } + const string lang = MappedFileReader::normalizeLanguage( + s_opt.preferLanguage==nullptr || !s_opt.preferLanguage[0] ? "" : s_opt.preferLanguage + ); + if (!lang.empty()) { + s_configLangQuery = "?l=" + lang; + } if (!s_opt.readOnly && s_opt.scanConfig && s_opt.initialScan == 0) { s_opt.initialScan = BROADCAST; } @@ -1431,7 +1441,7 @@ int main(int argc, char* argv[]) { setFacilitiesLogLevel(s_opt.logAreas, s_opt.logLevel); } - s_messageMap = new MessageMap(s_opt.checkConfig); + s_messageMap = new MessageMap(s_opt.checkConfig, lang); if (s_opt.checkConfig) { logNotice(lf_main, PACKAGE_STRING "." REVISION " performing configuration check..."); diff --git a/src/lib/ebus/filereader.h b/src/lib/ebus/filereader.h index d34a8db4..ff94a6c1 100755 --- a/src/lib/ebus/filereader.h +++ b/src/lib/ebus/filereader.h @@ -222,6 +222,11 @@ class MappedFileReader : public FileReader { */ static const string normalizeLanguage(const string& lang); + /** + * @return the preferred language code (up to 2 characters), or empty. + */ + const string getPreferLanguage() const { return m_preferLanguage; } + // @copydoc result_t readFromStream(istream* stream, const string& filename, const time_t& mtime, bool verbose, map* defaults, string* errorDescription, bool replace = false, size_t* hash = nullptr, diff --git a/src/lib/ebus/message.h b/src/lib/ebus/message.h index 70c6c36f..9ce3d284 100644 --- a/src/lib/ebus/message.h +++ b/src/lib/ebus/message.h @@ -1267,7 +1267,7 @@ class MessageMap : public MappedFileReader { * @param deleteData whether to delete the scan message @a DataField during @a Message destruction. */ explicit MessageMap(bool addAll = false, const string& preferLanguage = "", bool deleteData = true) - : MappedFileReader::MappedFileReader(true), + : MappedFileReader::MappedFileReader(true, preferLanguage), m_addAll(addAll), m_additionalScanMessages(false), m_maxIdLength(0), m_maxBroadcastIdLength(0), m_messageCount(0), m_conditionalMessageCount(0), m_passiveMessageCount(0) { m_scanMessage = Message::createScanMessage(false, deleteData);