-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo-kitchen.js
79 lines (69 loc) · 2.48 KB
/
demo-kitchen.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
#!/usr/bin/env node
const EBG = require("./BalanceGroup.class.js");
const virtualMeters = {
'oven': { type: 'downstream', reading:0},
'microwave': { type: 'downstream', reading:0},
'dishwasher': { type: 'downstream', reading:0},
'coffee': { type: 'downstream', reading:0},
'kitchenmeter': { type: 'upstream', reading:0}
}
const simulateReadings = function() {
let totalConsumption = 0;
for (const [key, value] of Object.entries(virtualMeters)) {
let deviceConsumption = Math.round(Math.random() * 10);
if(value.type == 'downstream') {
value.reading += deviceConsumption;
totalConsumption += deviceConsumption;
}
}
virtualMeters.kitchenmeter.reading += totalConsumption;
// add some metering variation
virtualMeters.kitchenmeter.reading += Math.round( (Math.random() * 2) - 1 );
}
const updateBalance = function() {
for (const [key, value] of Object.entries(virtualMeters)) {
ebg.addReading(key,value.reading);
}
}
let demo_name = "Kitchen Demo";
console.log(demo_name);
// Initialize Balance
const ebg = new EBG(demo_name);
for (const [key, value] of Object.entries(virtualMeters)) {
ebg.setFeedMeta(key,{type:value.type});
}
let itteration = 0;
const runReadingStep = function() {
itteration++;
simulateReadings();
updateBalance();
console.log("Meter Readings (Itteration:"+itteration+")");
console.table(virtualMeters);
}
const runBalanceStep = function() {
const lastInsert = ebg.close();
const balances = ebg.getBalances();
if(balances.length>0) {
// transponate table
let rows =0;
if(balances[balances.length-1].table.upstream.length > rows) rows=balances[balances.length-1].table.upstream.length;
if(balances[balances.length-1].table.downstream.length > rows) rows=balances[balances.length-1].table.downstream.length;
let table = [];
console.log('Balance from ',new Date(balances[balances.length-1].time.start).toLocaleTimeString(),' until ',new Date(balances[balances.length-1].time.end).toLocaleTimeString());
for(let i=0;i<rows;i++) {
let row = {}
if(i<balances[balances.length-1].table.upstream.length) {
row.upstream = balances[balances.length-1].table.upstream[i];
}
if(i<balances[balances.length-1].table.downstream.length) {
row.downstream = balances[balances.length-1].table.downstream[i];
}
table.push(row);
}
console.table(table);
} else {
console.log("No Balance");
}
}
setInterval(runReadingStep,2000);
setInterval(runBalanceStep,4000);