node-red-contrib-dashboard-bar-chart-data
Advanced tools
Comparing version 0.9.2 to 0.9.3
@@ -30,23 +30,55 @@ module.exports = function(RED) { | ||
function clearNode(msg, myNode, store) { | ||
store.set(msg.topic + '_data', {}); | ||
store.set(msg.topic + '_data_counter', {}); | ||
store.set(msg.topic + '_last', {}); | ||
var topics = store.get('topics')||[]; | ||
//maybe only 1 specific topic should be cleared | ||
if (msg.topic != "") { | ||
topics = [msg.topic]; | ||
} | ||
//for all topics | ||
for (var i = 0; i < topics.length; i++) { | ||
store.set(topics[i] + '_data', {}); | ||
store.set(topics[i] + '_data_counter', {}); | ||
store.set(topics[i] + '_last', {}); | ||
} | ||
//clear topic array (or only specific topic) | ||
var topicsNew = []; | ||
var topicsOld = store.get('topics')||[]; | ||
if (msg.topic != "" && topicsOld.length > 1) { | ||
topicsNew = topicsOld; | ||
topicsNew.splice(topicsNew.indexOf(msg.topic),1); | ||
} | ||
store.set('topics', topicsNew); | ||
msg.payload = {}; | ||
msg.info = 'cleared'; | ||
msg.info = 'data cleared ' + msg.topic; | ||
}; | ||
function restoreNode(msg, myNode, store) { | ||
var data = msg.payload[0].data[0]; | ||
var data = msg.payload[0].data; | ||
var keys = msg.bar_keys; | ||
var restored_data = {}; | ||
for (var i = 0; i < data.length; i++) { | ||
restored_data[keys[i]] = data[i]; | ||
var topics = msg.topics; | ||
//restore with older bar-chart-data version | ||
if (topics === undefined) { topics = [msg.topic]; } | ||
//restore data for all topics | ||
for (var i = 0; i < topics.length; i++) { | ||
restoredData = {}; | ||
topicData = data[i]; | ||
if (topicData === undefined) { topicData = []; }; | ||
for (var i2 = 0; i < topicData.length; i++) { | ||
restoredData[keys[i]] = topicData[i2]; | ||
} | ||
topic = topics[i]; | ||
store.set(topic + '_data', restoredData); | ||
store.set(topic + '_data_counter', msg.data_counter[i]); | ||
if (msg.hasOwnProperty("last")) { | ||
store.set(topic + '_last', Number(msg.last)); | ||
} | ||
} | ||
store.set(msg.topic + '_data', restored_data); | ||
store.set(msg.topic + '_data_counter', msg.data_counter); | ||
if (msg.hasOwnProperty("last")) { | ||
store.set(msg.topic + '_last', Number(msg.last)); | ||
} | ||
msg.info = 'restored'; | ||
//restore topics array | ||
store.set('topics', topics); | ||
msg.info = 'data restored'; | ||
}; | ||
@@ -61,3 +93,4 @@ | ||
var curDate = new Date(); | ||
saveTopic(msg.topic, store); //save topic to store (for cleaning and handling of multiple topics) | ||
var topics = store.get('topics'); | ||
@@ -119,4 +152,4 @@ //if is_meter_reading == true, use diff between last and current payload value | ||
m.labels = buildLabels(curDate); | ||
m.series = [myNode.unit]; | ||
m.data = [[]]; | ||
m.series = topics; | ||
m.data = []; | ||
//build factor for the rounding | ||
@@ -127,9 +160,18 @@ var precision = 1; | ||
} | ||
newkeys.forEach(function(key) { | ||
if (data.hasOwnProperty(key)) { | ||
m.data[0].push(Math.round(data[key]*precision)/precision); | ||
} else { | ||
m.data[0].push(0); | ||
} | ||
}); | ||
//build data array for each topic | ||
var dataAll = [] | ||
for (var i = 0; i < topics.length; i++) { | ||
m.data.push([]); //add new array | ||
topic = topics[i]; | ||
data = store.get(topic + '_data')||[]; | ||
newkeys.forEach(function(key) { | ||
if (data.hasOwnProperty(key)) { | ||
m.data[i].push(Math.round(data[key]*precision)/precision); | ||
} else { | ||
m.data[i].push(0); | ||
} | ||
}); | ||
dataAll.push(data); | ||
} | ||
msg.payload=[m]; | ||
@@ -141,9 +183,10 @@ | ||
msg.bar_keys = newkeys; | ||
msg.data_counter = dataCounter; | ||
msg.data_counter = getDataCounters(store); | ||
msg.topics = topics; | ||
//add min,max,sum | ||
msg.data_min = Math.min(...m.data[0]); | ||
msg.data_max = Math.max(...m.data[0]); | ||
msg.data_min = Math.min(...dataAll); | ||
msg.data_max = Math.max(...dataAll); | ||
const arrSum = arr => arr.reduce((a,b) => a + b, 0); | ||
msg.data_sum = arrSum(m.data[0]); | ||
msg.data_sum = arrSum(dataAll); | ||
@@ -158,3 +201,2 @@ //put all settings into msg (could be used for dynamic chart titles etc.) | ||
}; | ||
return msg; | ||
@@ -264,2 +306,19 @@ | ||
}; | ||
function saveTopic(topic, store) { | ||
var topics = store.get('topics')||[]; | ||
if (topics.indexOf(topic) == -1) { | ||
topics.push(topic); | ||
store.set('topics', topics); | ||
} | ||
}; | ||
function getDataCounters(store) { | ||
var topics = store.get('topics')||[]; | ||
var dataCounter = []; | ||
for (var i = 0; i < topics.length; i++) { | ||
dataCounter.push(store.get(topics[i]+'_data_counter')||0); | ||
} | ||
return dataCounter; | ||
}; | ||
}; | ||
@@ -266,0 +325,0 @@ |
{ | ||
"name": "node-red-contrib-dashboard-bar-chart-data", | ||
"version": "0.9.2", | ||
"version": "0.9.3", | ||
"description": "Transforms sensor measurements or meter readings to be displayed in dashboard-chart as bar-chart", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/arneman/node-red-contrib-dashboard-bar-chart-data", |
# node-red-contrib-dashboard-bar-chart-data | ||
<h2>Bar-chart-data</h2> | ||
<p><a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=4GDDLTAZKJH54&source=url"><img src="https://img.shields.io/badge/Donate-PayPal-blue.svg?style=flat-square" alt="Donate via PayPal"></a></p> | ||
<p>This node transforms <code>msg.payload</code>, attaches (and stores) past values and build a message that could be linked to the <code>ui_chart</code> node configured as "bar chart". | ||
It can handle measurements (e.g. current power consumption) or meter readings (e.g. gas/water/energy meter count) and calculate the sum, min, max or average in a customizable time range.</p> | ||
It can handle measurements (e.g. current power consumption) or meter readings (e.g. gas/water/energy meter count) and calculate the sum, min, max or average in a customizable time range. | ||
Works with multiple topics, too.</p> | ||
@@ -62,3 +64,4 @@ <h3> Properties :</h3> | ||
<dl> | ||
<dd>A message with the payload "clear" and the same topic like the sensor messages will reset the data storage (so that the chart will be blank again).<br> | ||
<dd>A message with the payload "clear" and the same topic like the sensor messages will reset the data for this sensor/topic. | ||
A message with "clear" without a topic (empty string) will delete all data, so that the chart will be blank again.<br> | ||
If you put the output of this node to the input of this node (via <code>persist node</code>), the data will be restored. | ||
@@ -65,0 +68,0 @@ This could be helpful to avoid getting a blank bar-chart after node-red has been restarted (reboot). |
Sorry, the diff of this file is not supported yet
67449
284
94