-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
tibber-data.js
116 lines (108 loc) · 3.28 KB
/
tibber-data.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
class TibberData {
constructor(payload) {
this.tibberData = payload;
}
getCurrentPrice() {
return this.tibberData.prices.current.total;
}
consumptionUnit() {
const totalConsumption = this.tibberData.totalConsumption;
return totalConsumption[0] ? totalConsumption[0].consumptionUnit : "";
}
priceData(showFromTime, showToTime, colorForPriceLevel) {
const tibberData = this.tibberData;
const firstPriceTime = tibberData.prices.twoDays[0].startsAt;
const priceData = extractPriceDataFromConsumption()
.concat(extractPriceDataFromPrices())
.filter((p) => {
return p.x >= showFromTime && p.x <= showToTime;
});
priceData.minPrice = priceData.reduce((min, p) => (p.y < min ? p.y : min), priceData[0].y);
priceData.maxPrice = priceData.reduce((max, p) => (p.y > max ? p.y : max), priceData[0].y);
return priceData;
function extractPriceDataFromConsumption() {
return tibberData.totalConsumption
.filter((c) => {
return c.to <= firstPriceTime;
})
.map((c) => {
return {
x: Date.parse(c.from),
y: c.unitPrice,
color: colorForPriceLevel("UNKNOWN", 0),
id: ""
};
});
}
function extractPriceDataFromPrices() {
const prices = tibberData.prices;
return prices.twoDays.map((p, i, arr) => {
const last = arr.length - 1;
const now = Date.parse(prices.current.startsAt);
const t = Date.parse(p.startsAt);
const ci = t < now ? 0 : t === now ? 1 : 2;
return {
x: t,
y: p.total,
color: colorForPriceLevel(p.level, ci),
id: t === now ? "cur" : i == last ? "last" : ""
};
});
}
}
consumptionData(showFromTime, showConsumptionParts, colorFromLabel) {
const res = [];
let opacity = 1;
if (showConsumptionParts) {
this.tibberData.consumptions.map((p) => {
if (p.data.length > 0) {
res.push({
label: p.label,
stack: 2,
color: colorFromLabel(p.label),
visible: true,
opacity: opacity,
data: p.data
.map((c) => {
let t = Date.parse(c.from);
return {
x: t,
y: c.consumption,
id: c.consumption === this.maxConsumption() ? "maxCon" : c.consumption === this.minConsumption() ? "minCon" : ""
};
})
.filter((c) => c.x >= showFromTime)
});
opacity = opacity * 0.8;
}
});
}
res.push({
label: "Total",
stack: 1,
visible: !showConsumptionParts,
opacity: 1,
data: this.tibberData.totalConsumption
.map((c) => {
let t = Date.parse(c.from);
return {
x: t,
y: c.consumption,
id: c.consumption === this.maxConsumption() ? "maxCon" : c.consumption === this.minConsumption() ? "minCon" : ""
};
})
.filter((c) => c.x >= showFromTime)
});
console.log("showFromTime: ", showFromTime);
console.log("res: ", res);
return res;
}
minConsumption() {
const totalConsumption = this.tibberData.totalConsumption;
return totalConsumption.reduce((min, c) => (c.consumption < min ? c.consumption : min), totalConsumption[0] ? totalConsumption[0].consumption : null);
}
maxConsumption() {
const totalConsumption = this.tibberData.totalConsumption;
return this.tibberData.totalConsumption.reduce((max, c) => (c.consumption > max ? c.consumption : max), totalConsumption[0] ? totalConsumption[0].consumption : null);
}
}