fix dump of divisor

This commit is contained in:
John
2024-12-12 22:04:35 +01:00
parent 2790860607
commit 85a3eef0a6
8 changed files with 35 additions and 26 deletions
+1
View File
@@ -2,6 +2,7 @@
## Bug Fixes ## Bug Fixes
* fix for device string symlink with colon * fix for device string symlink with colon
* fix "read" and "write" command response * fix "read" and "write" command response
* fix dump of divisor
# 24.1 (2024-10-27) # 24.1 (2024-10-27)
+1 -1
View File
@@ -191,7 +191,7 @@ bool decodeType(const DataType* type, const SymbolString& input, size_t length,
first = false; first = false;
*output << endl << " "; *output << endl << " ";
ostringstream::pos_type cnt = output->tellp(); ostringstream::pos_type cnt = output->tellp();
type->dump(OF_NONE, length, false, output); type->dump(OF_NONE, length, ad_none, output);
cnt = output->tellp() - cnt; cnt = output->tellp() - cnt;
while (cnt < 5) { while (cnt < 5) {
*output << " "; *output << " ";
+1 -1
View File
@@ -394,7 +394,7 @@ int main(int argc, char* argv[], char* envp[]) {
} }
if (s_opt.dumpConfig & OF_JSON) { if (s_opt.dumpConfig & OF_JSON) {
*out << "{\"datatypes\":["; *out << "{\"datatypes\":[";
DataTypeList::getInstance()->dump(s_opt.dumpConfig, true, out); DataTypeList::getInstance()->dump(s_opt.dumpConfig, out);
*out << "],\"templates\":["; *out << "],\"templates\":[";
const auto tmpl = s_scanHelper->getTemplates(""); const auto tmpl = s_scanHelper->getTemplates("");
tmpl->dump(s_opt.dumpConfig, out); tmpl->dump(s_opt.dumpConfig, out);
+1 -1
View File
@@ -2279,7 +2279,7 @@ result_t MainLoop::executeGet(const vector<string>& args, bool* connected, ostri
if (uri == "/datatypes") { if (uri == "/datatypes") {
*ostream << "["; *ostream << "[";
OutputFormat verbosity = OF_NAMES|OF_JSON|OF_ALL_ATTRS; OutputFormat verbosity = OF_NAMES|OF_JSON|OF_ALL_ATTRS;
DataTypeList::getInstance()->dump(verbosity, true, ostream); DataTypeList::getInstance()->dump(verbosity, ostream);
*ostream << "\n]"; *ostream << "\n]";
type = 6; type = 6;
*connected = false; *connected = false;
+1 -1
View File
@@ -555,7 +555,7 @@ void SingleDataField::dumpPrefix(bool prependFieldSeparator, OutputFormat output
} }
*output << FIELD_SEPARATOR; *output << FIELD_SEPARATOR;
} }
m_dataType->dump(outputFormat, m_length, true, output); m_dataType->dump(outputFormat, m_length, ad_normal, output);
} }
void SingleDataField::dumpSuffix(OutputFormat outputFormat, ostream* output) const { void SingleDataField::dumpSuffix(OutputFormat outputFormat, ostream* output) const {
+16 -16
View File
@@ -124,7 +124,7 @@ uint16_t floatToUint16(float value) {
return static_cast<uint16_t>((shift << 11) | (negative ? 0x8000 | (0x800-sig) : sig)); return static_cast<uint16_t>((shift << 11) | (negative ? 0x8000 | (0x800-sig) : sig));
} }
bool DataType::dump(OutputFormat outputFormat, size_t length, bool appendDivisor, ostream* output) const { bool DataType::dump(OutputFormat outputFormat, size_t length, AppendDivisor appendDivisor, ostream* output) const {
if (outputFormat & OF_JSON) { if (outputFormat & OF_JSON) {
*output << "\"type\": \"" << m_id << "\", \"isbits\": " *output << "\"type\": \"" << m_id << "\", \"isbits\": "
<< (getBitCount() < 8 ? "true" : "false"); << (getBitCount() < 8 ? "true" : "false");
@@ -149,7 +149,7 @@ bool DataType::dump(OutputFormat outputFormat, size_t length, bool appendDivisor
*output << static_cast<unsigned>(length); *output << static_cast<unsigned>(length);
} }
} }
if (appendDivisor) { if (appendDivisor != ad_none) {
*output << FIELD_SEPARATOR; *output << FIELD_SEPARATOR;
} }
} }
@@ -157,7 +157,7 @@ bool DataType::dump(OutputFormat outputFormat, size_t length, bool appendDivisor
} }
bool StringDataType::dump(OutputFormat outputFormat, size_t length, bool appendDivisor, ostream* output) const { bool StringDataType::dump(OutputFormat outputFormat, size_t length, AppendDivisor appendDivisor, ostream* output) const {
DataType::dump(outputFormat, length, appendDivisor, output); DataType::dump(outputFormat, length, appendDivisor, output);
if ((outputFormat & OF_JSON) && (outputFormat & OF_ALL_ATTRS)) { if ((outputFormat & OF_JSON) && (outputFormat & OF_ALL_ATTRS)) {
*output << ", \"result\": \"" << (isIgnored() ? "void" : "string") << "\""; *output << ", \"result\": \"" << (isIgnored() ? "void" : "string") << "\"";
@@ -300,7 +300,7 @@ result_t StringDataType::writeSymbols(size_t offset, size_t length, istringstrea
} }
bool DateTimeDataType::dump(OutputFormat outputFormat, size_t length, bool appendDivisor, ostream* output) const { bool DateTimeDataType::dump(OutputFormat outputFormat, size_t length, AppendDivisor appendDivisor, ostream* output) const {
DataType::dump(outputFormat, length, appendDivisor, output); DataType::dump(outputFormat, length, appendDivisor, output);
if ((outputFormat & OF_JSON) && (outputFormat & OF_ALL_ATTRS)) { if ((outputFormat & OF_JSON) && (outputFormat & OF_ALL_ATTRS)) {
*output << ", \"result\": \"" << (hasDate() ? hasTime() ? "datetime" : "date" : "time") << "\""; *output << ", \"result\": \"" << (hasDate() ? hasTime() ? "datetime" : "date" : "time") << "\"";
@@ -672,7 +672,7 @@ size_t NumberDataType::calcPrecision(int divisor) {
return precision; return precision;
} }
bool NumberDataType::dump(OutputFormat outputFormat, size_t length, bool appendDivisor, ostream* output) const { bool NumberDataType::dump(OutputFormat outputFormat, size_t length, AppendDivisor appendDivisor, ostream* output) const {
if (m_bitCount < 8) { if (m_bitCount < 8) {
DataType::dump(outputFormat, m_bitCount, appendDivisor, output); DataType::dump(outputFormat, m_bitCount, appendDivisor, output);
} else { } else {
@@ -681,11 +681,17 @@ bool NumberDataType::dump(OutputFormat outputFormat, size_t length, bool appendD
if ((outputFormat & OF_JSON) && (outputFormat & OF_ALL_ATTRS)) { if ((outputFormat & OF_JSON) && (outputFormat & OF_ALL_ATTRS)) {
*output << ", \"result\": \"number\""; *output << ", \"result\": \"number\"";
} }
if (!appendDivisor) { if (appendDivisor == ad_none) {
return false; return false;
} }
bool ret = false; bool ret = false;
if (m_baseType) { if (appendDivisor == ad_full && m_divisor != 1) {
if (outputFormat & OF_JSON) {
*output << ", \"divisor\": ";
}
*output << m_divisor;
ret = true;
} else if (m_baseType) {
if (m_baseType->m_divisor != m_divisor) { if (m_baseType->m_divisor != m_divisor) {
if (outputFormat & OF_JSON) { if (outputFormat & OF_JSON) {
*output << ", \"divisor\": "; *output << ", \"divisor\": ";
@@ -693,12 +699,6 @@ bool NumberDataType::dump(OutputFormat outputFormat, size_t length, bool appendD
*output << (m_divisor / m_baseType->m_divisor); *output << (m_divisor / m_baseType->m_divisor);
ret = true; ret = true;
} }
} else if (m_divisor != 1) {
if (outputFormat & OF_JSON) {
*output << ", \"divisor\": ";
}
*output << m_divisor;
ret = true;
} }
if (ret && (outputFormat & OF_JSON) && (outputFormat & OF_ALL_ATTRS)) { if (ret && (outputFormat & OF_JSON) && (outputFormat & OF_ALL_ATTRS)) {
*output << ", \"precision\": " << static_cast<unsigned>(getPrecision()); *output << ", \"precision\": " << static_cast<unsigned>(getPrecision());
@@ -1317,7 +1317,7 @@ DataTypeList* DataTypeList::getInstance() {
return &s_instance; return &s_instance;
} }
void DataTypeList::dump(OutputFormat outputFormat, bool appendDivisor, ostream* output) const { void DataTypeList::dump(OutputFormat outputFormat, ostream* output) const {
bool json = outputFormat & OF_JSON; bool json = outputFormat & OF_JSON;
string sep = "\n"; string sep = "\n";
for (const auto &it : m_typesById) { for (const auto &it : m_typesById) {
@@ -1329,9 +1329,9 @@ void DataTypeList::dump(OutputFormat outputFormat, bool appendDivisor, ostream*
*output << sep << " {"; *output << sep << " {";
} }
if ((dataType->getBitCount() % 8) != 0) { if ((dataType->getBitCount() % 8) != 0) {
dataType->dump(outputFormat, dataType->getBitCount(), appendDivisor, output); dataType->dump(outputFormat, dataType->getBitCount(), ad_full, output);
} else { } else {
dataType->dump(outputFormat, dataType->getBitCount() / 8, appendDivisor, output); dataType->dump(outputFormat, dataType->getBitCount() / 8, ad_full, output);
} }
if (json) { if (json) {
*output << "}"; *output << "}";
+12 -6
View File
@@ -137,6 +137,13 @@ constexpr inline enum OutputFormat operator~ (enum OutputFormat self) {
return (enum OutputFormat)(~(OutputFormatBaseType)self); return (enum OutputFormat)(~(OutputFormatBaseType)self);
} }
/** whether divisor should be appended to a dump. */
enum AppendDivisor {
ad_none, //!< no dump of divisor
ad_normal, //!< regular dump of divisor (i.e. not for base types)
ad_full, //!< full dump of divisor (i.e. also for base types)
};
/** the message part in which a data field is stored. */ /** the message part in which a data field is stored. */
enum PartType { enum PartType {
pt_any, //!< stored in any data (master or slave) pt_any, //!< stored in any data (master or slave)
@@ -286,7 +293,7 @@ class DataType {
* @param output the @a ostream to dump to. * @param output the @a ostream to dump to.
* @return true when a non-default divisor was written to the output. * @return true when a non-default divisor was written to the output.
*/ */
virtual bool dump(OutputFormat outputFormat, size_t length, bool appendDivisor, ostream* output) const; virtual bool dump(OutputFormat outputFormat, size_t length, AppendDivisor appendDivisor, ostream* output) const;
/** /**
* Internal method for reading the numeric raw value from a @a SymbolString. * Internal method for reading the numeric raw value from a @a SymbolString.
@@ -363,7 +370,7 @@ class StringDataType : public DataType {
virtual ~StringDataType() {} virtual ~StringDataType() {}
// @copydoc // @copydoc
bool dump(OutputFormat outputFormat, size_t length, bool appendDivisor, ostream* output) const override; bool dump(OutputFormat outputFormat, size_t length, AppendDivisor appendDivisor, ostream* output) const override;
// @copydoc // @copydoc
result_t readRawValue(size_t offset, size_t length, const SymbolString& input, result_t readRawValue(size_t offset, size_t length, const SymbolString& input,
@@ -410,7 +417,7 @@ class DateTimeDataType : public DataType {
virtual ~DateTimeDataType() {} virtual ~DateTimeDataType() {}
// @copydoc // @copydoc
bool dump(OutputFormat outputFormat, size_t length, bool appendDivisor, ostream* output) const override; bool dump(OutputFormat outputFormat, size_t length, AppendDivisor appendDivisor, ostream* output) const override;
/** /**
* @return true if date part is present. * @return true if date part is present.
@@ -503,7 +510,7 @@ class NumberDataType : public DataType {
static size_t calcPrecision(int divisor); static size_t calcPrecision(int divisor);
// @copydoc // @copydoc
bool dump(OutputFormat outputFormat, size_t length, bool appendDivisor, ostream* output) const override; bool dump(OutputFormat outputFormat, size_t length, AppendDivisor appendDivisor, ostream* output) const override;
/** /**
* Derive a new @a NumberDataType from this. * Derive a new @a NumberDataType from this.
@@ -658,10 +665,9 @@ class DataTypeList {
/** /**
* Dump the type list optionally including the divisor to the output. * Dump the type list optionally including the divisor to the output.
* @param outputFormat the @a OutputFormat options. * @param outputFormat the @a OutputFormat options.
* @param appendDivisor whether to append the divisor (if available).
* @param output the @a ostream to dump to. * @param output the @a ostream to dump to.
*/ */
void dump(OutputFormat outputFormat, bool appendDivisor, ostream* output) const; void dump(OutputFormat outputFormat, ostream* output) const;
/** /**
* Removes all @a DataType instances. * Removes all @a DataType instances.
+2
View File
@@ -221,6 +221,8 @@ int main() {
" ]\n" " ]\n"
" }: \n" " }: \n"
" \"field\": {\"value\": 42, \"raw\": [42]}", "ff75b509030d0100", "012a", "jNr"}, " \"field\": {\"value\": 42, \"raw\": [42]}", "ff75b509030d0100", "012a", "jNr"},
{"r,CIRCUIT,NAME,COMMENT,,,,0100,field,,temp", "r,cirCIRCUITcuit,naNAMEme,comCOMMENTment,ff,75,b509,0d0100,field,s,D2C,,°C,Temperatur: field=42.00 °C [Temperatur]", "ff75b509030d0100", "02a002", "DN"},
{"r,CIRCUIT,NAME,COMMENT,,,,0100,field,,D2C,,°C,Temperatur", "r,cirCIRCUITcuit,naNAMEme,comCOMMENTment,ff,75,b509,0d0100,field,s,D2C,,°C,Temperatur: field=42.00 °C [Temperatur]", "ff75b509030d0100", "02a002", "DN"},
}; };
templates = new DataFieldTemplates(); templates = new DataFieldTemplates();
unsigned int lineNo = 0; unsigned int lineNo = 0;