我試圖在 Save Timeseries 節點的一個 POST 中保存作為陣列獲得的一系列帶時間戳的時間序列讀數。我在轉換腳本節點函式的 msg 引數中獲取值,將其轉換為預期格式,并將它們保存在保存時間序列節點中。之后,我打算將它們繪制在折線圖中。
我嘗試以兩種不同的方式格式化輸出 JSON,但它總是只保存陣列的最后一個值。有什么辦法可以批量保存嗎?
以下是保存時間序列節點的 2 個不同輸出:
[{
"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
}]
謝謝!!
uj5u.com熱心網友回復:
在您的保存時間序列節點之前放置一個轉換 - 腳本節點。這些節點可以輸出作為單獨訊息的值串列(假設為 TB3.3.1)。
在你的情況下,我會建議這樣的
// 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
這將在每次讀取時向您發送一條訊息給保存時間序列節點,并將添加正確的時間戳。
我在我的解決方案中使用了類似的代碼,盡管在撰寫上述代碼之前我沒有測驗這個確切的例子,所以你可能需要稍微修改它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/353824.html
