-
Notifications
You must be signed in to change notification settings - Fork 1
/
fetchertest.js
56 lines (51 loc) · 1.52 KB
/
fetchertest.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
const FroniusFetcher = require("./FroniusFetcher");
const config = {
ip: "192.168.200.173",
wattConversionOptions: {
enabled: true,
threshold: 1200,
numDecimalDigits: 2,
},
};
async function fetch() {
const fetcher = new FroniusFetcher(config);
// const data = await fetcher.fetch();
const data = await fetcher.fetchDummyData();
console.log(data);
console.log(getWattString(data.energyTotal));
console.log(getWattString(data.currentEnergy));
}
function getWattString(value) {
const wattConversionOptions = config.wattConversionOptions;
const threshold = wattConversionOptions.threshold;
let wattString = "W";
let displayValue = value;
if (wattConversionOptions.enabled) {
switch (true) {
// GigaWatt
case value > threshold * Math.pow(1000, 2):
displayValue = (value / Math.pow(1000, 3)).toFixed(wattConversionOptions.numDecimalDigits);
wattString = "GW";
break;
// MegaWatt
case value > threshold * 1000:
displayValue = (value / Math.pow(1000, 2)).toFixed(wattConversionOptions.numDecimalDigits);
wattString = "MW";
break;
// KiloWatt
case value > threshold:
displayValue = (value / 1000).toFixed(wattConversionOptions.numDecimalDigits);
wattString = "KW";
break;
default:
displayValue = Math.round(value);
wattString = "W";
}
} else {
displayValue = Math.round(value);
}
return `${displayValue} ${wattString}`;
}
setInterval(async () => {
await fetch();
}, 3000);