🎨 Replace Arduino String with std::string
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
#include <ap_service.h>
|
||||
#include <string>
|
||||
|
||||
static const char *TAG = "APService";
|
||||
|
||||
APService::APService()
|
||||
: endpoint(APSettings::read, APSettings::update, this),
|
||||
_persistence(APSettings::read, APSettings::update, this, AP_SETTINGS_FILE) {
|
||||
addUpdateHandler([&](const String &originId) { reconfigureAP(); }, false);
|
||||
addUpdateHandler([&](const std::string &originId) { reconfigureAP(); }, false);
|
||||
}
|
||||
|
||||
APService::~APService() {}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include <event_socket.h>
|
||||
#include <string>
|
||||
|
||||
SemaphoreHandle_t clientSubscriptionsMutex = xSemaphoreCreateMutex();
|
||||
|
||||
@@ -78,7 +79,7 @@ esp_err_t EventSocket::onFrame(PsychicWebSocketRequest *request, httpd_ws_frame
|
||||
if (message_type == CONNECT) {
|
||||
ESP_LOGV("EventSocket", "Connect: %s", event);
|
||||
client_subscriptions[event].push_back(request->client()->socket());
|
||||
handleSubscribeCallbacks(event, String(request->client()->socket()));
|
||||
handleSubscribeCallbacks(event, std::to_string(request->client()->socket()));
|
||||
} else if (message_type == DISCONNECT) {
|
||||
ESP_LOGV("EventSocket", "Disconnect: %s", event);
|
||||
client_subscriptions[event].remove(request->client()->socket());
|
||||
@@ -155,22 +156,22 @@ void EventSocket::send(PsychicWebSocketClient *client, const char *data, size_t
|
||||
#endif
|
||||
}
|
||||
|
||||
void EventSocket::handleEventCallbacks(String event, JsonVariant &jsonObject, int originId) {
|
||||
void EventSocket::handleEventCallbacks(std::string event, JsonVariant &jsonObject, int originId) {
|
||||
for (auto &callback : event_callbacks[event]) {
|
||||
callback(jsonObject, originId);
|
||||
}
|
||||
}
|
||||
|
||||
void EventSocket::handleSubscribeCallbacks(String event, const String &originId) {
|
||||
void EventSocket::handleSubscribeCallbacks(std::string event, const std::string &originId) {
|
||||
for (auto &callback : subscribe_callbacks[event]) {
|
||||
callback(originId, true);
|
||||
}
|
||||
}
|
||||
|
||||
void EventSocket::onEvent(String event, EventCallback callback) {
|
||||
void EventSocket::onEvent(std::string event, EventCallback callback) {
|
||||
event_callbacks[event].push_back(std::move(callback));
|
||||
}
|
||||
|
||||
void EventSocket::onSubscribe(String event, SubscribeCallback callback) {
|
||||
void EventSocket::onSubscribe(std::string event, SubscribeCallback callback) {
|
||||
subscribe_callbacks[event].push_back(std::move(callback));
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
#include <filesystem.h>
|
||||
#include <string>
|
||||
|
||||
static const char *TAG = "FileService";
|
||||
|
||||
@@ -10,7 +11,10 @@ class Initializer {
|
||||
public:
|
||||
Initializer() {
|
||||
uploadHandler = new PsychicUploadHandler();
|
||||
uploadHandler->onUpload(uploadFile);
|
||||
uploadHandler->onUpload([](PsychicRequest *request, const String &filename, uint64_t index, uint8_t *data,
|
||||
size_t len, bool last) -> esp_err_t {
|
||||
return uploadFile(request, std::string(filename.c_str()), index, data, len, last);
|
||||
});
|
||||
uploadHandler->onRequest([](PsychicRequest *request) { return request->reply(200); });
|
||||
}
|
||||
};
|
||||
@@ -42,8 +46,8 @@ esp_err_t handleEdit(PsychicRequest *request, JsonVariant &json) {
|
||||
|
||||
bool deleteFile(const char *filename) { return ESP_FS.remove(filename); }
|
||||
|
||||
String listFiles(const String &directory, bool isRoot) {
|
||||
File root = ESP_FS.open(directory.startsWith("/") ? directory : "/" + directory);
|
||||
std::string listFiles(const std::string &directory, bool isRoot) {
|
||||
File root = ESP_FS.open(directory.find("/") == 0 ? directory.c_str() : ("/" + directory).c_str());
|
||||
if (!root.isDirectory()) return "{}";
|
||||
|
||||
File file = root.openNextFile();
|
||||
@@ -51,14 +55,14 @@ String listFiles(const String &directory, bool isRoot) {
|
||||
return isRoot ? "{ \"root\": {} }" : "{}";
|
||||
}
|
||||
|
||||
String output = isRoot ? "{ \"root\": {" : "{";
|
||||
std::string output = isRoot ? "{ \"root\": {" : "{";
|
||||
|
||||
while (file) {
|
||||
String name = String(file.name());
|
||||
std::string name = std::string(file.name());
|
||||
if (file.isDirectory()) {
|
||||
output += "\"" + name + "\": " + listFiles(name, false);
|
||||
} else {
|
||||
output += "\"" + name + "\": " + String(file.size());
|
||||
output += "\"" + name + "\": " + std::to_string(file.size());
|
||||
}
|
||||
|
||||
File next = root.openNextFile();
|
||||
@@ -72,15 +76,15 @@ String listFiles(const String &directory, bool isRoot) {
|
||||
return output;
|
||||
}
|
||||
|
||||
esp_err_t uploadFile(PsychicRequest *request, const String &filename, uint64_t index, uint8_t *data, size_t len,
|
||||
esp_err_t uploadFile(PsychicRequest *request, const std::string &filename, uint64_t index, uint8_t *data, size_t len,
|
||||
bool last) {
|
||||
File file;
|
||||
String path = "/www/" + filename;
|
||||
std::string path = "/www/" + filename;
|
||||
ESP_LOGI(TAG, "Writing %d/%d bytes to: %s\n", (int)index + (int)len, request->contentLength(), path.c_str());
|
||||
|
||||
if (last) ESP_LOGI(TAG, "%s is finished. Total bytes: %d\n", path.c_str(), (int)index + (int)len);
|
||||
|
||||
file = ESP_FS.open(path, !index ? FILE_WRITE : FILE_APPEND);
|
||||
file = ESP_FS.open(path.c_str(), !index ? FILE_WRITE : FILE_APPEND);
|
||||
if (!file) {
|
||||
ESP_LOGE(TAG, "Failed to open file");
|
||||
return ESP_FAIL;
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
#include <mdns_service.h>
|
||||
#include <string>
|
||||
|
||||
static const char *TAG = "MDNSService";
|
||||
|
||||
MDNSService::MDNSService()
|
||||
: _persistence(MDNSSettings::read, MDNSSettings::update, this, MDNS_SETTINGS_FILE),
|
||||
endpoint(MDNSSettings::read, MDNSSettings::update, this) {
|
||||
addUpdateHandler([&](const String &originId) { reconfigureMDNS(); }, false);
|
||||
addUpdateHandler([&](const std::string &originId) { reconfigureMDNS(); }, false);
|
||||
}
|
||||
|
||||
MDNSService::~MDNSService() {
|
||||
@@ -77,8 +78,8 @@ void MDNSService::getStatus(JsonVariant &root) {
|
||||
}
|
||||
|
||||
esp_err_t MDNSService::queryServices(PsychicRequest *request, JsonVariant &json) {
|
||||
String service = json["service"].as<String>();
|
||||
String proto = json["protocol"].as<String>();
|
||||
std::string service = json["service"].as<std::string>();
|
||||
std::string proto = json["protocol"].as<std::string>();
|
||||
|
||||
PsychicJsonResponse response = PsychicJsonResponse(request, false);
|
||||
JsonVariant root = response.getRoot();
|
||||
|
||||
@@ -33,7 +33,7 @@ CameraService::CameraService()
|
||||
: endpoint(CameraSettings::read, CameraSettings::update, this),
|
||||
_eventEndpoint(CameraSettings::read, CameraSettings::update, this, EVENT_CAMERA_SETTINGS),
|
||||
_persistence(CameraSettings::read, CameraSettings::update, this, CAMERA_SETTINGS_FILE) {
|
||||
addUpdateHandler([&](const String &originId) { updateCamera(); }, false);
|
||||
addUpdateHandler([&](const std::string &originId) { updateCamera(); }, false);
|
||||
}
|
||||
|
||||
esp_err_t CameraService::begin() {
|
||||
|
||||
@@ -6,7 +6,7 @@ Peripherals::Peripherals()
|
||||
EVENT_CONFIGURATION_SETTINGS),
|
||||
_persistence(PeripheralsConfiguration::read, PeripheralsConfiguration::update, this, DEVICE_CONFIG_FILE) {
|
||||
_accessMutex = xSemaphoreCreateMutex();
|
||||
addUpdateHandler([&](const String &originId) { updatePins(); }, false);
|
||||
addUpdateHandler([&](const std::string &originId) { updatePins(); }, false);
|
||||
}
|
||||
|
||||
void Peripherals::begin() {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "system_service.h"
|
||||
#include <string>
|
||||
|
||||
namespace system_service {
|
||||
|
||||
@@ -38,9 +39,9 @@ void reset() {
|
||||
File root = ESP_FS.open(FS_CONFIG_DIRECTORY);
|
||||
File file;
|
||||
while (file = root.openNextFile()) {
|
||||
String path = file.path();
|
||||
std::string path = file.path();
|
||||
file.close();
|
||||
ESP_FS.remove(path);
|
||||
ESP_FS.remove(path.c_str());
|
||||
}
|
||||
restart();
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
#include <wifi_service.h>
|
||||
#include <string>
|
||||
|
||||
WiFiService::WiFiService()
|
||||
: _persistence(WiFiSettings::read, WiFiSettings::update, this, WIFI_SETTINGS_FILE),
|
||||
endpoint(WiFiSettings::read, WiFiSettings::update, this) {
|
||||
addUpdateHandler([&](const String &originId) { reconfigureWiFiConnection(); }, false);
|
||||
addUpdateHandler([&](const std::string &originId) { reconfigureWiFiConnection(); }, false);
|
||||
}
|
||||
|
||||
WiFiService::~WiFiService() {}
|
||||
@@ -144,7 +145,7 @@ void WiFiService::connectToWiFi() {
|
||||
WiFi.getNetworkInfo(i, ssid_scan, sec_scan, rssi_scan, BSSID_scan, chan_scan);
|
||||
|
||||
for (auto &network : state().wifiSettings) {
|
||||
if (ssid_scan == network.ssid) {
|
||||
if (ssid_scan == network.ssid.c_str()) {
|
||||
if (rssi_scan >= FACTORY_WIFI_RSSI_THRESHOLD) {
|
||||
network.available = true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user