From 79a10055615d05db9968b9599e2e4d26d58addee Mon Sep 17 00:00:00 2001
From: Jochen Vothknecht <jochen3120@gmail.com>
Date: Mon, 11 Apr 2022 04:15:29 +0200
Subject: [PATCH] Begin implementing 'birth'-time metric

---
 CLC-qthing/CMakeLists.txt     |  2 +-
 CLC-qthing/SpiderLib/SNTP.cpp | 87 +++++++++++++++++++++++++++++++++++
 CLC-qthing/SpiderLib/SNTP.hpp | 38 +++++++++++++++
 CLC-qthing/device_main.cpp    | 11 +++++
 4 files changed, 137 insertions(+), 1 deletion(-)
 create mode 100644 CLC-qthing/SpiderLib/SNTP.cpp
 create mode 100644 CLC-qthing/SpiderLib/SNTP.hpp

diff --git a/CLC-qthing/CMakeLists.txt b/CLC-qthing/CMakeLists.txt
index bc7bdbc..2daa6ed 100644
--- a/CLC-qthing/CMakeLists.txt
+++ b/CLC-qthing/CMakeLists.txt
@@ -1,6 +1,6 @@
 idf_component_register(
   SRC_DIRS
-  "." "SiliconTorch"
+  "." "SiliconTorch" "SpiderLib"
 
   INCLUDE_DIRS
   "."
diff --git a/CLC-qthing/SpiderLib/SNTP.cpp b/CLC-qthing/SpiderLib/SNTP.cpp
new file mode 100644
index 0000000..fb8de95
--- /dev/null
+++ b/CLC-qthing/SpiderLib/SNTP.cpp
@@ -0,0 +1,87 @@
+#include "SNTP.hpp"
+
+// ESP32 specific
+#include "esp_log.h"
+#include "esp_sntp.h"
+#include "freertos/FreeRTOS.h"
+#include "freertos/task.h"
+
+// #include "driver/ledc.h"
+
+// C++ system level
+#include <ctime>
+#include <functional>
+#include <cinttypes>
+
+// project specific
+// #include "Metrics.hpp"
+
+
+/*extern "C" {
+  void setenv(char* env, char* value, int overwrite);
+  void tzset();
+}*/
+
+
+static const char* TAG = "SNTP";
+
+
+static void ntpTask(void* evHdl) {
+
+  std::function<void()>* syncEventHandler = (std::function<void()>*) evHdl;
+
+
+  time_t now = 0;
+  struct tm timeinfo = { 0 };
+
+  do {
+    vTaskDelay(100 / portTICK_PERIOD_MS);
+    time(&now);
+    localtime_r(&now, &timeinfo);
+  } while (timeinfo.tm_year < 2000);
+
+
+  (*syncEventHandler)();
+
+
+  ESP_LOGI(TAG, "Time sync complete: ");
+
+  vTaskDelete(NULL);
+}
+
+
+
+namespace SpiderLib {
+
+  SNTP::SNTP() {
+
+    sntp_setoperatingmode(SNTP_OPMODE_POLL);
+    sntp_setservername(0, "pool.ntp.org");  // TODO: make configurable!
+    sntp_init();
+
+    setenv("TZ", "CET-1CEST,M3.5.0,M10.5.0/3", 1);
+    tzset();
+
+    xTaskCreate(ntpTask, TAG, 8192, (void*)&syncEventHandler, 0, NULL);
+  }
+
+  void SNTP::registerTimeSyncHandler(std::function<void()> f) {
+    if (inSync()) {
+      f();
+    } else {
+      std::function<void()> _f = syncEventHandler;
+      syncEventHandler = [=]() {
+        _f();
+        f();
+      };
+    }
+  }
+
+  bool SNTP::inSync() {
+    return false;
+  }
+
+
+
+
+}
diff --git a/CLC-qthing/SpiderLib/SNTP.hpp b/CLC-qthing/SpiderLib/SNTP.hpp
new file mode 100644
index 0000000..2dc7d0d
--- /dev/null
+++ b/CLC-qthing/SpiderLib/SNTP.hpp
@@ -0,0 +1,38 @@
+#pragma once
+
+// ESP32 specific
+// #include "driver/ledc.h"
+
+// C++ system level
+// #include <cinttypes>
+#include <functional>
+
+// project specific
+// #include "Metrics.hpp"
+
+
+namespace SpiderLib {
+
+  class SNTP {
+    public:
+
+      SNTP(SNTP const&)           = delete;
+      void operator=(SNTP const&) = delete;
+
+      static SNTP& instance() {
+        static SNTP sntp;
+        return sntp;
+      }
+
+      bool inSync();
+
+      void registerTimeSyncHandler(std::function<void()> f);
+
+    private:
+
+      SNTP();
+
+      std::function<void()> syncEventHandler = [](){};
+
+  };
+}
diff --git a/CLC-qthing/device_main.cpp b/CLC-qthing/device_main.cpp
index e98a28a..8a591f9 100644
--- a/CLC-qthing/device_main.cpp
+++ b/CLC-qthing/device_main.cpp
@@ -12,6 +12,7 @@
 
 // ### LIBS FOR TESTING ###
 #include <cstdlib>
+#include "SpiderLib/SNTP.hpp"
 #include "SiliconTorch/CyanBusCRC.hpp"
 #include "SiliconTorch/CyanBusTOUT.hpp"
 // ###     END LIBS     ###
@@ -24,6 +25,16 @@ CyanLight::CyanLightControl* ctrl;
 
 void device_main() {
 
+  //SpiderLib::SNTP& ntp = SpiderLib::SNTP::instance();
+
+  // ntp.registerTimeSyncHandler([](){
+  //   ESP_LOGE("EVENT…", "…HANDLER()!");
+  // });
+
+  cfg.apply();
+
+  return;
+
   // Needed for packet parsing, animation rendering and stuff
   qthing::power_managment_max_power();
 
-- 
GitLab