From ef00fd13bc33ca941721dbe535075bee704f5129 Mon Sep 17 00:00:00 2001
From: Jochen Vothknecht <jochen3120@gmail.com>
Date: Tue, 24 May 2022 08:55:23 +0200
Subject: [PATCH] Building more commands

---
 CLC-qthing/SiliconTorch/NVSExplorer.cpp | 56 ++++++++++++++++---------
 1 file changed, 37 insertions(+), 19 deletions(-)

diff --git a/CLC-qthing/SiliconTorch/NVSExplorer.cpp b/CLC-qthing/SiliconTorch/NVSExplorer.cpp
index 2cf7b0a..547f517 100644
--- a/CLC-qthing/SiliconTorch/NVSExplorer.cpp
+++ b/CLC-qthing/SiliconTorch/NVSExplorer.cpp
@@ -123,14 +123,31 @@ namespace SiliconTorch {
       };
 
 
-      auto sharp = [](std::vector<std::string>& topic, qthing::multipart_message_t msg) {
-        ESP_LOGE(TAG, "sharpyyyyy handler called on topic[ %s ]  HÖ HÖ HÖ HÖ HÖÖÖöööö       topic{ [0] = %s  [1] = %s }", msg.topic.c_str(), topic[0].c_str(), topic[1].c_str());
+      auto lsKeys = [&](std::vector<std::string>& topic, qthing::multipart_message_t msg) {
+        
+        std::string delimiter = " ";
+        char* buffer = NULL;
+
+        if (msg.length > 0) {
+          buffer = new char[msg.length + 1];
+          snprintf(buffer, msg.length + 1, "%s", msg.payload);
+          delimiter = std::string(buffer);
+        }
+
+        auto keys = listKeys(topic[1]);
+        std::string msgOut = SpiderLib::Util::join(keys, delimiter);
+
+        // TODO: return topic…?
+        qthing::publish_message(deviceTopic(topic[1]), msgOut);
+
+        if (buffer != NULL) delete buffer;
       };
 
-      qthing::add_binary_message_callback(deviceTopic("#"), wrapMessageHandler(sharp));
+      // TODO: return topic…?
+      qthing::add_binary_message_callback(deviceTopic("ls/+"), wrapMessageHandler(lsKeys));
 
 
-      auto lsNS = [](std::vector<std::string>& topic, qthing::multipart_message_t msg) {
+      auto lsNS = [&](std::vector<std::string>& topic, qthing::multipart_message_t msg) {
         
         std::string delimiter = " ";
         char* buffer = NULL;
@@ -141,33 +158,27 @@ namespace SiliconTorch {
           delimiter = std::string(buffer);
         }
 
+        auto nameSpaces = listNamespaces();
+        std::string msgOut = SpiderLib::Util::join(nameSpaces, delimiter);
 
-        // TODO: implement!
-
+        // TODO: return topic…?
+        qthing::publish_message(deviceTopic(topic[0]), msgOut);
 
         if (buffer != NULL) delete buffer;
       };
 
-      qthing::add_binary_message_callback(deviceTopic("+/ls"), wrapMessageHandler(lsNS));
+      // TODO: return topic…?
+      qthing::add_binary_message_callback(deviceTopic("ls"), wrapMessageHandler(lsNS));
 
 
 
-      auto rmKey = [](std::vector<std::string>& topic, qthing::multipart_message_t msg) {
-        
+      auto rmKey = [&](std::vector<std::string>& topic, qthing::multipart_message_t msg) {
+        this->rmKey(topic[0], topic[1]);
       };
 
       qthing::add_binary_message_callback(deviceTopic("+/+/rm"), wrapMessageHandler(rmKey));
 
 
-      /* qthing::add_binary_message_callback("+/+/test", [&](qthing::multipart_message_t msg) {
-
-        char buf[128];
-        memset(buf, 0x00, 128);
-        memcpy(buf, msg.payload, std::min(msg.length, (uint32_t)127));
-
-        ESP_LOGW("+/+/test", "topic[ %s ]  message[ %s ]", msg.topic.c_str(), buf);
-      }); */
-
     }
 
 
@@ -190,7 +201,14 @@ namespace SiliconTorch {
 
 
     void NVSExplorer::rmKey(const std::string& nameSpace, const std::string& key) {
-      // X
+      mutex.run([&]() {
+        nvs_handle_t _nvs;
+        ESP_LOGW(TAG, "rmKey() -> %s / %s", nameSpace.c_str(), key.c_str());
+        ESP_LOGW(TAG, "open() -> %s", esp_err_to_name( nvs_open(nameSpace.c_str(), NVS_READWRITE, &_nvs) ));
+        ESP_LOGW(TAG, "erase() -> %s", esp_err_to_name( nvs_erase_key(_nvs, key.c_str()) ));
+        ESP_LOGW(TAG, "commit() -> %s", esp_err_to_name( nvs_commit(_nvs) ));
+        ESP_LOGW(TAG, "erase²() -> %s", esp_err_to_name( nvs_erase_key(_nvs, key.c_str()) ));
+      });
     }
 
     std::set<std::string> NVSExplorer::listKeys(const std::string& nameSpace) {
-- 
GitLab