🎨 Replace Arduino String with std::string
This commit is contained in:
@@ -6,11 +6,12 @@
|
||||
#include <list>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
enum message_type_t { CONNECT = 0, DISCONNECT = 1, EVENT = 2, PING = 3, PONG = 4, BINARY_EVENT = 5 };
|
||||
|
||||
typedef std::function<void(JsonVariant &root, int originId)> EventCallback;
|
||||
typedef std::function<void(const String &originId, bool sync)> SubscribeCallback;
|
||||
typedef std::function<void(const std::string &originId, bool sync)> SubscribeCallback;
|
||||
|
||||
class EventSocket {
|
||||
public:
|
||||
@@ -20,9 +21,9 @@ class EventSocket {
|
||||
|
||||
bool hasSubscribers(const char *event);
|
||||
|
||||
void onEvent(String event, EventCallback callback);
|
||||
void onEvent(std::string event, EventCallback callback);
|
||||
|
||||
void onSubscribe(String event, SubscribeCallback callback);
|
||||
void onSubscribe(std::string event, SubscribeCallback callback);
|
||||
|
||||
void emit(const char *event, JsonVariant &payload, const char *originId = "", bool onlyToSameOrigin = false);
|
||||
|
||||
@@ -31,12 +32,12 @@ class EventSocket {
|
||||
PsychicHttpServer &_server;
|
||||
const char *_route;
|
||||
|
||||
std::map<String, std::list<int>> client_subscriptions;
|
||||
std::map<String, std::list<EventCallback>> event_callbacks;
|
||||
std::map<String, std::list<SubscribeCallback>> subscribe_callbacks;
|
||||
void handleEventCallbacks(String event, JsonVariant &jsonObject, int originId);
|
||||
std::map<std::string, std::list<int>> client_subscriptions;
|
||||
std::map<std::string, std::list<EventCallback>> event_callbacks;
|
||||
std::map<std::string, std::list<SubscribeCallback>> subscribe_callbacks;
|
||||
void handleEventCallbacks(std::string event, JsonVariant &jsonObject, int originId);
|
||||
void send(PsychicWebSocketClient *client, const char *data, size_t len);
|
||||
void handleSubscribeCallbacks(String event, const String &originId);
|
||||
void handleSubscribeCallbacks(std::string event, const std::string &originId);
|
||||
|
||||
void onWSOpen(PsychicWebSocketClient *client);
|
||||
void onWSClose(PsychicWebSocketClient *client);
|
||||
|
||||
@@ -17,10 +17,10 @@
|
||||
namespace FileSystem {
|
||||
extern PsychicUploadHandler *uploadHandler;
|
||||
|
||||
String listFiles(const String &directory, bool isRoot = true);
|
||||
std::string listFiles(const std::string &directory, bool isRoot = true);
|
||||
bool deleteFile(const char *filename);
|
||||
bool editFile(const char *filename, const char *content);
|
||||
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);
|
||||
|
||||
esp_err_t getFiles(PsychicRequest *request);
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <utils/json_utils.h>
|
||||
#include <utils/ip_utils.h>
|
||||
#include <template/state_result.h>
|
||||
#include <string>
|
||||
|
||||
#include <DNSServer.h>
|
||||
#include <IPAddress.h>
|
||||
@@ -58,8 +59,8 @@ enum APNetworkStatus { ACTIVE = 0, INACTIVE, LINGERING };
|
||||
class APSettings {
|
||||
public:
|
||||
uint8_t provisionMode;
|
||||
String ssid;
|
||||
String password;
|
||||
std::string ssid;
|
||||
std::string password;
|
||||
uint8_t channel;
|
||||
bool ssidHidden;
|
||||
uint8_t maxClients;
|
||||
@@ -76,8 +77,8 @@ class APSettings {
|
||||
|
||||
static void read(APSettings &settings, JsonVariant &root) {
|
||||
root["provision_mode"] = settings.provisionMode;
|
||||
root["ssid"] = settings.ssid;
|
||||
root["password"] = settings.password;
|
||||
root["ssid"] = settings.ssid.c_str();
|
||||
root["password"] = settings.password.c_str();
|
||||
root["channel"] = settings.channel;
|
||||
root["ssid_hidden"] = settings.ssidHidden;
|
||||
root["max_clients"] = settings.maxClients;
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <utils/json_utils.h>
|
||||
#include <template/state_result.h>
|
||||
#include <filesystem.h>
|
||||
#include <string>
|
||||
|
||||
#ifndef FACTORY_MDNS_HOSTNAME
|
||||
#define FACTORY_MDNS_HOSTNAME "esp32"
|
||||
@@ -14,31 +15,31 @@
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
String key;
|
||||
String value;
|
||||
std::string key;
|
||||
std::string value;
|
||||
|
||||
void serialize(JsonVariant &json) const {
|
||||
json["key"] = key;
|
||||
json["value"] = value;
|
||||
json["key"] = key.c_str();
|
||||
json["value"] = value.c_str();
|
||||
}
|
||||
|
||||
bool deserialize(const JsonVariant &json) {
|
||||
key = json["key"].as<String>();
|
||||
value = json["value"].as<String>();
|
||||
key = json["key"].as<std::string>();
|
||||
value = json["value"].as<std::string>();
|
||||
|
||||
return key.length() > 0;
|
||||
}
|
||||
} mdns_txt_record_t;
|
||||
|
||||
typedef struct {
|
||||
String service;
|
||||
String protocol;
|
||||
std::string service;
|
||||
std::string protocol;
|
||||
uint16_t port;
|
||||
std::vector<mdns_txt_record_t> txtRecords;
|
||||
|
||||
void serialize(JsonVariant &json) const {
|
||||
json["service"] = service;
|
||||
json["protocol"] = protocol;
|
||||
json["service"] = service.c_str();
|
||||
json["protocol"] = protocol.c_str();
|
||||
json["port"] = port;
|
||||
|
||||
if (txtRecords.size() > 0) {
|
||||
@@ -51,8 +52,8 @@ typedef struct {
|
||||
}
|
||||
|
||||
bool deserialize(const JsonVariant &json) {
|
||||
service = json["service"].as<String>();
|
||||
protocol = json["protocol"].as<String>();
|
||||
service = json["service"].as<std::string>();
|
||||
protocol = json["protocol"].as<std::string>();
|
||||
port = json["port"] | 0;
|
||||
|
||||
txtRecords.clear();
|
||||
@@ -72,14 +73,14 @@ typedef struct {
|
||||
|
||||
class MDNSSettings {
|
||||
public:
|
||||
String hostname;
|
||||
String instance;
|
||||
std::string hostname;
|
||||
std::string instance;
|
||||
std::vector<mdns_service_t> services;
|
||||
std::vector<mdns_txt_record_t> globalTxtRecords;
|
||||
|
||||
static void read(MDNSSettings &settings, JsonVariant &root) {
|
||||
root["hostname"] = settings.hostname;
|
||||
root["instance"] = settings.instance;
|
||||
root["hostname"] = settings.hostname.c_str();
|
||||
root["instance"] = settings.instance.c_str();
|
||||
|
||||
JsonArray servicesArray = root["services"].to<JsonArray>();
|
||||
for (const auto &service : settings.services) {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include <Arduino.h>
|
||||
#include <template/state_result.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <string>
|
||||
|
||||
#ifndef FACTORY_NTP_ENABLED
|
||||
#define FACTORY_NTP_ENABLED true
|
||||
@@ -21,15 +22,15 @@
|
||||
class NTPSettings {
|
||||
public:
|
||||
bool enabled;
|
||||
String tzLabel;
|
||||
String tzFormat;
|
||||
String server;
|
||||
std::string tzLabel;
|
||||
std::string tzFormat;
|
||||
std::string server;
|
||||
|
||||
static void read(NTPSettings &settings, JsonVariant &root) {
|
||||
root["enabled"] = settings.enabled;
|
||||
root["server"] = settings.server;
|
||||
root["tz_label"] = settings.tzLabel;
|
||||
root["tz_format"] = settings.tzFormat;
|
||||
root["server"] = settings.server.c_str();
|
||||
root["tz_label"] = settings.tzLabel.c_str();
|
||||
root["tz_format"] = settings.tzFormat.c_str();
|
||||
}
|
||||
|
||||
static StateUpdateResult update(JsonVariant &root, NTPSettings &settings) {
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <vector>
|
||||
#include <ArduinoJson.h>
|
||||
#include <template/state_result.h>
|
||||
#include <string>
|
||||
|
||||
/*
|
||||
* I2C software connection
|
||||
@@ -20,11 +21,11 @@
|
||||
class PinConfig {
|
||||
public:
|
||||
int pin;
|
||||
String mode;
|
||||
String type;
|
||||
String role;
|
||||
std::string mode;
|
||||
std::string type;
|
||||
std::string role;
|
||||
|
||||
PinConfig(int p, String m, String t, String r) : pin(p), mode(m), type(t), role(r) {}
|
||||
PinConfig(int p, std::string m, std::string t, std::string r) : pin(p), mode(m), type(t), role(r) {}
|
||||
};
|
||||
|
||||
class PeripheralsConfiguration {
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <utils/json_utils.h>
|
||||
#include <utils/ip_utils.h>
|
||||
#include <template/state_result.h>
|
||||
#include <string>
|
||||
|
||||
#ifndef FACTORY_WIFI_SSID
|
||||
#define FACTORY_WIFI_SSID ""
|
||||
@@ -24,10 +25,10 @@
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
String ssid;
|
||||
std::string ssid;
|
||||
uint8_t bssid[6];
|
||||
int32_t channel;
|
||||
String password;
|
||||
std::string password;
|
||||
bool staticIPConfig;
|
||||
IPAddress localIP;
|
||||
IPAddress gatewayIP;
|
||||
@@ -37,8 +38,8 @@ typedef struct {
|
||||
bool available;
|
||||
|
||||
void serialize(JsonVariant &json) const {
|
||||
json["ssid"] = ssid;
|
||||
json["password"] = password;
|
||||
json["ssid"] = ssid.c_str();
|
||||
json["password"] = password.c_str();
|
||||
json["static_ip_config"] = staticIPConfig;
|
||||
if (staticIPConfig) {
|
||||
JsonUtils::writeIP(json, "local_ip", localIP);
|
||||
@@ -50,8 +51,8 @@ typedef struct {
|
||||
}
|
||||
|
||||
bool deserialize(const JsonVariant &json) {
|
||||
String newSsid = json["ssid"].as<String>();
|
||||
String newPassword = json["password"].as<String>();
|
||||
std::string newSsid = json["ssid"].as<std::string>();
|
||||
std::string newPassword = json["password"].as<std::string>();
|
||||
if (newSsid.length() < 1 || newSsid.length() > 31 || newPassword.length() > 64) {
|
||||
ESP_LOGE("WiFiSettings", "SSID or password length is invalid");
|
||||
return false;
|
||||
@@ -98,11 +99,11 @@ inline wifi_settings_t createDefaultWiFiSettings() {
|
||||
|
||||
class WiFiSettings {
|
||||
public:
|
||||
String hostname;
|
||||
std::string hostname;
|
||||
bool priorityBySignalStrength;
|
||||
std::vector<wifi_settings_t> wifiSettings;
|
||||
static void read(WiFiSettings &settings, JsonVariant &root) {
|
||||
root["hostname"] = settings.hostname;
|
||||
root["hostname"] = settings.hostname.c_str();
|
||||
root["priority_RSSI"] = settings.priorityBySignalStrength;
|
||||
JsonArray wifiNetworks = root["wifi_networks"].to<JsonArray>();
|
||||
for (const auto &wifi : settings.wifiSettings) {
|
||||
@@ -129,7 +130,7 @@ class WiFiSettings {
|
||||
networkCount++;
|
||||
}
|
||||
}
|
||||
} else if (String(FACTORY_WIFI_SSID).length() > 0) {
|
||||
} else if (std::string(FACTORY_WIFI_SSID).length() > 0) {
|
||||
ESP_LOGI("WiFiSettings", "No WiFi config found - using factory settings");
|
||||
settings.wifiSettings.push_back(createDefaultWiFiSettings());
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
if (handle != nullptr) vTaskDelete(handle)
|
||||
|
||||
struct task_t {
|
||||
String name;
|
||||
std::string name;
|
||||
TaskHandle_t handle;
|
||||
uint32_t stackSize;
|
||||
UBaseType_t priority;
|
||||
|
||||
@@ -82,7 +82,7 @@ class FSPersistence {
|
||||
|
||||
void enableUpdateHandler() {
|
||||
if (!_updateHandlerId) {
|
||||
_updateHandlerId = _statefulService->addUpdateHandler([&](const String &originId) { writeToFS(); });
|
||||
_updateHandlerId = _statefulService->addUpdateHandler([&](const std::string &originId) { writeToFS(); });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,11 +99,11 @@ class FSPersistence {
|
||||
// "/directory1/directory2/filename" We create a directory for each missing
|
||||
// parent
|
||||
void mkdirs() {
|
||||
String path(_filePath);
|
||||
int index = 0;
|
||||
while ((index = path.indexOf('/', index + 1)) != -1) {
|
||||
String segment = path.substring(0, index);
|
||||
if (!_fs->exists(segment)) _fs->mkdir(segment);
|
||||
std::string path(_filePath);
|
||||
size_t index = 0;
|
||||
while ((index = path.find('/', index + 1)) != std::string::npos) {
|
||||
std::string segment = path.substr(0, index);
|
||||
if (!_fs->exists(segment.c_str())) _fs->mkdir(segment.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include <functional>
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/semphr.h>
|
||||
#include <string>
|
||||
|
||||
#include <template/state_result.h>
|
||||
|
||||
@@ -17,8 +18,8 @@ template <typename T>
|
||||
using JsonStateReader = std::function<void(T &settings, JsonVariant &root)>;
|
||||
|
||||
using HandlerId = size_t;
|
||||
using StateUpdateCallback = std::function<void(const String &originId)>;
|
||||
using StateHookCallback = std::function<void(const String &originId, StateUpdateResult &result)>;
|
||||
using StateUpdateCallback = std::function<void(const std::string &originId)>;
|
||||
using StateHookCallback = std::function<void(const std::string &originId, StateUpdateResult &result)>;
|
||||
|
||||
class HandlerBase {
|
||||
protected:
|
||||
@@ -40,7 +41,7 @@ class UpdateHandler : public HandlerBase {
|
||||
UpdateHandler(StateUpdateCallback callback, bool allowRemove)
|
||||
: HandlerBase(allowRemove), callback_(std::move(callback)) {}
|
||||
|
||||
void invoke(const String &originId) const { callback_(originId); }
|
||||
void invoke(const std::string &originId) const { callback_(originId); }
|
||||
};
|
||||
|
||||
class HookHandler : public HandlerBase {
|
||||
@@ -50,7 +51,7 @@ class HookHandler : public HandlerBase {
|
||||
HookHandler(StateHookCallback callback, bool allowRemove)
|
||||
: HandlerBase(allowRemove), callback_(std::move(callback)) {}
|
||||
|
||||
void invoke(const String &originId, StateUpdateResult &result) const { callback_(originId, result); }
|
||||
void invoke(const std::string &originId, StateUpdateResult &result) const { callback_(originId, result); }
|
||||
};
|
||||
|
||||
template <class T>
|
||||
@@ -83,7 +84,7 @@ class StatefulService {
|
||||
[id](const HookHandler &handler) { return handler.isRemovable() && handler.getId() == id; });
|
||||
}
|
||||
|
||||
StateUpdateResult update(std::function<StateUpdateResult(T &)> stateUpdater, const String &originId) {
|
||||
StateUpdateResult update(std::function<StateUpdateResult(T &)> stateUpdater, const std::string &originId) {
|
||||
lock();
|
||||
StateUpdateResult result = stateUpdater(state_);
|
||||
unlock();
|
||||
@@ -98,7 +99,7 @@ class StatefulService {
|
||||
return result;
|
||||
}
|
||||
|
||||
StateUpdateResult update(JsonVariant &jsonObject, JsonStateUpdater<T> stateUpdater, const String &originId) {
|
||||
StateUpdateResult update(JsonVariant &jsonObject, JsonStateUpdater<T> stateUpdater, const std::string &originId) {
|
||||
lock();
|
||||
StateUpdateResult result = stateUpdater(jsonObject, state_);
|
||||
unlock();
|
||||
@@ -125,13 +126,13 @@ class StatefulService {
|
||||
unlock();
|
||||
}
|
||||
|
||||
void callUpdateHandlers(const String &originId) {
|
||||
void callUpdateHandlers(const std::string &originId) {
|
||||
for (const UpdateHandler &updateHandler : updateHandlers_) {
|
||||
updateHandler.invoke(originId);
|
||||
}
|
||||
}
|
||||
|
||||
void callHookHandlers(const String &originId, StateUpdateResult &result) {
|
||||
void callHookHandlers(const std::string &originId, StateUpdateResult &result) {
|
||||
for (const HookHandler &hookHandler : hookHandlers_) {
|
||||
hookHandler.invoke(originId, result);
|
||||
}
|
||||
@@ -145,7 +146,7 @@ class StatefulService {
|
||||
inline void lock() { xSemaphoreTakeRecursive(mutex_, portMAX_DELAY); }
|
||||
inline void unlock() { xSemaphoreGiveRecursive(mutex_); }
|
||||
|
||||
void notifyStateChange(const String &originId, StateUpdateResult &result) {
|
||||
void notifyStateChange(const std::string &originId, StateUpdateResult &result) {
|
||||
callHookHandlers(originId, result);
|
||||
if (result == StateUpdateResult::CHANGED) {
|
||||
callUpdateHandlers(originId);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <PsychicHttp.h>
|
||||
#include <string>
|
||||
|
||||
// #include <event_socket.h>
|
||||
#include <template/stateful_service.h>
|
||||
@@ -11,7 +12,7 @@ class EventEndpoint {
|
||||
EventEndpoint(JsonStateReader<T> stateReader, JsonStateUpdater<T> stateUpdater, StatefulService<T> *statefulService,
|
||||
const char *event)
|
||||
: _stateReader(stateReader), _stateUpdater(stateUpdater), _statefulService(statefulService), _event(event) {
|
||||
_statefulService->addUpdateHandler([&](const String &originId) { syncState(originId); }, false);
|
||||
_statefulService->addUpdateHandler([&](const std::string &originId) { syncState(originId); }, false);
|
||||
}
|
||||
|
||||
void begin() {
|
||||
@@ -28,10 +29,10 @@ class EventEndpoint {
|
||||
const char *_event;
|
||||
|
||||
void updateState(JsonVariant &root, int originId) {
|
||||
_statefulService->update(root, _stateUpdater, String(originId));
|
||||
_statefulService->update(root, _stateUpdater, std::to_string(originId));
|
||||
}
|
||||
|
||||
void syncState(const String &originId, bool sync = false) {
|
||||
void syncState(const std::string &originId, bool sync = false) {
|
||||
JsonDocument jsonDocument;
|
||||
JsonVariant root = jsonDocument.to<JsonVariant>();
|
||||
_statefulService->read(root, _stateReader);
|
||||
|
||||
@@ -4,25 +4,26 @@
|
||||
#include <Arduino.h>
|
||||
#include <utils/ip_utils.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <string>
|
||||
|
||||
class JsonUtils {
|
||||
public:
|
||||
static void readIP(const JsonVariant &root, const String &key, IPAddress &ip, const String &def) {
|
||||
static void readIP(const JsonVariant &root, const std::string &key, IPAddress &ip, const std::string &def) {
|
||||
IPAddress defaultIp = {};
|
||||
if (!defaultIp.fromString(def)) {
|
||||
if (!defaultIp.fromString(def.c_str())) {
|
||||
defaultIp = INADDR_NONE;
|
||||
}
|
||||
readIP(root, key, ip, defaultIp);
|
||||
}
|
||||
|
||||
static void readIP(const JsonVariant &root, const String &key, IPAddress &ip,
|
||||
static void readIP(const JsonVariant &root, const std::string &key, IPAddress &ip,
|
||||
const IPAddress &defaultIp = INADDR_NONE) {
|
||||
if (!root[key].is<String>() || !ip.fromString(root[key].as<String>())) {
|
||||
if (!root[key].is<std::string>() || !ip.fromString(root[key].as<std::string>().c_str())) {
|
||||
ip = defaultIp;
|
||||
}
|
||||
}
|
||||
|
||||
static void writeIP(JsonVariant &root, const String &key, const IPAddress &ip) {
|
||||
static void writeIP(JsonVariant &root, const std::string &key, const IPAddress &ip) {
|
||||
if (IPUtils::isSet(ip)) {
|
||||
root[key] = ip.toString();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user