From a63a460da822dbbd9e5db5de080610721c52a7ed Mon Sep 17 00:00:00 2001
From: Jochen Vothknecht <jochen3120@gmail.com>
Date: Tue, 12 Apr 2022 19:21:19 +0200
Subject: [PATCH] Completing SNTP module

---
 CLC-qthing/SpiderLib/SNTP.cpp | 24 ++++++++++++++++--------
 CLC-qthing/device_main.cpp    |  9 +++++----
 2 files changed, 21 insertions(+), 12 deletions(-)

diff --git a/CLC-qthing/SpiderLib/SNTP.cpp b/CLC-qthing/SpiderLib/SNTP.cpp
index fb8de95..3bd5615 100644
--- a/CLC-qthing/SpiderLib/SNTP.cpp
+++ b/CLC-qthing/SpiderLib/SNTP.cpp
@@ -10,6 +10,7 @@
 
 // C++ system level
 #include <ctime>
+#include <cstring>
 #include <functional>
 #include <cinttypes>
 
@@ -30,27 +31,27 @@ static void ntpTask(void* evHdl) {
 
   std::function<void()>* syncEventHandler = (std::function<void()>*) evHdl;
 
-
   time_t now = 0;
-  struct tm timeinfo = { 0 };
+  struct tm timeinfo;
+  memset(&timeinfo, 0, sizeof(tm));
 
   do {
     vTaskDelay(100 / portTICK_PERIOD_MS);
     time(&now);
     localtime_r(&now, &timeinfo);
-  } while (timeinfo.tm_year < 2000);
+  } while (timeinfo.tm_year < 100);
 
 
-  (*syncEventHandler)();
+  ESP_LOGI(TAG, "Time synchronized: %02d:%02d:%02d %d-%02d-%02d", timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec,
+            timeinfo.tm_year + 1900, timeinfo.tm_mon + 1, timeinfo.tm_mday);
 
 
-  ESP_LOGI(TAG, "Time sync complete: ");
+  (*syncEventHandler)();
 
   vTaskDelete(NULL);
 }
 
 
-
 namespace SpiderLib {
 
   SNTP::SNTP() {
@@ -59,7 +60,7 @@ namespace SpiderLib {
     sntp_setservername(0, "pool.ntp.org");  // TODO: make configurable!
     sntp_init();
 
-    setenv("TZ", "CET-1CEST,M3.5.0,M10.5.0/3", 1);
+    setenv("TZ", "CET-1CEST,M3.5.0,M10.5.0/3", 1);  // germany
     tzset();
 
     xTaskCreate(ntpTask, TAG, 8192, (void*)&syncEventHandler, 0, NULL);
@@ -78,7 +79,14 @@ namespace SpiderLib {
   }
 
   bool SNTP::inSync() {
-    return false;
+    time_t now = 0;
+    struct tm timeinfo;
+    memset(&timeinfo, 0, sizeof(tm));
+
+    time(&now);
+    localtime_r(&now, &timeinfo);
+
+    return timeinfo.tm_year > 100;
   }
 
 
diff --git a/CLC-qthing/device_main.cpp b/CLC-qthing/device_main.cpp
index 8a591f9..c0f0509 100644
--- a/CLC-qthing/device_main.cpp
+++ b/CLC-qthing/device_main.cpp
@@ -25,12 +25,13 @@ CyanLight::CyanLightControl* ctrl;
 
 void device_main() {
 
-  //SpiderLib::SNTP& ntp = SpiderLib::SNTP::instance();
+  SpiderLib::SNTP& ntp = SpiderLib::SNTP::instance();
 
-  // ntp.registerTimeSyncHandler([](){
-  //   ESP_LOGE("EVENT…", "…HANDLER()!");
-  // });
+  ntp.registerTimeSyncHandler([](){
+    ESP_LOGE("EVENT…", "…HANDLER()!");
+  });
 
+  qthing::enable_wifi();
   cfg.apply();
 
   return;
-- 
GitLab