Skip to content

Commit

Permalink
Merge branch 'pr1920' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
tbnobody committed May 2, 2024
2 parents c08969b + 9780043 commit d5a2490
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 14 deletions.
8 changes: 8 additions & 0 deletions include/__compiled_constants.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once

// The referenced values are generated by pio-scripts/auto_firmware_version.py


extern const char *__COMPILED_GIT_HASH__;
// extern const char *__COMPILED_DATE_TIME_UTC_STR__;
60 changes: 55 additions & 5 deletions pio-scripts/auto_firmware_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#
# Copyright (C) 2022 Thomas Basler and others
#
import os
import pkg_resources

Import("env")
Expand All @@ -15,15 +16,64 @@

from dulwich import porcelain

def get_firmware_specifier_build_flag():

def updateFileIfChanged(filename, content):
mustUpdate = True
try:
fp = open(filename, "rb")
if fp.read() == content:
mustUpdate = False
fp.close()
except:
pass
if mustUpdate:
fp = open(filename, "wb")
fp.write(content)
fp.close()
return mustUpdate


def get_build_version():
try:
build_version = porcelain.describe('.') # '.' refers to the repository root dir
except:
build_version = "g0000000"
build_flag = "-D AUTO_GIT_HASH=\\\"" + build_version + "\\\""
print ("Firmware Revision: " + build_version)
return build_version


def get_firmware_specifier_build_flag():
build_version = get_build_version()
build_flag = "-D AUTO_GIT_HASH=\\\"" + build_version + "\\\""
return (build_flag)

env.Append(
BUILD_FLAGS=[get_firmware_specifier_build_flag()]
)

def do_main():
if 0:
# this results in a full recompilation of the whole project after each commit
env.Append(
BUILD_FLAGS=[get_firmware_specifier_build_flag()]
)
else:
# we just create a .c file containing the needed datas
targetfile = os.path.join(env.subst("$BUILD_DIR"), "__compiled_constants.c")
lines = ""
lines += "/* Generated file within build process - Do NOT edit */\n"

if 0:
# Add the current date and time as string in UTC timezone
from datetime import datetime, timezone
now = datetime.now(tz=timezone.utc)
COMPILED_DATE_TIME_UTC_STR = now.strftime("%Y/%m/%d %H:%M:%S")
lines += 'const char *__COMPILED_DATE_TIME_UTC_STR__ = "%s";\n' % (COMPILED_DATE_TIME_UTC_STR)

if 1:
# Add the description of the current git revision
lines += 'const char *__COMPILED_GIT_HASH__ = "%s";\n' % (get_build_version())

updateFileIfChanged(targetfile, bytes(lines, "utf-8"))

# Add the created file to the buildfiles - platformio knows how to handle *.c files
env.AppendUnique(PIOBUILDFILES=[targetfile])

do_main()
5 changes: 3 additions & 2 deletions src/MqttHandleHass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "NetworkSettings.h"
#include "Utils.h"
#include "defaults.h"
#include "__compiled_constants.h"

MqttHandleHassClass MqttHandleHass;

Expand Down Expand Up @@ -380,7 +381,7 @@ void MqttHandleHassClass::createInverterInfo(JsonDocument& root, std::shared_ptr
getDtuUrl(),
"OpenDTU",
inv->typeName(),
AUTO_GIT_HASH,
__COMPILED_GIT_HASH__,
getDtuUniqueId());
}

Expand All @@ -393,7 +394,7 @@ void MqttHandleHassClass::createDtuInfo(JsonDocument& root)
getDtuUrl(),
"OpenDTU",
"OpenDTU",
AUTO_GIT_HASH);
__COMPILED_GIT_HASH__);
}

void MqttHandleHassClass::createDeviceInfo(
Expand Down
3 changes: 2 additions & 1 deletion src/NetworkSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "defaults.h"
#include <ESPmDNS.h>
#include <ETH.h>
#include "__compiled_constants.h"

NetworkSettingsClass::NetworkSettingsClass()
: _loopTask(TASK_IMMEDIATE, TASK_FOREVER, std::bind(&NetworkSettingsClass::loop, this))
Expand Down Expand Up @@ -136,7 +137,7 @@ void NetworkSettingsClass::handleMDNS()

MDNS.addService("http", "tcp", 80);
MDNS.addService("opendtu", "tcp", 80);
MDNS.addServiceTxt("opendtu", "tcp", "git_hash", AUTO_GIT_HASH);
MDNS.addServiceTxt("opendtu", "tcp", "git_hash", __COMPILED_GIT_HASH__);

MessageOutput.println("done");
} else {
Expand Down
3 changes: 2 additions & 1 deletion src/WebApi_prometheus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "NetworkSettings.h"
#include "WebApi.h"
#include <Hoymiles.h>
#include "__compiled_constants.h"

void WebApiPrometheusClass::init(AsyncWebServer& server, Scheduler& scheduler)
{
Expand All @@ -29,7 +30,7 @@ void WebApiPrometheusClass::onPrometheusMetricsGet(AsyncWebServerRequest* reques
stream->print("# HELP opendtu_build Build info\n");
stream->print("# TYPE opendtu_build gauge\n");
stream->printf("opendtu_build{name=\"%s\",id=\"%s\",version=\"%d.%d.%d\"} 1\n",
NetworkSettings.getHostname().c_str(), AUTO_GIT_HASH, CONFIG_VERSION >> 24 & 0xff, CONFIG_VERSION >> 16 & 0xff, CONFIG_VERSION >> 8 & 0xff);
NetworkSettings.getHostname().c_str(), __COMPILED_GIT_HASH__, CONFIG_VERSION >> 24 & 0xff, CONFIG_VERSION >> 16 & 0xff, CONFIG_VERSION >> 8 & 0xff);

stream->print("# HELP opendtu_platform Platform info\n");
stream->print("# TYPE opendtu_platform gauge\n");
Expand Down
7 changes: 2 additions & 5 deletions src/WebApi_sysstatus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
#include <Hoymiles.h>
#include <LittleFS.h>
#include <ResetReason.h>

#ifndef AUTO_GIT_HASH
#define AUTO_GIT_HASH ""
#endif
#include "__compiled_constants.h"

void WebApiSysstatusClass::init(AsyncWebServer& server, Scheduler& scheduler)
{
Expand Down Expand Up @@ -64,7 +61,7 @@ void WebApiSysstatusClass::onSystemStatus(AsyncWebServerRequest* request)
char version[16];
snprintf(version, sizeof(version), "%d.%d.%d", CONFIG_VERSION >> 24 & 0xff, CONFIG_VERSION >> 16 & 0xff, CONFIG_VERSION >> 8 & 0xff);
root["config_version"] = version;
root["git_hash"] = AUTO_GIT_HASH;
root["git_hash"] = __COMPILED_GIT_HASH__;
root["pioenv"] = PIOENV;

root["uptime"] = esp_timer_get_time() / 1000000;
Expand Down

0 comments on commit d5a2490

Please sign in to comment.