-
Notifications
You must be signed in to change notification settings - Fork 0
/
nr-balancegroup.js
84 lines (80 loc) · 3.23 KB
/
nr-balancegroup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
module.exports = function(RED) {
function BGNode(config) {
const BG = require("./BalanceGroup.class.js");
RED.nodes.createNode(this,config);
const node = this;
const storage = node.context();
let bg = new BG(config.name);
let data = storage.get("json");
if((typeof data !== 'undefined') && (data !== null)) {
bg.fromString(data);
}
bg.on('Consensus',function(idx) {
if(idx>0) {
const balances = bg.getBalances();
const balance = balances[balances.length-1];
let ctrlMsg = {
topic:"consensus",
payload:balance
};
node.send(["consensus",ctrlMsg]);
}
});
node.on('input', async function(msg) {
node.status({fill:'yellow',shape:"dot",text:'processing'});
if((msg.topic == '_meta')&&(typeof msg.payload == 'object')&&(typeof msg.payload.feedId !== 'undefined')) {
bg.setFeedMeta(msg.payload.feedId,msg.payload.meta);
} else
if((msg.topic == '_meta')&&(Array.isArray(msg.payload))) {
for(let i=0;i<msg.payload.length;i++) {
bg.setFeedMeta(msg.payload[i].feedId,msg.payload[i].meta);
}
} else
if((msg.topic == '_ctrl')&&(msg.payload == 'missing')) {
node.send(["missing",{topic:"missing",payload:bg.missingPositions()}]);
;
} else
if((msg.topic == '_ctrl')&&(msg.payload == 'close')) {
bg.close();
let balances = bg.getBalances();
let lastBalance = balances.pop();
let ctrlMsg = {
topic:"close",
payload:lastBalance
};
node.send(["close",ctrlMsg]);
} else
if((msg.topic == '_ctrl')&&(msg.payload == 'balances')) {
console.log(bg);
let ctrlMsg = {
topic:"balances",
payload:bg.getBalances()
};
node.send(["balances",ctrlMsg]);
} else
if((typeof msg.payload == 'object')&&(msg.payload !== null)) {
let topic_ext = '';
if((typeof msg.topic !== 'undefined') && (msg.topic !== null) && (msg.topic.length > 0)) {
topic_ext = msg.topic + "_";
}
for (const [key, value] of Object.entries(msg.payload)) {
if(!isNaN(value)) {
bg.addReading(topic_ext+key,value);
}
}
} else
if((!isNaN(msg.payload)) && (typeof msg.topic !== 'undefined')) {
bg.addReading(msg.topic,msg.payload);
}
await storage.set("json",bg.toString());
await storage.set("data",JSON.parse(bg.toString()));
let txt = "";
let sums = bg.sums();
for (const [key, value] of Object.entries(sums)) {
txt += key+":"+ Math.round(value) + " ";
}
node.status({fill:'white',shape:"dot",text:txt});
});
}
RED.nodes.registerType("BalanceGroup",BGNode);
}