From ce3c3e0b3ea00bc908effa5ba84ca599582c8782 Mon Sep 17 00:00:00 2001 From: Proddy Date: Tue, 13 Feb 2024 20:19:58 +0100 Subject: [PATCH] tidy up lambda's --- lib/framework/ESP8266React.cpp | 2 +- lib/framework/FSPersistence.h | 2 +- lib/framework/FactoryResetService.cpp | 2 +- lib/framework/MqttSettingsService.cpp | 2 +- lib/framework/NTPStatus.cpp | 2 +- lib/framework/NetworkSettingsService.cpp | 2 +- lib/framework/UploadFileService.cpp | 2 +- src/analogsensor.cpp | 6 +++--- src/console.cpp | 2 +- src/devices/boiler.cpp | 2 +- src/devices/solar.cpp | 4 ++-- src/mqtt.cpp | 3 ++- src/shower.cpp | 4 ++-- src/system.cpp | 14 +++++++------- src/telegram.cpp | 2 +- src/temperaturesensor.cpp | 4 ++-- src/web/WebCustomEntityService.cpp | 18 +++++++++--------- 17 files changed, 37 insertions(+), 36 deletions(-) diff --git a/lib/framework/ESP8266React.cpp b/lib/framework/ESP8266React.cpp index 057cef0cb..d81fe7b83 100644 --- a/lib/framework/ESP8266React.cpp +++ b/lib/framework/ESP8266React.cpp @@ -65,7 +65,7 @@ ESP8266React::ESP8266React(AsyncWebServer * server, FS * fs) void ESP8266React::begin() { _networkSettingsService.begin(); - _networkSettingsService.read([this](NetworkSettings & networkSettings) { + _networkSettingsService.read([&](NetworkSettings & networkSettings) { DefaultHeaders & defaultHeaders = DefaultHeaders::Instance(); if (networkSettings.enableCORS) { defaultHeaders.addHeader("Access-Control-Allow-Origin", networkSettings.CORSOrigin); diff --git a/lib/framework/FSPersistence.h b/lib/framework/FSPersistence.h index ff2205aeb..1fe93c8bc 100644 --- a/lib/framework/FSPersistence.h +++ b/lib/framework/FSPersistence.h @@ -80,7 +80,7 @@ class FSPersistence { void enableUpdateHandler() { if (!_updateHandlerId) { - _updateHandlerId = _statefulService->addUpdateHandler([this] { writeToFS(); }); + _updateHandlerId = _statefulService->addUpdateHandler([&] { writeToFS(); }); } } diff --git a/lib/framework/FactoryResetService.cpp b/lib/framework/FactoryResetService.cpp index c7f4ee0d1..3a23f6a3b 100644 --- a/lib/framework/FactoryResetService.cpp +++ b/lib/framework/FactoryResetService.cpp @@ -8,7 +8,7 @@ FactoryResetService::FactoryResetService(AsyncWebServer * server, FS * fs, Secur } void FactoryResetService::handleRequest(AsyncWebServerRequest * request) { - request->onDisconnect([this]() { factoryReset(); }); + request->onDisconnect([this] { factoryReset(); }); request->send(200); } diff --git a/lib/framework/MqttSettingsService.cpp b/lib/framework/MqttSettingsService.cpp index 8f7c45738..2ac8958d1 100644 --- a/lib/framework/MqttSettingsService.cpp +++ b/lib/framework/MqttSettingsService.cpp @@ -13,7 +13,7 @@ MqttSettingsService::MqttSettingsService(AsyncWebServer * server, FS * fs, Secur , _disconnectReason(espMqttClientTypes::DisconnectReason::TCP_DISCONNECTED) , _mqttClient(nullptr) { WiFi.onEvent([this](WiFiEvent_t event, WiFiEventInfo_t info) { WiFiEvent(event); }); - addUpdateHandler([this]() { onConfigUpdated(); }, false); + addUpdateHandler([this] { onConfigUpdated(); }, false); } MqttSettingsService::~MqttSettingsService() { diff --git a/lib/framework/NTPStatus.cpp b/lib/framework/NTPStatus.cpp index 95b9c32a6..db5afdc90 100644 --- a/lib/framework/NTPStatus.cpp +++ b/lib/framework/NTPStatus.cpp @@ -37,7 +37,7 @@ void NTPStatus::ntpStatus(AsyncWebServerRequest * request) { time_t now = time(nullptr); // only provide enabled/disabled status for now - root["status"] = []() { + root["status"] = [] { if (esp_sntp_enabled()) { if (emsesp::EMSESP::system_.ntp_connected()) { return 2; diff --git a/lib/framework/NetworkSettingsService.cpp b/lib/framework/NetworkSettingsService.cpp index cf628175a..2b986a0d8 100644 --- a/lib/framework/NetworkSettingsService.cpp +++ b/lib/framework/NetworkSettingsService.cpp @@ -7,7 +7,7 @@ NetworkSettingsService::NetworkSettingsService(AsyncWebServer * server, FS * fs, , _fsPersistence(NetworkSettings::read, NetworkSettings::update, this, fs, NETWORK_SETTINGS_FILE) , _lastConnectionAttempt(0) , _stopping(false) { - addUpdateHandler([this]() { reconfigureWiFiConnection(); }, false); + addUpdateHandler([this] { reconfigureWiFiConnection(); }, false); WiFi.onEvent([this](WiFiEvent_t event, WiFiEventInfo_t info) { WiFiEvent(event, info); }); } diff --git a/lib/framework/UploadFileService.cpp b/lib/framework/UploadFileService.cpp index ee818b4d6..13a8e5a09 100644 --- a/lib/framework/UploadFileService.cpp +++ b/lib/framework/UploadFileService.cpp @@ -84,7 +84,7 @@ void UploadFileService::handleUpload(AsyncWebServerRequest * request, const Stri Update.setMD5(_md5.data()); _md5.front() = '\0'; } - request->onDisconnect([this]() { handleEarlyDisconnect(); }); // success, let's make sure we end the update if the client hangs up + 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 return; diff --git a/src/analogsensor.cpp b/src/analogsensor.cpp index 411c6a7bc..13a31130e 100644 --- a/src/analogsensor.cpp +++ b/src/analogsensor.cpp @@ -37,7 +37,7 @@ void AnalogSensor::start() { Command::add( EMSdevice::DeviceType::ANALOGSENSOR, F_(setvalue), - [this](const char * value, const int8_t id) { return command_setvalue(value, id); }, + [&](const char * value, const int8_t id) { return command_setvalue(value, id); }, FL_(setiovalue_cmd), CommandFlag::ADMIN_ONLY); @@ -48,7 +48,7 @@ void AnalogSensor::start() { // load settings from the customization file, sorts them and initializes the GPIOs void AnalogSensor::reload() { - EMSESP::webSettingsService.read([this](WebSettings & settings) { analog_enabled_ = settings.analog_enabled; }); + EMSESP::webSettingsService.read([&](WebSettings & settings) { analog_enabled_ = settings.analog_enabled; }); #if defined(EMSESP_STANDALONE) analog_enabled_ = true; // for local offline testing @@ -63,7 +63,7 @@ void AnalogSensor::reload() { } // load the list of analog sensors from the customization service // and store them locally and then activate them - EMSESP::webCustomizationService.read([this](WebCustomization & settings) { + EMSESP::webCustomizationService.read([&](WebCustomization & settings) { auto it = sensors_.begin(); for (auto & sensor_ : sensors_) { // update existing sensors diff --git a/src/console.cpp b/src/console.cpp index d3c2d32a9..24933e8a0 100644 --- a/src/console.cpp +++ b/src/console.cpp @@ -604,7 +604,7 @@ void EMSESPShell::display_banner() { println(); // set console name - EMSESP::esp8266React.getNetworkSettingsService()->read([this](NetworkSettings & networkSettings) { console_hostname_ = networkSettings.hostname.c_str(); }); + EMSESP::esp8266React.getNetworkSettingsService()->read([&](NetworkSettings & networkSettings) { console_hostname_ = networkSettings.hostname.c_str(); }); if (console_hostname_.empty()) { console_hostname_ = "ems-esp"; } diff --git a/src/devices/boiler.cpp b/src/devices/boiler.cpp index a2fbe953c..8a54d7429 100644 --- a/src/devices/boiler.cpp +++ b/src/devices/boiler.cpp @@ -1118,7 +1118,7 @@ void Boiler::check_active() { // check forceheatingoff option if (!Helpers::hasValue(forceHeatingOff_, EMS_VALUE_BOOL)) { - EMSESP::webSettingsService.read([this](WebSettings & settings) { forceHeatingOff_ = settings.boiler_heatingoff ? EMS_VALUE_BOOL_ON : 0; }); + EMSESP::webSettingsService.read([&](WebSettings & settings) { forceHeatingOff_ = settings.boiler_heatingoff ? EMS_VALUE_BOOL_ON : 0; }); has_update(&forceHeatingOff_); } static uint32_t lastSendHeatingOff = 0; diff --git a/src/devices/solar.cpp b/src/devices/solar.cpp index a83538bb2..2c405aca2 100644 --- a/src/devices/solar.cpp +++ b/src/devices/solar.cpp @@ -554,7 +554,7 @@ void Solar::process_SM10Monitor(std::shared_ptr telegram) { has_update(solarPumpMod_, solarpumpmod); if (!Helpers::hasValue(maxFlow_)) { - EMSESP::webSettingsService.read([this](WebSettings & settings) { maxFlow_ = settings.solar_maxflow; }); + EMSESP::webSettingsService.read([&](WebSettings & settings) { maxFlow_ = settings.solar_maxflow; }); has_update(&maxFlow_); } @@ -1047,7 +1047,7 @@ bool Solar::set_SM10MaxFlow(const char * value, const int8_t id) { return false; } maxFlow_ = (flow * 10); - EMSESP::webSettingsService.update([this](WebSettings & settings) { + EMSESP::webSettingsService.update([&](WebSettings & settings) { settings.solar_maxflow = maxFlow_; return StateUpdateResult::CHANGED; }); diff --git a/src/mqtt.cpp b/src/mqtt.cpp index 10c318b8c..6bca33d74 100644 --- a/src/mqtt.cpp +++ b/src/mqtt.cpp @@ -20,6 +20,7 @@ #include "emsesp.h" #include "version.h" #include "emsdevice.h" +#include namespace emsesp { @@ -381,7 +382,7 @@ void Mqtt::start() { // add the 'publish' command ('call system publish' in console or via API) Command::add(EMSdevice::DeviceType::SYSTEM, F_(publish), System::command_publish, FL_(publish_cmd)); - // create last will topic with the base prefixed. It has to be static because asyncmqttclient destroys the reference + // create last will topic with the base prefixed. It has to be static because the client destroys the reference static char will_topic[MQTT_TOPIC_MAX_SIZE]; if (!Mqtt::base().empty()) { snprintf(will_topic, MQTT_TOPIC_MAX_SIZE, "%s/status", Mqtt::base().c_str()); diff --git a/src/shower.cpp b/src/shower.cpp index fa09568c2..4d83c837d 100644 --- a/src/shower.cpp +++ b/src/shower.cpp @@ -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([this](WebSettings & settings) { + EMSESP::webSettingsService.read([&](WebSettings & settings) { shower_timer_ = settings.shower_timer; shower_alert_ = settings.shower_alert; shower_alert_trigger_ = settings.shower_alert_trigger * 60000; // convert from minutes @@ -35,7 +35,7 @@ void Shower::start() { Command::add( EMSdevice::DeviceType::BOILER, F_(coldshot), - [this](const char * value, const int8_t id, JsonObject output) { + [&](const char * value, const int8_t id, JsonObject output) { LOG_INFO("Forcing coldshot..."); if (shower_state_) { output["message"] = "OK"; diff --git a/src/system.cpp b/src/system.cpp index c4659d3f0..926acef97 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -202,7 +202,7 @@ bool System::command_syslog_level(const char * value, const int8_t id) { if (Helpers::value2enum(value, s, FL_(list_syslog_level))) { bool changed = false; EMSESP::webSettingsService.update( - [this](WebSettings & settings) { + [&](WebSettings & settings) { if (settings.syslog_level != (int8_t)s - 1) { settings.syslog_level = (int8_t)s - 1; changed = true; @@ -295,7 +295,7 @@ void System::format(uuid::console::Shell & shell) { } void System::syslog_init() { - EMSESP::webSettingsService.read([this](WebSettings & settings) { + EMSESP::webSettingsService.read([&](WebSettings & settings) { syslog_enabled_ = settings.syslog_enabled; syslog_level_ = settings.syslog_level; syslog_mark_interval_ = settings.syslog_mark_interval; @@ -349,7 +349,7 @@ void System::syslog_init() { // read some specific system settings to store locally for faster access void System::reload_settings() { - EMSESP::webSettingsService.read([this](WebSettings & settings) { + EMSESP::webSettingsService.read([&](WebSettings & settings) { version_ = settings.version; pbutton_gpio_ = settings.pbutton_gpio; @@ -427,7 +427,7 @@ void System::start() { refreshHeapMem(); // refresh free heap and max alloc heap #endif - EMSESP::esp8266React.getNetworkSettingsService()->read([this](NetworkSettings & networkSettings) { + EMSESP::esp8266React.getNetworkSettingsService()->read([&](NetworkSettings & networkSettings) { hostname(networkSettings.hostname.c_str()); // sets the hostname }); @@ -1104,14 +1104,14 @@ bool System::check_upgrade(bool factory_settings) { // if we're coming from 3.4.4 or 3.5.0b14 which had no version stored then we need to apply new settings if (missing_version) { LOG_INFO("Setting MQTT Entity ID format to v3.4 format"); - EMSESP::esp8266React.getMqttSettingsService()->update([this](MqttSettings & mqttSettings) { + EMSESP::esp8266React.getMqttSettingsService()->update([&](MqttSettings & mqttSettings) { mqttSettings.entity_format = 0; // use old Entity ID format from v3.4 return StateUpdateResult::CHANGED; }); } // Network Settings Wifi tx_power is now using the value * 4. - EMSESP::esp8266React.getNetworkSettingsService()->update([this](NetworkSettings & networkSettings) { + EMSESP::esp8266React.getNetworkSettingsService()->update([&](NetworkSettings & networkSettings) { if (networkSettings.tx_power == 20) { networkSettings.tx_power = WIFI_POWER_19_5dBm; // use 19.5 as we don't have 20 anymore LOG_INFO("Setting WiFi TX Power to Auto"); @@ -1130,7 +1130,7 @@ bool System::check_upgrade(bool factory_settings) { // if we did a change, set the new version and reboot if (save_version) { - EMSESP::webSettingsService.update([this](WebSettings & settings) { + EMSESP::webSettingsService.update([&](WebSettings & settings) { settings.version = EMSESP_APP_VERSION; return StateUpdateResult::CHANGED; }); diff --git a/src/telegram.cpp b/src/telegram.cpp index 36ffdbf04..83382a457 100644 --- a/src/telegram.cpp +++ b/src/telegram.cpp @@ -243,7 +243,7 @@ void RxService::add_empty(const uint8_t src, const uint8_t dest, const uint16_t // send out request to EMS bus for all devices void TxService::start() { // grab the bus ID and tx_mode - EMSESP::webSettingsService.read([this](WebSettings & settings) { + EMSESP::webSettingsService.read([&](WebSettings & settings) { ems_bus_id(settings.ems_bus_id); tx_mode(settings.tx_mode); }); diff --git a/src/temperaturesensor.cpp b/src/temperaturesensor.cpp index 49b4c1f5f..d07a478a5 100644 --- a/src/temperaturesensor.cpp +++ b/src/temperaturesensor.cpp @@ -53,7 +53,7 @@ void TemperatureSensor::start() { // load settings void TemperatureSensor::reload() { // load the service settings - EMSESP::webSettingsService.read([this](WebSettings & settings) { + EMSESP::webSettingsService.read([&](WebSettings & settings) { dallas_gpio_ = settings.dallas_gpio; parasite_ = settings.dallas_parasite; }); @@ -604,7 +604,7 @@ 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([this](WebCustomization & settings) { + EMSESP::webCustomizationService.read([&](WebCustomization & settings) { auto sensors = settings.sensorCustomizations; if (!sensors.empty()) { for (const auto & sensor : sensors) { diff --git a/src/web/WebCustomEntityService.cpp b/src/web/WebCustomEntityService.cpp index 26ac4f6b8..7cbc1b7a0 100644 --- a/src/web/WebCustomEntityService.cpp +++ b/src/web/WebCustomEntityService.cpp @@ -128,7 +128,7 @@ StateUpdateResult WebCustomEntity::update(JsonObject root, WebCustomEntity & web // set value by api command bool WebCustomEntityService::command_setvalue(const char * value, const std::string name) { - EMSESP::webCustomEntityService.read([this](WebCustomEntity & webEntity) { customEntityItems = &webEntity.customEntityItems; }); + EMSESP::webCustomEntityService.read([&](WebCustomEntity & webEntity) { customEntityItems = &webEntity.customEntityItems; }); for (CustomEntityItem & entityItem : *customEntityItems) { if (Helpers::toLower(entityItem.name) == Helpers::toLower(name)) { if (entityItem.ram == 1) { @@ -251,7 +251,7 @@ void WebCustomEntityService::show_values(JsonObject output) { // process json output for info/commands and value_info bool WebCustomEntityService::get_value_info(JsonObject output, const char * cmd) { - EMSESP::webCustomEntityService.read([this](WebCustomEntity & webEntity) { customEntityItems = &webEntity.customEntityItems; }); + EMSESP::webCustomEntityService.read([&](WebCustomEntity & webEntity) { customEntityItems = &webEntity.customEntityItems; }); // if it's commands... if (Helpers::toLower(cmd) == F_(commands)) { @@ -365,7 +365,7 @@ void WebCustomEntityService::publish(const bool force) { return; } - EMSESP::webCustomEntityService.read([this](WebCustomEntity & webEntity) { customEntityItems = &webEntity.customEntityItems; }); + EMSESP::webCustomEntityService.read([&](WebCustomEntity & webEntity) { customEntityItems = &webEntity.customEntityItems; }); if (customEntityItems->size() == 0) { return; } @@ -456,7 +456,7 @@ void WebCustomEntityService::publish(const bool force) { // count only entities with valid value or command to show in dashboard uint8_t WebCustomEntityService::count_entities() { - EMSESP::webCustomEntityService.read([this](WebCustomEntity & webEntity) { customEntityItems = &webEntity.customEntityItems; }); + EMSESP::webCustomEntityService.read([&](WebCustomEntity & webEntity) { customEntityItems = &webEntity.customEntityItems; }); if (customEntityItems->size() == 0) { return 0; } @@ -473,7 +473,7 @@ uint8_t WebCustomEntityService::count_entities() { } uint8_t WebCustomEntityService::has_commands() { - EMSESP::webCustomEntityService.read([this](WebCustomEntity & webEntity) { customEntityItems = &webEntity.customEntityItems; }); + EMSESP::webCustomEntityService.read([&](WebCustomEntity & webEntity) { customEntityItems = &webEntity.customEntityItems; }); uint8_t count = 0; for (const CustomEntityItem & entity : *customEntityItems) { count += entity.writeable ? 1 : 0; @@ -484,7 +484,7 @@ uint8_t WebCustomEntityService::has_commands() { // send to dashboard, msgpack don't like serialized, use number void WebCustomEntityService::generate_value_web(JsonObject output) { - EMSESP::webCustomEntityService.read([this](WebCustomEntity & webEntity) { customEntityItems = &webEntity.customEntityItems; }); + EMSESP::webCustomEntityService.read([&](WebCustomEntity & webEntity) { customEntityItems = &webEntity.customEntityItems; }); output["label"] = (std::string) "Custom Entities"; JsonArray data = output["data"].to(); @@ -556,7 +556,7 @@ void WebCustomEntityService::generate_value_web(JsonObject output) { // fetch telegram, called from emsesp::fetch void WebCustomEntityService::fetch() { - EMSESP::webCustomEntityService.read([this](WebCustomEntity & webEntity) { customEntityItems = &webEntity.customEntityItems; }); + EMSESP::webCustomEntityService.read([&](WebCustomEntity & webEntity) { customEntityItems = &webEntity.customEntityItems; }); const uint8_t len[] = {1, 1, 1, 2, 2, 3, 3}; for (auto & entity : *customEntityItems) { @@ -582,7 +582,7 @@ void WebCustomEntityService::fetch() { // called on process telegram, read from telegram bool WebCustomEntityService::get_value(std::shared_ptr telegram) { bool has_change = false; - EMSESP::webCustomEntityService.read([this](WebCustomEntity & webEntity) { customEntityItems = &webEntity.customEntityItems; }); + EMSESP::webCustomEntityService.read([&](WebCustomEntity & webEntity) { customEntityItems = &webEntity.customEntityItems; }); // read-length of BOOL, INT, UINT, SHORT, USHORT, ULONG, TIME const uint8_t len[] = {1, 1, 1, 2, 2, 3, 3}; for (auto & entity : *customEntityItems) { @@ -626,7 +626,7 @@ bool WebCustomEntityService::get_value(std::shared_ptr telegram) // hard coded tests #ifdef EMSESP_TEST void WebCustomEntityService::test() { - update([this](WebCustomEntity & webCustomEntity) { + update([&](WebCustomEntity & webCustomEntity) { webCustomEntity.customEntityItems.clear(); // test 1 auto entityItem = CustomEntityItem();