iobroker.meter-statistics 0.0.4 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -0
- package/admin/jsonConfig.json +69 -30
- package/admin/meter-statistics.png +0 -0
- package/admin/meter-statistics.svg +10 -10
- package/build/calculator.js +93 -0
- package/build/calculator.js.map +7 -0
- package/build/lib/typedef.js.map +1 -1
- package/build/main.js +87 -147
- package/build/main.js.map +3 -3
- package/build/period.js +61 -0
- package/build/period.js.map +7 -0
- package/build/round.js +42 -0
- package/build/round.js.map +7 -0
- package/io-package.json +15 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -108,6 +108,10 @@ Please refer to the [`dev-server` documentation](https://github.com/ioBroker/dev
|
|
|
108
108
|
Placeholder for the next version (at the beginning of the line):
|
|
109
109
|
### **WORK IN PROGRESS**
|
|
110
110
|
-->
|
|
111
|
+
### 0.1.0 (2023-03-18)
|
|
112
|
+
* [TASK] Code refactoring
|
|
113
|
+
* [TASK] Add test cases
|
|
114
|
+
|
|
111
115
|
### 0.0.4 (2023-03-05)
|
|
112
116
|
* [BUGFIX] Fix calculation of predicted total
|
|
113
117
|
* [TASK] Update project
|
package/admin/jsonConfig.json
CHANGED
|
@@ -9,14 +9,24 @@
|
|
|
9
9
|
},
|
|
10
10
|
"units": {
|
|
11
11
|
"type": "panel",
|
|
12
|
+
"xs": 12,
|
|
12
13
|
"label": "units",
|
|
13
14
|
"items": {
|
|
14
15
|
"meterUnit": {
|
|
15
16
|
"type": "text",
|
|
16
|
-
"
|
|
17
|
+
"xs": 12,
|
|
18
|
+
"sm": 6,
|
|
19
|
+
"md": 4,
|
|
20
|
+
"lg": 3,
|
|
21
|
+
"label": "Unit of meter value",
|
|
22
|
+
"newLine": true
|
|
17
23
|
},
|
|
18
24
|
"paymentUnit": {
|
|
19
25
|
"type": "text",
|
|
26
|
+
"xs": 12,
|
|
27
|
+
"sm": 6,
|
|
28
|
+
"md": 4,
|
|
29
|
+
"lg": 3,
|
|
20
30
|
"label": "Unit of payment"
|
|
21
31
|
}
|
|
22
32
|
}
|
|
@@ -28,28 +38,43 @@
|
|
|
28
38
|
},
|
|
29
39
|
"payment": {
|
|
30
40
|
"type": "panel",
|
|
41
|
+
"xs": 12,
|
|
31
42
|
"label": "payment",
|
|
32
43
|
"items": {
|
|
33
44
|
"paymentValue": {
|
|
34
45
|
"type": "number",
|
|
46
|
+
"xs": 12,
|
|
47
|
+
"sm": 6,
|
|
48
|
+
"md": 4,
|
|
49
|
+
"lg": 3,
|
|
35
50
|
"label": "Payment value",
|
|
36
51
|
"newLine": true
|
|
37
52
|
},
|
|
38
53
|
"paymentCount": {
|
|
39
54
|
"type": "number",
|
|
55
|
+
"xs": 12,
|
|
56
|
+
"sm": 6,
|
|
57
|
+
"md": 4,
|
|
58
|
+
"lg": 3,
|
|
40
59
|
"label": "Payment count",
|
|
41
|
-
"min": 1
|
|
42
|
-
"newLine": true
|
|
60
|
+
"min": 1
|
|
43
61
|
},
|
|
44
62
|
"paymentBasePrice": {
|
|
45
63
|
"type": "number",
|
|
64
|
+
"xs": 12,
|
|
65
|
+
"sm": 6,
|
|
66
|
+
"md": 4,
|
|
67
|
+
"lg": 3,
|
|
46
68
|
"label": "Base price that is paid once",
|
|
47
69
|
"newLine": true
|
|
48
70
|
},
|
|
49
71
|
"paymentCorrectionOffset": {
|
|
50
72
|
"type": "number",
|
|
51
|
-
"
|
|
52
|
-
"
|
|
73
|
+
"xs": 12,
|
|
74
|
+
"sm": 6,
|
|
75
|
+
"md": 4,
|
|
76
|
+
"lg": 3,
|
|
77
|
+
"label": "Payment correction offset"
|
|
53
78
|
}
|
|
54
79
|
}
|
|
55
80
|
},
|
|
@@ -59,33 +84,47 @@
|
|
|
59
84
|
"size": 2
|
|
60
85
|
},
|
|
61
86
|
"meters": {
|
|
62
|
-
"type": "
|
|
63
|
-
"
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
"
|
|
68
|
-
"
|
|
69
|
-
"
|
|
70
|
-
"
|
|
71
|
-
|
|
72
|
-
{
|
|
73
|
-
"attr": "alias",
|
|
74
|
-
"width": "30%",
|
|
75
|
-
"title": "Meter alias",
|
|
76
|
-
"type": "text" ,
|
|
77
|
-
"validator": "!!data.alias",
|
|
78
|
-
"validatorNoSaveOnError": true
|
|
87
|
+
"type": "panel",
|
|
88
|
+
"xs": 12,
|
|
89
|
+
"label": "payment",
|
|
90
|
+
"items": {
|
|
91
|
+
"summaryName": {
|
|
92
|
+
"type": "text",
|
|
93
|
+
"xs": 12,
|
|
94
|
+
"sm": 6,
|
|
95
|
+
"label": "Name of summary node that contains sum of all meters",
|
|
96
|
+
"newLine": true
|
|
79
97
|
},
|
|
80
|
-
{
|
|
81
|
-
"
|
|
82
|
-
"
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
98
|
+
"meters": {
|
|
99
|
+
"type": "table",
|
|
100
|
+
"items": [
|
|
101
|
+
{
|
|
102
|
+
"attr": "objectId",
|
|
103
|
+
"width": "50%",
|
|
104
|
+
"title": "Object ID",
|
|
105
|
+
"type": "objectId",
|
|
106
|
+
"validator": "!!data.objectId",
|
|
107
|
+
"validatorNoSaveOnError": true
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
"attr": "alias",
|
|
111
|
+
"width": "30%",
|
|
112
|
+
"title": "Meter alias",
|
|
113
|
+
"type": "text",
|
|
114
|
+
"validator": "!!data.alias",
|
|
115
|
+
"validatorNoSaveOnError": true
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
"attr": "pricePerUnit",
|
|
119
|
+
"width": "20%",
|
|
120
|
+
"title": "Price per unit",
|
|
121
|
+
"type": "number",
|
|
122
|
+
"validator": "!!data.pricePerUnit",
|
|
123
|
+
"validatorNoSaveOnError": true
|
|
124
|
+
}
|
|
125
|
+
]
|
|
87
126
|
}
|
|
88
|
-
|
|
127
|
+
}
|
|
89
128
|
}
|
|
90
129
|
}
|
|
91
130
|
}
|
|
Binary file
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
|
2
2
|
<svg
|
|
3
|
-
width="
|
|
3
|
+
width="400"
|
|
4
4
|
height="400"
|
|
5
|
-
viewBox="0 0
|
|
5
|
+
viewBox="0 0 105.83333 105.83333"
|
|
6
6
|
version="1.1"
|
|
7
7
|
id="svg5"
|
|
8
8
|
inkscape:version="1.1.2 (0a00cf5339, 2022-02-04)"
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
inkscape:document-units="mm"
|
|
23
23
|
showgrid="false"
|
|
24
24
|
inkscape:zoom="1.1152113"
|
|
25
|
-
inkscape:cx="
|
|
25
|
+
inkscape:cx="43.041171"
|
|
26
26
|
inkscape:cy="171.71634"
|
|
27
27
|
inkscape:window-width="1862"
|
|
28
28
|
inkscape:window-height="1016"
|
|
@@ -156,36 +156,36 @@
|
|
|
156
156
|
id="rect31"
|
|
157
157
|
width="78.309998"
|
|
158
158
|
height="104.76833"
|
|
159
|
-
x="
|
|
159
|
+
x="13.761666"
|
|
160
160
|
y="0.53250003"
|
|
161
161
|
inkscape:path-effect="#path-effect3631"
|
|
162
|
-
d="M
|
|
162
|
+
d="M 16.4075,0.53250003 H 89.425831 A 2.6458333,2.6458333 45 0 1 92.071664,3.1783333 V 102.655 a 2.6458333,2.6458333 135 0 1 -2.645833,2.64583 H 16.4075 A 2.6458333,2.6458333 45 0 1 13.761666,102.655 V 3.1783333 A 2.6458333,2.6458333 135 0 1 16.4075,0.53250003 Z"
|
|
163
163
|
sodipodi:type="rect" />
|
|
164
164
|
<path
|
|
165
165
|
style="fill:#ffffff;fill-opacity:1;stroke:#b1b1b1;stroke-width:1.065;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
|
|
166
166
|
id="rect3114"
|
|
167
167
|
width="57.143333"
|
|
168
168
|
height="30.68499"
|
|
169
|
-
x="
|
|
169
|
+
x="24.344997"
|
|
170
170
|
y="12.382141"
|
|
171
171
|
inkscape:path-effect="#path-effect3633"
|
|
172
|
-
d="m
|
|
172
|
+
d="m 26.990831,12.382141 h 51.851667 a 2.6458333,2.6458333 45 0 1 2.645833,2.645833 v 25.393324 a 2.6458333,2.6458333 135 0 1 -2.645833,2.645833 H 26.990831 A 2.6458333,2.6458333 45 0 1 24.344997,40.421298 V 15.027974 a 2.6458333,2.6458333 135 0 1 2.645834,-2.645833 z"
|
|
173
173
|
sodipodi:type="rect" />
|
|
174
174
|
<text
|
|
175
175
|
xml:space="preserve"
|
|
176
176
|
style="font-style:normal;font-weight:normal;font-size:20.2809px;line-height:1.25;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.362157"
|
|
177
|
-
x="
|
|
177
|
+
x="34.916286"
|
|
178
178
|
y="29.418192"
|
|
179
179
|
id="text1838"
|
|
180
180
|
transform="scale(0.78891107,1.26757)"><tspan
|
|
181
181
|
sodipodi:role="line"
|
|
182
182
|
id="tspan1836"
|
|
183
183
|
style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:20.2809px;font-family:Ani;-inkscape-font-specification:Ani;stroke-width:0.362157"
|
|
184
|
-
x="
|
|
184
|
+
x="34.916286"
|
|
185
185
|
y="29.418192">0000.0</tspan></text>
|
|
186
186
|
<g
|
|
187
187
|
id="g59969"
|
|
188
|
-
transform="translate(
|
|
188
|
+
transform="translate(12.749672)">
|
|
189
189
|
<text
|
|
190
190
|
xml:space="preserve"
|
|
191
191
|
style="font-style:normal;font-weight:normal;font-size:14.5511px;line-height:1.25;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#b1b1b1;fill-opacity:1;stroke:none;stroke-width:0.202098"
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
|
+
mod
|
|
23
|
+
));
|
|
24
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
25
|
+
var calculator_exports = {};
|
|
26
|
+
__export(calculator_exports, {
|
|
27
|
+
default: () => Calculator
|
|
28
|
+
});
|
|
29
|
+
module.exports = __toCommonJS(calculator_exports);
|
|
30
|
+
var import_period = __toESM(require("./period"));
|
|
31
|
+
class Calculator {
|
|
32
|
+
constructor(config) {
|
|
33
|
+
this.period = new import_period.default();
|
|
34
|
+
this.config = config;
|
|
35
|
+
this.summaryName = config.summaryName;
|
|
36
|
+
}
|
|
37
|
+
initMeterInfo() {
|
|
38
|
+
return {
|
|
39
|
+
balance: 0,
|
|
40
|
+
consumptionAverage: 0,
|
|
41
|
+
consumptionPredictedTotal: 0,
|
|
42
|
+
consumptionTotal: 0,
|
|
43
|
+
costs: 0,
|
|
44
|
+
paid: 0,
|
|
45
|
+
recommendedPayment: 0
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
calculateMeterInfos(meterStates) {
|
|
49
|
+
const summary = this.initMeterInfo();
|
|
50
|
+
const collection = {
|
|
51
|
+
[this.summaryName]: summary
|
|
52
|
+
};
|
|
53
|
+
const meters = [];
|
|
54
|
+
for (const state of meterStates) {
|
|
55
|
+
const info = this.initMeterInfo();
|
|
56
|
+
const definition = state.meter;
|
|
57
|
+
collection[definition.alias] = info;
|
|
58
|
+
meters.push({ info, state, definition });
|
|
59
|
+
}
|
|
60
|
+
for (const { info, state } of meters) {
|
|
61
|
+
info.consumptionTotal = state.consumption;
|
|
62
|
+
info.consumptionAverage = this.period.calculateValuePerDay(state.consumption);
|
|
63
|
+
info.consumptionPredictedTotal = this.period.calculateExtrapolateValueForPeriod(state.consumption);
|
|
64
|
+
summary.consumptionTotal += info.consumptionTotal;
|
|
65
|
+
summary.consumptionAverage += info.consumptionAverage;
|
|
66
|
+
summary.consumptionPredictedTotal += info.consumptionPredictedTotal;
|
|
67
|
+
}
|
|
68
|
+
const basePrice = this.period.calculateElapsedAmountInPeriod(this.config.paymentBasePrice);
|
|
69
|
+
const paid = this.calculatePaid();
|
|
70
|
+
for (const { info, state, definition } of meters) {
|
|
71
|
+
const factor = info.consumptionTotal / summary.consumptionTotal;
|
|
72
|
+
info.costs = state.consumption * definition.pricePerUnit + basePrice * factor;
|
|
73
|
+
info.paid = paid * factor;
|
|
74
|
+
info.balance = info.paid - info.costs;
|
|
75
|
+
info.recommendedPayment = this.calculateRecommendation(info.costs);
|
|
76
|
+
summary.costs += info.costs;
|
|
77
|
+
summary.paid += info.paid;
|
|
78
|
+
summary.balance += info.balance;
|
|
79
|
+
summary.recommendedPayment += info.recommendedPayment;
|
|
80
|
+
}
|
|
81
|
+
return collection;
|
|
82
|
+
}
|
|
83
|
+
calculatePaid() {
|
|
84
|
+
const config = this.config;
|
|
85
|
+
return config.paymentCorrectionOffset + this.period.calculateElapsedAmountInPeriod(config.paymentValue * config.paymentCount);
|
|
86
|
+
}
|
|
87
|
+
calculateRecommendation(costs) {
|
|
88
|
+
return this.period.calculateExtrapolateValueForPeriod(costs) / this.config.paymentCount;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
92
|
+
0 && (module.exports = {});
|
|
93
|
+
//# sourceMappingURL=calculator.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/calculator.ts"],
|
|
4
|
+
"sourcesContent": ["import {CalculatorConfig, MeterDefinition, MeterInfo, MeterInfoCollection, MeterState} from './lib/typedef';\nimport Period from './period';\n\nexport default class Calculator {\n\n private readonly summaryName: string;\n\n private readonly config: CalculatorConfig;\n private readonly period: Period;\n\n constructor(config: CalculatorConfig) {\n this.period = new Period();\n this.config = config;\n this.summaryName = config.summaryName;\n }\n\n private initMeterInfo(): MeterInfo {\n return {\n balance: 0,\n consumptionAverage: 0,\n consumptionPredictedTotal: 0,\n consumptionTotal: 0,\n costs: 0,\n paid: 0,\n recommendedPayment: 0,\n };\n }\n public calculateMeterInfos(meterStates: MeterState[]): MeterInfoCollection\n {\n const summary = this.initMeterInfo();\n const collection: MeterInfoCollection = {\n [this.summaryName]: summary,\n };\n\n const meters: Array<{info: MeterInfo, state: MeterState, definition: MeterDefinition}> = [];\n\n for (const state of meterStates) {\n const info = this.initMeterInfo();\n\n const definition = state.meter;\n collection[definition.alias] = info;\n\n meters.push({info, state, definition});\n }\n\n for (const {info, state} of meters) {\n // TODO use period depending on state.startValueDateTime\n // const startValueDateTime = state.startValueDateTime;\n info.consumptionTotal = state.consumption;\n info.consumptionAverage = this.period.calculateValuePerDay(state.consumption);\n info.consumptionPredictedTotal = this.period.calculateExtrapolateValueForPeriod(state.consumption);\n\n summary.consumptionTotal += info.consumptionTotal;\n summary.consumptionAverage += info.consumptionAverage;\n summary.consumptionPredictedTotal += info.consumptionPredictedTotal;\n }\n\n const basePrice = this.period.calculateElapsedAmountInPeriod(this.config.paymentBasePrice);\n const paid = this.calculatePaid();\n for (const {info, state, definition} of meters) {\n const factor = info.consumptionTotal / summary.consumptionTotal;\n\n info.costs = state.consumption * definition.pricePerUnit + basePrice * factor;\n info.paid = paid * factor;\n info.balance = info.paid - info.costs;\n info.recommendedPayment = this.calculateRecommendation(info.costs);\n\n summary.costs += info.costs;\n summary.paid += info.paid;\n summary.balance += info.balance;\n summary.recommendedPayment += info.recommendedPayment;\n }\n\n return collection;\n }\n\n\n private calculatePaid(): number {\n const config = this.config;\n return config.paymentCorrectionOffset + this.period.calculateElapsedAmountInPeriod(config.paymentValue * config.paymentCount);\n }\n\n private calculateRecommendation(costs: number): number {\n return this.period.calculateExtrapolateValueForPeriod(costs) / this.config.paymentCount;\n }\n\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,oBAAmB;AAEnB,MAAO,WAAyB;AAAA,EAO5B,YAAY,QAA0B;AAClC,SAAK,SAAS,IAAI,cAAAA,QAAO;AACzB,SAAK,SAAS;AACd,SAAK,cAAc,OAAO;AAAA,EAC9B;AAAA,EAEQ,gBAA2B;AAC/B,WAAO;AAAA,MACH,SAAS;AAAA,MACT,oBAAoB;AAAA,MACpB,2BAA2B;AAAA,MAC3B,kBAAkB;AAAA,MAClB,OAAO;AAAA,MACP,MAAM;AAAA,MACN,oBAAoB;AAAA,IACxB;AAAA,EACJ;AAAA,EACO,oBAAoB,aAC3B;AACI,UAAM,UAAU,KAAK,cAAc;AACnC,UAAM,aAAkC;AAAA,MACpC,CAAC,KAAK,cAAc;AAAA,IACxB;AAEA,UAAM,SAAmF,CAAC;AAE1F,eAAW,SAAS,aAAa;AAC7B,YAAM,OAAO,KAAK,cAAc;AAEhC,YAAM,aAAa,MAAM;AACzB,iBAAW,WAAW,SAAS;AAE/B,aAAO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face MeterState {\n meter: MeterDefinition;\n startValue: number;\n currentValue: number;\n consumption: number;\n}\n\nexport interface MeterDefinition {\n objectId: string;\n alias: string;\n pricePerUnit: number;\n}\n\nexport interface MeterConsumption {\n total: number;\n average: number;\n predicted: number;\n}\n\nexport interface DayInfo {\n firstDayOfYear: Date;\n daysInYear: number;\n daysSinceStartOfYear: number;\n}\n"],
|
|
4
|
+
"sourcesContent": ["export interface MeterState {\n meter: MeterDefinition;\n startValue: number;\n startValueDateTime: Date|null;\n currentValue: number;\n consumption: number;\n}\n\nexport interface MeterDefinition {\n objectId: string;\n alias: string;\n pricePerUnit: number;\n}\n\nexport interface CalculatorConfig {\n paymentValue: number;\n paymentCount: number;\n paymentCorrectionOffset: number;\n paymentBasePrice: number;\n summaryName: string;\n}\n\nexport interface MeterConsumption {\n total: number;\n average: number;\n predicted: number;\n}\n\nexport interface MeterInfo {\n balance: number;\n consumptionAverage: number;\n consumptionPredictedTotal: number;\n consumptionTotal: number;\n costs: number;\n paid: number;\n recommendedPayment: number;\n}\n\nexport interface MeterInfoCollection {\n [key: string]: MeterInfo\n}\n\nexport interface DayInfo {\n firstDayOfYear: Date;\n daysInYear: number;\n daysSinceStartOfYear: number;\n}\n"],
|
|
5
5
|
"mappings": ";;;;;;;;;;;;;;AAAA;AAAA;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/build/main.js
CHANGED
|
@@ -18,6 +18,8 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
18
18
|
mod
|
|
19
19
|
));
|
|
20
20
|
var utils = __toESM(require("@iobroker/adapter-core"));
|
|
21
|
+
var import_calculator = __toESM(require("./calculator"));
|
|
22
|
+
var import_round = __toESM(require("./round"));
|
|
21
23
|
class MeterStatistics extends utils.Adapter {
|
|
22
24
|
constructor(options = {}) {
|
|
23
25
|
super({
|
|
@@ -31,84 +33,27 @@ class MeterStatistics extends utils.Adapter {
|
|
|
31
33
|
}
|
|
32
34
|
async onReady() {
|
|
33
35
|
await this.assertObjectsExist();
|
|
34
|
-
const dayInfo = this.getDayInfo();
|
|
35
36
|
const meterStates = await this.getMeterStates();
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
-
this.
|
|
39
|
-
this.writeState("summary.consumptionPredictedTotal", this.roundConsumption(predicted));
|
|
40
|
-
const costs = this.calculateCosts(meterStates, dayInfo);
|
|
41
|
-
this.writeState("summary.costs", this.roundCosts(costs));
|
|
42
|
-
const paid = this.calculatePaid(dayInfo);
|
|
43
|
-
this.writeState("summary.paid", this.roundCosts(paid));
|
|
44
|
-
const current = paid - costs;
|
|
45
|
-
this.writeState("summary.balance", this.roundCosts(current));
|
|
46
|
-
const recommendedPayment = this.calculateRecommendation(costs, dayInfo);
|
|
47
|
-
this.writeState("summary.recommendedPayment", this.roundCosts(recommendedPayment));
|
|
37
|
+
const calculator = new import_calculator.default(this.config);
|
|
38
|
+
const meterInfos = calculator.calculateMeterInfos(meterStates);
|
|
39
|
+
this.writeMeterInfos(meterInfos);
|
|
48
40
|
this.end();
|
|
49
41
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
this.writeState(`
|
|
58
|
-
this.writeState(`
|
|
59
|
-
this.writeState(`
|
|
60
|
-
totalConsumption += meterState.consumption;
|
|
61
|
-
}
|
|
62
|
-
return {
|
|
63
|
-
total: totalConsumption,
|
|
64
|
-
average: totalConsumption / dayInfo.daysSinceStartOfYear,
|
|
65
|
-
predicted: totalConsumption / dayInfo.daysSinceStartOfYear * dayInfo.daysInYear
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
calculateCosts(meterStates, dayInfo) {
|
|
69
|
-
let usedCosts = 0;
|
|
70
|
-
const basePricePerDay = this.config.paymentBasePrice / dayInfo.daysInYear;
|
|
71
|
-
usedCosts += basePricePerDay * dayInfo.daysSinceStartOfYear;
|
|
72
|
-
for (const meterState of meterStates) {
|
|
73
|
-
const meter = meterState.meter;
|
|
74
|
-
this.writeState(`meter.${meter.alias}.costs`, this.roundCosts(meterState.consumption * meterState.meter.pricePerUnit));
|
|
75
|
-
usedCosts += meterState.consumption * meterState.meter.pricePerUnit;
|
|
42
|
+
writeMeterInfos(meterInfos) {
|
|
43
|
+
for (const meterName in meterInfos) {
|
|
44
|
+
const meterInfo = meterInfos[meterName];
|
|
45
|
+
this.writeState(`meters.${meterName}.balance`, import_round.default.costs(meterInfo.balance));
|
|
46
|
+
this.writeState(`meters.${meterName}.consumptionAverage`, import_round.default.consumption(meterInfo.consumptionAverage));
|
|
47
|
+
this.writeState(`meters.${meterName}.consumptionPredictedTotal`, import_round.default.consumption(meterInfo.consumptionPredictedTotal));
|
|
48
|
+
this.writeState(`meters.${meterName}.consumptionTotal`, import_round.default.consumption(meterInfo.consumptionTotal));
|
|
49
|
+
this.writeState(`meters.${meterName}.costs`, import_round.default.costs(meterInfo.costs));
|
|
50
|
+
this.writeState(`meters.${meterName}.paid`, import_round.default.costs(meterInfo.paid));
|
|
51
|
+
this.writeState(`meters.${meterName}.recommendedPayment`, import_round.default.costs(meterInfo.recommendedPayment));
|
|
76
52
|
}
|
|
77
|
-
return usedCosts;
|
|
78
|
-
}
|
|
79
|
-
calculatePaid(dayInfo) {
|
|
80
|
-
let paid = this.config.paymentCorrectionOffset;
|
|
81
|
-
const paymentPerDay = this.config.paymentValue * this.config.paymentCount / dayInfo.daysInYear;
|
|
82
|
-
paid += paymentPerDay * dayInfo.daysSinceStartOfYear;
|
|
83
|
-
return paid;
|
|
84
|
-
}
|
|
85
|
-
calculateRecommendation(costs, dayInfo) {
|
|
86
|
-
return this.roundCosts(costs / dayInfo.daysSinceStartOfYear * dayInfo.daysInYear / this.config.paymentCount);
|
|
87
|
-
}
|
|
88
|
-
roundCosts(costs) {
|
|
89
|
-
return Math.round(costs * 100) / 100;
|
|
90
|
-
}
|
|
91
|
-
roundConsumption(consumption) {
|
|
92
|
-
return Math.round(consumption * 1e3) / 1e3;
|
|
93
|
-
}
|
|
94
|
-
getDaysInYear() {
|
|
95
|
-
return 365;
|
|
96
|
-
}
|
|
97
|
-
getDaysSince(start) {
|
|
98
|
-
const now = new Date();
|
|
99
|
-
const diff = now.valueOf() - start.valueOf();
|
|
100
|
-
const oneDay = 1e3 * 60 * 60 * 24;
|
|
101
|
-
return Math.floor(diff / oneDay);
|
|
102
53
|
}
|
|
103
54
|
writeState(id, value) {
|
|
104
|
-
this.
|
|
105
|
-
|
|
106
|
-
async getMeterStates() {
|
|
107
|
-
const meterStates = [];
|
|
108
|
-
for (const meter of this.config.meters) {
|
|
109
|
-
meterStates.push(await this.getMeterState(meter));
|
|
110
|
-
}
|
|
111
|
-
return meterStates;
|
|
55
|
+
const promise = this.setStateAsync(id, { val: value, ack: false });
|
|
56
|
+
this.pendingPromises.push(promise);
|
|
112
57
|
}
|
|
113
58
|
getNumericStateValue(state, onErrorMessage) {
|
|
114
59
|
const stateValue = state == null ? void 0 : state.val;
|
|
@@ -117,31 +62,61 @@ class MeterStatistics extends utils.Adapter {
|
|
|
117
62
|
}
|
|
118
63
|
throw new Error(onErrorMessage);
|
|
119
64
|
}
|
|
65
|
+
async getMeterStates() {
|
|
66
|
+
const meterStates = [];
|
|
67
|
+
for (const meter of this.config.meters) {
|
|
68
|
+
meterStates.push(await this.getMeterState(meter));
|
|
69
|
+
}
|
|
70
|
+
return meterStates;
|
|
71
|
+
}
|
|
120
72
|
async getMeterState(meter) {
|
|
73
|
+
var _a;
|
|
74
|
+
const meterName = meter.alias;
|
|
75
|
+
const currentValueState = await this.getForeignStateAsync(meter.objectId);
|
|
121
76
|
const currentValue = this.getNumericStateValue(
|
|
122
|
-
|
|
123
|
-
`Could not read meter "${
|
|
124
|
-
);
|
|
77
|
+
currentValueState,
|
|
78
|
+
`Could not read meter "${meterName}" with id: ${meter.objectId}`
|
|
79
|
+
) || 0;
|
|
80
|
+
const startValueState = await this.getStateAsync(`configuration.${meterName}.startValue`);
|
|
81
|
+
let startValueDateTime = null;
|
|
82
|
+
if (!startValueState) {
|
|
83
|
+
await this.setStateAsync(`configuration.${meterName}.startValue`, { val: currentValue, ack: true });
|
|
84
|
+
startValueDateTime = new Date(import_round.default.precise((currentValueState == null ? void 0 : currentValueState.ts) || Date.now(), -3));
|
|
85
|
+
await this.setStateAsync(`configuration.${meterName}.readingDateTime`, { val: startValueDateTime.toISOString(), ack: true });
|
|
86
|
+
}
|
|
125
87
|
const startValue = this.getNumericStateValue(
|
|
126
|
-
await this.getStateAsync(`
|
|
127
|
-
`Could not read start value for meter "${
|
|
88
|
+
startValueState || await this.getStateAsync(`configuration.${meterName}.startValue`),
|
|
89
|
+
`Could not read start value for meter "${meterName}".`
|
|
128
90
|
);
|
|
91
|
+
if (!startValueDateTime) {
|
|
92
|
+
const startValueDateTimeValue = (_a = await this.getStateAsync(`configuration.${meterName}.readingDateTime`)) == null ? void 0 : _a.val;
|
|
93
|
+
if (typeof startValueDateTimeValue === "string") {
|
|
94
|
+
try {
|
|
95
|
+
startValueDateTime = new Date(Date.parse(startValueDateTimeValue));
|
|
96
|
+
} catch (e) {
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
129
100
|
return {
|
|
130
101
|
meter,
|
|
131
102
|
currentValue,
|
|
132
103
|
startValue,
|
|
104
|
+
startValueDateTime,
|
|
133
105
|
consumption: currentValue - startValue
|
|
134
106
|
};
|
|
135
107
|
}
|
|
136
|
-
|
|
137
|
-
const
|
|
138
|
-
const daysInYear = this.getDaysInYear();
|
|
139
|
-
const daysSinceStartOfYear = this.getDaysSince(firstDayOfYear);
|
|
140
|
-
return { firstDayOfYear, daysInYear, daysSinceStartOfYear };
|
|
141
|
-
}
|
|
142
|
-
async assertObjectsExist() {
|
|
108
|
+
assertObjectsExist() {
|
|
109
|
+
const promises = [];
|
|
143
110
|
for (const meter of this.config.meters) {
|
|
144
|
-
|
|
111
|
+
promises.push(...this.assertMeterObjectsExist(meter.alias));
|
|
112
|
+
}
|
|
113
|
+
promises.push(...this.assertMeterObjectsExist(this.config.summaryName, false));
|
|
114
|
+
return Promise.allSettled(promises);
|
|
115
|
+
}
|
|
116
|
+
assertMeterObjectsExist(meterName, hasStartValue = true) {
|
|
117
|
+
const promises = [];
|
|
118
|
+
if (hasStartValue) {
|
|
119
|
+
promises.push(this.setObjectNotExistsAsync(`configuration.${meterName}.startValue`, {
|
|
145
120
|
type: "state",
|
|
146
121
|
common: {
|
|
147
122
|
name: "Start value",
|
|
@@ -152,60 +127,24 @@ class MeterStatistics extends utils.Adapter {
|
|
|
152
127
|
unit: this.config.meterUnit
|
|
153
128
|
},
|
|
154
129
|
native: {}
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
type: "state",
|
|
158
|
-
common: {
|
|
159
|
-
name: "Average consumption in period",
|
|
160
|
-
type: "number",
|
|
161
|
-
role: "state",
|
|
162
|
-
read: true,
|
|
163
|
-
write: false,
|
|
164
|
-
unit: this.config.meterUnit
|
|
165
|
-
},
|
|
166
|
-
native: {}
|
|
167
|
-
});
|
|
168
|
-
await this.setObjectNotExistsAsync(`meter.${meter.alias}.consumptionTotal`, {
|
|
169
|
-
type: "state",
|
|
170
|
-
common: {
|
|
171
|
-
name: "Total consumption in period",
|
|
172
|
-
type: "number",
|
|
173
|
-
role: "state",
|
|
174
|
-
read: true,
|
|
175
|
-
write: false,
|
|
176
|
-
unit: this.config.meterUnit
|
|
177
|
-
},
|
|
178
|
-
native: {}
|
|
179
|
-
});
|
|
180
|
-
await this.setObjectNotExistsAsync(`meter.${meter.alias}.consumptionPredictedTotal`, {
|
|
181
|
-
type: "state",
|
|
182
|
-
common: {
|
|
183
|
-
name: "Total predicted consumption in period",
|
|
184
|
-
type: "number",
|
|
185
|
-
role: "state",
|
|
186
|
-
read: true,
|
|
187
|
-
write: false,
|
|
188
|
-
unit: this.config.meterUnit
|
|
189
|
-
},
|
|
190
|
-
native: {}
|
|
191
|
-
});
|
|
192
|
-
await this.setObjectNotExistsAsync(`meter.${meter.alias}.costs`, {
|
|
130
|
+
}));
|
|
131
|
+
promises.push(this.setObjectNotExistsAsync(`configuration.${meterName}.readingDateTime`, {
|
|
193
132
|
type: "state",
|
|
194
133
|
common: {
|
|
195
|
-
name: "
|
|
196
|
-
|
|
134
|
+
name: "Timestamp of reading start value",
|
|
135
|
+
desc: "ONLY INFORMATION: Does not affect any calculation yet.",
|
|
136
|
+
type: "string",
|
|
197
137
|
role: "state",
|
|
198
138
|
read: true,
|
|
199
|
-
write:
|
|
200
|
-
unit: this.config.paymentUnit
|
|
139
|
+
write: true
|
|
201
140
|
},
|
|
202
141
|
native: {}
|
|
203
|
-
});
|
|
142
|
+
}));
|
|
204
143
|
}
|
|
205
|
-
|
|
144
|
+
promises.push(this.setObjectNotExistsAsync(`meters.${meterName}.consumptionAverage`, {
|
|
206
145
|
type: "state",
|
|
207
146
|
common: {
|
|
208
|
-
name: "
|
|
147
|
+
name: "Average consumption in period",
|
|
209
148
|
type: "number",
|
|
210
149
|
role: "state",
|
|
211
150
|
read: true,
|
|
@@ -213,11 +152,11 @@ class MeterStatistics extends utils.Adapter {
|
|
|
213
152
|
unit: this.config.meterUnit
|
|
214
153
|
},
|
|
215
154
|
native: {}
|
|
216
|
-
});
|
|
217
|
-
|
|
155
|
+
}));
|
|
156
|
+
promises.push(this.setObjectNotExistsAsync(`meters.${meterName}.consumptionTotal`, {
|
|
218
157
|
type: "state",
|
|
219
158
|
common: {
|
|
220
|
-
name: "Total
|
|
159
|
+
name: "Total consumption in period",
|
|
221
160
|
type: "number",
|
|
222
161
|
role: "state",
|
|
223
162
|
read: true,
|
|
@@ -225,11 +164,11 @@ class MeterStatistics extends utils.Adapter {
|
|
|
225
164
|
unit: this.config.meterUnit
|
|
226
165
|
},
|
|
227
166
|
native: {}
|
|
228
|
-
});
|
|
229
|
-
|
|
167
|
+
}));
|
|
168
|
+
promises.push(this.setObjectNotExistsAsync(`meters.${meterName}.consumptionPredictedTotal`, {
|
|
230
169
|
type: "state",
|
|
231
170
|
common: {
|
|
232
|
-
name: "
|
|
171
|
+
name: "Total predicted consumption in period",
|
|
233
172
|
type: "number",
|
|
234
173
|
role: "state",
|
|
235
174
|
read: true,
|
|
@@ -237,11 +176,11 @@ class MeterStatistics extends utils.Adapter {
|
|
|
237
176
|
unit: this.config.meterUnit
|
|
238
177
|
},
|
|
239
178
|
native: {}
|
|
240
|
-
});
|
|
241
|
-
|
|
179
|
+
}));
|
|
180
|
+
promises.push(this.setObjectNotExistsAsync(`meters.${meterName}.costs`, {
|
|
242
181
|
type: "state",
|
|
243
182
|
common: {
|
|
244
|
-
name: "Total
|
|
183
|
+
name: "Total costs",
|
|
245
184
|
type: "number",
|
|
246
185
|
role: "state",
|
|
247
186
|
read: true,
|
|
@@ -249,11 +188,11 @@ class MeterStatistics extends utils.Adapter {
|
|
|
249
188
|
unit: this.config.paymentUnit
|
|
250
189
|
},
|
|
251
190
|
native: {}
|
|
252
|
-
});
|
|
253
|
-
|
|
191
|
+
}));
|
|
192
|
+
promises.push(this.setObjectNotExistsAsync(`meters.${meterName}.paid`, {
|
|
254
193
|
type: "state",
|
|
255
194
|
common: {
|
|
256
|
-
name: "Total
|
|
195
|
+
name: "Total paid value",
|
|
257
196
|
type: "number",
|
|
258
197
|
role: "state",
|
|
259
198
|
read: true,
|
|
@@ -261,8 +200,8 @@ class MeterStatistics extends utils.Adapter {
|
|
|
261
200
|
unit: this.config.paymentUnit
|
|
262
201
|
},
|
|
263
202
|
native: {}
|
|
264
|
-
});
|
|
265
|
-
|
|
203
|
+
}));
|
|
204
|
+
promises.push(this.setObjectNotExistsAsync(`meters.${meterName}.balance`, {
|
|
266
205
|
type: "state",
|
|
267
206
|
common: {
|
|
268
207
|
name: "Current balance",
|
|
@@ -273,8 +212,8 @@ class MeterStatistics extends utils.Adapter {
|
|
|
273
212
|
unit: this.config.paymentUnit
|
|
274
213
|
},
|
|
275
214
|
native: {}
|
|
276
|
-
});
|
|
277
|
-
|
|
215
|
+
}));
|
|
216
|
+
promises.push(this.setObjectNotExistsAsync(`meters.${meterName}.recommendedPayment`, {
|
|
278
217
|
type: "state",
|
|
279
218
|
common: {
|
|
280
219
|
name: "Recommended payment per payment interval",
|
|
@@ -285,7 +224,8 @@ class MeterStatistics extends utils.Adapter {
|
|
|
285
224
|
unit: this.config.paymentUnit
|
|
286
225
|
},
|
|
287
226
|
native: {}
|
|
288
|
-
});
|
|
227
|
+
}));
|
|
228
|
+
return promises;
|
|
289
229
|
}
|
|
290
230
|
end() {
|
|
291
231
|
if (this.unloaded)
|
package/build/main.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/main.ts"],
|
|
4
|
-
"sourcesContent": ["/*\n * Created with @iobroker/create-adapter v2.3.0\n */\n\n// The adapter-core module gives you access to the core ioBroker functions\n// you need to create an adapter\nimport * as utils from '@iobroker/adapter-core';\nimport {DayInfo, MeterConsumption, MeterDefinition, MeterState} from './lib/typedef';\n\n// Load your modules here, e.g.:\n// import * as fs from \"fs\";\n\nclass MeterStatistics extends utils.Adapter {\n\n private unloaded = false;\n\n private pendingPromises: Array<Promise<any>> = [];\n\n public constructor(options: Partial<utils.AdapterOptions> = {}) {\n super({\n ...options,\n name: 'meter-statistics',\n });\n this.on('ready', this.onReady.bind(this));\n this.on('unload', this.onUnload.bind(this));\n }\n\n /**\n * Is called when databases are connected and adapter received configuration.\n */\n private async onReady(): Promise<void> {\n\n await this.assertObjectsExist();\n\n const dayInfo = this.getDayInfo();\n const meterStates = await this.getMeterStates();\n\n const {total, average, predicted} = this.calculateMeterConsumption(meterStates, dayInfo);\n this.writeState('summary.consumptionTotal', this.roundConsumption(total));\n this.writeState('summary.consumptionAverage', this.roundConsumption(average));\n this.writeState('summary.consumptionPredictedTotal', this.roundConsumption(predicted));\n\n const costs = this.calculateCosts(meterStates, dayInfo);\n this.writeState('summary.costs', this.roundCosts(costs));\n\n const paid = this.calculatePaid(dayInfo);\n this.writeState('summary.paid', this.roundCosts(paid));\n\n const current = paid - costs;\n this.writeState('summary.balance', this.roundCosts(current));\n\n const recommendedPayment = this.calculateRecommendation(costs, dayInfo);\n this.writeState('summary.recommendedPayment', this.roundCosts(recommendedPayment));\n\n this.end();\n }\n\n private addPendingPromise(promise: Promise<any>): void {\n this.pendingPromises.push(promise);\n }\n\n private calculateMeterConsumption(meterStates: MeterState[], dayInfo: DayInfo): MeterConsumption {\n\n let totalConsumption = 0;\n\n for (const meterState of meterStates) {\n const meter = meterState.meter;\n this.writeState(`meter.${meter.alias}.consumptionTotal`, this.roundConsumption(meterState.consumption));\n this.writeState(`meter.${meter.alias}.consumptionAverage`, this.roundConsumption(meterState.consumption / dayInfo.daysSinceStartOfYear));\n this.writeState(`meter.${meter.alias}.consumptionPredictedTotal`, this.roundConsumption(meterState.consumption / dayInfo.daysSinceStartOfYear * dayInfo.daysInYear));\n totalConsumption += meterState.consumption;\n }\n\n return {\n total: totalConsumption,\n average: totalConsumption / dayInfo.daysSinceStartOfYear,\n predicted: totalConsumption / dayInfo.daysSinceStartOfYear * dayInfo.daysInYear,\n };\n }\n\n private calculateCosts(meterStates: MeterState[], dayInfo: DayInfo): number {\n\n let usedCosts = 0;\n\n const basePricePerDay = this.config.paymentBasePrice / dayInfo.daysInYear;\n usedCosts += basePricePerDay * dayInfo.daysSinceStartOfYear;\n\n for (const meterState of meterStates) {\n const meter = meterState.meter;\n this.writeState(`meter.${meter.alias}.costs`, this.roundCosts(meterState.consumption * meterState.meter.pricePerUnit));\n usedCosts += meterState.consumption * meterState.meter.pricePerUnit;\n }\n\n return usedCosts;\n }\n\n private calculatePaid(dayInfo: DayInfo): number {\n\n let paid = this.config.paymentCorrectionOffset;\n\n const paymentPerDay = this.config.paymentValue * this.config.paymentCount / dayInfo.daysInYear;\n paid += paymentPerDay * dayInfo.daysSinceStartOfYear;\n\n return paid;\n }\n\n private calculateRecommendation(costs: number, dayInfo: DayInfo): number {\n return this.roundCosts(costs / dayInfo.daysSinceStartOfYear * dayInfo.daysInYear / this.config.paymentCount);\n }\n\n\n public roundCosts(costs: number): number {\n return Math.round(costs * 100) / 100;\n }\n\n public roundConsumption(consumption: number): number {\n return Math.round(consumption * 1000) / 1000;\n }\n\n public getDaysInYear(): number {\n return 365;\n }\n\n public getDaysSince(start: Date): number {\n const now = new Date();\n const diff = now.valueOf() - start.valueOf();\n const oneDay = 1000 * 60 * 60 * 24;\n return Math.floor(diff / oneDay);\n }\n\n private writeState(id: string, value: any): void {\n this.addPendingPromise(this.setStateAsync(id, {val: value, ack: true}));\n }\n\n private async getMeterStates(): Promise<MeterState[]> {\n\n const meterStates = [];\n\n for (const meter of this.config.meters) {\n meterStates.push(await this.getMeterState(meter));\n }\n\n return meterStates;\n }\n\n private getNumericStateValue(state: null|undefined|ioBroker.State, onErrorMessage: string): number {\n const stateValue = state?.val;\n if (typeof stateValue === 'number') {\n return stateValue;\n }\n throw new Error(onErrorMessage);\n }\n\n private async getMeterState(meter: MeterDefinition): Promise<MeterState> {\n\n const currentValue = this.getNumericStateValue(\n await this.getForeignStateAsync(meter.objectId),\n `Could not read meter \"${meter.alias}\" with id: ${meter.objectId}`\n );\n const startValue = this.getNumericStateValue(\n await this.getStateAsync(`meter.${meter.alias}.startValue`),\n `Could not read start value for meter \"${meter.alias}\".`\n );\n\n return {\n meter,\n currentValue,\n startValue,\n consumption: currentValue - startValue,\n };\n }\n\n private getDayInfo(): DayInfo {\n\n const firstDayOfYear = new Date((new Date()).getFullYear(), 0, 0);\n const daysInYear = this.getDaysInYear();\n const daysSinceStartOfYear = this.getDaysSince(firstDayOfYear);\n\n return {firstDayOfYear, daysInYear, daysSinceStartOfYear};\n }\n\n private async assertObjectsExist(): Promise<void> {\n\n for (const meter of this.config.meters) {\n\n await this.setObjectNotExistsAsync(`meter.${meter.alias}.startValue`, {\n type: 'state',\n common: {\n name: 'Start value',\n type: 'number',\n role: 'state',\n read: true,\n write: true,\n unit: this.config.meterUnit,\n },\n native: {},\n });\n await this.setObjectNotExistsAsync(`meter.${meter.alias}.consumptionAverage`, {\n type: 'state',\n common: {\n name: 'Average consumption in period',\n type: 'number',\n role: 'state',\n read: true,\n write: false,\n unit: this.config.meterUnit,\n },\n native: {},\n });\n await this.setObjectNotExistsAsync(`meter.${meter.alias}.consumptionTotal`, {\n type: 'state',\n common: {\n name: 'Total consumption in period',\n type: 'number',\n role: 'state',\n read: true,\n write: false,\n unit: this.config.meterUnit,\n },\n native: {},\n });\n await this.setObjectNotExistsAsync(`meter.${meter.alias}.consumptionPredictedTotal`, {\n type: 'state',\n common: {\n name: 'Total predicted consumption in period',\n type: 'number',\n role: 'state',\n read: true,\n write: false,\n unit: this.config.meterUnit,\n },\n native: {},\n });\n await this.setObjectNotExistsAsync(`meter.${meter.alias}.costs`, {\n type: 'state',\n common: {\n name: 'Total costs',\n type: 'number',\n role: 'state',\n read: true,\n write: false,\n unit: this.config.paymentUnit,\n },\n native: {},\n });\n\n }\n\n await this.setObjectNotExistsAsync(`summary.consumptionTotal`, {\n type: 'state',\n common: {\n name: 'Total consumption in period',\n type: 'number',\n role: 'state',\n read: true,\n write: false,\n unit: this.config.meterUnit,\n },\n native: {},\n });\n\n await this.setObjectNotExistsAsync(`summary.consumptionPredictedTotal`, {\n type: 'state',\n common: {\n name: 'Total predicted consumption in period',\n type: 'number',\n role: 'state',\n read: true,\n write: false,\n unit: this.config.meterUnit,\n },\n native: {},\n });\n\n await this.setObjectNotExistsAsync(`summary.consumptionAverage`, {\n type: 'state',\n common: {\n name: 'Average consumption in period',\n type: 'number',\n role: 'state',\n read: true,\n write: false,\n unit: this.config.meterUnit,\n },\n native: {},\n });\n\n await this.setObjectNotExistsAsync(`summary.paid`, {\n type: 'state',\n common: {\n name: 'Total paid value',\n type: 'number',\n role: 'state',\n read: true,\n write: false,\n unit: this.config.paymentUnit,\n },\n native: {},\n });\n\n await this.setObjectNotExistsAsync(`summary.costs`, {\n type: 'state',\n common: {\n name: 'Total costs',\n type: 'number',\n role: 'state',\n read: true,\n write: false,\n unit: this.config.paymentUnit,\n },\n native: {},\n });\n\n await this.setObjectNotExistsAsync(`summary.balance`, {\n type: 'state',\n common: {\n name: 'Current balance',\n type: 'number',\n role: 'state',\n read: true,\n write: false,\n unit: this.config.paymentUnit,\n },\n native: {},\n });\n\n await this.setObjectNotExistsAsync(`summary.recommendedPayment`, {\n type: 'state',\n common: {\n name: 'Recommended payment per payment interval',\n type: 'number',\n role: 'state',\n read: true,\n write: false,\n unit: this.config.paymentUnit,\n },\n native: {},\n });\n }\n\n end(): void {\n if (this.unloaded) return;\n Promise.allSettled(this.pendingPromises).finally(() => {\n if (this.stop) {\n this.stop();\n }\n });\n }\n\n /**\n * Is called when adapter shuts down - callback has to be called under any circumstances!\n */\n private onUnload(callback: () => void): void {\n this.unloaded = true;\n try {\n // Here you must clear all timeouts or intervals that may still be active\n callback();\n } catch (e) {\n callback();\n }\n }\n\n}\n\nif (require.main !== module) {\n // Export the constructor in compact mode\n module.exports = (options: Partial<utils.AdapterOptions> | undefined) => new MeterStatistics(options);\n} else {\n // otherwise start the instance directly\n (() => new MeterStatistics())();\n}\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;AAMA,YAAuB;
|
|
6
|
-
"names": []
|
|
4
|
+
"sourcesContent": ["/*\n * Created with @iobroker/create-adapter v2.3.0\n */\n\n// The adapter-core module gives you access to the core ioBroker functions\n// you need to create an adapter\nimport * as utils from '@iobroker/adapter-core';\nimport {MeterDefinition, MeterInfoCollection, MeterState} from './lib/typedef';\nimport Calculator from './calculator';\nimport Round from './round';\n\n// Load your modules here, e.g.:\n// import * as fs from \"fs\";\n\nclass MeterStatistics extends utils.Adapter {\n\n private unloaded = false;\n\n private pendingPromises: Array<Promise<any>> = [];\n\n public constructor(options: Partial<utils.AdapterOptions> = {}) {\n super({\n ...options,\n name: 'meter-statistics',\n });\n this.on('ready', this.onReady.bind(this));\n this.on('unload', this.onUnload.bind(this));\n }\n\n /**\n * Is called when databases are connected and adapter received configuration.\n */\n private async onReady(): Promise<void> {\n\n await this.assertObjectsExist();\n\n const meterStates = await this.getMeterStates();\n\n const calculator = new Calculator(this.config);\n\n const meterInfos = calculator.calculateMeterInfos(meterStates);\n\n this.writeMeterInfos(meterInfos);\n\n this.end();\n }\n\n private writeMeterInfos(meterInfos: MeterInfoCollection): void {\n for (const meterName in meterInfos) {\n const meterInfo = meterInfos[meterName];\n\n this.writeState(`meters.${meterName}.balance`, Round.costs(meterInfo.balance));\n this.writeState(`meters.${meterName}.consumptionAverage`, Round.consumption(meterInfo.consumptionAverage));\n this.writeState(`meters.${meterName}.consumptionPredictedTotal`, Round.consumption(meterInfo.consumptionPredictedTotal));\n this.writeState(`meters.${meterName}.consumptionTotal`, Round.consumption(meterInfo.consumptionTotal));\n this.writeState(`meters.${meterName}.costs`, Round.costs(meterInfo.costs));\n this.writeState(`meters.${meterName}.paid`, Round.costs(meterInfo.paid));\n this.writeState(`meters.${meterName}.recommendedPayment`, Round.costs(meterInfo.recommendedPayment));\n }\n }\n\n\n private writeState(id: string, value: any): void {\n const promise = this.setStateAsync(id, {val: value, ack: false});\n this.pendingPromises.push(promise);\n }\n\n private getNumericStateValue(state: null|undefined|ioBroker.State, onErrorMessage: string): number {\n const stateValue = state?.val;\n if (typeof stateValue === 'number') {\n return stateValue;\n }\n throw new Error(onErrorMessage);\n }\n\n private async getMeterStates(): Promise<MeterState[]> {\n\n const meterStates = [];\n\n for (const meter of this.config.meters) {\n meterStates.push(await this.getMeterState(meter));\n }\n\n return meterStates;\n }\n\n private async getMeterState(meter: MeterDefinition): Promise<MeterState> {\n\n const meterName = meter.alias;\n const currentValueState = await this.getForeignStateAsync(meter.objectId);\n const currentValue = this.getNumericStateValue(\n currentValueState,\n `Could not read meter \"${meterName}\" with id: ${meter.objectId}`\n ) || 0;\n\n const startValueState = await this.getStateAsync(`configuration.${meterName}.startValue`);\n let startValueDateTime: Date|null = null;\n if (!startValueState) {\n await this.setStateAsync(`configuration.${meterName}.startValue`, {val: currentValue, ack: true});\n startValueDateTime = new Date(Round.precise(currentValueState?.ts || Date.now(), -3));\n await this.setStateAsync(`configuration.${meterName}.readingDateTime`, {val: startValueDateTime.toISOString(), ack: true});\n }\n const startValue = this.getNumericStateValue(\n startValueState || await this.getStateAsync(`configuration.${meterName}.startValue`),\n `Could not read start value for meter \"${meterName}\".`\n );\n if (!startValueDateTime) {\n const startValueDateTimeValue = (await this.getStateAsync(`configuration.${meterName}.readingDateTime`))?.val;\n if (typeof startValueDateTimeValue === 'string') {\n try {\n startValueDateTime = new Date(Date.parse(startValueDateTimeValue));\n } catch (e) {}\n }\n }\n\n return {\n meter,\n currentValue,\n startValue,\n startValueDateTime,\n consumption: currentValue - startValue,\n };\n }\n\n private assertObjectsExist(): Promise<any> {\n const promises = [];\n\n for (const meter of this.config.meters) {\n promises.push(...this.assertMeterObjectsExist(meter.alias));\n }\n promises.push(...this.assertMeterObjectsExist(this.config.summaryName, false));\n\n return Promise.allSettled(promises);\n }\n\n private assertMeterObjectsExist(meterName: string, hasStartValue = true): ioBroker.SetObjectPromise[] {\n const promises = [];\n if (hasStartValue) {\n promises.push(this.setObjectNotExistsAsync(`configuration.${meterName}.startValue`, {\n type: 'state',\n common: {\n name: 'Start value',\n type: 'number',\n role: 'state',\n read: true,\n write: true,\n unit: this.config.meterUnit,\n },\n native: {},\n }));\n promises.push(this.setObjectNotExistsAsync(`configuration.${meterName}.readingDateTime`, {\n type: 'state',\n common: {\n name: 'Timestamp of reading start value',\n desc: 'ONLY INFORMATION: Does not affect any calculation yet.',\n type: 'string',\n role: 'state',\n read: true,\n write: true,\n },\n native: {},\n }));\n }\n promises.push(this.setObjectNotExistsAsync(`meters.${meterName}.consumptionAverage`, {\n type: 'state',\n common: {\n name: 'Average consumption in period',\n type: 'number',\n role: 'state',\n read: true,\n write: false,\n unit: this.config.meterUnit,\n },\n native: {},\n }));\n promises.push(this.setObjectNotExistsAsync(`meters.${meterName}.consumptionTotal`, {\n type: 'state',\n common: {\n name: 'Total consumption in period',\n type: 'number',\n role: 'state',\n read: true,\n write: false,\n unit: this.config.meterUnit,\n },\n native: {},\n }));\n promises.push(this.setObjectNotExistsAsync(`meters.${meterName}.consumptionPredictedTotal`, {\n type: 'state',\n common: {\n name: 'Total predicted consumption in period',\n type: 'number',\n role: 'state',\n read: true,\n write: false,\n unit: this.config.meterUnit,\n },\n native: {},\n }));\n promises.push(this.setObjectNotExistsAsync(`meters.${meterName}.costs`, {\n type: 'state',\n common: {\n name: 'Total costs',\n type: 'number',\n role: 'state',\n read: true,\n write: false,\n unit: this.config.paymentUnit,\n },\n native: {},\n }));\n promises.push(this.setObjectNotExistsAsync(`meters.${meterName}.paid`, {\n type: 'state',\n common: {\n name: 'Total paid value',\n type: 'number',\n role: 'state',\n read: true,\n write: false,\n unit: this.config.paymentUnit,\n },\n native: {},\n }));\n promises.push(this.setObjectNotExistsAsync(`meters.${meterName}.balance`, {\n type: 'state',\n common: {\n name: 'Current balance',\n type: 'number',\n role: 'state',\n read: true,\n write: false,\n unit: this.config.paymentUnit,\n },\n native: {},\n }));\n promises.push(this.setObjectNotExistsAsync(`meters.${meterName}.recommendedPayment`, {\n type: 'state',\n common: {\n name: 'Recommended payment per payment interval',\n type: 'number',\n role: 'state',\n read: true,\n write: false,\n unit: this.config.paymentUnit,\n },\n native: {},\n }));\n\n return promises;\n }\n\n end(): void {\n if (this.unloaded) return;\n Promise.allSettled(this.pendingPromises).finally(() => {\n if (this.stop) {\n this.stop();\n }\n });\n }\n\n /**\n * Is called when adapter shuts down - callback has to be called under any circumstances!\n */\n private onUnload(callback: () => void): void {\n this.unloaded = true;\n try {\n // Here you must clear all timeouts or intervals that may still be active\n callback();\n } catch (e) {\n callback();\n }\n }\n\n}\n\nif (require.main !== module) {\n // Export the constructor in compact mode\n module.exports = (options: Partial<utils.AdapterOptions> | undefined) => new MeterStatistics(options);\n} else {\n // otherwise start the instance directly\n (() => new MeterStatistics())();\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;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BAAqB;AAAA,MACtE,MAAM;AAAA,MACN,QAAQ;AAAA,QACJ,MAAM;AAAA,QACN,MAAM;AAAA,QACN,MAAM;AAAA,QACN,MAAM;AAAA,QACN,OAAO;AAAA,QACP,MAAM,KAAK,OAAO;AAAA,MACtB;AAAA,MACA,QAAQ,CAAC;AAAA,IACb,CAAC,CAAC;AACF,aAAS,KAAK,KAAK,wBAAwB,UAAU,gCAAgC;AAAA,MACjF,MAAM;AAAA,MACN,QAAQ;AAAA,QACJ,MAAM;AAAA,QACN,MAAM;AAAA,QACN,MAAM;AAAA,QACN,MAAM;AAAA,QACN,OAAO;AAAA,QACP,MAAM,KAAK,OAAO;AAAA,MACtB;AAAA,MACA,QAAQ,CAAC;AAAA,IACb,CAAC,CAAC;AAEF,WAAO;AAAA,EACX;AAAA,EAEA,MAAY;AACR,QAAI,KAAK;AAAU;AACnB,YAAQ,WAAW,KAAK,eAAe,EAAE,QAAQ,MAAM;AACnD,UAAI,KAAK,MAAM;AACX,aAAK,KAAK;AAAA,MACd;AAAA,IACJ,CAAC;AAAA,EACL;AAAA,EAKQ,SAAS,UAA4B;AACzC,SAAK,WAAW;AAChB,QAAI;AAEA,eAAS;AAAA,IACb,SAAS,GAAP;AACE,eAAS;AAAA,IACb;AAAA,EACJ;AAEJ;AAEA,IAAI,QAAQ,SAAS,QAAQ;AAEzB,SAAO,UAAU,CAAC,YAAuD,IAAI,gBAAgB,OAAO;AACxG,OAAO;AAEH,GAAC,MAAM,IAAI,gBAAgB,GAAG;AAClC;",
|
|
6
|
+
"names": ["Calculator", "Round"]
|
|
7
7
|
}
|
package/build/period.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var period_exports = {};
|
|
20
|
+
__export(period_exports, {
|
|
21
|
+
default: () => Period
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(period_exports);
|
|
24
|
+
class Period {
|
|
25
|
+
constructor(startOfPeriod = null) {
|
|
26
|
+
const now = this.now;
|
|
27
|
+
const periodStartDateTime = startOfPeriod || new Date(now.getFullYear(), 0, 0);
|
|
28
|
+
const periodEndDateTime = this.incrementDatetimeByOneYear(periodStartDateTime);
|
|
29
|
+
const periodStartTimestamp = periodStartDateTime.valueOf();
|
|
30
|
+
const periodEndTimestamp = periodEndDateTime.valueOf();
|
|
31
|
+
const unitsPerDay = new Date("1900-01-02T00:00:00").valueOf() - new Date("1900-01-01T00:00:00").valueOf();
|
|
32
|
+
const unitsInPeriod = periodEndTimestamp - periodStartTimestamp;
|
|
33
|
+
const elapsedUnitsInPeriod = now.valueOf() - periodStartTimestamp;
|
|
34
|
+
this.perDayFactor = 1 / elapsedUnitsInPeriod * unitsPerDay;
|
|
35
|
+
this.extrapolatePerPeriodFactor = 1 / elapsedUnitsInPeriod * unitsInPeriod;
|
|
36
|
+
this.elapsedInPeriodFactor = 1 / unitsInPeriod * elapsedUnitsInPeriod;
|
|
37
|
+
}
|
|
38
|
+
get now() {
|
|
39
|
+
if (!Period.now) {
|
|
40
|
+
Period.now = new Date();
|
|
41
|
+
}
|
|
42
|
+
return Period.now;
|
|
43
|
+
}
|
|
44
|
+
incrementDatetimeByOneYear(date) {
|
|
45
|
+
const newDate = new Date(date);
|
|
46
|
+
newDate.setFullYear(date.getFullYear() + 1);
|
|
47
|
+
return newDate;
|
|
48
|
+
}
|
|
49
|
+
calculateValuePerDay(amountInPeriod) {
|
|
50
|
+
return amountInPeriod * this.perDayFactor;
|
|
51
|
+
}
|
|
52
|
+
calculateExtrapolateValueForPeriod(amountInPeriod) {
|
|
53
|
+
return amountInPeriod * this.extrapolatePerPeriodFactor;
|
|
54
|
+
}
|
|
55
|
+
calculateElapsedAmountInPeriod(totalAmountInYear) {
|
|
56
|
+
return totalAmountInYear * this.elapsedInPeriodFactor;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
60
|
+
0 && (module.exports = {});
|
|
61
|
+
//# sourceMappingURL=period.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/period.ts"],
|
|
4
|
+
"sourcesContent": ["export default class Period {\n\n private static now: null|Date;\n private readonly perDayFactor: number;\n private readonly extrapolatePerPeriodFactor: number;\n private readonly elapsedInPeriodFactor: number;\n\n constructor(startOfPeriod: Date|null = null) {\n const now = this.now;\n\n const periodStartDateTime = startOfPeriod || new Date(now.getFullYear(), 0, 0);\n const periodEndDateTime = this.incrementDatetimeByOneYear(periodStartDateTime);\n\n const periodStartTimestamp = periodStartDateTime.valueOf();\n const periodEndTimestamp = periodEndDateTime.valueOf();\n\n const unitsPerDay = (new Date('1900-01-02T00:00:00')).valueOf() - (new Date('1900-01-01T00:00:00')).valueOf();\n const unitsInPeriod = periodEndTimestamp - periodStartTimestamp;\n const elapsedUnitsInPeriod = now.valueOf() - periodStartTimestamp;\n\n this.perDayFactor = 1 / elapsedUnitsInPeriod * unitsPerDay;\n this.extrapolatePerPeriodFactor = 1 / elapsedUnitsInPeriod * unitsInPeriod;\n this.elapsedInPeriodFactor = 1 / unitsInPeriod * elapsedUnitsInPeriod;\n }\n\n private get now(): Date {\n if (!Period.now) {\n Period.now = new Date();\n }\n return Period.now;\n }\n\n private incrementDatetimeByOneYear(date: Date): Date {\n const newDate = new Date(date);\n newDate.setFullYear(date.getFullYear() + 1);\n return newDate;\n }\n\n public calculateValuePerDay(amountInPeriod: number): number {\n return amountInPeriod * this.perDayFactor;\n }\n\n public calculateExtrapolateValueForPeriod(amountInPeriod: number): number {\n return amountInPeriod * this.extrapolatePerPeriodFactor;\n }\n\n public calculateElapsedAmountInPeriod(totalAmountInYear: number): number {\n return totalAmountInYear * this.elapsedInPeriodFactor;\n }\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,MAAO,OAAqB;AAAA,EAOxB,YAAY,gBAA2B,MAAM;AACzC,UAAM,MAAM,KAAK;AAEjB,UAAM,sBAAsB,iBAAiB,IAAI,KAAK,IAAI,YAAY,GAAG,GAAG,CAAC;AAC7E,UAAM,oBAAoB,KAAK,2BAA2B,mBAAmB;AAE7E,UAAM,uBAAuB,oBAAoB,QAAQ;AACzD,UAAM,qBAAqB,kBAAkB,QAAQ;AAErD,UAAM,cAAe,IAAI,KAAK,qBAAqB,EAAG,QAAQ,IAAK,IAAI,KAAK,qBAAqB,EAAG,QAAQ;AAC5G,UAAM,gBAAgB,qBAAqB;AAC3C,UAAM,uBAAuB,IAAI,QAAQ,IAAI;AAE7C,SAAK,eAAe,IAAI,uBAAuB;AAC/C,SAAK,6BAA6B,IAAI,uBAAuB;AAC7D,SAAK,wBAAwB,IAAI,gBAAgB;AAAA,EACrD;AAAA,EAEA,IAAY,MAAY;AACpB,QAAI,CAAC,OAAO,KAAK;AACb,aAAO,MAAM,IAAI,KAAK;AAAA,IAC1B;AACA,WAAO,OAAO;AAAA,EAClB;AAAA,EAEQ,2BAA2B,MAAkB;AACjD,UAAM,UAAU,IAAI,KAAK,IAAI;AAC7B,YAAQ,YAAY,KAAK,YAAY,IAAI,CAAC;AAC1C,WAAO;AAAA,EACX;AAAA,EAEO,qBAAqB,gBAAgC;AACxD,WAAO,iBAAiB,KAAK;AAAA,EACjC;AAAA,EAEO,mCAAmC,gBAAgC;AACtE,WAAO,iBAAiB,KAAK;AAAA,EACjC;AAAA,EAEO,+BAA+B,mBAAmC;AACrE,WAAO,oBAAoB,KAAK;AAAA,EACpC;AACJ;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
package/build/round.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var round_exports = {};
|
|
20
|
+
__export(round_exports, {
|
|
21
|
+
default: () => round_default
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(round_exports);
|
|
24
|
+
class Round {
|
|
25
|
+
costs(costs) {
|
|
26
|
+
return this.precise(costs, 2);
|
|
27
|
+
}
|
|
28
|
+
consumption(consumption) {
|
|
29
|
+
return this.precise(consumption, 3);
|
|
30
|
+
}
|
|
31
|
+
precise(value, scale) {
|
|
32
|
+
if (null === value) {
|
|
33
|
+
return value;
|
|
34
|
+
}
|
|
35
|
+
const factor = Math.pow(10, scale);
|
|
36
|
+
return Math.round(value * factor) / factor;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
var round_default = new Round();
|
|
40
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
41
|
+
0 && (module.exports = {});
|
|
42
|
+
//# sourceMappingURL=round.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/round.ts"],
|
|
4
|
+
"sourcesContent": ["class Round {\n\n public costs(costs: number): number {\n return this.precise(costs, 2);\n }\n\n public consumption(consumption: number): number {\n return this.precise(consumption, 3);\n }\n\n public precise(value: number, scale: number): number {\n if (null === value) {\n return value;\n }\n const factor = Math.pow(10, scale);\n return Math.round(value * factor) / factor;\n }\n\n}\n\nexport default new Round();\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,MAAM,MAAM;AAAA,EAED,MAAM,OAAuB;AAChC,WAAO,KAAK,QAAQ,OAAO,CAAC;AAAA,EAChC;AAAA,EAEO,YAAY,aAA6B;AAC5C,WAAO,KAAK,QAAQ,aAAa,CAAC;AAAA,EACtC;AAAA,EAEO,QAAQ,OAAe,OAAuB;AACjD,QAAI,SAAS,OAAO;AAChB,aAAO;AAAA,IACX;AACA,UAAM,SAAS,KAAK,IAAI,IAAI,KAAK;AACjC,WAAO,KAAK,MAAM,QAAQ,MAAM,IAAI;AAAA,EACxC;AAEJ;AAEA,IAAO,gBAAQ,IAAI,MAAM;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
package/io-package.json
CHANGED
|
@@ -1,8 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"common": {
|
|
3
3
|
"name": "meter-statistics",
|
|
4
|
-
"version": "0.0
|
|
4
|
+
"version": "0.1.0",
|
|
5
5
|
"news": {
|
|
6
|
+
"0.1.0": {
|
|
7
|
+
"en": "[TASK] Code refactoring\n[TASK] Add test cases",
|
|
8
|
+
"de": "[TASK] Code-Refactoring\n(TASK) Testfälle hinzufügen",
|
|
9
|
+
"ru": "[TASK] Код рефакторинг\n[ТАСК] Добавить тестовые случаи",
|
|
10
|
+
"pt": "[TASK] Refactoring de código\n[TASK] Adicionar casos de teste",
|
|
11
|
+
"nl": "Code refactoring\n[TASK] Voeg testzaken toe",
|
|
12
|
+
"fr": "[TASK] Code refactoring\n[TASK] Ajouter les cas d ' essai",
|
|
13
|
+
"it": "[TASK] Refactoring del codice\n[TASK] Aggiungere i casi di test",
|
|
14
|
+
"es": "[TASK] Code refactoring\n[TASK] Agregar casos de prueba",
|
|
15
|
+
"pl": "(TASK) Code refactoring (ang.)\n(TASK) (ang.). Addd test case",
|
|
16
|
+
"uk": "[TASK] Рефакторинг коду\n[ТАСК] Додавання тестових випадків",
|
|
17
|
+
"zh-cn": "[TASK]守则\n[TASK] 增加试验案件"
|
|
18
|
+
},
|
|
6
19
|
"0.0.4": {
|
|
7
20
|
"en": "[BUGFIX] Fix calculation of predicted total\n[TASK] Update project",
|
|
8
21
|
"de": "[BUGFIX] Berechnung der prognostizierten Summe festsetzen\n[TASK] Projekt aktualisieren",
|
|
@@ -126,6 +139,7 @@
|
|
|
126
139
|
"paymentCount": 0,
|
|
127
140
|
"paymentCorrectionOffset": 0,
|
|
128
141
|
"paymentBasePrice": 0,
|
|
142
|
+
"summaryName": "summary",
|
|
129
143
|
"meters": []
|
|
130
144
|
},
|
|
131
145
|
"objects": [],
|