最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - How to encode and decode a Sparkplug B payload of type PropertySet? - Stack Overflow

programmeradmin1浏览0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default 0

It 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" }
            },
        }
    ]
};
发布评论

评论列表(0)

  1. 暂无评论