Skip to content

Commit

Permalink
Some updates for price fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
gskjold committed Nov 5, 2023
1 parent 63c3d31 commit 445973f
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 51 deletions.
3 changes: 2 additions & 1 deletion lib/EntsoePriceApi/include/EntsoeApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class EntsoeApi {
char* getToken();
char* getCurrency();
char* getArea();
char* getSource();
float getValueForHour(int8_t);
float getValueForHour(time_t, int8_t);

Expand All @@ -34,7 +35,7 @@ class EntsoeApi {
private:
RemoteDebug* debugger;
EntsoeConfig* config = NULL;
HTTPClient http;
HTTPClient* http = NULL;

uint8_t currentDay = 0, currentHour = 0;
uint8_t tomorrowFetchMinute = 15; // How many minutes over 13:00 should it fetch prices
Expand Down
1 change: 1 addition & 0 deletions lib/EntsoePriceApi/include/PricesContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ struct PricesContainer {
char currency[4];
char measurementUnit[4];
int32_t points[25];
char source[4];
};
#endif
3 changes: 3 additions & 0 deletions lib/EntsoePriceApi/src/EntsoeA44Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,11 @@ size_t EntsoeA44Parser::write(uint8_t byte) {
}

void EntsoeA44Parser::get(PricesContainer* container) {
memset(container, 0, sizeof(*container));

strcpy(container->currency, currency);
strcpy(container->measurementUnit, measurementUnit);
strcpy(container->source, "EOE");

for(uint8_t i = 0; i < 25; i++) {
container->points[i] = points[i] == ENTSOE_NO_VALUE ? ENTSOE_NO_VALUE : points[i] * 10000;
Expand Down
69 changes: 45 additions & 24 deletions lib/EntsoePriceApi/src/EntsoeApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,14 @@ void EntsoeApi::setup(EntsoeConfig& config) {
if(tomorrow != NULL) delete tomorrow;
today = tomorrow = NULL;

http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
http.setReuse(false);
http.setTimeout(60000);
http.setUserAgent("ams2mqtt/" + String(FirmwareVersion::VersionString));
http.useHTTP10(true);
if(http != NULL) {
delete http;
}
http = new HTTPClient();
http->setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
http->setReuse(false);
http->setTimeout(60000);
http->setUserAgent("ams2mqtt/" + String(FirmwareVersion::VersionString));

#if defined(AMS2MQTT_PRICE_KEY)
key = new uint8_t[16] AMS2MQTT_PRICE_KEY;
Expand Down Expand Up @@ -67,6 +70,21 @@ char* EntsoeApi::getArea() {
return this->config->area;
}

char* EntsoeApi::getSource() {
if(this->today != NULL && this->tomorrow != NULL) {
if(strcmp(this->today->source, this->tomorrow->source) == 0) {
return this->today->source;
} else {
return "MIX";
}
} else if(today != NULL) {
return this->today->source;
} else if(tomorrow != NULL) {
return this->tomorrow->source;
}
return "";
}

float EntsoeApi::getValueForHour(int8_t hour) {
time_t cur = time(nullptr);
return getValueForHour(cur, hour);
Expand Down Expand Up @@ -209,7 +227,7 @@ bool EntsoeApi::loop() {

bool EntsoeApi::retrieve(const char* url, Stream* doc) {
#if defined(ESP32)
if(http.begin(url)) {
if(http->begin(url)) {
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Connection established\n"));

#if defined(ESP32)
Expand All @@ -218,7 +236,7 @@ bool EntsoeApi::retrieve(const char* url, Stream* doc) {
ESP.wdtFeed();
#endif

int status = http.GET();
int status = http->GET();

#if defined(ESP32)
esp_task_wdt_reset();
Expand All @@ -228,8 +246,8 @@ bool EntsoeApi::retrieve(const char* url, Stream* doc) {

if(status == HTTP_CODE_OK) {
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Receiving data\n"));
http.writeToStream(doc);
http.end();
http->writeToStream(doc);
http->end();
lastError = 0;
nextFetchDelayMinutes = 1;
return true;
Expand All @@ -238,15 +256,15 @@ bool EntsoeApi::retrieve(const char* url, Stream* doc) {
if(status == 429) {
nextFetchDelayMinutes = 15;
} else if(status == 404) {
nextFetchDelayMinutes = 60;
nextFetchDelayMinutes = 10;
} else {
nextFetchDelayMinutes = 30;
nextFetchDelayMinutes = 2;
}
if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf_P(PSTR("(EntsoeApi) Communication error, returned status: %d\n"), status);
if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf(http.errorToString(status).c_str());
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf(http.getString().c_str());
if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf(http->errorToString(status).c_str());
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf(http->getString().c_str());

http.end();
http->end();
return false;
}
} else {
Expand Down Expand Up @@ -349,11 +367,11 @@ PricesContainer* EntsoeApi::fetchPrices(time_t t) {
#if defined(ESP8266)
WiFiClient client;
client.setTimeout(5000);
if(http.begin(client, buf)) {
if(http->begin(client, buf)) {
#elif defined(ESP32)
if(http.begin(buf)) {
if(http->begin(buf)) {
#endif
int status = http.GET();
int status = http->GET();

#if defined(ESP32)
esp_task_wdt_reset();
Expand All @@ -363,8 +381,8 @@ PricesContainer* EntsoeApi::fetchPrices(time_t t) {

if(status == HTTP_CODE_OK) {
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("Receiving data\n"));
data = http.getString();
http.end();
data = http->getString();
http->end();

uint8_t* content = (uint8_t*) (data.c_str());
if(debugger->isActive(RemoteDebug::DEBUG)) {
Expand Down Expand Up @@ -403,15 +421,18 @@ PricesContainer* EntsoeApi::fetchPrices(time_t t) {
if(status == 429) {
nextFetchDelayMinutes = 60;
} else if(status == 404) {
nextFetchDelayMinutes = 180;
nextFetchDelayMinutes = 15;
} else {
nextFetchDelayMinutes = 30;
nextFetchDelayMinutes = 5;
}
if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf_P(PSTR("(EntsoeApi) Communication error, returned status: %d\n"), status);
if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf(http.errorToString(status).c_str());
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf(http.getString().c_str());
if(debugger->isActive(RemoteDebug::ERROR)) {
debugger->printf(http->errorToString(status).c_str());
debugger->println();
}
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf(http->getString().c_str());

http.end();
http->end();
}
}
}
Expand Down
18 changes: 9 additions & 9 deletions lib/SvelteUi/app/dist/index.js

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions lib/SvelteUi/app/src/lib/Helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ export function mqttError(err) {

export function priceError(err) {
switch(err) {
case 400:
return "Unrecognized data in request";
case 401:
case 403:
return "Unauthorized, check API key";
Expand All @@ -156,6 +158,7 @@ export function priceError(err) {
return "Exceeded API rate limit";
case 500:
return "Internal server error";
case -1: return "Connection error";
case -2: return "Incomplete data received";
case -3: return "Invalid data, tag missing";
case -51: return "Authentication failed";
Expand Down Expand Up @@ -229,4 +232,12 @@ export function getResetReason(sysinfo) {
default: return "Unknown";
}
}
}

export function getPriceSourceName(code) {
if(code == "EOE") return "ENTSO-E";
if(code == "HKS") return "hvakosterstrommen.no";
if(code == "EDS") return "Energy Data Service";
if(code == "MIX") return "Mixed sources";
return "Unknown (" + code + ")";
}
4 changes: 2 additions & 2 deletions lib/SvelteUi/app/src/lib/PricePlot.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script>
import { zeropad, addHours } from './Helpers.js';
import { zeropad, addHours, getPriceSourceName } from './Helpers.js';
import BarChart from './BarChart.svelte';
export let json;
Expand Down Expand Up @@ -122,5 +122,5 @@
</script>

<a href="https://transparency.entsoe.eu/" target="_blank" class="text-xs float-right z-40">Provided by ENTSO-E</a>
<a href="https://transparency.entsoe.eu/" target="_blank" class="text-xs float-right z-40">Provided by: {getPriceSourceName(json.source)}</a>
<BarChart config={config} />
30 changes: 15 additions & 15 deletions lib/SvelteUi/app/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ export default defineConfig({
plugins: [svelte()],
server: {
proxy: {
"/data.json": "http://192.168.233.244",
"/energyprice.json": "http://192.168.233.244",
"/dayplot.json": "http://192.168.233.244",
"/monthplot.json": "http://192.168.233.244",
"/temperature.json": "http://192.168.233.244",
"/sysinfo.json": "http://192.168.233.244",
"/configuration.json": "http://192.168.233.244",
"/tariff.json": "http://192.168.233.244",
"/save": "http://192.168.233.244",
"/reboot": "http://192.168.233.244",
"/configfile": "http://192.168.233.244",
"/upgrade": "http://192.168.233.244",
"/mqtt-ca": "http://192.168.233.244",
"/mqtt-cert": "http://192.168.233.244",
"/mqtt-key": "http://192.168.233.244",
"/data.json": "http://192.168.233.237",
"/energyprice.json": "http://192.168.233.237",
"/dayplot.json": "http://192.168.233.237",
"/monthplot.json": "http://192.168.233.237",
"/temperature.json": "http://192.168.233.237",
"/sysinfo.json": "http://192.168.233.237",
"/configuration.json": "http://192.168.233.237",
"/tariff.json": "http://192.168.233.237",
"/save": "http://192.168.233.237",
"/reboot": "http://192.168.233.237",
"/configfile": "http://192.168.233.237",
"/upgrade": "http://192.168.233.237",
"/mqtt-ca": "http://192.168.233.237",
"/mqtt-cert": "http://192.168.233.237",
"/mqtt-key": "http://192.168.233.237",
}
}
})
1 change: 1 addition & 0 deletions lib/SvelteUi/json/energyprice.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"currency" : "%s",
"source" : "%s",
"00" : %s,
"01" : %s,
"02" : %s,
Expand Down
1 change: 1 addition & 0 deletions lib/SvelteUi/src/AmsWebServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,7 @@ void AmsWebServer::energyPriceJson() {

snprintf_P(buf, BufferSize, ENERGYPRICE_JSON,
eapi == NULL ? "" : eapi->getCurrency(),
eapi == NULL ? "" : eapi->getSource(),
prices[0] == ENTSOE_NO_VALUE ? "null" : String(prices[0], 4).c_str(),
prices[1] == ENTSOE_NO_VALUE ? "null" : String(prices[1], 4).c_str(),
prices[2] == ENTSOE_NO_VALUE ? "null" : String(prices[2], 4).c_str(),
Expand Down

0 comments on commit 445973f

Please sign in to comment.