-
Notifications
You must be signed in to change notification settings - Fork 0
/
nr-balance.js
52 lines (47 loc) · 1.7 KB
/
nr-balance.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
module.exports = function(RED) {
function EBNode(config) {
const Balance = require("./Balance.class.js");
RED.nodes.createNode(this,config);
const node = this;
const storage = node.context();
node.balance = new Balance(config.id);
const storedBalance = storage.get(config.id);
if((typeof storedBalance !== 'undefined') && (storedBalance !== null)) {
node.balance.fromString(storedBalance);
}
let dirtyCount = 0;
let lastPersisted = new Date().getTime();
this.addReading = function(feedId,upstream_or_downstream,reading,timems) {
dirtyCount++;
return node.balance.addReading(feedId,upstream_or_downstream,reading,timems);
}
this.addMeta = function(key,value) {
return node.balance.addMeta(key,value);
}
this.clearing = function() {
dirtyCount++;
return node.balance.clearing();
}
this.readingCleared = function() {
return node.balance.readingCleared();
}
this.carryOver = function() {
return node.balance.carryOver();
}
this.readingSettled = function() {
return node.balance.readingSettled();
}
this.lastBalance = function() {
return node.balance.lastBalance();
}
const persist = function() {
if((dirtyCount > 5) || ((new Date().getTime() - lastPersisted) > 60000)) {
storage.set(config.id,node.balance.toString());
lastPersisted = new Date().getTime();
dirtyCount = 0;
}
}
setInterval(persist,5000);
}
RED.nodes.registerType("Balance",EBNode);
}