mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2026-04-02 15:06:32 +03:00
Merge branch 'dev' of https://github.com/emsesp/EMS-ESP32 into dev
This commit is contained in:
@@ -19,7 +19,7 @@
|
||||
#ifndef EMSESP_COMMAND_H_
|
||||
#define EMSESP_COMMAND_H_
|
||||
|
||||
#include <unordered_map>
|
||||
#include <map>
|
||||
|
||||
#include "console.h"
|
||||
#include <esp32-psram.h>
|
||||
@@ -153,8 +153,8 @@ class Command {
|
||||
|
||||
class SUrlParser {
|
||||
private:
|
||||
std::unordered_map<std::string, std::string> m_keysvalues;
|
||||
std::vector<std::string> m_folders;
|
||||
std::map<std::string, std::string> m_keysvalues;
|
||||
std::vector<std::string> m_folders;
|
||||
|
||||
public:
|
||||
SUrlParser() = default;
|
||||
@@ -166,7 +166,7 @@ class SUrlParser {
|
||||
return m_folders;
|
||||
};
|
||||
|
||||
std::unordered_map<std::string, std::string> & params() {
|
||||
std::map<std::string, std::string> & params() {
|
||||
return m_keysvalues;
|
||||
};
|
||||
|
||||
|
||||
@@ -1720,8 +1720,8 @@ void EMSdevice::get_value_json(JsonObject json, DeviceValue & dv) {
|
||||
|
||||
// generate Prometheus metrics format from device values
|
||||
std::string EMSdevice::get_metrics_prometheus(const int8_t tag) {
|
||||
std::string result;
|
||||
std::unordered_map<std::string, bool> seen_metrics;
|
||||
std::string result;
|
||||
std::map<std::string, bool> seen_metrics;
|
||||
|
||||
// Helper function to check if a device value type is supported for Prometheus metrics
|
||||
auto is_supported_type = [](uint8_t type) -> bool {
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
#include "emsdevicevalue.h"
|
||||
|
||||
#include <esp32-psram.h>
|
||||
#include <unordered_map>
|
||||
#include <map>
|
||||
|
||||
namespace emsesp {
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
#include <string>
|
||||
#include <functional>
|
||||
#include <deque>
|
||||
#include <unordered_map>
|
||||
#include <map>
|
||||
#include <list>
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
@@ -19,14 +19,15 @@
|
||||
#ifndef EMSESP_EMSFACTORY_H_
|
||||
#define EMSESP_EMSFACTORY_H_
|
||||
|
||||
#include <map>
|
||||
#include <memory> // for unique_ptr
|
||||
#include <map>
|
||||
|
||||
#include "emsdevice.h"
|
||||
// Forward declaration
|
||||
namespace emsesp {
|
||||
class EMSdevice;
|
||||
}
|
||||
|
||||
// Macro for class registration
|
||||
// Anonymous namespace is used to make the definitions here private to the current
|
||||
// compilation unit (current file). It is equivalent to the old C static keyword.
|
||||
#define REGISTER_FACTORY(derivedClass, device_type) \
|
||||
namespace { \
|
||||
auto registry_##derivedClass = ConcreteEMSFactory<derivedClass>(device_type); \
|
||||
@@ -34,30 +35,29 @@
|
||||
|
||||
namespace emsesp {
|
||||
|
||||
class EMSdevice; // forward declaration, for gcc linking
|
||||
|
||||
class EMSFactory {
|
||||
public:
|
||||
virtual ~EMSFactory() = default;
|
||||
|
||||
// Register factory object of derived class
|
||||
// using the device_type as the unique identifier
|
||||
static auto registerFactory(const uint8_t device_type, EMSFactory * factory) -> void {
|
||||
auto & reg = EMSFactory::getRegister();
|
||||
reg[device_type] = factory;
|
||||
}
|
||||
|
||||
using FactoryMap = std::map<uint8_t, EMSFactory *>;
|
||||
|
||||
// Register factory object of derived class using the device_type as the unique identifier
|
||||
static inline auto registerFactory(const uint8_t device_type, EMSFactory * factory) -> void {
|
||||
getRegister()[device_type] = factory;
|
||||
}
|
||||
|
||||
// returns all registered classes (really only for debugging)
|
||||
static auto device_handlers() -> FactoryMap {
|
||||
return EMSFactory::getRegister();
|
||||
static inline auto device_handlers() -> const FactoryMap & {
|
||||
return getRegister();
|
||||
}
|
||||
|
||||
// Construct derived class returning an unique ptr
|
||||
static auto add(const uint8_t device_type, uint8_t device_id, uint8_t product_id, const char * version, const char * default_name, uint8_t flags, uint8_t brand)
|
||||
-> std::unique_ptr<EMSdevice> {
|
||||
return std::unique_ptr<EMSdevice>(EMSFactory::makeRaw(device_type, device_id, product_id, version, default_name, flags, brand));
|
||||
if (auto * ptr = makeRaw(device_type, device_id, product_id, version, default_name, flags, brand)) {
|
||||
return std::unique_ptr<EMSdevice>(ptr);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
virtual auto construct(uint8_t device_type, uint8_t device_id, uint8_t product_id, const char * version, const char * name, uint8_t flags, uint8_t brand) const
|
||||
@@ -65,7 +65,7 @@ class EMSFactory {
|
||||
|
||||
private:
|
||||
// Force global variable to be initialized, thus it avoids the "initialization order fiasco"
|
||||
static auto getRegister() -> FactoryMap & {
|
||||
static inline auto getRegister() -> FactoryMap & {
|
||||
static FactoryMap classRegister{};
|
||||
return classRegister;
|
||||
}
|
||||
@@ -74,11 +74,9 @@ class EMSFactory {
|
||||
// find which EMS device it is and use that class
|
||||
static auto makeRaw(const uint8_t device_type, uint8_t device_id, uint8_t product_id, const char * version, const char * name, uint8_t flags, uint8_t brand)
|
||||
-> EMSdevice * {
|
||||
auto it = EMSFactory::getRegister().find(device_type);
|
||||
if (it != EMSFactory::getRegister().end()) {
|
||||
return it->second->construct(device_type, device_id, product_id, version, name, flags, brand);
|
||||
}
|
||||
return nullptr;
|
||||
const auto & reg = getRegister();
|
||||
const auto it = reg.find(device_type);
|
||||
return (it != reg.end()) ? it->second->construct(device_type, device_id, product_id, version, name, flags, brand) : nullptr;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -86,7 +84,7 @@ template <typename DerivedClass>
|
||||
class ConcreteEMSFactory : EMSFactory {
|
||||
public:
|
||||
// Register this global object on the EMSFactory register
|
||||
ConcreteEMSFactory(const uint8_t device_type) {
|
||||
explicit ConcreteEMSFactory(const uint8_t device_type) {
|
||||
EMSFactory::registerFactory(device_type, this);
|
||||
}
|
||||
|
||||
|
||||
@@ -344,8 +344,12 @@ bool isnum(const std::string & s) {
|
||||
std::string commands(std::string & expr, bool quotes) {
|
||||
auto expr_new = Helpers::toLower(expr);
|
||||
for (uint8_t device = 0; device < EMSdevice::DeviceType::UNKNOWN; device++) {
|
||||
std::string d = (std::string)EMSdevice::device_type_2_device_name(device) + "/";
|
||||
auto f = expr_new.find(d);
|
||||
// Optimized: build string with reserve to avoid temporary allocations
|
||||
std::string d;
|
||||
d.reserve(32); // typical device name length + "/"
|
||||
d = EMSdevice::device_type_2_device_name(device);
|
||||
d += "/";
|
||||
auto f = expr_new.find(d);
|
||||
while (f != std::string::npos) {
|
||||
// entity names are alphanumeric or _
|
||||
auto e = expr_new.find_first_not_of("/._abcdefghijklmnopqrstuvwxyz0123456789", f);
|
||||
@@ -367,9 +371,11 @@ std::string commands(std::string & expr, bool quotes) {
|
||||
JsonDocument doc_in;
|
||||
JsonObject output = doc_out.to<JsonObject>();
|
||||
JsonObject input = doc_in.to<JsonObject>();
|
||||
std::string cmd_s = "api/" + std::string(cmd);
|
||||
// Optimized: use stack buffer for small strings to avoid heap allocation
|
||||
char cmd_s[COMMAND_MAX_LENGTH + 5]; // "api/" prefix + cmd
|
||||
snprintf(cmd_s, sizeof(cmd_s), "api/%s", cmd);
|
||||
|
||||
auto return_code = Command::process(cmd_s.c_str(), true, input, output);
|
||||
auto return_code = Command::process(cmd_s, true, input, output);
|
||||
// check for no value (entity is valid but has no value set)
|
||||
if (return_code != CommandRet::OK && return_code != CommandRet::NO_VALUE) {
|
||||
return expr = "";
|
||||
@@ -725,7 +731,7 @@ std::string compute(const std::string & expr) {
|
||||
// if there is data, force a POST
|
||||
if (value.length() || Helpers::toLower(method) == "post") {
|
||||
if (value.find_first_of('{') != std::string::npos) {
|
||||
http.addHeader("Content-Type", "application/json"); // auto-set to JSON
|
||||
http.addHeader(asyncsrv::T_Content_Type, asyncsrv::T_application_json, false); // auto-set to JSON
|
||||
}
|
||||
httpResult = http.POST(value.c_str());
|
||||
} else {
|
||||
|
||||
@@ -1761,8 +1761,8 @@ void System::get_value_json(JsonObject output, const std::string & circuit, cons
|
||||
|
||||
// generate Prometheus metrics format from system values
|
||||
std::string System::get_metrics_prometheus() {
|
||||
std::string result;
|
||||
std::unordered_map<std::string, bool> seen_metrics;
|
||||
std::string result;
|
||||
std::map<std::string, bool> seen_metrics;
|
||||
|
||||
result.reserve(16000);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user