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
13 changed files with 61 additions and 53 deletions

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) {