Skip to content

Commit 46cd8c6

Browse files
authored
Added hour to tariff peaks (#1028)
* Added hour to tariff peaks * Some adjustments
1 parent c307103 commit 46cd8c6

File tree

9 files changed

+159
-151
lines changed

9 files changed

+159
-151
lines changed

lib/EnergyAccounting/include/EnergyAccounting.h

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
#include "PriceService.h"
1414

1515
struct EnergyAccountingPeak {
16+
uint8_t day;
17+
uint8_t hour;
18+
uint16_t value;
19+
};
20+
21+
struct EnergyAccountingPeak6 {
1622
uint8_t day;
1723
uint16_t value;
1824
};
@@ -32,34 +38,19 @@ struct EnergyAccountingData {
3238
EnergyAccountingPeak peaks[5];
3339
};
3440

35-
struct EnergyAccountingData5 {
36-
uint8_t version;
37-
uint8_t month;
38-
uint16_t costYesterday;
39-
uint16_t costThisMonth;
40-
uint16_t costLastMonth;
41-
uint16_t incomeYesterday;
42-
uint16_t incomeThisMonth;
43-
uint16_t incomeLastMonth;
44-
EnergyAccountingPeak peaks[5];
45-
};
46-
47-
struct EnergyAccountingData4 {
41+
struct EnergyAccountingData6 {
4842
uint8_t version;
4943
uint8_t month;
50-
uint16_t costYesterday;
51-
uint16_t costThisMonth;
52-
uint16_t costLastMonth;
53-
EnergyAccountingPeak peaks[5];
54-
};
55-
56-
struct EnergyAccountingData2 {
57-
uint8_t version;
58-
uint8_t month;
59-
uint16_t maxHour;
60-
uint16_t costYesterday;
61-
uint16_t costThisMonth;
62-
uint16_t costLastMonth;
44+
int32_t costYesterday;
45+
int32_t costThisMonth;
46+
int32_t costLastMonth;
47+
int32_t incomeYesterday;
48+
int32_t incomeThisMonth;
49+
int32_t incomeLastMonth;
50+
uint32_t lastMonthImport;
51+
uint32_t lastMonthExport;
52+
uint8_t lastMonthAccuracy;
53+
EnergyAccountingPeak6 peaks[5];
6354
};
6455

6556
struct EnergyAccountingRealtimeData {
@@ -142,7 +133,7 @@ class EnergyAccounting {
142133
String currency = "";
143134

144135
void calcDayCost();
145-
bool updateMax(uint16_t val, uint8_t day);
136+
bool updateMax(uint16_t val, uint8_t day, uint8_t hour);
146137
};
147138

148139
#endif

lib/EnergyAccounting/src/EnergyAccounting.cpp

Lines changed: 25 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,15 @@ bool EnergyAccounting::update(AmsData* amsData) {
7272
this->realtimeData->currentHour = local.Hour;
7373
this->realtimeData->currentDay = local.Day;
7474
if(!load()) {
75-
data = { 6, local.Month,
75+
data = { 7, local.Month,
7676
0, 0, 0, // Cost
7777
0, 0, 0, // Income
7878
0, 0, 0, // Last month import, export and accuracy
79-
0, 0, // Peak 1
80-
0, 0, // Peak 2
81-
0, 0, // Peak 3
82-
0, 0, // Peak 4
83-
0, 0 // Peak 5
79+
0, 0, 0, // Peak 1
80+
0, 0, 0, // Peak 2
81+
0, 0, 0, // Peak 3
82+
0, 0, 0, // Peak 4
83+
0, 0, 0 // Peak 5
8484
};
8585
}
8686
init = true;
@@ -97,7 +97,7 @@ bool EnergyAccounting::update(AmsData* amsData) {
9797
uint16_t val = round(ds->getHourImport(oneHrAgo.Hour) / 10.0);
9898

9999
breakTime(tz->toLocal(now-3600), oneHrAgoLocal);
100-
ret |= updateMax(val, oneHrAgoLocal.Day);
100+
ret |= updateMax(val, oneHrAgoLocal.Day, oneHrAgoLocal.Hour);
101101

102102
this->realtimeData->currentHour = local.Hour; // Need to be defined here so that day cost is correctly calculated
103103
if(local.Hour > 0) {
@@ -407,85 +407,29 @@ bool EnergyAccounting::load() {
407407
char buf[file.size()];
408408
file.readBytes(buf, file.size());
409409

410-
if(buf[0] == 6) {
410+
if(buf[0] == 7) {
411411
EnergyAccountingData* data = (EnergyAccountingData*) buf;
412412
memcpy(&this->data, data, sizeof(this->data));
413413
ret = true;
414-
} else if(buf[0] == 5) {
415-
EnergyAccountingData5* data = (EnergyAccountingData5*) buf;
416-
this->data = { 6, data->month,
417-
((uint32_t) data->costYesterday) * 10,
418-
((uint32_t) data->costThisMonth) * 100,
419-
((uint32_t) data->costLastMonth) * 100,
420-
((uint32_t) data->incomeYesterday) * 10,
421-
((uint32_t) data->incomeThisMonth) * 100,
422-
((uint32_t) data->incomeLastMonth) * 100,
423-
0,0,0, // Last month import, export and accuracy
424-
data->peaks[0].day, data->peaks[0].value,
425-
data->peaks[1].day, data->peaks[1].value,
426-
data->peaks[2].day, data->peaks[2].value,
427-
data->peaks[3].day, data->peaks[3].value,
428-
data->peaks[4].day, data->peaks[4].value
429-
};
430-
ret = true;
431-
} else if(buf[0] == 4) {
432-
EnergyAccountingData4* data = (EnergyAccountingData4*) buf;
433-
this->data = { 5, data->month,
434-
((uint32_t) data->costYesterday) * 10,
435-
((uint32_t) data->costThisMonth) * 100,
436-
((uint32_t) data->costLastMonth) * 100,
437-
0,0,0, // Income from production
438-
0,0,0, // Last month import, export and accuracy
439-
data->peaks[0].day, data->peaks[0].value,
440-
data->peaks[1].day, data->peaks[1].value,
441-
data->peaks[2].day, data->peaks[2].value,
442-
data->peaks[3].day, data->peaks[3].value,
443-
data->peaks[4].day, data->peaks[4].value
444-
};
445-
ret = true;
446-
} else if(buf[0] == 3) {
447-
EnergyAccountingData* data = (EnergyAccountingData*) buf;
448-
this->data = { 5, data->month,
449-
data->costYesterday * 10,
414+
} else if(buf[0] == 6) {
415+
EnergyAccountingData6* data = (EnergyAccountingData6*) buf;
416+
this->data = { 7, data->month,
417+
data->costYesterday,
450418
data->costThisMonth,
451419
data->costLastMonth,
452-
0,0,0, // Income from production
453-
0,0,0, // Last month import, export and accuracy
454-
data->peaks[0].day, data->peaks[0].value,
455-
data->peaks[1].day, data->peaks[1].value,
456-
data->peaks[2].day, data->peaks[2].value,
457-
data->peaks[3].day, data->peaks[3].value,
458-
data->peaks[4].day, data->peaks[4].value
420+
data->incomeYesterday,
421+
data->incomeThisMonth,
422+
data->incomeLastMonth,
423+
data->lastMonthImport,
424+
data->lastMonthExport,
425+
data->lastMonthAccuracy,
426+
data->peaks[0].day, 0, data->peaks[0].value,
427+
data->peaks[1].day, 0, data->peaks[1].value,
428+
data->peaks[2].day, 0, data->peaks[2].value,
429+
data->peaks[3].day, 0, data->peaks[3].value,
430+
data->peaks[4].day, 0, data->peaks[4].value
459431
};
460432
ret = true;
461-
} else {
462-
data = { 5, 0,
463-
0, 0, 0, // Cost
464-
0,0,0, // Income from production
465-
0,0,0, // Last month import, export and accuracy
466-
0, 0, // Peak 1
467-
0, 0, // Peak 2
468-
0, 0, // Peak 3
469-
0, 0, // Peak 4
470-
0, 0 // Peak 5
471-
};
472-
if(buf[0] == 2) {
473-
EnergyAccountingData2* data = (EnergyAccountingData2*) buf;
474-
this->data.month = data->month;
475-
this->data.costYesterday = data->costYesterday * 10;
476-
this->data.costThisMonth = data->costThisMonth;
477-
this->data.costLastMonth = data->costLastMonth;
478-
uint8_t b = 0;
479-
for(uint8_t i = sizeof(this->data); i < file.size(); i+=2) {
480-
this->data.peaks[b].day = b;
481-
memcpy(&this->data.peaks[b].value, buf+i, 2);
482-
b++;
483-
if(b >= config->hours || b >= 5) break;
484-
}
485-
ret = true;
486-
} else {
487-
ret = false;
488-
}
489433
}
490434

491435
file.close();
@@ -518,11 +462,12 @@ void EnergyAccounting::setData(EnergyAccountingData& data) {
518462
this->data = data;
519463
}
520464

521-
bool EnergyAccounting::updateMax(uint16_t val, uint8_t day) {
465+
bool EnergyAccounting::updateMax(uint16_t val, uint8_t day, uint8_t hour) {
522466
for(uint8_t i = 0; i < 5; i++) {
523467
if(data.peaks[i].day == day || data.peaks[i].day == 0) {
524468
if(val > data.peaks[i].value) {
525469
data.peaks[i].day = day;
470+
data.peaks[i].hour = hour;
526471
data.peaks[i].value = val;
527472
return true;
528473
}

lib/SvelteUi/app/dist/index.js

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/SvelteUi/app/src/lib/BarChart.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
{#if !isNaN(yScale(tick.value))}
6262
<g class="tick tick-{tick.value} tick-{tick.color}" transform="translate(0, {yScale(tick.value)})">
6363
<line x2="100%"></line>
64-
<text y="-4" x={tick.align == 'right' ? '85%' : ''}>{tick.label}</text>
64+
<text y="-4" x={tick.align == 'right' ? '90%' : ''}>{tick.label}</text>
6565
</g>
6666
{/if}
6767
{/each}

lib/SvelteUi/app/src/lib/Dashboard.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@
128128
{/if}
129129
{#if uiVisibility(sysinfo.ui.t, data.pr && (data.pr.startsWith("NO") || data.pr.startsWith("10YNO") || data.pr.startsWith('10Y1001A1001A4')))}
130130
<div class="cnt h-64">
131-
<TariffPeakChart title={translations.dashboard?.tariffpeak ?? "Tariff peaks"} tariffData={tariffData} translations={translations}/>
131+
<TariffPeakChart title={translations.dashboard?.tariffpeak ?? "Tariff peaks"} tariffData={tariffData} realtime={data.ea} translations={translations}/>
132132
</div>
133133
{/if}
134134
{#if uiVisibility(sysinfo.ui.l, data.hm == 1)}

lib/SvelteUi/app/src/lib/TariffPeakChart.svelte

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script>
2-
import { zeropad } from './Helpers.js';
2+
import { ampcol, zeropad } from './Helpers.js';
33
import BarChart from './BarChart.svelte';
44
55
export let title;
@@ -12,6 +12,7 @@
1212
let min = 0;
1313
1414
export let tariffData;
15+
export let realtime;
1516
1617
$: {
1718
let i = 0;
@@ -24,16 +25,36 @@
2425
label: 0
2526
});
2627
28+
console.log(realtime);
29+
30+
if(tariffData && !isNaN(realtime?.h?.u)) {
31+
points.push({
32+
label: realtime.h.u.toFixed(2),
33+
value: realtime.h.u,
34+
title: realtime.h.u.toFixed(2) + ' kWh',
35+
color: ampcol(realtime.h.u/tariffData.c*100.0)
36+
});
37+
xTicks.push({
38+
label: translations.common?.now ?? "Now"
39+
});
40+
}
41+
2742
if(tariffData && tariffData.p) {
2843
for(i = 0; i < tariffData.p.length; i++) {
2944
let peak = tariffData.p[i];
45+
let daylabel = peak.d > 0 ? zeropad(peak.d) + "." + (translations.months ? translations.months?.[new Date().getMonth()] : zeropad(new Date().getMonth()+1)) : "-";
46+
let title = daylabel;
47+
if(!isNaN(peak.h))
48+
title = title + ' ' + zeropad(peak.h) + ':00';
49+
title = title + ': ' + peak.v.toFixed(2) + ' kWh';
3050
points.push({
3151
label: peak.v.toFixed(2),
3252
value: peak.v,
53+
title: title,
3354
color: dark ? '#5c2da5' : '#7c3aed'
3455
});
3556
xTicks.push({
36-
label: peak.d > 0 ? zeropad(peak.d) + "." + (translations.months ? translations.months?.[new Date().getMonth()] : zeropad(new Date().getMonth()+1)) : "-"
57+
label: daylabel
3758
})
3859
max = Math.max(max, peak.v);
3960
}
@@ -71,7 +92,7 @@
7192
config = {
7293
title: title,
7394
dark: document.documentElement.classList.contains('dark'),
74-
padding: { top: 20, right: 35, bottom: 20, left: 35 },
95+
padding: { top: 20, right: 20, bottom: 20, left: 20 },
7596
y: {
7697
min: min,
7798
max: max,

lib/SvelteUi/app/vite.config.js

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,27 @@ export default defineConfig({
1717
plugins: [svelte()],
1818
server: {
1919
proxy: {
20-
"/data.json": "http://192.168.21.192",
21-
"/energyprice.json": "http://192.168.21.192",
22-
"/dayplot.json": "http://192.168.21.192",
23-
"/monthplot.json": "http://192.168.21.192",
24-
"/temperature.json": "http://192.168.21.192",
25-
"/sysinfo.json": "http://192.168.21.192",
26-
"/configuration.json": "http://192.168.21.192",
27-
"/tariff.json": "http://192.168.21.192",
28-
"/realtime.json": "http://192.168.21.192",
29-
"/priceconfig.json": "http://192.168.21.192",
30-
"/translations.json": "http://192.168.21.192",
31-
"/cloudkey.json": "http://192.168.21.192",
32-
"/wifiscan.json": "http://192.168.21.192",
33-
"/save": "http://192.168.21.192",
34-
"/reboot": "http://192.168.21.192",
35-
"/configfile": "http://192.168.21.192",
36-
"/upgrade": "http://192.168.21.192",
37-
"/mqtt-ca": "http://192.168.21.192",
38-
"/mqtt-cert": "http://192.168.21.192",
39-
"/mqtt-key": "http://192.168.21.192",
40-
"/logo.svg": "http://192.168.21.192",
20+
"/data.json": "http://192.168.21.122",
21+
"/energyprice.json": "http://192.168.21.122",
22+
"/dayplot.json": "http://192.168.21.122",
23+
"/monthplot.json": "http://192.168.21.122",
24+
"/temperature.json": "http://192.168.21.122",
25+
"/sysinfo.json": "http://192.168.21.122",
26+
"/configuration.json": "http://192.168.21.122",
27+
"/tariff.json": "http://192.168.21.122",
28+
"/realtime.json": "http://192.168.21.122",
29+
"/priceconfig.json": "http://192.168.21.122",
30+
"/translations.json": "http://192.168.21.122",
31+
"/cloudkey.json": "http://192.168.21.122",
32+
"/wifiscan.json": "http://192.168.21.122",
33+
"/save": "http://192.168.21.122",
34+
"/reboot": "http://192.168.21.122",
35+
"/configfile": "http://192.168.21.122",
36+
"/upgrade": "http://192.168.21.122",
37+
"/mqtt-ca": "http://192.168.21.122",
38+
"/mqtt-cert": "http://192.168.21.122",
39+
"/mqtt-key": "http://192.168.21.122",
40+
"/logo.svg": "http://192.168.21.122",
4141
}
4242
}
4343
})

lib/SvelteUi/src/AmsWebServer.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2097,8 +2097,9 @@ void AmsWebServer::tariffJson() {
20972097
String peaks;
20982098
for(uint8_t x = 0;x < min((uint8_t) 5, eac->hours); x++) {
20992099
EnergyAccountingPeak peak = ea->getPeak(x+1);
2100-
int len = snprintf_P(buf, BufferSize, PSTR("{\"d\":%d,\"v\":%.2f}"),
2100+
int len = snprintf_P(buf, BufferSize, PSTR("{\"d\":%d,\"h\":%d,\"v\":%.2f}"),
21012101
peak.day,
2102+
peak.hour,
21022103
peak.value / 100.0
21032104
);
21042105
buf[len] = '\0';
@@ -2592,7 +2593,7 @@ void AmsWebServer::configFileDownload() {
25922593
EnergyAccountingConfig eac;
25932594
config->getEnergyAccountingConfig(eac);
25942595
EnergyAccountingData ead = ea->getData();
2595-
server.sendContent(buf, snprintf_P(buf, BufferSize, PSTR("energyaccounting %d %d %.2f %.2f %.2f %.2f %.2f %.2f %d %.2f %d %.2f %d %.2f %d %.2f %d %.2f %.2f %.2f"),
2596+
server.sendContent(buf, snprintf_P(buf, BufferSize, PSTR("energyaccounting %d %d %.2f %.2f %.2f %.2f %.2f %.2f %d %d %.2f %d %d %.2f %d %d %.2f %d %d %.2f %d %d %.2f %.2f %.2f"),
25962597
ead.version,
25972598
ead.month,
25982599
ea->getCostYesterday(),
@@ -2602,14 +2603,19 @@ void AmsWebServer::configFileDownload() {
26022603
ea->getIncomeThisMonth(),
26032604
ea->getIncomeLastMonth(),
26042605
ead.peaks[0].day,
2606+
ead.peaks[0].hour,
26052607
ead.peaks[0].value / 100.0,
26062608
ead.peaks[1].day,
2609+
ead.peaks[1].hour,
26072610
ead.peaks[1].value / 100.0,
26082611
ead.peaks[2].day,
2612+
ead.peaks[2].hour,
26092613
ead.peaks[2].value / 100.0,
26102614
ead.peaks[3].day,
2615+
ead.peaks[3].hour,
26112616
ead.peaks[3].value / 100.0,
26122617
ead.peaks[4].day,
2618+
ead.peaks[4].hour,
26132619
ead.peaks[4].value / 100.0,
26142620
ea->getUseLastMonth(),
26152621
ea->getProducedLastMonth()

0 commit comments

Comments
 (0)