From a02cecb6eb188d5da483363ba38f8726dd90f886 Mon Sep 17 00:00:00 2001
From: wellenvogel <andreas@wellenvogel.de>
Date: Thu, 9 Dec 2021 17:19:04 +0100
Subject: [PATCH] add example for formatting boat data

---
 lib/exampletask/GwExampleTask.cpp | 38 ++++++++++++++++++++++++++++---
 lib/exampletask/GwExampleTask.h   |  4 ++--
 2 files changed, 37 insertions(+), 5 deletions(-)

diff --git a/lib/exampletask/GwExampleTask.cpp b/lib/exampletask/GwExampleTask.cpp
index c5e1858..f91125c 100644
--- a/lib/exampletask/GwExampleTask.cpp
+++ b/lib/exampletask/GwExampleTask.cpp
@@ -39,6 +39,38 @@ class GetBoatDataRequest: public GwMessage{
             longitude=api->getBoatData()->Longitude->getDataWithDefault(INVALID_COORD);
         };
 };
+
+String formatValue(GwApi::BoatValue *value){
+    if (! value->valid) return "----";
+    static const int bsize=30;
+    char buffer[bsize+1];
+    buffer[0]=0;
+    if (value->getFormat() == "formatDate"){
+        time_t tv=tNMEA0183Msg::daysToTime_t(value->value);
+        tmElements_t parts;
+        tNMEA0183Msg::breakTime(tv,parts);
+        snprintf(buffer,bsize,"%04d/%02d/%02d",parts.tm_year+1900,parts.tm_mon+1,parts.tm_mday);
+    }
+    else if(value->getFormat() == "formatTime"){
+        double inthr;
+        double intmin;
+        double intsec;
+        double val;
+        val=modf(value->value/3600.0,&inthr);
+        val=modf(val*3600.0/60.0,&intmin);
+        modf(val*60.0,&intsec);
+        snprintf(buffer,bsize,"%02.0f:%02.0f:%02.0f",inthr,intmin,intsec);
+    }
+    else if (value->getFormat() == "formatFixed0"){
+        snprintf(buffer,bsize,"%.0f",value->value);
+    }
+    else{
+        snprintf(buffer,bsize,"%.4f",value->value);
+    }
+    buffer[bsize]=0;
+    return String(buffer);
+}
+
 void exampleTask(GwApi *api){
     GwLog *logger=api->getLogger();
     //get some configuration data
@@ -111,8 +143,8 @@ void exampleTask(GwApi *api){
             //both values are there - so we have a valid position
             if (! hasPosition2){
                 //access to the values via iterator->second (iterator->first would be the name)
-                if (exampleSwitch) LOG_DEBUG(GwLog::LOG,"(2)position availale lat=%f, lon=%f",
-                    latitude->value,longitude->value);
+                if (exampleSwitch) LOG_DEBUG(GwLog::LOG,"(2)position availale lat=%s, lon=%s",
+                    formatValue(latitude).c_str(),formatValue(longitude).c_str());
                 hasPosition2=true;
             }
         }
@@ -124,7 +156,7 @@ void exampleTask(GwApi *api){
         }
         if (testValue->valid){
             if (! lastTestValueValid || lastTestValue != testValue->value){
-                LOG_DEBUG(GwLog::LOG,"%s new value %f",testValue->getName().c_str(),testValue->value);
+                LOG_DEBUG(GwLog::LOG,"%s new value %s",testValue->getName().c_str(),formatValue(testValue).c_str());
                 lastTestValueValid=true;
                 lastTestValue=testValue->value;
             }
diff --git a/lib/exampletask/GwExampleTask.h b/lib/exampletask/GwExampleTask.h
index 4200d7a..4fa5a90 100644
--- a/lib/exampletask/GwExampleTask.h
+++ b/lib/exampletask/GwExampleTask.h
@@ -27,9 +27,9 @@ void exampleTask(GwApi *param);
 void exampleInit(GwApi *param);
 //make the task known to the core
 //the task function should not return (unless you delete the task - see example code)
-DECLARE_USERTASK(exampleTask)
+//DECLARE_USERTASK(exampleTask)
 //if your task is not happy with the default 2000 bytes of stack, replace the DECLARE_USERTASK
-//with: DECLARE_USERTASK_PARAM(exampleTask,4000);
+DECLARE_USERTASK_PARAM(exampleTask,4000);
 //this would create our task with a stack size of 4000 bytes