Skip to content
Snippets Groups Projects
Commit 3443598f authored by fxk8y's avatar fxk8y :spider:
Browse files

Adding string API endpoints

parent 5cec2319
No related branches found
No related tags found
No related merge requests found
...@@ -536,7 +536,78 @@ namespace SiliconTorch { ...@@ -536,7 +536,78 @@ namespace SiliconTorch {
ESP_LOGI(TAG, "Set namespace[ %s ] key[ %s ] to i64[ %lld ]", topic[1].c_str(), topic[2].c_str(), value); ESP_LOGI(TAG, "Set namespace[ %s ] key[ %s ] to i64[ %lld ]", topic[1].c_str(), topic[2].c_str(), value);
else ESP_LOGW(TAG, "Setting namespace[ %s ] key[ %s ] to i64[ %lld ] FAILED", topic[1].c_str(), topic[2].c_str(), value); else ESP_LOGW(TAG, "Setting namespace[ %s ] key[ %s ] to i64[ %lld ] FAILED", topic[1].c_str(), topic[2].c_str(), value);
// publish error…? // publish error…?
};
qthing::add_binary_message_callback(requestTopic("get_strLen/+/+"), wrapMessageHandler(
[&](std::vector<std::string>& topic, qthing::multipart_message_t msg) {
size_t testValue = 1337000; // way too big for NVS
size_t length = getStringLength(topic[1], topic[2], testValue);
if (length < testValue) { // key exists and is of type string
char buffer[32];
std::snprintf(&buffer[0], 32, "%d", length);
std::string value(&buffer[0]);
qthing::publish_message(responseTopic("get_strLen", topic[1], topic[2]), value);
} else {
// publish error…?
ESP_LOGW(TAG, "Retrieving |str|[] from namespace[ %s ] key[ %s ] FAILED", topic[1].c_str(), topic[2].c_str());
}
} ));
qthing::add_binary_message_callback(requestTopic("get_str/+/+"), wrapMessageHandler(
[&](std::vector<std::string>& topic, qthing::multipart_message_t msg) {
size_t testValue = 1337000; // way too big for NVS
if ( getStringLength(topic[1], topic[2], testValue) < testValue ) { // key exists and is of type string
std::string value = getString(topic[1], topic[2]);
qthing::publish_message(responseTopic("get_str", topic[1], topic[2]), value);
} else {
// publish error…?
ESP_LOGW(TAG, "Retrieving str[] from namespace[ %s ] key[ %s ] FAILED", topic[1].c_str(), topic[2].c_str());
}
} ));
// #######################################
// ### TODO: remove after testing!! ###
// #######################################
qthing::add_binary_message_callback(requestTopic("get_LONGstr/+/+"), wrapMessageHandler(
[&](std::vector<std::string>& topic, qthing::multipart_message_t msg) {
size_t testValue = 1337000; // way too big for NVS
if ( getStringLength(topic[1], topic[2], testValue) < testValue ) { // key exists and is of type string
char buffer[256];
std::memset(&buffer[0], 0x00, 256);
getLongString(topic[1], topic[2], &buffer[0]);
std::string value(&buffer[0]);
qthing::publish_message(responseTopic("get_LONGstr", topic[1], topic[2]), value);
} else {
// publish error…?
ESP_LOGW(TAG, "Retrieving str[] from namespace[ %s ] key[ %s ] FAILED", topic[1].c_str(), topic[2].c_str());
}
} ));
// #######################################
// #######################################
qthing::add_binary_message_callback(requestTopic("set_i64/+/+"), wrapMessageHandler(setI64)); qthing::add_binary_message_callback(requestTopic("set_i64/+/+"), wrapMessageHandler(setI64));
...@@ -842,6 +913,75 @@ namespace SiliconTorch { ...@@ -842,6 +913,75 @@ namespace SiliconTorch {
} }
size_t NVSExplorer::getStringLength(const std::string& nameSpace, const std::string& key, size_t defaultValue) {
size_t value = defaultValue;
nvs_handle_t nvs;
if ( nvs_open(nameSpace.c_str(), NVS_READONLY, &nvs) == ESP_OK ) {
nvs_get_str(nvs, key.c_str(), NULL, &value);
nvs_close(nvs);
}
return value;
}
std::string NVSExplorer::getString(const std::string& nameSpace, const std::string& key, const std::string& defaultValue) {
nvs_handle_t nvs;
if ( nvs_open(nameSpace.c_str(), NVS_READONLY, &nvs) == ESP_OK ) {
size_t size;
if ( nvs_get_str(nvs, key.c_str(), NULL, &size) == ESP_OK ) {
char buffer[size + 1];
std::memset(&buffer[0], 0x00, size + 1);
nvs_get_str(nvs, key.c_str(), &buffer[0], &size);
nvs_close(nvs);
std::string out(&buffer[0]);
return out;
}
}
return defaultValue;
}
size_t NVSExplorer::getLongString(const std::string& nameSpace, const std::string& key, char* const buffer) {
size_t size = 0;
nvs_handle_t nvs;
if ( nvs_open(nameSpace.c_str(), NVS_READONLY, &nvs) == ESP_OK ) {
nvs_get_str(nvs, key.c_str(), buffer, &size);
nvs_close(nvs);
}
return size;
}
bool NVSExplorer::setString(const std::string& nameSpace, const std::string& key, const std::string& value) {
bool success = true;
nvs_handle_t nvs;
if ( nvs_open(nameSpace.c_str(), NVS_READWRITE, &nvs) == ESP_OK ) {
success &= nvs_set_str(nvs, key.c_str(), value.c_str()) == ESP_OK;
success &= nvs_commit(nvs) == ESP_OK;
nvs_close(nvs);
return success;
} else {
return false;
}
}
void NVSExplorer::rmKey(const std::string& nameSpace, const std::string& key) { void NVSExplorer::rmKey(const std::string& nameSpace, const std::string& key) {
nvs_handle_t _nvs; nvs_handle_t _nvs;
ESP_LOGW(TAG, "rmKey() -> %s / %s", nameSpace.c_str(), key.c_str()); ESP_LOGW(TAG, "rmKey() -> %s / %s", nameSpace.c_str(), key.c_str());
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment