-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEasyenergyApp.qml
237 lines (211 loc) · 9.81 KB
/
EasyenergyApp.qml
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import QtQuick 1.1
import qb.components 1.0
import qb.base 1.0
import "easyenergy.js" as EasyenergyJS
App {
id: root
// These are the URL's for the QML resources from which our widgets will be instantiated.
// By making them a URL type property they will automatically be converted to full paths,
// preventing problems when passing them around to code that comes from a different path.
property url trayUrl : "EasyenergyTray.qml";
property url tileUrl : "EasyenergyTile.qml";
property url thumbnailIcon: "./drawables/easyenergyIcon.png"
property url easyenergyScreenUrl : "EasyenergyScreen.qml"
property url easyenergySettingsUrl : "EasyenergySettings.qml"
property EasyenergySettings easyenergySettings
// these are the default settings
// for tax values see next site to update if it is changed
// https://www.belastingdienst.nl/wps/wcm/connect/bldcontentnl/belastingdienst/zakelijk/overige_belastingen/belastingen_op_milieugrondslag/tarieven_milieubelastingen/tabellen_tarieven_milieubelastingen
property variant settings: {
"includeTax" : true,
"tariffEnergyTax": 0.1046,
"tariffODETax": 0.0132,
"tariffVAT": 21,
"domoticzEnable": false,
"domoticzHost": "domoticz.local",
"domoticzPort": "8080",
"domoticzIdx": "1",
"lookbackHours": 2,
"lookforwardHours": 18,
"scaleGraph": true,
"showColorinDim": true,
}
property variant tariffValues: [] // will contain the collected tariffs
property real minTariffValue // will contain the min tariff from the collected
property real maxTariffValue // will contain the max tariff from the collected
property real tariffQ1 // will contain the average low part of the collected (splicing the Q1 and Q2)
property real tariffMedian // will contain the average low part of the collected (splicing the Q1 and Q2)
property real tariffQ3 // will contain the average high part of the collected (splicing the Q3 and Q4)
property real currentTariffUsage // will contain the current tariff (usage)
property real currentTariffReturn // will contain the current tariff (return) *future use*
property int currentHour // will containt the current hour
property int startHour // will contain the start hour of the collected tariffs
property int datapoints // will contain the number of datapoints
function init() {
registry.registerWidget("screen", easyenergyScreenUrl, this);
registry.registerWidget("screen", easyenergySettingsUrl, this, "easyenergySettings");
// disable the systray for now registry.registerWidget("systrayIcon", trayUrl, this, "easyenergyTray");
registry.registerWidget("tile", tileUrl, this, null, {thumbLabel: "EasyEnergy", thumbIcon: thumbnailIcon, thumbCategory: "general", thumbWeight: 30, baseTileWeight: 10, baseTileSolarWeight: 10, thumbIconVAlignment: "center"});
}
Component.onCompleted: {
// load the settings on completed is recommended instead of during init
loadSettings();
}
function loadSettings() {
var settingsFile = new XMLHttpRequest();
settingsFile.onreadystatechange = function() {
if (settingsFile.readyState == XMLHttpRequest.DONE) {
if (settingsFile.responseText.length > 0) {
var temp = JSON.parse(settingsFile.responseText);
for (var setting in settings) {
if (temp[setting] === undefined ) { temp[setting] = settings[setting]; } // use default if no saved setting exists
}
settings = temp;
collectTariffsTimer.interval = 10000; // set refresh of timer after 10 sec to get new tariffs in case of parameter changed after load
}
}
}
settingsFile.open("GET", "file:///HCBv2/qml/apps/easyenergy/easyenergy.settings", true);
settingsFile.send();
}
function updateDomoticz() {
var alertStatus = 4;
if (currentTariffUsage < tariffQ3) { alertStatus = 3; }
if (currentTariffUsage < tariffMedian) { alertStatus = 2; }
if (currentTariffUsage < tariffQ1) { alertStatus = 1; }
var request = ("http://"+settings.domoticzHost+":"+settings.domoticzPort+"/json.htm?type=command¶m=udevice&idx="+settings.domoticzIdx+"&nvalue="+alertStatus+"&svalue="+normalizeTariff(currentTariffUsage))
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", request, true);
xmlhttp.send();
}
function currentTextColor() {
// set tile text color based on calculated averages
var colorNow = "#FF0000";
if (currentTariffUsage < tariffQ3) { colorNow = "#FF6600"; }
if (currentTariffUsage < tariffQ1) { colorNow = "#00FF00"; }
return colorNow;
}
function normalizeTariff(tariff) {
// adds tax to tariffs if requested and presents in euros with max 4 decimals
var normalizedTariff = (settings.includeTax) ? parseInt((settings.tariffEnergyTax + settings.tariffODETax + tariff) * ((settings.tariffVAT / 100)+1) * 10000)/10000 : parseInt(tariff * 10000)/10000 ;
return normalizedTariff;
}
function getCurrentTariffs() {
var now = new Date();
currentHour = now.getHours();
startHour = currentHour - settings.lookbackHours; // start the graph at the start point set
now.setHours(startHour,0,0,0);
var endDate = new Date(now.getTime() + ((settings.lookforwardHours + settings.lookbackHours) * 3600 * 1000)); // end the graph at the end piont set
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
var res = xmlhttp.responseText;
var jsonRes = JSON.parse(res);
datapoints = jsonRes.length;
var tariffs = [];
minTariffValue = 1000;
maxTariffValue = 0;
// walk trhough the xml result and put the values into a temporary array
for (var i = 0; i < jsonRes.length; i++) {
// since a few weeks easyenergy includes tax in the reported tariffusage, so remove it first
tariffs[i] = (jsonRes[i].TariffUsage / ((settings.tariffVAT / 100) + 1) );
if (minTariffValue > tariffs[i]) {
minTariffValue = tariffs[i];
}
if (maxTariffValue < tariffs[i]) {
maxTariffValue = tariffs[i];
}
}
tariffValues = tariffs.slice(); // copy the collected tarrifs into the app property (somehow not possible without the tariffs array)
// calculate the quartiles for the low and high tariff
var quartiles= EasyenergyJS.getQuartiles(tariffs);
tariffQ1 = quartiles[0];
tariffMedian = quartiles[1];
tariffQ3 = quartiles[2];
// set the current tariff and normalize
currentTariffUsage = tariffs[settings.lookbackHours];
if (settings.domoticzEnable) { updateDomoticz(); }
}
else {
console.log("Easyenergy URL fetch failed! Fetching using APX dashboard");
getCurrentTariffsAPX();
}
}
}
var urlAppend = "startTimestamp=" + encodeURIComponent(now.toISOString()) + "&endTimestamp=" + encodeURIComponent(endDate.toISOString());
var urlEasyEnergy = "https://mijn.easyenergy.com/nl/api/tariff/getapxtariffs?" + urlAppend;
xmlhttp.open("GET", urlEasyEnergy, true);
xmlhttp.send();
}
function getCurrentTariffsAPX() {
var now = new Date();
currentHour = now.getHours();
startHour = currentHour - settings.lookbackHours; // start the graph at the start point set
now.setHours(startHour,0,0,0);
var endDate = new Date(now.getTime() + ((settings.lookforwardHours + settings.lookbackHours) * 3600 * 1000)); // end the graph at the end piont set
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
var res = xmlhttp.responseText;
var jsonRes = JSON.parse(res);
var tariffsTemp = [];
minTariffValue = 1000;
maxTariffValue = 0;
for (var i = 0; i < jsonRes.quote.length; i++) {
var quoteDateApplied = jsonRes.quote[i].date_applied;
var quoteHour = jsonRes.quote[i].values[1].value;
var quotePrice = jsonRes.quote[i].values[3].value / 1000;
var quoteTime = quoteDateApplied + (quoteHour - 1) * 3600000 // this works ok in winter time.. need to check this when it is summer time
var quoteTarrif = {timestamp: quoteTime, tariff: quotePrice};
if (quoteTime >= now.getTime() && quoteTime <= endDate.getTime() ) {
tariffsTemp.push(quoteTarrif);
}
}
tariffsTemp.sort(function(a, b){return a.timestamp - b.timestamp});
datapoints = tariffsTemp.length;
var tariffs = [];
for (var i = 0; i < tariffsTemp.length; i++) {
tariffs[i] = tariffsTemp[i].tariff;
if (minTariffValue > tariffs[i]) {
minTariffValue = tariffs[i];
}
if (maxTariffValue < tariffs[i]) {
maxTariffValue = tariffs[i];
}
}
tariffValues = tariffs.slice();
// calculate the quartiles for the low and high tariff
var quartiles= EasyenergyJS.getQuartiles(tariffs);
tariffQ1 = quartiles[0];
tariffMedian = quartiles[1];
tariffQ3 = quartiles[2];
// set the current tariff and normalize
currentTariffUsage = tariffs[settings.lookbackHours];
if (settings.domoticzEnable) { updateDomoticz(); }
}
else {
console.log("APX URL fetch failed!");
}
}
}
var urlAPX = "https://www.apxgroup.com/rest-api/quotes/APX%20Power%20NL%20Hourly?type=all&limit=3"
xmlhttp.open("GET", urlAPX, true);
xmlhttp.send();
}
Timer {
id: collectTariffsTimer
interval: 300000
triggeredOnStart: true
running: true
repeat: true
onTriggered: {
// update interval to only update at the start of the next hour
var now = new Date();
var secondsUntilNextHour = ((59 - now.getMinutes()) * 60) + (60 - now.getSeconds());
collectTariffsTimer.interval = secondsUntilNextHour * 1000;
getCurrentTariffs();
}
}
}