From 7a567e69fb6d3e0164cff3cd141a48c8585b980f Mon Sep 17 00:00:00 2001 From: John Date: Fri, 5 Nov 2021 19:45:04 +0100 Subject: [PATCH] move helper method to SymbolString --- src/lib/ebus/message.cpp | 18 ++---------------- src/lib/ebus/symbol.cpp | 19 +++++++++++++++++++ src/lib/ebus/symbol.h | 9 +++++++++ 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src/lib/ebus/message.cpp b/src/lib/ebus/message.cpp index f4bf2d95..d1464bac 100644 --- a/src/lib/ebus/message.cpp +++ b/src/lib/ebus/message.cpp @@ -935,20 +935,6 @@ void Message::dumpField(const string& fieldName, bool withConditions, OutputForm dumpAttribute(false, outputFormat, fieldName, output); } -void addData(const SymbolString& data, ostringstream* output) { - if (data.size() == 0) { - return; - } - *output << ",\n \"" << (data.isMaster() ? "master" : "slave") << "\": ["; - for (size_t pos = 0; pos < data.size(); pos++) { - if (pos > 0) { - *output << ", "; - } - *output << dec << static_cast(data[pos]); - } - *output << "]"; -} - void Message::decodeJson(bool leadingSeparator, bool appendDirectionCondition, bool withData, bool addRaw, OutputFormat outputFormat, ostringstream* output) const { outputFormat |= OF_JSON; @@ -1005,8 +991,8 @@ void Message::decodeJson(bool leadingSeparator, bool appendDirectionCondition, b appendAttributes(outputFormat, output); if (hasData) { if (addRaw) { - addData(m_lastMasterData, output); - addData(m_lastSlaveData, output); + m_lastMasterData.dumpJson(true, output); + m_lastSlaveData.dumpJson(true, output); *output << dec; } size_t pos = (size_t)output->tellp(); diff --git a/src/lib/ebus/symbol.cpp b/src/lib/ebus/symbol.cpp index c6e91612..b97ea1da 100755 --- a/src/lib/ebus/symbol.cpp +++ b/src/lib/ebus/symbol.cpp @@ -28,6 +28,7 @@ namespace ebusd { using std::ostringstream; using std::nouppercase; using std::setw; +using std::dec; using std::hex; using std::setfill; @@ -157,6 +158,24 @@ const string SymbolString::getStr(size_t skipFirstSymbols) const { return sstr.str(); } +bool SymbolString::dumpJson(bool withSeparator, ostringstream* output) const { + if (size() == 0) { + return false; + } + if (withSeparator) { + *output << ",\n "; + } + *output << "\"" << (isMaster() ? "master" : "slave") << "\": ["; + for (size_t pos = 0; pos < size(); pos++) { + if (pos > 0) { + *output << ", "; + } + *output << dec << static_cast(m_data[pos]); + } + *output << "]"; + return true; +} + symbol_t SymbolString::calcCrc() const { symbol_t crc = 0; for (size_t i = 0; i < m_data.size(); i++) { diff --git a/src/lib/ebus/symbol.h b/src/lib/ebus/symbol.h index c3aef275..e69f6844 100755 --- a/src/lib/ebus/symbol.h +++ b/src/lib/ebus/symbol.h @@ -67,6 +67,7 @@ namespace ebusd { using std::string; using std::vector; +using std::ostringstream; /** the base type for symbols sent to/from the eBUS. */ typedef unsigned char symbol_t; @@ -158,6 +159,14 @@ class SymbolString { */ const string getStr(size_t skipFirstSymbols = 0) const; + /** + * Dump the data in JSON format to the output. + * @param withSeparator true to prepend the field separator. + * @param output the @a ostringstream to format the messages to. + * @return true if something was written to the output. + */ + bool dumpJson(bool withSeparator, ostringstream* output) const; + /** * Return a reference to the symbol at the specified index. * @param index the index of the symbol to return.