Move version constants into its own compile-unit

Speeds up the build by moving those constants into a separate compile-unit.
Currently, since the version header is auto regenerated on each build,
all modules that include it, also have to be recompiled every time.
This commit is contained in:
david-beinder 2023-04-28 23:43:05 +02:00
parent 13dda2bb19
commit 849eac1c0f
No known key found for this signature in database
13 changed files with 61 additions and 53 deletions

2
.gitignore vendored
View File

@ -7,7 +7,7 @@
.vscode
.pio
platformio-user.ini
/lib/AmsConfiguration/include/version.h
/lib/FirmwareVersion/src/generated_version.h
/src/web/root
/src/AmsToMqttBridge.ino.cpp
/test

View File

@ -2,7 +2,7 @@
#include <lwip/apps/sntp.h>
#include "LittleFS.h"
#include "AmsStorage.h"
#include "version.h"
#include "FirmwareVersion.h"
AmsDataStorage::AmsDataStorage(RemoteDebug* debugger) {
day.version = 5;
@ -28,20 +28,20 @@ bool AmsDataStorage::update(AmsData* data) {
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR("(AmsDataStorage) Timezone is missing\n"));
return false;
}
if(now < BUILD_EPOCH) {
if(data->getMeterTimestamp() > BUILD_EPOCH) {
if(now < FirmwareVersion::BuildEpoch) {
if(data->getMeterTimestamp() > FirmwareVersion::BuildEpoch) {
now = data->getMeterTimestamp();
if(debugger->isActive(RemoteDebug::DEBUG)) {
debugger->printf_P(PSTR("(AmsDataStorage) Using meter timestamp, which is: %lu\n"), (int32_t) now);
}
} else if(data->getPackageTimestamp() > BUILD_EPOCH) {
} else if(data->getPackageTimestamp() > FirmwareVersion::BuildEpoch) {
now = data->getPackageTimestamp();
if(debugger->isActive(RemoteDebug::DEBUG)) {
debugger->printf_P(PSTR("(AmsDataStorage) Using package timestamp, which is: %lu\n"), (int32_t) now);
}
}
}
if(now < BUILD_EPOCH) {
if(now < FirmwareVersion::BuildEpoch) {
if(debugger->isActive(RemoteDebug::VERBOSE)) {
debugger->printf_P(PSTR("(AmsDataStorage) Invalid time: %lu\n"), (int32_t) now);
}
@ -551,7 +551,7 @@ bool AmsDataStorage::isHappy() {
bool AmsDataStorage::isDayHappy() {
time_t now = time(nullptr);
if(now < BUILD_EPOCH) return false;
if(now < FirmwareVersion::BuildEpoch) return false;
tmElements_t tm, last;
if(now < day.lastMeterReadTime) {
@ -579,7 +579,7 @@ bool AmsDataStorage::isMonthHappy() {
}
time_t now = time(nullptr);
if(now < BUILD_EPOCH) return false;
if(now < FirmwareVersion::BuildEpoch) return false;
tmElements_t tm, last;
if(now < month.lastMeterReadTime) {

View File

@ -1,7 +1,7 @@
#include "EnergyAccounting.h"
#include "LittleFS.h"
#include "AmsStorage.h"
#include "version.h"
#include "FirmwareVersion.h"
EnergyAccounting::EnergyAccounting(RemoteDebug* debugger) {
data.version = 1;
@ -33,7 +33,7 @@ bool EnergyAccounting::isInitialized() {
bool EnergyAccounting::update(AmsData* amsData) {
if(config == NULL) return false;
time_t now = time(nullptr);
if(now < BUILD_EPOCH) return false;
if(now < FirmwareVersion::BuildEpoch) return false;
if(tz == NULL) {
if(debugger->isActive(RemoteDebug::VERBOSE)) debugger->printf_P(PSTR("(EnergyAccounting) Timezone is missing\n"));
return false;
@ -188,7 +188,7 @@ float EnergyAccounting::getUseThisHour() {
float EnergyAccounting::getUseToday() {
float ret = 0.0;
time_t now = time(nullptr);
if(now < BUILD_EPOCH) return 0.0;
if(now < FirmwareVersion::BuildEpoch) return 0.0;
if(tz == NULL) return 0.0;
tmElements_t utc, local;
breakTime(tz->toLocal(now), local);
@ -201,7 +201,7 @@ float EnergyAccounting::getUseToday() {
float EnergyAccounting::getUseThisMonth() {
time_t now = time(nullptr);
if(now < BUILD_EPOCH) return 0.0;
if(now < FirmwareVersion::BuildEpoch) return 0.0;
float ret = 0;
for(uint8_t i = 0; i < currentDay; i++) {
ret += ds->getDayImport(i) / 1000.0;
@ -216,7 +216,7 @@ float EnergyAccounting::getProducedThisHour() {
float EnergyAccounting::getProducedToday() {
float ret = 0.0;
time_t now = time(nullptr);
if(now < BUILD_EPOCH) return 0.0;
if(now < FirmwareVersion::BuildEpoch) return 0.0;
tmElements_t utc, local;
breakTime(tz->toLocal(now), local);
for(uint8_t i = 0; i < currentHour; i++) {
@ -228,7 +228,7 @@ float EnergyAccounting::getProducedToday() {
float EnergyAccounting::getProducedThisMonth() {
time_t now = time(nullptr);
if(now < BUILD_EPOCH) return 0.0;
if(now < FirmwareVersion::BuildEpoch) return 0.0;
float ret = 0;
for(uint8_t i = 0; i < currentDay; i++) {
ret += ds->getDayExport(i) / 1000.0;

View File

@ -3,7 +3,7 @@
#include "Uptime.h"
#include "TimeLib.h"
#include "DnbCurrParser.h"
#include "version.h"
#include "FirmwareVersion.h"
#include "GcmParser.h"
@ -37,7 +37,7 @@ void EntsoeApi::setup(EntsoeConfig& config) {
http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
http.setReuse(false);
http.setTimeout(60000);
http.setUserAgent("ams2mqtt/" + String(VERSION));
http.setUserAgent("ams2mqtt/" + String(FirmwareVersion::VersionString));
http.useHTTP10(true);
#if defined(AMS2MQTT_PRICE_KEY)
@ -135,7 +135,7 @@ bool EntsoeApi::loop() {
if(now < 10000) return false; // Grace period
time_t t = time(nullptr);
if(t < BUILD_EPOCH) return false;
if(t < FirmwareVersion::BuildEpoch) return false;
#ifndef AMS2MQTT_PRICE_KEY
if(strlen(getToken()) == 0) {

View File

@ -0,0 +1,12 @@
#ifndef _FIRMWARE_VERSION_h
#define _FIRMWARE_VERSION_h
class FirmwareVersion {
public:
static long BuildEpoch;
static const char* VersionString;
};
#endif

View File

@ -0,0 +1,5 @@
#include "FirmwareVersion.h"
#include "generated_version.h"
long FirmwareVersion::BuildEpoch = BUILD_EPOCH;
const char* FirmwareVersion::VersionString = VERSION_STRING;

View File

@ -1,7 +1,7 @@
#include "HomeAssistantMqttHandler.h"
#include "hexutils.h"
#include "Uptime.h"
#include "version.h"
#include "FirmwareVersion.h"
#include "json/ha1_json.h"
#include "json/ha2_json.h"
#include "json/ha3_json.h"
@ -329,7 +329,7 @@ bool HomeAssistantMqttHandler::publishSystem(HwTools* hw, EntsoeApi* eapi, Energ
hw->getVcc(),
hw->getWifiRssi(),
hw->getTemperature(),
VERSION
FirmwareVersion::VersionString
);
bool ret = mqtt->publish(topic + "/state", json);
loop();
@ -353,7 +353,7 @@ void HomeAssistantMqttHandler::publishSensor(const HomeAssistantSensor& sensor)
deviceUid.c_str(),
deviceName.c_str(),
deviceModel.c_str(),
VERSION,
FirmwareVersion::VersionString,
manufacturer.c_str(),
deviceUrl.c_str(),
strlen_P(sensor.devcl) > 0 ? ",\"dev_cla\":\"" : "",

View File

@ -1,5 +1,5 @@
#include "JsonMqttHandler.h"
#include "version.h"
#include "FirmwareVersion.h"
#include "hexutils.h"
#include "Uptime.h"
#include "json/json1_json.h"
@ -341,7 +341,7 @@ bool JsonMqttHandler::publishSystem(HwTools* hw, EntsoeApi* eapi, EnergyAccounti
hw->getVcc(),
hw->getWifiRssi(),
hw->getTemperature(),
VERSION
FirmwareVersion::VersionString
);
bool ret = mqtt->publish(topic, json);
loop();

View File

@ -1,5 +1,6 @@
#include "AmsWebServer.h"
#include "AmsWebHeaders.h"
#include "FirmwareVersion.h"
#include "base64.h"
#include "hexutils.h"
@ -31,8 +32,6 @@
#include "html/conf_ui_json.h"
#include "html/firmware_html.h"
#include "version.h"
#if defined(ESP32)
#include <esp_task_wdt.h>
#include <esp_wifi.h>
@ -64,9 +63,9 @@ void AmsWebServer::setup(AmsConfiguration* config, GpioConfig* gpioConfig, Meter
this->ea = ea;
server.on(F("/"), HTTP_GET, std::bind(&AmsWebServer::indexHtml, this));
snprintf_P(buf, 32, PSTR("/index-%s.js"), VERSION);
snprintf_P(buf, 32, PSTR("/index-%s.js"), FirmwareVersion::VersionString);
server.on(buf, HTTP_GET, std::bind(&AmsWebServer::indexJs, this));
snprintf_P(buf, 32, PSTR("/index-%s.css"), VERSION);
snprintf_P(buf, 32, PSTR("/index-%s.css"), FirmwareVersion::VersionString);
server.on(buf, HTTP_GET, std::bind(&AmsWebServer::indexCss, this));
server.on(F("/configuration"), HTTP_GET, std::bind(&AmsWebServer::indexHtml, this));
@ -263,7 +262,7 @@ void AmsWebServer::sysinfoJson() {
meterId.replace(F("\\"), F("\\\\"));
int size = snprintf_P(buf, BufferSize, SYSINFO_JSON,
VERSION,
FirmwareVersion::VersionString,
#if defined(CONFIG_IDF_TARGET_ESP32S2)
"esp32s2",
#elif defined(CONFIG_IDF_TARGET_ESP32C3)
@ -824,7 +823,7 @@ void AmsWebServer::configurationJson() {
server.setContentLength(CONTENT_LENGTH_UNKNOWN);
server.send_P(200, MIME_JSON, PSTR("{\"version\":\""));
server.sendContent_P(VERSION);
server.sendContent_P(FirmwareVersion::VersionString);
server.sendContent_P(PSTR("\","));
snprintf_P(buf, BufferSize, CONF_GENERAL_JSON,
ntpConfig.timezone,
@ -1536,7 +1535,7 @@ void AmsWebServer::upgrade() {
}
void AmsWebServer::upgradeFromUrl(String url, String nextVersion) {
config->setUpgradeInformation(0xFF, 0xFF, VERSION, nextVersion.c_str());
config->setUpgradeInformation(0xFF, 0xFF, FirmwareVersion::VersionString, nextVersion.c_str());
WiFiClient client;
#if defined(ESP8266)
@ -1555,10 +1554,10 @@ void AmsWebServer::upgradeFromUrl(String url, String nextVersion) {
#if defined(ESP8266)
ESP8266HTTPUpdate httpUpdate = ESP8266HTTPUpdate(60000);
String currentVersion = VERSION;
String currentVersion = FirmwareVersion::VersionString;
#elif defined(ESP32)
HTTPUpdate httpUpdate = HTTPUpdate(60000);
String currentVersion = String(VERSION) + "-" + chipType;
String currentVersion = String(FirmwareVersion::VersionString) + "-" + chipType;
#endif
httpUpdate.rebootOnUpdate(false);
@ -1566,7 +1565,7 @@ void AmsWebServer::upgradeFromUrl(String url, String nextVersion) {
HTTPUpdateResult ret = httpUpdate.update(client, url, currentVersion);
int lastError = httpUpdate.getLastError();
config->setUpgradeInformation(ret, ret == HTTP_UPDATE_OK ? 0 : lastError, VERSION, nextVersion.c_str());
config->setUpgradeInformation(ret, ret == HTTP_UPDATE_OK ? 0 : lastError, FirmwareVersion::VersionString, nextVersion.c_str());
switch(ret) {
case HTTP_UPDATE_FAILED:
debugger->printf_P(PSTR("Update failed\n"));
@ -1604,7 +1603,7 @@ void AmsWebServer::firmwarePost() {
if(rebootForUpgrade) {
server.send(200);
} else {
config->setUpgradeInformation(0xFF, 0xFF, VERSION, "");
config->setUpgradeInformation(0xFF, 0xFF, FirmwareVersion::VersionString, "");
if(server.hasArg(F("url"))) {
String url = server.arg(F("url"));
if(!url.isEmpty() && (url.startsWith(F("http://")) || url.startsWith(F("https://")))) {
@ -1891,7 +1890,7 @@ void AmsWebServer::configFileDownload() {
server.setContentLength(CONTENT_LENGTH_UNKNOWN);
server.send_P(200, MIME_PLAIN, PSTR("amsconfig\n"));
server.sendContent(buf, snprintf_P(buf, BufferSize, PSTR("version %s\n"), VERSION));
server.sendContent(buf, snprintf_P(buf, BufferSize, PSTR("version %s\n"), FirmwareVersion::VersionString));
server.sendContent(buf, snprintf_P(buf, BufferSize, PSTR("boardType %d\n"), sys.boardType));
if(includeWifi) {

View File

@ -2,7 +2,7 @@
extra_configs = platformio-user.ini
[common]
lib_deps = EEPROM, LittleFS, DNSServer, https://github.com/256dpi/arduino-mqtt.git, OneWireNg@0.10.0, DallasTemperature@3.9.1, EspSoftwareSerial@6.14.1, https://github.com/gskjold/RemoteDebug.git, Time@1.6.1, Timezone@1.2.4, AmsConfiguration, AmsData, AmsDataStorage, HwTools, Uptime, AmsDecoder, EntsoePriceApi, EnergyAccounting, RawMqttHandler, JsonMqttHandler, DomoticzMqttHandler, HomeAssistantMqttHandler, SvelteUi
lib_deps = EEPROM, LittleFS, DNSServer, https://github.com/256dpi/arduino-mqtt.git, OneWireNg@0.10.0, DallasTemperature@3.9.1, EspSoftwareSerial@6.14.1, https://github.com/gskjold/RemoteDebug.git, Time@1.6.1, Timezone@1.2.4, FirmwareVersion, AmsConfiguration, AmsData, AmsDataStorage, HwTools, Uptime, AmsDecoder, EntsoePriceApi, EnergyAccounting, RawMqttHandler, JsonMqttHandler, DomoticzMqttHandler, HomeAssistantMqttHandler, SvelteUi
lib_ignore = OneWire
extra_scripts =
pre:scripts/addversion.py

View File

@ -2,7 +2,7 @@ import os
import subprocess
from time import time
FILENAME_VERSION_H = 'lib/AmsConfiguration/include/version.h'
FILENAME_VERSION_H = 'lib/FirmwareVersion/src/generated_version.h'
version = os.environ.get('GITHUB_TAG')
if version == None:
try:
@ -15,9 +15,7 @@ if version == None:
version = "SNAPSHOT"
hf = """
#ifndef VERSION
#define VERSION "{}"
#endif
#define VERSION_STRING "{}"
#define BUILD_EPOCH {}
""".format(version, round(time()))
with open(FILENAME_VERSION_H, 'w+') as f:

View File

@ -36,8 +36,7 @@ ADC_MODE(ADC_VCC);
#include <driver/uart.h>
#endif
#include "version.h"
#include "FirmwareVersion.h"
#include "AmsToMqttBridge.h"
#include "AmsStorage.h"
#include "AmsDataStorage.h"
@ -309,7 +308,7 @@ void setup() {
}
flashed = Update.end(true);
}
config.setUpgradeInformation(flashed ? 2 : 0, 0xFF, VERSION, "");
config.setUpgradeInformation(flashed ? 2 : 0, 0xFF, FirmwareVersion::VersionString, "");
firmwareFile.close();
} else {
debugW_P(PSTR("AP button pressed, skipping firmware update and deleting firmware file."));
@ -1126,15 +1125,15 @@ void handleDataSuccess(AmsData* data) {
}
time_t now = time(nullptr);
if(now < BUILD_EPOCH && data->getListType() >= 3) {
if(data->getMeterTimestamp() > BUILD_EPOCH) {
if(now < FirmwareVersion::BuildEpoch && data->getListType() >= 3) {
if(data->getMeterTimestamp() > FirmwareVersion::BuildEpoch) {
debugI_P(PSTR("Using timestamp from meter"));
now = data->getMeterTimestamp();
} else if(data->getPackageTimestamp() > BUILD_EPOCH) {
} else if(data->getPackageTimestamp() > FirmwareVersion::BuildEpoch) {
debugI_P(PSTR("Using timestamp from meter (DLMS)"));
now = data->getPackageTimestamp();
}
if(now > BUILD_EPOCH) {
if(now > FirmwareVersion::BuildEpoch) {
timeval tv { now, 0};
settimeofday(&tv, nullptr);
}
@ -1143,7 +1142,7 @@ void handleDataSuccess(AmsData* data) {
meterState.apply(*data);
bool saveData = false;
if(!ds.isHappy() && now > BUILD_EPOCH) {
if(!ds.isHappy() && now > FirmwareVersion::BuildEpoch) {
debugD_P(PSTR("Its time to update data storage"));
tmElements_t tm;
breakTime(now, tm);
@ -1619,7 +1618,7 @@ void MQTT_connect() {
time_t epoch = time(nullptr);
if(mqttConfig.ssl) {
if(epoch < BUILD_EPOCH) {
if(epoch < FirmwareVersion::BuildEpoch) {
debugI_P(PSTR("NTP not ready for MQTT SSL"));
return;
}

View File

@ -1,5 +0,0 @@
#ifndef VERSION
#define VERSION "538de5e"
#endif
#define BUILD_EPOCH 1668532199