diff --git a/src/lib/ebus/data.cpp b/src/lib/ebus/data.cpp index 75cdfe64..6ccbf871 100644 --- a/src/lib/ebus/data.cpp +++ b/src/lib/ebus/data.cpp @@ -532,16 +532,20 @@ result_t SingleDataField::read(const SymbolString& data, size_t offset, if (leadingSeparator) { *output << ","; } - if (!shortFormat) { - *output << "\n "; + if (fieldIndex < 0 && !shortFormat) { + *output << "\n "; } if (outputIndex >= 0 || m_name.empty() || !(outputFormat & OF_NAMES)) { - *output << "\"" << static_cast(outputIndex < 0 ? 0 : outputIndex) << "\":"; + if (fieldIndex < 0) { + *output << "\"" << static_cast(outputIndex < 0 ? 0 : outputIndex) << "\":"; + } if (!shortFormat) { *output << " {\"name\": \"" << m_name << "\"" << ", \"value\": "; } } else { - *output << "\"" << m_name << "\":"; + if (fieldIndex < 0) { + *output << "\"" << m_name << "\":"; + } if (!shortFormat) { *output << " {\"value\": "; } diff --git a/src/lib/ebus/data.h b/src/lib/ebus/data.h index 24175239..27507b24 100644 --- a/src/lib/ebus/data.h +++ b/src/lib/ebus/data.h @@ -249,6 +249,12 @@ class DataField : public AttributedItem { const map& values, map* attributes, vector* fields) const = 0; + /** + * Get the field count (excluding ignored fields). + * @return the field count (excluding ignored fields). + */ + virtual size_t getCount() const = 0; + /** * Get the specified field name. * @param fieldIndex the index of the field (excluding ignored fields), or -1 for this. @@ -392,6 +398,9 @@ class SingleDataField : public DataField { */ bool hasFullByteOffset(bool after) const; + // @copydoc + size_t getCount() const override { return isIgnored() ? 0 : 1; } + // @copydoc virtual string getName(ssize_t fieldIndex) const { return isIgnored() || fieldIndex > 0 ? "" : m_name; @@ -628,6 +637,9 @@ class DataFieldSet : public DataField { // @copydoc size_t getLength(PartType partType, size_t maxLength) const override; + // @copydoc + size_t getCount() const override { return m_fields.size() - m_ignoredCount; } + // @copydoc string getName(ssize_t fieldIndex) const override; diff --git a/src/lib/ebus/message.h b/src/lib/ebus/message.h index 4e24500b..7969dc78 100644 --- a/src/lib/ebus/message.h +++ b/src/lib/ebus/message.h @@ -267,12 +267,18 @@ class Message : public AttributedItem { */ static bool checkLevel(const string& level, const string& checkLevels); + /** + * Get the number of non-ignored fields. + * @return the number of non-ignored fields. + */ + size_t getFieldCount() const { return m_data->getCount(); } + /** * Get the specified field name. * @param fieldIndex the index of the field (excluding ignored fields). - * @return the field name, or the index as string if not unique or not available. + * @return the field name, or the index as string if not unique, or empty not available. */ - virtual string getFieldName(ssize_t fieldIndex) const { return m_data->getName(fieldIndex); } + string getFieldName(ssize_t fieldIndex) const { return m_data->getName(fieldIndex); } /** * Get whether this is a write message. diff --git a/src/lib/ebus/test/test_data.cpp b/src/lib/ebus/test/test_data.cpp index 3149b652..89d950ec 100644 --- a/src/lib/ebus/test/test_data.cpp +++ b/src/lib/ebus/test/test_data.cpp @@ -472,13 +472,13 @@ int main() { {"x,,bi3:2,0=off;1=on", "1", "10feffff0108", "00", "n"}, {"x,,bi3:2,0=off;1=on,ja/nein,Wahrheitswert", "x=on ja/nein [Wahrheitswert]", "10feffff0108", "00", "vvv"}, {"x,,bi3:2,0=off;1=on,ja/nein,Wahrheitswert", "x=1 ja/nein [Wahrheitswert]", "10feffff0108", "00", "vvvn"}, - {"x,,bi3:2,0=off;1=on,ja/nein,Wahrheitswert", "\n \"x\": {\"value\": \"on\"}", "10feffff0108", "00", "vj"}, - {",,bi3:2,0=off;1=on,ja/nein,Wahrheitswert", "\n \"0\": {\"name\": \"\", \"value\": \"on\"}", "10feffff0108", "00", "vj"}, - {",,bi3:2,0=off;1=on,ja/nein,Wahrheitswert", "\n \"0\": {\"name\": \"\", \"value\": \"on\"}", "10feffff0108", "00", "j"}, - {"x,,bi3:2,0=off;1=on,ja/nein,Wahrheitswert", "\n \"x\": {\"value\": \"on\", \"unit\": \"ja/nein\", \"comment\": \"Wahrheitswert\"}", "10feffff0108", "00", "vvvj"}, - {"x,,bi3:2,0=off;1=on,ja/nein,Wahrheitswert", "\n \"x\": {\"value\": 1}", "10feffff0108", "00", "vnj"}, - {"x,,bi3:2,0=off;1=on,ja/nein,Wahrheitswert", "\n \"x\": {\"value\": 1, \"unit\": \"ja/nein\", \"comment\": \"Wahrheitswert\"}", "10feffff0108", "00", "vvvnj"}, - {"x,,bi3:2,0=off;1=on,ja/nein,Wahrheitswert", "\n \"0\": {\"name\": \"x\", \"value\": 1}", "10feffff0108", "00", "nj"}, + {"x,,bi3:2,0=off;1=on,ja/nein,Wahrheitswert", "\n \"x\": {\"value\": \"on\"}", "10feffff0108", "00", "vj"}, + {",,bi3:2,0=off;1=on,ja/nein,Wahrheitswert", "\n \"0\": {\"name\": \"\", \"value\": \"on\"}", "10feffff0108", "00", "vj"}, + {",,bi3:2,0=off;1=on,ja/nein,Wahrheitswert", "\n \"0\": {\"name\": \"\", \"value\": \"on\"}", "10feffff0108", "00", "j"}, + {"x,,bi3:2,0=off;1=on,ja/nein,Wahrheitswert", "\n \"x\": {\"value\": \"on\", \"unit\": \"ja/nein\", \"comment\": \"Wahrheitswert\"}", "10feffff0108", "00", "vvvj"}, + {"x,,bi3:2,0=off;1=on,ja/nein,Wahrheitswert", "\n \"x\": {\"value\": 1}", "10feffff0108", "00", "vnj"}, + {"x,,bi3:2,0=off;1=on,ja/nein,Wahrheitswert", "\n \"x\": {\"value\": 1, \"unit\": \"ja/nein\", \"comment\": \"Wahrheitswert\"}", "10feffff0108", "00", "vvvnj"}, + {"x,,bi3:2,0=off;1=on,ja/nein,Wahrheitswert", "\n \"0\": {\"name\": \"x\", \"value\": 1}", "10feffff0108", "00", "nj"}, {"x,,uch,1=test;2=high;3=off;0x10=on", "on", "10feffff0110", "00", ""}, {"x,s,uch", "3", "1050ffff00", "0103", ""}, {"x,,d2b,,°C,Aussentemperatur", "x=18.004 °C [Aussentemperatur]", "10fe0700090112", "00", "vvv"}, diff --git a/src/lib/ebus/test/test_message.cpp b/src/lib/ebus/test/test_message.cpp index 80f25620..7ee0fed8 100644 --- a/src/lib/ebus/test/test_message.cpp +++ b/src/lib/ebus/test/test_message.cpp @@ -87,10 +87,10 @@ int main() { {"r,cir,Status01,VL/RL/AussenTemp/VLWW/SpeicherTemp/Status,,08,B511,01,,,temp1;temp1;temp2;temp1;temp1;pumpstate", "28.0;24.0;4.938;35.0;41.0;4", "ff08b5110101", "093830f00446520400ff", "d"}, {"r,message circuit,message name,message comment,,25,B509,0d2800,,,tempsensor", "temp=-14.00 Temperatursensor [Temperatur];sensor=ok [Fühlerstatus]", "ff25b509030d2800", "0320ff00", "D"}, {"r,message circuit,message name,message comment,,25,B509,0d2800,,,tempsensor,,field unit,field comment", "temp=-14.00 field unit [field comment];sensor=ok [Fühlerstatus]", "ff25b509030d2800", "0320ff00", "D"}, - {"r,message circuit,message name,message comment,,25,B509,0d2800,,,tempsensor,,field unit,field comment", "\n \"temp\": {\"value\": -14.00},\n \"sensor\": {\"value\": \"ok\"}", "ff25b509030d2800", "0320ff00", "j"}, - {"r,message circuit,message name,message comment,,25,B509,0d2800,,,tempsensor,,field unit,field comment", "\n \"temp\": {\"value\": -14.00, \"unit\": \"field unit\", \"comment\": \"field comment\"},\n" " \"sensor\": {\"value\": \"ok\", \"comment\": \"Fühlerstatus\"}", "ff25b509030d2800", "0320ff00", "J"}, + {"r,message circuit,message name,message comment,,25,B509,0d2800,,,tempsensor,,field unit,field comment", "\n \"temp\": {\"value\": -14.00},\n \"sensor\": {\"value\": \"ok\"}", "ff25b509030d2800", "0320ff00", "j"}, + {"r,message circuit,message name,message comment,,25,B509,0d2800,,,tempsensor,,field unit,field comment", "\n \"temp\": {\"value\": -14.00, \"unit\": \"field unit\", \"comment\": \"field comment\"},\n" " \"sensor\": {\"value\": \"ok\", \"comment\": \"Fühlerstatus\"}", "ff25b509030d2800", "0320ff00", "J"}, {"r,message circuit,message name,message comment,,25,B509,0d2800,,,temp,,field unit,field comment,,,sensor", "temp=-14.00 field unit [field comment];sensor=ok [Fühlerstatus]", "ff25b509030d2800", "0320ff00", "D"}, - {"r,message circuit,message name,message comment,,25,B509,0d2800,,,D2C,,°C,Temperatur,,,sensor", "\n \"0\": {\"name\": \"\", \"value\": -14.00},\n \"1\": {\"name\": \"sensor\", \"value\": \"ok\"}", "ff25b509030d2800", "0320ff00", "j"}, + {"r,message circuit,message name,message comment,,25,B509,0d2800,,,D2C,,°C,Temperatur,,,sensor", "\n \"0\": {\"name\": \"\", \"value\": -14.00},\n \"1\": {\"name\": \"sensor\", \"value\": \"ok\"}", "ff25b509030d2800", "0320ff00", "j"}, {"r,cir,name,,,25,B509,0d2800,,,tempsensorc", "-14.00", "ff25b509030d2800", "0320ff55", ""}, {"r,cir,name,,,25,B509,0d28,,m,sensorc,,,,,,temp", "-14.00", "ff25b509030d2855", "0220ff", ""}, {"u,cir,first,,,fe,0700,,x,,bda", "26.10.2014", "fffe07000426100614", "00", "p"}, @@ -157,13 +157,13 @@ int main() { " \"zz\": 117,\n" " \"id\": [181, 9, 13, 1, 0],\n" " \"fields\": {\n" - " \"0\": {\"name\": \"field\", \"value\": 42}\n" + " \"0\": {\"name\": \"field\", \"value\": 42}\n" " },\n" " \"fielddefs\": [\n" " { \"name\": \"field\", \"slave\": true, \"type\": \"UCH\", \"isbits\": false, \"length\": 1, \"unit\": \"\", \"comment\": \"\"}\n" " ]\n" " }: \n" - " \"field\": {\"value\": 42}", "ff75b509030d0100", "012a", "jN"}, + " \"field\": {\"value\": 42}", "ff75b509030d0100", "012a", "jN"}, }; templates = new DataFieldTemplates(); unsigned int lineNo = 0;