-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneric-tariff-calculator.js
107 lines (92 loc) · 2.79 KB
/
generic-tariff-calculator.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
/**
* Class for calculating distribution prices.
*
* Copyright Markus Sipilä 2024. Published under Eclipse Public Licence v 2.0.
*/
class GenericTariffCalculator {
/**
* Constructor.
*
* @param {object} parameters
* Object with following properities:
* - {ZonedDateTime} start
* - {ZonedDateTime} end
* - {number} fallbackPrice
* - {Item} spotItem
*/
constructor(parameters) {
this.start = parameters.start;
this.end = parameters.end;
this.fallbackPrice = parameters.fallbackPrice;
this.spotItem = parameters.spotItem;
this.tariffs = [];
this.distributionPrices = new items.TimeSeries('REPLACE');
this.totalPrices = new items.TimeSeries('REPLACE');
}
/**
* Adds new tariff to the calculator.
*
* @param {Tariff} tariff
*/
addTariff(tariff) {
this.tariffs.push(tariff);
};
/**
* Calculates distribution price and total price for the given period.
*/
calculate() {
console.log("generic-tariff-calculator.js: Calculating distribution and total prices...");
// Read spot prices from persistence
const spotPrices = this.spotItem.persistence.getAllStatesBetween(this.start, this.end);
// Early exit if spot prices are not available.
if (spotPrices.length < 1) {
console.error(`generic-tariff-calculator.js: Aborting tariff calculations, no spot prices available for ${this.start} - ${this.end}`);
}
// Calculate distribution prices and total prices for the same timestamps as spot prices.
for (let i = 0; i < spotPrices.length; i++) {
let current = spotPrices[i].timestamp;
let distributionPrice = null;
// Find the first tariff that matches.
for (let j = 0; j < this.tariffs.length; j++) {
let t = this.tariffs[j];
if (t.matches(current)) {
console.debug(`${current}: match to ${t.getName()} (${t.getPrice()})`);
distributionPrice = t.getPrice();
break;
}
}
// If no tariff was found, use the fallback price.
if (distributionPrice === null) {
console.debug(`${current}: fallback price (${this.fallbackPrice})`);
distributionPrice = this.fallbackPrice;
}
// Calculate total price.
let totalPrice = spotPrices[i].numericState + distributionPrice;
// Add points to timeseries
this.distributionPrices.add(current, distributionPrice.toFixed(2));
this.totalPrices.add(current, totalPrice.toFixed(2));
}
}
/**
* Returns distribution prices.
*
* @return {TimeSeries}
*/
getDistributionPrices() {
return this.distributionPrices;
}
/**
* Returns total prices.
*
* @return {TimeSeries}
*/
getTotalPrices() {
return this.totalPrices;
}
}
/**
* Exports.
*/
module.exports = {
GenericTariffCalculator
}