I'm trying to encode/decode a Sparkplug Payload with a metric of type "PropertySet". I'm using the npm library (v1.0.3)
This is my code so far:
const sparkplug = require("sparkplug-payload").get("spBv1.0");
const payload = {
metrics: [
{
name: "exampleMetric",
type: "PropertySet",
properties: {
"bla": { type: "Boolean", value: true },
"foo": { type: "String", value: "hello" }
},
value: {
"bla": { type: "Boolean", value: true },
"foo": { type: "String", value: "hello" }
}
}
]
};
const encodedPayload = sparkplug.encodePayload(payload);
console.log("Encoded Payload:", encodedPayload);
console.log("Decoded Payload:", sparkplug.decodePayload(encodedPayload));
Encoding works - or at least I get printed out some bytes. But decoding fails with
Uncaught TypeError TypeError: Cannot read properties of undefined (reading 'keys') at decodePropertySet (/path/to/mydemoapp/node_modules/sparkplug-payload/lib/sparkplugbpayload.js:470:50)
I assume the error is somewhere in how I'm declaring my original payload.
Can someone please help me find out the problem?
Thanks.
I'm trying to encode/decode a Sparkplug Payload with a metric of type "PropertySet". I'm using the npm library https://www.npmjs/package/sparkplug-payload (v1.0.3)
This is my code so far:
const sparkplug = require("sparkplug-payload").get("spBv1.0");
const payload = {
metrics: [
{
name: "exampleMetric",
type: "PropertySet",
properties: {
"bla": { type: "Boolean", value: true },
"foo": { type: "String", value: "hello" }
},
value: {
"bla": { type: "Boolean", value: true },
"foo": { type: "String", value: "hello" }
}
}
]
};
const encodedPayload = sparkplug.encodePayload(payload);
console.log("Encoded Payload:", encodedPayload);
console.log("Decoded Payload:", sparkplug.decodePayload(encodedPayload));
Encoding works - or at least I get printed out some bytes. But decoding fails with
Uncaught TypeError TypeError: Cannot read properties of undefined (reading 'keys') at decodePropertySet (/path/to/mydemoapp/node_modules/sparkplug-payload/lib/sparkplugbpayload.js:470:50)
I assume the error is somewhere in how I'm declaring my original payload.
Can someone please help me find out the problem?
Thanks.
Share Improve this question asked Jan 29 at 9:11 ÑhoskoÑhosko 8052 gold badges10 silver badges29 bronze badges1 Answer
Reset to default 0It seems that I misunderstood the concept of PropertySet inside Sparkplug.
A PropertySet is not the value of a metric—it’s an additional set of key-value pairs that provide extra metadata for the metric.
With that information, the following payload worked correctly:
const payload = {
metrics: [
{
name: "exampleMetric",
type: "Int32",
value: 42,
properties: {
"bla": { type: "Boolean", value: true },
"foo": { type: "String", value: "hello" }
},
}
]
};