code cleanup - sonarlint

This commit is contained in:
proddy
2024-10-02 09:02:24 +02:00
parent 465f14a113
commit e5b98dadde
21 changed files with 97 additions and 119 deletions

3
.gitignore vendored
View File

@@ -68,3 +68,6 @@ venv/
# cspell
words-found-verbose.txt
# sonarlint
compile_commands.json

View File

@@ -0,0 +1,4 @@
{
"sonarCloudOrganization": "emsesp",
"projectKey": "emsesp_EMS-ESP32"
}

View File

@@ -86,7 +86,6 @@ void UploadFileService::handleUpload(AsyncWebServerRequest * request, const Stri
Update.setMD5(_md5.data());
_md5.front() = '\0';
}
// emsesp::EMSESP::system_.upload_status(true); // force just in case, this is stop UART, MQTT and other services
request->onDisconnect([this] { handleEarlyDisconnect(); }); // success, let's make sure we end the update if the client hangs up
} else {
handleError(request, 507); // failed to begin, send an error response Insufficient Storage

View File

@@ -102,9 +102,6 @@ void AnalogSensor::reload(bool get_nvs) {
}
}
if (!found) {
// if (!System::is_valid_gpio(sensor.gpio)) {
// continue;
// }
sensors_.emplace_back(sensor.gpio, sensor.name, sensor.offset, sensor.factor, sensor.uom, sensor.type);
sensors_.back().ha_registered = false; // this will trigger recreate of the HA config
if (sensor.type == AnalogType::COUNTER || sensor.type >= AnalogType::DIGITAL_OUT) {
@@ -610,7 +607,7 @@ void AnalogSensor::publish_values(const bool force) {
// see if we need to create the [devs] discovery section, as this needs only to be done once for all sensors
bool is_ha_device_created = false;
for (auto & sensor : sensors_) {
for (auto const & sensor : sensors_) {
if (sensor.ha_registered) {
is_ha_device_created = true;
break;

View File

@@ -420,7 +420,7 @@ uint8_t Command::call(const uint8_t device_type, const char * command, const cha
} else {
if (single_command) {
// log as DEBUG (TRACE) regardless if compiled with EMSESP_DEBUG
logger_.debug(("%sCalled command %s"), ro.c_str(), info_s);
logger_.debug("%sCalled command %s", ro.c_str(), info_s);
} else {
if (id > 0) {
LOG_INFO(("%sCalled command %s with value %s and id %d on device 0x%02X"), ro.c_str(), info_s, value, id, device_id);
@@ -499,7 +499,7 @@ void Command::erase_command(const uint8_t device_type, const char * cmd, uint8_t
return;
}
auto it = cmdfunctions_.begin();
for (auto & cf : cmdfunctions_) {
for (auto const & cf : cmdfunctions_) {
if (Helpers::toLower(cmd) == Helpers::toLower(cf.cmd_) && (cf.device_type_ == device_type) && ((flag & 0x3F) == (cf.flags_ & 0x3F))) {
cmdfunctions_.erase(it);
return;
@@ -565,9 +565,9 @@ void Command::show(uuid::console::Shell & shell, uint8_t device_type, bool verbo
// if not in verbose mode, just print them on a single line and exit
if (!verbose) {
sorted_cmds.push_front(F_(info));
sorted_cmds.push_front(F_(commands));
sorted_cmds.push_front(F_(values));
sorted_cmds.emplace_front(F_(info));
sorted_cmds.emplace_front(F_(commands));
sorted_cmds.emplace_front(F_(values));
for (const auto & cl : sorted_cmds) {
shell.print(cl);
shell.print(" ");

View File

@@ -297,7 +297,7 @@ uint8_t EMSdevice::decode_brand(uint8_t value) {
}
// returns string of a human friendly description of the EMS device
const std::string EMSdevice::to_string() {
std::string EMSdevice::to_string() {
// for devices that haven't been lookup yet, don't show all details
if (product_id_ == 0) {
return std::string(name()) + " (DeviceID:" + Helpers::hextoa(device_id_) + ")";
@@ -313,7 +313,7 @@ const std::string EMSdevice::to_string() {
// returns out brand + device name
// translated
const std::string EMSdevice::to_string_short() {
std::string EMSdevice::to_string_short() {
if (brand_ == Brand::NO_BRAND) {
return std::string(device_type_2_device_name_translated()) + ": " + name();
}
@@ -553,11 +553,11 @@ void EMSdevice::add_device_value(int8_t tag, // to b
}
}
uint8_t state = DeviceValueState::DV_DEFAULT; // determine state
std::string custom_fullname = std::string(""); // custom fullname
auto short_name = name[0]; // entity name
bool has_cmd = (f != nullptr); // is it a command?
bool ignore = false; // ignore this entity?
uint8_t state = DeviceValueState::DV_DEFAULT; // determine state
auto custom_fullname = std::string(""); // custom fullname
auto short_name = name[0]; // entity name
bool has_cmd = (f != nullptr); // is it a command?
bool ignore = false; // ignore this entity?
// get fullname, getting translation if it exists
const char * const * fullname;
@@ -1025,7 +1025,7 @@ void EMSdevice::generate_values_web(JsonObject output) {
// this is used only for WebCustomizationService::device_entities()
void EMSdevice::generate_values_web_customization(JsonArray output) {
for (auto & dv : devicevalues_) {
// also show commands and entities that have an empty full name
// also show commands and entities that have an empty fullname
JsonObject obj = output.add<JsonObject>();
uint8_t fahrenheit = !EMSESP::system_.fahrenheit() ? 0 : (dv.uom == DeviceValueUOM::DEGREES) ? 2 : (dv.uom == DeviceValueUOM::DEGREES_R) ? 1 : 0;

View File

@@ -42,7 +42,6 @@ class EMSdevice {
, flags_(flags)
, brand_(brand) {
strlcpy(version_, version, sizeof(version_));
custom_name_ = ""; // init custom name to blank
}
// static functions, used outside the class like in console.cpp, command.cpp, emsesp.cpp, mqtt.cpp
@@ -113,7 +112,7 @@ class EMSdevice {
}
// set custom device name
inline void custom_name(std::string & custom_name) {
inline void custom_name(std::string const & custom_name) {
custom_name_ = custom_name;
}
std::string name(); // returns either default or custom name if defined
@@ -205,17 +204,16 @@ class EMSdevice {
int get_modbus_value(uint8_t tag, const std::string & shortname, std::vector<uint16_t> & result);
int modbus_value_to_json(uint8_t tag, const std::string & shortname, const std::vector<uint8_t> & modbus_data, JsonObject jsonValue);
const char * brand_to_char();
const std::string to_string();
const std::string to_string_short();
const char * brand_to_char();
std::string to_string();
std::string to_string_short();
enum Handlers : uint8_t { ALL, RECEIVED, FETCHED, PENDING, IGNORED };
void show_telegram_handlers(uuid::console::Shell & shell) const;
char * show_telegram_handlers(char * result, const size_t len, const uint8_t handlers);
void show_mqtt_handlers(uuid::console::Shell & shell) const;
// void list_device_entries(JsonObject output) const;
void add_handlers_ignored(const uint16_t handler);
void add_handlers_ignored(const uint16_t handler);
void set_climate_minmax(int8_t tag, int16_t min, uint32_t max);
void setCustomizationEntity(const std::string & entity_id);
@@ -456,15 +454,13 @@ class EMSdevice {
uint8_t count_entities();
bool has_entities() const;
/*
void reserve_device_values(uint8_t elements) {
devicevalues_.reserve(elements);
}
// void reserve_device_values(uint8_t elements) {
// devicevalues_.reserve(elements);
// }
void reserve_telegram_functions(uint8_t elements) {
telegram_functions_.reserve(elements);
}
*/
// void reserve_telegram_functions(uint8_t elements) {
// telegram_functions_.reserve(elements);
// }
#if defined(EMSESP_STANDALONE)
struct TelegramFunctionDump {
@@ -487,10 +483,10 @@ class EMSdevice {
uint8_t device_id_ = 0;
uint8_t product_id_ = 0;
char version_[6];
const char * default_name_; // the fixed name the EMS model taken from the device library
std::string custom_name_; // custom name
uint8_t flags_ = 0;
uint8_t brand_ = Brand::NO_BRAND;
const char * default_name_; // the fixed name the EMS model taken from the device library
std::string custom_name_ = ""; // custom name
uint8_t flags_ = 0;
uint8_t brand_ = Brand::NO_BRAND;
bool ha_config_done_ = false;
bool has_update_ = false;

View File

@@ -208,7 +208,7 @@ void EMSESP::uart_init() {
uint8_t tx_mode = 0;
uint8_t rx_gpio = 0;
uint8_t tx_gpio = 0;
EMSESP::webSettingsService.read([&](WebSettings & settings) {
EMSESP::webSettingsService.read([&](WebSettings const & settings) {
tx_mode = settings.tx_mode;
rx_gpio = settings.rx_gpio;
tx_gpio = settings.tx_gpio;
@@ -279,7 +279,7 @@ void EMSESP::show_ems(uuid::console::Shell & shell) {
if (bus_status() != BUS_STATUS_OFFLINE) {
shell.printfln("EMS Bus info:");
EMSESP::webSettingsService.read([&](WebSettings & settings) { shell.printfln(" Tx mode: %d", settings.tx_mode); });
EMSESP::webSettingsService.read([&](WebSettings const & settings) { shell.printfln(" Tx mode: %d", settings.tx_mode); });
shell.printfln(" Bus protocol: %s", EMSbus::is_ht3() ? "HT3" : "Buderus");
shell.printfln(" #recognized EMS devices: %d", EMSESP::emsdevices.size());
shell.printfln(" #telegrams received: %d", rxservice_.telegram_count());
@@ -1317,7 +1317,7 @@ bool EMSESP::add_device(const uint8_t device_id, const uint8_t product_id, const
emsdevices.push_back(EMSFactory::add(device_type, device_id, product_id, version, default_name, flags, brand));
// see if we have a custom device name in our Customizations list, and if so set it
webCustomizationService.read([&](WebCustomization & settings) {
webCustomizationService.read([&](WebCustomization const & settings) {
for (EntityCustomization e : settings.entityCustomizations) {
if ((e.device_id == device_id) && (e.product_id == product_id)) {
LOG_DEBUG("Have customizations for %s with deviceID 0x%02X productID %d", e.custom_name.c_str(), device_id, product_id);

View File

@@ -798,10 +798,6 @@ uint16_t Helpers::string2minutes(const std::string & str) {
if (tmp > 60) {
return 0;
}
// Serial.print("*");
// Serial.print(tmp);
// Serial.println("*");
res += tmp;
}
// Or we got an extra colon

View File

@@ -14,9 +14,7 @@
#include <utility>
#if defined(EMSESP_STANDALONE)
#include <modbus_test.h>
#include <../test/test_modbus/modbus_test.h>
#endif
#ifndef EMSESP_STANDALONE

View File

@@ -124,10 +124,7 @@ void Roomctrl::send(uint8_t addr) {
}
temperature(addr, 0x10, hc); // send to master-thermostat
}
} else if (type_[hc] == RC200 || type_[hc] == RC100) {
send_time_[hc] = uuid::get_uptime();
temperature(addr, 0x10, hc);
} else if (type_[hc] == FB10) {
} else if (type_[hc] == RC200 || type_[hc] == RC100 || type_[hc] == FB10) {
send_time_[hc] = uuid::get_uptime();
temperature(addr, 0x10, hc); // send to master-thermostat (https://github.com/emsesp/EMS-ESP32/issues/336)
} else { // type==RC20 or SENSOR

View File

@@ -25,7 +25,7 @@ uuid::log::Logger Shower::logger_{F_(shower), uuid::log::Facility::CONSOLE};
static bool force_coldshot = false;
void Shower::start() {
EMSESP::webSettingsService.read([&](WebSettings & settings) {
EMSESP::webSettingsService.read([&](WebSettings const & settings) {
shower_timer_ = settings.shower_timer;
shower_alert_ = settings.shower_alert;
shower_alert_trigger_ = settings.shower_alert_trigger * 60; // convert from minutes to seconds
@@ -61,7 +61,6 @@ void Shower::loop() {
return;
}
// uint32_t time_now = uuid::get_uptime(); // in ms
auto time_now = uuid::get_uptime_sec(); // in sec
// if already in cold mode, ignore all this logic until we're out of the cold blast

View File

@@ -118,13 +118,13 @@ bool System::command_fetch(const char * value, const int8_t id) {
if (value_s == "all") {
LOG_INFO("Requesting data from EMS devices");
EMSESP::fetch_device_values();
} else if (value_s == (F_(boiler))) {
} else if (value_s == F_(boiler)) {
EMSESP::fetch_device_values_type(EMSdevice::DeviceType::BOILER);
} else if (value_s == (F_(thermostat))) {
} else if (value_s == F_(thermostat)) {
EMSESP::fetch_device_values_type(EMSdevice::DeviceType::THERMOSTAT);
} else if (value_s == (F_(solar))) {
} else if (value_s == F_(solar)) {
EMSESP::fetch_device_values_type(EMSdevice::DeviceType::SOLAR);
} else if (value_s == (F_(mixer))) {
} else if (value_s == F_(mixer)) {
EMSESP::fetch_device_values_type(EMSdevice::DeviceType::MIXER);
}
} else {
@@ -339,9 +339,6 @@ void System::syslog_init() {
syslog_.destination(syslog_host_.c_str(), syslog_port_);
syslog_.hostname(hostname().c_str());
// removed in 3.6.0
// Command::add(EMSdevice::DeviceType::SYSTEM, F_(syslog), System::command_syslog_level, FL_(changeloglevel_cmd), CommandFlag::ADMIN_ONLY);
} else if (syslog_.started()) {
// in case service is still running, this flushes the queue
// https://github.com/emsesp/EMS-ESP/issues/496
@@ -1053,7 +1050,7 @@ void System::show_system(uuid::console::Shell & shell) {
shell.printfln(" WiFi Network: Disconnected");
break;
case WL_NO_SHIELD:
// case WL_NO_SHIELD:
default:
shell.printfln(" WiFi MAC address: %s", WiFi.macAddress().c_str());
shell.printfln(" WiFi Network: not connected");
@@ -1087,7 +1084,7 @@ void System::show_system(uuid::console::Shell & shell) {
} else {
shell.printfln(" Syslog: %s", syslog_.started() ? "started" : "stopped");
shell.print(" ");
shell.printfln(F_(host_fmt), !syslog_host_.isEmpty() ? syslog_host_.c_str() : (F_(unset)));
shell.printfln(F_(host_fmt), !syslog_host_.isEmpty() ? syslog_host_.c_str() : F_(unset));
shell.printfln(" IP: %s", uuid::printable_to_string(syslog_.ip()).c_str());
shell.print(" ");
shell.printfln(F_(port_fmt), syslog_port_);
@@ -1167,7 +1164,7 @@ bool System::check_upgrade(bool factory_settings) {
if (!factory_settings) {
// fetch current version from settings file
EMSESP::webSettingsService.read([&](WebSettings & settings) { settingsVersion = settings.version.c_str(); });
EMSESP::webSettingsService.read([&](WebSettings const & settings) { settingsVersion = settings.version.c_str(); });
// see if we're missing a version, will be < 3.5.0b13 from Dec 23 2022
missing_version = (settingsVersion.empty() || (settingsVersion.length() < 5));
@@ -1338,14 +1335,11 @@ bool System::get_value_info(JsonObject output, const char * cmd) {
if (!strcmp(cmd, F_(entities))) {
for (JsonPair p : root) {
if (p.value().is<JsonObject>()) {
// String prefix = p.key().c_str();
for (JsonPair p1 : p.value().as<JsonObject>()) {
JsonObject entity = output[std::string(p.key().c_str()) + "." + p1.key().c_str()].to<JsonObject>();
get_value_json(entity, p.key().c_str(), p1.key().c_str(), p1.value());
}
} // else { // we don't have pairs in json root object
// get_value_json(entity, "", p.key().c_str(), p.value());
// }
}
}
return true;
}
@@ -1498,7 +1492,7 @@ bool System::command_info(const char * value, const int8_t id, JsonObject output
}
});
#ifndef EMSESP_STANDALONE
EMSESP::esp8266React.getAPSettingsService()->read([&](APSettings & settings) {
EMSESP::esp8266React.getAPSettingsService()->read([&](const APSettings & settings) {
const char * pM[] = {"always", "disconnected", "never"};
node["APProvisionMode"] = pM[settings.provisionMode];
node["APSecurity"] = settings.password.length() ? "wpa2" : "open";
@@ -1510,11 +1504,10 @@ bool System::command_info(const char * value, const int8_t id, JsonObject output
node = output["ntp"].to<JsonObject>();
#ifndef EMSESP_STANDALONE
node["NTPStatus"] = EMSESP::system_.ntp_connected() ? "connected" : "disconnected";
EMSESP::esp8266React.getNTPSettingsService()->read([&](NTPSettings & settings) {
EMSESP::esp8266React.getNTPSettingsService()->read([&](const NTPSettings & settings) {
node["enabled"] = settings.enabled;
node["server"] = settings.server;
node["tzLabel"] = settings.tzLabel;
// node["tz format"] = settings.tzFormat;
});
#endif
@@ -1527,7 +1520,7 @@ bool System::command_info(const char * value, const int8_t id, JsonObject output
node["MQTTPublishFails"] = Mqtt::publish_fails();
node["MQTTConnects"] = Mqtt::connect_count();
}
EMSESP::esp8266React.getMqttSettingsService()->read([&](MqttSettings & settings) {
EMSESP::esp8266React.getMqttSettingsService()->read([&](const MqttSettings & settings) {
node["enabled"] = settings.enabled;
node["clientID"] = settings.clientId;
node["keepAlive"] = settings.keepAlive;
@@ -1606,7 +1599,6 @@ bool System::command_info(const char * value, const int8_t id, JsonObject output
node["busStatus"] = "unknown";
break;
}
// if (EMSESP::bus_status() != EMSESP::BUS_STATUS_OFFLINE) {
node["busProtocol"] = EMSbus::is_ht3() ? "HT3" : "Buderus";
node["busTelegramsReceived"] = EMSESP::rxservice_.telegram_count();
node["busReads"] = EMSESP::txservice_.telegram_read_count();
@@ -1616,11 +1608,10 @@ bool System::command_info(const char * value, const int8_t id, JsonObject output
node["busWritesFailed"] = EMSESP::txservice_.telegram_write_fail_count();
node["busRxLineQuality"] = EMSESP::rxservice_.quality();
node["busTxLineQuality"] = (EMSESP::txservice_.read_quality() + EMSESP::txservice_.read_quality()) / 2;
// }
// Settings
node = output["settings"].to<JsonObject>();
EMSESP::webSettingsService.read([&](WebSettings & settings) {
EMSESP::webSettingsService.read([&](const WebSettings & settings) {
node["boardProfile"] = settings.board_profile;
node["locale"] = settings.locale;
node["txMode"] = settings.tx_mode;
@@ -1707,7 +1698,6 @@ bool System::command_info(const char * value, const int8_t id, JsonObject output
obj["name"] = F_(temperaturesensor);
obj["entities"] = EMSESP::temperaturesensor_.count_entities();
}
// if (EMSESP::analog_enabled()) {
if (EMSESP::analogsensor_.count_entities()) {
JsonObject obj = devices.add<JsonObject>();
obj["type"] = F_(analogsensor);
@@ -1870,7 +1860,7 @@ std::string System::reset_reason(uint8_t cpu) const {
break;
}
#endif
return ("Unknown");
return "Unknown";
}
#pragma GCC diagnostic pop

View File

@@ -60,7 +60,6 @@ class System {
static bool command_fetch(const char * value, const int8_t id);
static bool command_restart(const char * value, const int8_t id);
static bool command_format(const char * value, const int8_t id);
// static bool command_syslog_level(const char * value, const int8_t id);
static bool command_watch(const char * value, const int8_t id);
static bool command_message(const char * value, const int8_t id);
static bool command_info(const char * value, const int8_t id, JsonObject output);

View File

@@ -53,7 +53,7 @@ void TemperatureSensor::start() {
// load settings
void TemperatureSensor::reload() {
// load the service settings
EMSESP::webSettingsService.read([&](WebSettings & settings) {
EMSESP::webSettingsService.read([&](WebSettings const & settings) {
dallas_gpio_ = settings.dallas_gpio;
parasite_ = settings.dallas_parasite;
});
@@ -274,7 +274,7 @@ int16_t TemperatureSensor::get_temperature_c(const uint8_t addr[]) {
case 11:
raw_value &= ~0x1;
break;
case 12:
default: // 12
break;
}
}
@@ -302,7 +302,7 @@ bool TemperatureSensor::update(const std::string & id, const std::string & name,
sensor.set_offset(offset);
// store the new name and offset in our configuration
EMSESP::webCustomizationService.update([&](WebCustomization & settings) {
EMSESP::webCustomizationService.update([&id, &name, &offset, &sensor](WebCustomization & settings) {
// look it up to see if it exists
bool found = false;
for (auto & SensorCustomization : settings.sensorCustomizations) {
@@ -315,10 +315,10 @@ bool TemperatureSensor::update(const std::string & id, const std::string & name,
}
}
if (!found) {
SensorCustomization newSensor = SensorCustomization();
newSensor.id = id;
newSensor.name = name;
newSensor.offset = offset;
auto newSensor = SensorCustomization();
newSensor.id = id;
newSensor.name = name;
newSensor.offset = offset;
settings.sensorCustomizations.push_back(newSensor);
LOG_DEBUG("Adding new customization for sensor ID %s", id.c_str());
}
@@ -401,9 +401,9 @@ void TemperatureSensor::publish_sensor(const Sensor & sensor) {
if (Mqtt::enabled() && Mqtt::publish_single()) {
char topic[Mqtt::MQTT_TOPIC_MAX_SIZE];
if (Mqtt::publish_single2cmd()) {
snprintf(topic, sizeof(topic), "%s/%s", (F_(temperaturesensor)), sensor.name().c_str());
snprintf(topic, sizeof(topic), "%s/%s", F_(temperaturesensor), sensor.name().c_str());
} else {
snprintf(topic, sizeof(topic), "%s%s/%s", (F_(temperaturesensor)), "_data", sensor.name().c_str());
snprintf(topic, sizeof(topic), "%s%s/%s", F_(temperaturesensor), "_data", sensor.name().c_str());
}
char payload[10];
Mqtt::queue_publish(topic, Helpers::render_value(payload, sensor.temperature_c, 10, EMSESP::system_.fahrenheit() ? 2 : 0));
@@ -513,7 +513,7 @@ void TemperatureSensor::publish_values(const bool force) {
// see if we need to create the [devs] discovery section, as this needs only to be done once for all sensors
bool is_ha_device_created = false;
for (auto & sensor : sensors_) {
for (const auto & sensor : sensors_) {
if (sensor.ha_registered) {
is_ha_device_created = true;
break;
@@ -570,8 +570,8 @@ std::string TemperatureSensor::Sensor::name() const {
// look up in customization service for a specific sensor
// and set the name and offset from that entry if it exists
bool TemperatureSensor::Sensor::apply_customization() {
EMSESP::webCustomizationService.read([&](WebCustomization & settings) {
auto sensors = settings.sensorCustomizations;
EMSESP::webCustomizationService.read([&](const WebCustomization & settings) {
auto const & sensors = settings.sensorCustomizations;
if (!sensors.empty()) {
for (const auto & sensor : sensors) {
if (id_ == sensor.id) {

View File

@@ -90,23 +90,23 @@ class TemperatureSensor {
return sensors_;
}
uint32_t reads() {
uint32_t reads() const {
return sensorreads_;
}
uint32_t fails() {
uint32_t fails() const {
return sensorfails_;
}
bool sensor_enabled() {
bool sensor_enabled() const {
return (dallas_gpio_ != 0);
}
bool have_sensors() {
bool have_sensors() const {
return (!sensors_.empty());
}
size_t count_entities() {
size_t count_entities() const {
return sensors_.size();
}

View File

@@ -58,7 +58,7 @@ void WebAPIService::webAPIService(AsyncWebServerRequest * request) {
void WebAPIService::parse(AsyncWebServerRequest * request, JsonObject input) {
// check if the user has admin privileges (token is included and authorized)
bool is_admin = false;
EMSESP::webSettingsService.read([&](WebSettings & settings) {
EMSESP::webSettingsService.read([&](WebSettings const & settings) {
Authentication authentication = _securityManager->authenticateRequest(request);
is_admin = settings.notoken_api || AuthenticationPredicates::IS_ADMIN(authentication);
});
@@ -94,7 +94,7 @@ void WebAPIService::parse(AsyncWebServerRequest * request, JsonObject input) {
emsesp::EMSESP::system_.refreshHeapMem();
// output json buffer
AsyncJsonResponse * response = new AsyncJsonResponse(false);
auto response = new AsyncJsonResponse(false);
// add more mem if needed - won't be needed in ArduinoJson 7
// while (!response->getSize()) {

View File

@@ -47,7 +47,7 @@ void WebCustomEntityService::begin() {
void WebCustomEntity::read(WebCustomEntity & webEntity, JsonObject root) {
JsonArray entity = root["entities"].to<JsonArray>();
uint8_t counter = 0;
for (const CustomEntityItem & entityItem : webEntity.customEntityItems) {
for (CustomEntityItem & entityItem : webEntity.customEntityItems) {
JsonObject ei = entity.add<JsonObject>();
ei["id"] = counter++; // id is only used to render the table and must be unique
ei["ram"] = entityItem.ram;
@@ -213,7 +213,7 @@ bool WebCustomEntityService::command_setvalue(const char * value, const int8_t i
// output of a single value
// if add_uom is true it will add the UOM string to the value
void WebCustomEntityService::render_value(JsonObject output, CustomEntityItem entity, const bool useVal, const bool web, const bool add_uom) {
void WebCustomEntityService::render_value(JsonObject output, CustomEntityItem & entity, const bool useVal, const bool web, const bool add_uom) {
char payload[12];
std::string name = useVal ? "value" : entity.name;
switch (entity.value_type) {
@@ -262,7 +262,7 @@ void WebCustomEntityService::render_value(JsonObject output, CustomEntityItem en
output[name] = add_uom ? serialized(v + ' ' + EMSdevice::uom_to_string(entity.uom)) : serialized(v);
}
break;
case DeviceValueType::STRING:
// case DeviceValueType::STRING:
default:
// if no type treat it as a string
if (entity.data.length() > 0) {
@@ -275,7 +275,7 @@ void WebCustomEntityService::render_value(JsonObject output, CustomEntityItem en
// display all custom entities
// adding each one, with UOM to a json object string
void WebCustomEntityService::show_values(JsonObject output) {
for (const CustomEntityItem & entity : *customEntityItems_) {
for (CustomEntityItem & entity : *customEntityItems_) {
render_value(output, entity, false, false, true); // with add_uom
}
}
@@ -285,14 +285,14 @@ bool WebCustomEntityService::get_value_info(JsonObject output, const char * cmd)
// if no custom entries, return empty json
// even if we're looking for a specific entity
// https://github.com/emsesp/EMS-ESP32/issues/1297
if (customEntityItems_->size() == 0) {
if (customEntityItems_->empty()) {
return true;
}
// if it's info or values...
if (!strlen(cmd) || !strcmp(cmd, F_(values)) || !strcmp(cmd, F_(info))) {
// list all names
for (const CustomEntityItem & entity : *customEntityItems_) {
for (CustomEntityItem & entity : *customEntityItems_) {
render_value(output, entity);
}
return true;
@@ -300,7 +300,7 @@ bool WebCustomEntityService::get_value_info(JsonObject output, const char * cmd)
// list all entities
if (!strcmp(cmd, F_(entities))) {
for (const auto & entity : *customEntityItems_) {
for (auto & entity : *customEntityItems_) {
auto nest = output[entity.name].to<JsonObject>();
get_value_json(nest, entity);
}
@@ -309,7 +309,7 @@ bool WebCustomEntityService::get_value_info(JsonObject output, const char * cmd)
// specific value info
const char * attribute_s = Command::get_attribute(cmd);
for (const auto & entity : *customEntityItems_) {
for (auto & entity : *customEntityItems_) {
if (Helpers::toLower(entity.name) == cmd) {
get_value_json(output, entity);
return Command::set_attribute(output, cmd, attribute_s);
@@ -319,7 +319,7 @@ bool WebCustomEntityService::get_value_info(JsonObject output, const char * cmd)
}
// build the json for specific entity
void WebCustomEntityService::get_value_json(JsonObject output, const CustomEntityItem & entity) {
void WebCustomEntityService::get_value_json(JsonObject output, CustomEntityItem & entity) {
output["name"] = entity.name;
output["fullname"] = entity.name;
output["storage"] = entity.ram ? "ram" : "ems";
@@ -344,7 +344,7 @@ void WebCustomEntityService::get_value_json(JsonObject output, const CustomEntit
}
// publish single value
void WebCustomEntityService::publish_single(const CustomEntityItem & entity) {
void WebCustomEntityService::publish_single(CustomEntityItem & entity) {
if (!Mqtt::enabled() || !Mqtt::publish_single()) {
return;
}
@@ -372,11 +372,11 @@ void WebCustomEntityService::publish(const bool force) {
return;
}
if (customEntityItems_->size() == 0) {
if (customEntityItems_->empty()) {
return;
}
if (Mqtt::publish_single() && force) {
for (const CustomEntityItem & entityItem : *customEntityItems_) {
for (CustomEntityItem & entityItem : *customEntityItems_) {
publish_single(entityItem);
}
}
@@ -385,7 +385,7 @@ void WebCustomEntityService::publish(const bool force) {
JsonObject output = doc.to<JsonObject>();
bool ha_created = ha_registered_;
for (const CustomEntityItem & entityItem : *customEntityItems_) {
for (CustomEntityItem & entityItem : *customEntityItems_) {
render_value(output, entityItem);
// create HA config
if (Mqtt::ha_enabled() && !ha_registered_) {
@@ -454,14 +454,15 @@ void WebCustomEntityService::publish(const bool force) {
// count only entities with valid value or command to show in dashboard
uint8_t WebCustomEntityService::count_entities() {
if (customEntityItems_->size() == 0) {
if (customEntityItems_->empty()) {
return 0;
}
JsonDocument doc;
JsonObject output = doc.to<JsonObject>();
uint8_t count = 0;
for (const CustomEntityItem & entity : *customEntityItems_) {
for (CustomEntityItem & entity : *customEntityItems_) {
render_value(output, entity);
if (output[entity.name].is<JsonVariantConst>() || entity.writeable) {
count++;
@@ -557,7 +558,7 @@ void WebCustomEntityService::generate_value_web(JsonObject output) {
void WebCustomEntityService::fetch() {
const uint8_t len[] = {1, 1, 1, 2, 2, 3, 3, 4};
for (auto & entity : *customEntityItems_) {
for (auto const & entity : *customEntityItems_) {
if (entity.device_id > 0 && entity.type_id > 0) { // this excludes also RAM type
bool needFetch = true;
uint8_t fetchblock = entity.type_id > 0x0FF ? 25 : 27;
@@ -579,7 +580,6 @@ void WebCustomEntityService::fetch() {
}
}
}
// EMSESP::logger().debug("fetch custom entities");
}
// called on process telegram, read from telegram

View File

@@ -55,14 +55,14 @@ class WebCustomEntityService : public StatefulService<WebCustomEntity> {
WebCustomEntityService(AsyncWebServer * server, FS * fs, SecurityManager * securityManager);
void begin();
void publish_single(const CustomEntityItem & entity);
void publish_single(CustomEntityItem & entity);
void publish(const bool force = false);
bool command_setvalue(const char * value, const int8_t id, const char * name);
bool get_value_info(JsonObject output, const char * cmd);
void get_value_json(JsonObject output, const CustomEntityItem & entity);
void get_value_json(JsonObject output, CustomEntityItem & entity);
bool get_value(std::shared_ptr<const Telegram> telegram);
void fetch();
void render_value(JsonObject output, CustomEntityItem entity, const bool useVal = false, const bool web = false, const bool add_uom = false);
void render_value(JsonObject output, CustomEntityItem & entity, const bool useVal = false, const bool web = false, const bool add_uom = false);
void show_values(JsonObject output);
void generate_value_web(JsonObject output);

View File

@@ -134,7 +134,7 @@ bool WebSchedulerService::command_setvalue(const char * value, const int8_t id,
// process json output for info/commands and value_info
bool WebSchedulerService::get_value_info(JsonObject output, const char * cmd) {
if (scheduleItems_->size() == 0) {
if (scheduleItems_->empty()) {
return true;
}
@@ -233,7 +233,7 @@ void WebSchedulerService::publish(const bool force) {
return;
}
if (scheduleItems_->size() == 0) {
if (scheduleItems_->empty()) {
return;
}
@@ -449,7 +449,7 @@ void WebSchedulerService::loop() {
static uint32_t last_uptime_sec = 0;
// get list of scheduler events and exit if it's empty
if (scheduleItems_->size() == 0) {
if (scheduleItems_->empty()) {
return;
}