首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Thingsboard :在一个保存时间序列节点帖子中保存多个时间序列读数

Thingsboard :在一个保存时间序列节点帖子中保存多个时间序列读数
EN

Stack Overflow用户
提问于 2021-11-06 03:46:33
回答 1查看 55关注 0票数 0

我正在尝试将一系列带有时间戳的时间序列读数保存为一个数组,保存在保存时间序列节点的一篇文章中。我获得转换脚本节点函数的msg参数中的值,将其转换为预期的格式,并将它们保存在save timeseries节点中。在此之后,我打算将它们绘制在折线图中。

我尝试过用两种不同的方法格式化输出JSON,但它总是只保存数组的最后一个值。有没有办法实现批量节省呢?

下面是save timeseries节点的两个不同输出:

代码语言:javascript
复制
[{
    "ts": 1636160453000,
    "reading": {
        "value": 536.5833333333334
    }
}, {
    "ts": 1636158641000,
    "reading": {
        "value": 538.4666666666667
    }
}, {
    "ts": 1636156829000,
    "reading": {
        "value": 547.9333333333333
    }
}, {
    "ts": 1636155019000,
    "reading": {
        "value": 523.4666666666667
    }
}, {
    "ts": 1636153207000,
    "reading": {
        "value": 549.8666666666667
    }
}, {
    "ts": 1636151455000,
    "reading": {
        "value": 516.0344827586207
    }
}, {
    "ts": 1636149643000,
    "reading": {
        "value": 500.26666666666665
    }
}, {
    "ts": 1636147831000,
    "reading": {
        "value": 496.56666666666666
    }
}, {
    "ts": 1636146020000,
    "reading": {
        "value": 521.7
    }
}, {
    "ts": 1636144210000,
    "reading": {
        "value": 543
    }
}]

***************************

[{
    "ts": 1636160453000,
    "reading": 588.5714285714286
}, {
    "ts": 1636158641000,
    "reading": 538.4666666666667
}, {
    "ts": 1636156829000,
    "reading": 547.9333333333333
}, {
    "ts": 1636155019000,
    "reading": 523.4666666666667
}, {
    "ts": 1636153207000,
    "reading": 549.8666666666667
}, {
    "ts": 1636151455000,
    "reading": 516.0344827586207
}, {
    "ts": 1636149643000,
    "reading": 500.26666666666665
}, {
    "ts": 1636147831000,
    "reading": 496.56666666666666
}, {
    "ts": 1636146020000,
    "reading": 521.7
}, {
    "ts": 1636144210000,
    "reading": 543
}]

谢谢你!!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-08 04:14:18

在保存timeseries节点之前放置一个Transformation - Script节点。这些节点可以输出充当单个消息的值列表(假设TB3.3.1)。

在你的情况下,我建议这样做

代码语言:javascript
复制
// Don't include these, I'm just including them to get this snippet working
var msg = [{"ts": 12345, "readings": { "value": 500}}, {"ts": 54321, "readings": { "value": 600}}]
var metadata = {"deviceName": "Example", "deviceType": "default", "ts": 100000}

// Include below this line
var result = []
msg.forEach(function (reading) {
  var newMsg = reading.readings;
  var newMeta = metadata
  
  newMeta.ts = reading.ts
  result.push({
    "msg": newMsg,
    "metadata": newMeta,
    "msgType": "POST_TELEMETRY_REQUEST"
  })
})

console.log(result) // Delete me
//return result // <<< Uncomment this line

这将发送一条消息给您每次读取时保存时间序列节点,并添加正确的时间戳。

我在我的解决方案中使用了类似的代码,尽管我在编写上面的代码之前没有测试过这个例子,所以您可能需要稍微修改一下。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69861305

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档