From 6a0d6b0c49c2a45115f5bd3aa28570644028f15a Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Wed, 25 May 2022 05:07:04 +0300 Subject: [PATCH] sns: more flash strings --- code/espurna/light.cpp | 26 +++++++++++++------------- code/espurna/prometheus.cpp | 25 ++++++++++++------------- code/espurna/relay.cpp | 16 ++++++++-------- code/espurna/rfbridge.cpp | 4 ++-- code/espurna/sensor.cpp | 13 ++++++++++--- code/espurna/terminal.cpp | 2 +- code/espurna/wifi.cpp | 2 +- 7 files changed, 47 insertions(+), 41 deletions(-) diff --git a/code/espurna/light.cpp b/code/espurna/light.cpp index 34fb6429..e647681d 100644 --- a/code/espurna/light.cpp +++ b/code/espurna/light.cpp @@ -2359,30 +2359,30 @@ void _lightWebSocketOnConnected(JsonObject& root) { void _lightWebSocketOnAction(uint32_t client_id, const char* action, JsonObject& data) { if (_light_has_color) { - if (strcmp(action, "color") == 0) { - if (data.containsKey("rgb")) { - _lightFromRgbPayload(data["rgb"].as()); + if (STRING_VIEW("color") == action) { + if (data.containsKey(F("rgb"))) { + _lightFromRgbPayload(data[F("rgb")].as()); lightUpdate(); } else if (data.containsKey("hsv")) { - _lightFromHsvPayload(data["hsv"].as()); + _lightFromHsvPayload(data[F("hsv")].as()); lightUpdate(); } } } - if (strcmp(action, "mireds") == 0) { - if (data.containsKey("mireds")) { - _fromMireds(data["mireds"].as()); + if (STRING_VIEW("mireds") == action) { + if (data.containsKey(F("mireds"))) { + _fromMireds(data[F("mireds")].as()); lightUpdate(); } - } else if (strcmp(action, "channel") == 0) { - if (data.containsKey("id") && data.containsKey("value")) { - lightChannel(data["id"].as(), data["value"].as()); + } else if (STRING_VIEW("channel") == action) { + if (data.containsKey(F("id")) && data.containsKey(F("value"))) { + lightChannel(data[F("id")].as(), data[F("value")].as()); lightUpdate(); } - } else if (strcmp(action, "brightness") == 0) { - if (data.containsKey("value")) { - lightBrightness(data["value"].as()); + } else if (STRING_VIEW("brightness") == action) { + if (data.containsKey(F("value"))) { + lightBrightness(data[F("value")].as()); lightUpdate(); } } diff --git a/code/espurna/prometheus.cpp b/code/espurna/prometheus.cpp index b19a5391..031399c8 100644 --- a/code/espurna/prometheus.cpp +++ b/code/espurna/prometheus.cpp @@ -25,11 +25,11 @@ namespace build { namespace { constexpr bool relaySupport() { - return RELAY_SUPPORT == 1; + return 1 == RELAY_SUPPORT; } constexpr bool sensorSupport() { - return SENSOR_SUPPORT == 1; + return 1 == SENSOR_SUPPORT; } static_assert(relaySupport() || sensorSupport(), ""); @@ -45,25 +45,24 @@ void handler(AsyncWebServerRequest* request) { // Note: Response 'stream' backing buffer is customizable. Default is 1460 bytes (see ESPAsyncWebServer.h) // In case printf overflows, memory of CurrentSize+N{overflow} will be allocated to replace // the existing buffer. Previous buffer will be copied into the new and destroyed after that. - AsyncResponseStream *response = request->beginResponseStream("text/plain"); + auto *response = request->beginResponseStream("text/plain"); - #if RELAY_SUPPORT - for (unsigned char index = 0; index < relayCount(); ++index) { - response->printf("relay%u %d\n", index, static_cast(relayStatus(index))); + if (build::relaySupport()) { + for (size_t index = 0; index < relayCount(); ++index) { + response->printf_P(PSTR("relay%u %d\n"), index, relayStatus(index) ? 1 : 0); } - #endif + } - #if SENSOR_SUPPORT - for (unsigned char index = 0; index < magnitudeCount(); ++index) { + if (build::sensorSupport()) { + for (size_t index = 0; index < magnitudeCount(); ++index) { auto value = magnitudeValue(index); if (value) { value.topic.remove('/'); - response->printf("%s %s\n", - value.topic.c_str(), - value.repr.c_str()); + response->printf_P(PSTR("%s %s\n"), + value.topic.c_str(), value.repr.c_str()); } } - #endif + } response->write('\n'); diff --git a/code/espurna/relay.cpp b/code/espurna/relay.cpp index 9d17f4bd..c22f71e7 100644 --- a/code/espurna/relay.cpp +++ b/code/espurna/relay.cpp @@ -2163,8 +2163,8 @@ void _relayConfigure() { namespace { -bool _relayWebSocketOnKeyCheck(const char * key, JsonVariant&) { - return (strncmp(key, "relay", 5) == 0); +bool _relayWebSocketOnKeyCheck(const char* key, JsonVariant&) { + return strncmp_P(key, PSTR("relay"), 5) == 0; } void _relayWebSocketUpdate(JsonObject& root) { @@ -2187,8 +2187,8 @@ void _relayWebSocketSendRelays(JsonObject& root) { ::web::ws::EnumerableConfig config{root, STRING_VIEW("relayConfig")}; auto& container = config.root(); - container["size"] = _relays.size(); - container["start"] = 0; + container[F("size")] = _relays.size(); + container[F("start")] = 0; config(STRING_VIEW("relays"), _relays.size(), espurna::relay::settings::query::IndexedSettings); @@ -2216,14 +2216,14 @@ void _relayWebSocketOnConnected(JsonObject& root) { } void _relayWebSocketOnAction(uint32_t, const char* action, JsonObject& data) { - if (strcmp(action, "relay") == 0) { - if (!data.is("id") || !data.is("status")) { + if (strncmp_P(action, PSTR("relay"), 5) == 0) { + if (!data.is(F("id")) || !data.is(F("status"))) { return; } _relayHandlePayload( - data["id"].as(), - data["status"].as().c_str()); + data[F("id")].as(), + data[F("status")].as().c_str()); } } diff --git a/code/espurna/rfbridge.cpp b/code/espurna/rfbridge.cpp index 4eb48e88..fa6524ba 100644 --- a/code/espurna/rfbridge.cpp +++ b/code/espurna/rfbridge.cpp @@ -432,7 +432,7 @@ void _rfbWebSocketOnConnected(JsonObject& root) { void _rfbWebSocketOnAction(uint32_t client_id, const char* action, JsonObject& data) { #if RELAY_SUPPORT - if (strncmp(action, "rfb", 3) != 0) { + if (STRING_VIEW("rfb") == action) { return; } @@ -458,7 +458,7 @@ void _rfbWebSocketOnAction(uint32_t client_id, const char* action, JsonObject& d } bool _rfbWebSocketOnKeyCheck(const char * key, JsonVariant& value) { - return (strncmp(key, "rfb", 3) == 0); + return strncmp_P(key, PSTR("rfb"), 3) == 0; } #endif // WEB_SUPPORT diff --git a/code/espurna/sensor.cpp b/code/espurna/sensor.cpp index 2918dc7b..e1fdb55f 100644 --- a/code/espurna/sensor.cpp +++ b/code/espurna/sensor.cpp @@ -2176,7 +2176,7 @@ void _sensorWebSocketSendData(JsonObject& root) { } void _sensorWebSocketOnAction(uint32_t client_id, const char* action, JsonObject& data) { - if (strcmp(action, "emon-expected") == 0) { + if (STRING_VIEW("emon-expected") == action) { auto id = data["id"].as(); if (id < _magnitudes.size()) { auto expected = data["expected"].as(); @@ -2190,10 +2190,17 @@ void _sensorWebSocketOnAction(uint32_t client_id, const char* action, JsonObject root[key] = _sensorApiEmonExpectedValue(magnitude, expected); }); } - } else if (strcmp(action, "emon-reset-ratios") == 0) { + return; + } + + if (STRING_VIEW("emon-reset-ratios") == action) { _sensorApiEmonResetRatios(); - } else if (strcmp(action, "analog-calibrate") == 0) { + return; + } + + if (STRING_VIEW("analog-calibrate") == action) { _sensorApiAnalogCalibrate(); + return; } } diff --git a/code/espurna/terminal.cpp b/code/espurna/terminal.cpp index 662fd57d..503d48d3 100644 --- a/code/espurna/terminal.cpp +++ b/code/espurna/terminal.cpp @@ -702,7 +702,7 @@ void terminalWebApiSetup() { apiRegister(getSetting("termWebApiPath", TERMINAL_WEB_API_PATH), [](ApiRequest& api) { api.handle([](AsyncWebServerRequest* request) { - AsyncResponseStream *response = request->beginResponseStream("text/plain"); + AsyncResponseStream *response = request->beginResponseStream(F("text/plain")); for (auto* name : _terminal.names()) { response->print(name); response->print("\r\n"); diff --git a/code/espurna/wifi.cpp b/code/espurna/wifi.cpp index ce190c03..09fa6719 100644 --- a/code/espurna/wifi.cpp +++ b/code/espurna/wifi.cpp @@ -2519,7 +2519,7 @@ void onScan(uint32_t client_id) { } void onAction(uint32_t client_id, const char* action, JsonObject&) { - if (strcmp(action, "scan") == 0) { + if (STRING_VIEW("scan") == action) { onScan(client_id); } }