Replace haspGetVersion() with haspDevice.get_version()

This commit is contained in:
fvanroie 2021-05-18 04:19:26 +02:00
parent af9e45645e
commit 798cf8bd5e
12 changed files with 52 additions and 58 deletions

View File

@ -16,4 +16,24 @@ const char* BaseDevice::get_model()
return PIOENV; return PIOENV;
#endif #endif
} }
}
const char* BaseDevice::get_version()
{
#ifdef HASP_MODEL
return (QUOTE(HASP_VER_MAJ) "." QUOTE(HASP_VER_MIN) "." QUOTE(HASP_VER_REV));
#else
return PIOENV;
#endif
}
const char* BaseDevice::get_hostname()
{
return _hostname; //.c_str();
}
void BaseDevice::set_hostname(const char* hostname)
{
strncpy(_hostname, hostname, STR_LEN_HOSTNAME);
}
} // namespace dev

View File

@ -20,6 +20,8 @@
#include "ArduinoJson.h" #include "ArduinoJson.h"
#define STR_LEN_HOSTNAME 64
namespace dev { namespace dev {
class BaseDevice { class BaseDevice {
@ -29,13 +31,9 @@ class BaseDevice {
virtual void reboot() virtual void reboot()
{} {}
virtual const char* get_hostname() const char* get_hostname();
{ void set_hostname(const char*);
return ""; const char* get_core_version()
}
virtual void set_hostname(const char*)
{}
virtual const char* get_core_version()
{ {
return ""; return "";
} }
@ -44,6 +42,7 @@ class BaseDevice {
return ""; return "";
} }
virtual const char* get_model(); virtual const char* get_model();
virtual const char* get_version();
virtual const char* get_hardware_id() virtual const char* get_hardware_id()
{ {
return ""; return "";
@ -100,6 +99,10 @@ class BaseDevice {
snprintf(buffer, sizeof(buffer), "%d", pin); snprintf(buffer, sizeof(buffer), "%d", pin);
return buffer; return buffer;
} }
private:
// std::string _hostname;
char _hostname[STR_LEN_HOSTNAME];
}; };
} // namespace dev } // namespace dev

View File

@ -97,7 +97,7 @@ static void halGetResetInfo(String& resetReason)
Esp32Device::Esp32Device() Esp32Device::Esp32Device()
{ {
_hostname = MQTT_NODENAME; BaseDevice::set_hostname(MQTT_NODENAME);
_backlight_invert = (TFT_BACKLIGHT_ON == LOW); _backlight_invert = (TFT_BACKLIGHT_ON == LOW);
_backlight_power = 1; _backlight_power = 1;
_backlight_level = 255; _backlight_level = 255;
@ -129,14 +129,6 @@ void Esp32Device::show_info()
if(_sketch_size == 0) _sketch_size = ESP.getSketchSize(); // slow: takes ~1 second if(_sketch_size == 0) _sketch_size = ESP.getSketchSize(); // slow: takes ~1 second
} }
const char* Esp32Device::get_hostname()
{
return _hostname.c_str();
}
void Esp32Device::set_hostname(const char* hostname)
{
_hostname = hostname;
}
const char* Esp32Device::get_core_version() const char* Esp32Device::get_core_version()
{ {
return esp_get_idf_version(); // == ESP.getSdkVersion(); return esp_get_idf_version(); // == ESP.getSdkVersion();

View File

@ -19,8 +19,6 @@ class Esp32Device : public BaseDevice {
void reboot() override; void reboot() override;
void show_info() override; void show_info() override;
const char* get_hostname();
void set_hostname(const char*);
const char* get_core_version(); const char* get_core_version();
const char* get_chip_model(); const char* get_chip_model();
const char* get_hardware_id(); const char* get_hardware_id();
@ -40,7 +38,6 @@ class Esp32Device : public BaseDevice {
bool is_system_pin(uint8_t pin) override; bool is_system_pin(uint8_t pin) override;
private: private:
std::string _hostname;
std::string _hardware_id; std::string _hardware_id;
uint32_t _sketch_size; // cached because function is slow uint32_t _sketch_size; // cached because function is slow

View File

@ -534,10 +534,10 @@ void hasp_background(uint16_t pageid, uint16_t imageid)
/////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////
void haspGetVersion(char* version, size_t len) // void haspGetVersion(char* version, size_t len)
{ // {
snprintf_P(version, len, PSTR("%u.%u.%u"), HASP_VER_MAJ, HASP_VER_MIN, HASP_VER_REV); // snprintf_P(version, len, PSTR("%u.%u.%u"), HASP_VER_MAJ, HASP_VER_MIN, HASP_VER_REV);
} // }
void haspClearPage(uint16_t pageid) void haspClearPage(uint16_t pageid)
{ {
@ -577,8 +577,7 @@ void hasp_get_info(JsonDocument& doc)
char size_buf[32]; char size_buf[32];
JsonObject info = doc.createNestedObject(F(D_MANUFACTURER)); JsonObject info = doc.createNestedObject(F(D_MANUFACTURER));
haspGetVersion(size_buf, sizeof(size_buf)); info[F(D_INFO_VERSION)] = haspDevice.get_version();
info[F(D_INFO_VERSION)] = size_buf;
buffer = __DATE__; buffer = __DATE__;
buffer += (" "); buffer += (" ");

View File

@ -50,7 +50,7 @@ void haspEverySecond(void);
void haspReconnect(void); void haspReconnect(void);
void haspDisconnect(void); void haspDisconnect(void);
void haspGetVersion(char* version, size_t len); // void haspGetVersion(char* version, size_t len);
// void haspBackground(uint16_t pageid, uint16_t imageid); // void haspBackground(uint16_t pageid, uint16_t imageid);
// void haspNewObject(const JsonObject & config, uint8_t & saved_page_id); // void haspNewObject(const JsonObject & config, uint8_t & saved_page_id);

View File

@ -810,6 +810,7 @@ void dispatch_send_discovery(const char*, const char*)
doc[F("mf")] = F(D_MANUFACTURER); doc[F("mf")] = F(D_MANUFACTURER);
doc[F("hwid")] = haspDevice.get_hardware_id(); doc[F("hwid")] = haspDevice.get_hardware_id();
doc[F("pages")] = haspPages.count(); doc[F("pages")] = haspPages.count();
doc[F("sw")] = haspDevice.get_version();
JsonObject input = doc.createNestedObject(F("input")); JsonObject input = doc.createNestedObject(F("input"));
JsonArray relay = doc.createNestedArray(F("power")); JsonArray relay = doc.createNestedArray(F("power"));
@ -821,10 +822,8 @@ void dispatch_send_discovery(const char*, const char*)
#endif #endif
char data[1024]; char data[1024];
haspGetVersion(data, sizeof(data));
doc[F("sw")] = data;
size_t len = serializeJson(doc, data); size_t len = serializeJson(doc, data);
switch(mqtt_send_discovery(data, len)) { switch(mqtt_send_discovery(data, len)) {
case MQTT_ERR_OK: case MQTT_ERR_OK:
LOG_TRACE(TAG_MQTT_PUB, F(MQTT_TOPIC_DISCOVERY " => %s"), data); LOG_TRACE(TAG_MQTT_PUB, F(MQTT_TOPIC_DISCOVERY " => %s"), data);
@ -854,9 +853,9 @@ void dispatch_statusupdate(const char*, const char*)
char buffer[128]; char buffer[128];
hasp_get_sleep_state(topic); hasp_get_sleep_state(topic);
haspGetVersion(buffer, sizeof(buffer));
snprintf_P(data, sizeof(data), PSTR("{\"node\":\"%s\",\"idle\":\"%s\",\"version\":\"%s\",\"uptime\":%lu,"), snprintf_P(data, sizeof(data), PSTR("{\"node\":\"%s\",\"idle\":\"%s\",\"version\":\"%s\",\"uptime\":%lu,"),
haspDevice.get_hostname(), topic, buffer, long(millis() / 1000)); // \"status\":\"available\", haspDevice.get_hostname(), topic, haspDevice.get_version(),
long(millis() / 1000)); // \"status\":\"available\",
#if HASP_USE_WIFI > 0 || HASP_USE_ETHERNET > 0 #if HASP_USE_WIFI > 0 || HASP_USE_ETHERNET > 0
network_get_statusupdate(buffer, sizeof(buffer)); network_get_statusupdate(buffer, sizeof(buffer));

View File

@ -177,9 +177,6 @@ void debugLvglLogEvent(lv_log_level_t level, const char* file, uint32_t line, co
// Send the HASP header and version to the output device specified // Send the HASP header and version to the output device specified
void debugPrintHaspHeader(Print* output) void debugPrintHaspHeader(Print* output)
{ {
char buffer[16];
haspGetVersion(buffer, sizeof(buffer));
#ifdef ARDUINO #ifdef ARDUINO
if(debugAnsiCodes) output->print(TERM_COLOR_YELLOW); if(debugAnsiCodes) output->print(TERM_COLOR_YELLOW);
output->println(); output->println();
@ -190,7 +187,7 @@ void debugPrintHaspHeader(Print* output)
" |__|__|__|__|_____|__|\r\n" " |__|__|__|__|_____|__|\r\n"
" Home Automation Switch Plate\r\n" " Home Automation Switch Plate\r\n"
" Open Hardware edition v")); " Open Hardware edition v"));
output->println(buffer); output->println(haspDevice.get_version());
output->println(); output->println();
#else #else
if(debugAnsiCodes) debug_print(output, TERM_COLOR_YELLOW); if(debugAnsiCodes) debug_print(output, TERM_COLOR_YELLOW);
@ -201,7 +198,7 @@ void debugPrintHaspHeader(Print* output)
" |__|__|__|__|_____|__|\r\n" " |__|__|__|__|_____|__|\r\n"
" Home Automation Switch Plate\r\n" " Home Automation Switch Plate\r\n"
" Open Hardware edition v")); " Open Hardware edition v"));
debug_print(output, buffer); debug_print(output, haspDevice.get_version());
debug_newline(output); debug_newline(output);
debug_newline(output); debug_newline(output);
#endif #endif

View File

@ -75,10 +75,7 @@ void mqtt_ha_add_device_ids(JsonDocument& doc)
ids.add(haspDevice.get_hostname()); ids.add(haspDevice.get_hostname());
ids.add(HASP_MAC_ADDRESS_STR); ids.add(HASP_MAC_ADDRESS_STR);
char buffer[32]; device[F("sw")] = haspDevice.get_version();
haspGetVersion(buffer, sizeof(buffer));
device[F("sw")] = buffer;
device[FPSTR(FP_MQTT_HA_NAME)] = haspDevice.get_hostname(); device[FPSTR(FP_MQTT_HA_NAME)] = haspDevice.get_hostname();
device[FPSTR(FP_MQTT_HA_MODEL)] = F(PIOENV); device[FPSTR(FP_MQTT_HA_MODEL)] = F(PIOENV);
device[FPSTR(FP_MQTT_HA_MANUFACTURER)] = F(D_MANUFACTURER); device[FPSTR(FP_MQTT_HA_MANUFACTURER)] = F(D_MANUFACTURER);

View File

@ -250,16 +250,13 @@ bool httpIsAuthenticated(const __FlashStringHelper* notused)
void webSendFooter() void webSendFooter()
{ {
char buffer[16];
haspGetVersion(buffer, sizeof(buffer));
#if defined(STM32F4xx) #if defined(STM32F4xx)
webServer.sendContent(HTTP_END); webServer.sendContent(HTTP_END);
webServer.sendContent(buffer); webServer.sendContent(haspDevice.get_version());
webServer.sendContent(HTTP_FOOTER); webServer.sendContent(HTTP_FOOTER);
#else #else
webServer.sendContent_P(HTTP_END); webServer.sendContent_P(HTTP_END);
webServer.sendContent(buffer); webServer.sendContent(haspDevice.get_version());
webServer.sendContent_P(HTTP_FOOTER); webServer.sendContent_P(HTTP_FOOTER);
#endif #endif
} }
@ -279,10 +276,9 @@ void webSendPage(const char* nodename, uint32_t httpdatalength, bool gohome = fa
{ {
{ {
char buffer[64]; char buffer[64];
haspGetVersion(buffer, sizeof(buffer));
/* Calculate Content Length upfront */ /* Calculate Content Length upfront */
uint32_t contentLength = strlen(buffer); // version length uint32_t contentLength = strlen(haspDevice.get_version()); // version length
contentLength += sizeof(HTTP_DOCTYPE) - 1; contentLength += sizeof(HTTP_DOCTYPE) - 1;
contentLength += sizeof(HTTP_HEADER) - 1 - 2 + strlen(nodename); // -2 for %s contentLength += sizeof(HTTP_HEADER) - 1 - 2 + strlen(nodename); // -2 for %s
contentLength += sizeof(HTTP_SCRIPT) - 1; contentLength += sizeof(HTTP_SCRIPT) - 1;
@ -624,11 +620,7 @@ void webHandleInfo()
/* HASP Stats */ /* HASP Stats */
httpMessage += F("<b>HASP Version: </b>"); httpMessage += F("<b>HASP Version: </b>");
{ httpMessage += haspDevice.get_version();
char version[32];
haspGetVersion(version, sizeof(version));
httpMessage += version;
}
httpMessage += F("<br/><b>Build DateTime: </b>"); httpMessage += F("<br/><b>Build DateTime: </b>");
httpMessage += __DATE__; httpMessage += __DATE__;
httpMessage += F(" "); httpMessage += F(" ");

View File

@ -60,8 +60,7 @@ void mdnsStart()
MDNS.addService(service, proto, 80); MDNS.addService(service, proto, 80);
strcpy_P(key, PSTR("app_version")); strcpy_P(key, PSTR("app_version"));
haspGetVersion(value, sizeof(value)); MDNS.addServiceTxt(service, proto, key, haspDevice.get_version());
MDNS.addServiceTxt(service, proto, key, value);
strcpy_P(key, PSTR("app_name")); strcpy_P(key, PSTR("app_name"));
strcpy_P(value, PSTR(D_MANUFACTURER)); strcpy_P(value, PSTR(D_MANUFACTURER));

View File

@ -70,10 +70,9 @@ void TASMO_TELE_JSON()
char data[3 * 128]; char data[3 * 128];
{ {
char buffer[128]; char buffer[128];
haspGetVersion(buffer, sizeof(buffer));
snprintf_P(data, sizeof(data), PSTR("{\"status\":\"available\",\"version\":\"%s\",\"uptime\":%lu,"), buffer, snprintf_P(data, sizeof(data), PSTR("{\"status\":\"available\",\"version\":\"%s\",\"uptime\":%lu,"),
long(millis() / 1000)); haspDevice.get_version(), long(millis() / 1000));
snprintf_P(buffer, sizeof(buffer), PSTR("\"espCanUpdate\":\"false\",\"page\":%u,\"numPages\":%u,"), snprintf_P(buffer, sizeof(buffer), PSTR("\"espCanUpdate\":\"false\",\"page\":%u,\"numPages\":%u,"),
haspGetPage(), (HASP_NUM_PAGES)); haspGetPage(), (HASP_NUM_PAGES));