From 3df6fefe064b39764fc53b4604c58ef1c0b38700 Mon Sep 17 00:00:00 2001
From: YunHorn Technology <dp.s@yunhorn.com>
Date: Fri, 29 Dec 2023 11:11:01 +0800
Subject: [PATCH] revised decoder.js

---
 Core/Src/sts_cmox_hmac_sha.c |  2 +-
 LoRaWAN/App/lora_app.c       |  5 ----
 decoder.js                   | 45 ++++++++++++++++++++++++++++++++++--
 3 files changed, 44 insertions(+), 8 deletions(-)

diff --git a/Core/Src/sts_cmox_hmac_sha.c b/Core/Src/sts_cmox_hmac_sha.c
index 8ab13e4..da1ffed 100644
--- a/Core/Src/sts_cmox_hmac_sha.c
+++ b/Core/Src/sts_cmox_hmac_sha.c
@@ -44,7 +44,7 @@ uint32_t sts_hmac_verify(void)
 
 	ret = memcmp(hmac_result.hmac_tag, (void *)sts_ac_code, sizeof(sts_ac_code));
 	hmac_result.ac_pass  = (ret == 0x0)?1U:0U;
-	APP_LOG(TS_OFF, VLEVEL_L, "\r\nHMAC Verify Success = %u \r\n", hmac_result.ac_pass);
+	APP_LOG(TS_OFF, VLEVEL_H, "\r\nHMAC Verify Success = %u \r\n", hmac_result.ac_pass);
 
 	return ret;
 }
diff --git a/LoRaWAN/App/lora_app.c b/LoRaWAN/App/lora_app.c
index b709f48..146651c 100644
--- a/LoRaWAN/App/lora_app.c
+++ b/LoRaWAN/App/lora_app.c
@@ -2214,9 +2214,6 @@ void OnRestoreSTSCFGContextProcess(void)
 
 	OnTxPeriodicityChanged(APP_TX_DUTYCYCLE);				// in msec unit
 
-
-	APP_LOG(TS_OFF, VLEVEL_L, "\r\n ================ STS SERVICE MASK --1 = %d \r\n", sts_service_mask);
-
 	if ((sts_cfg_nvm.ac[0] ==0x0 )&& (sts_cfg_nvm.ac[19]==0x0))
 	{
 		//OnTxPeriodicityChanged(APP_TX_DUTYCYCLE);				// in msec unit
@@ -2243,8 +2240,6 @@ void OnRestoreSTSCFGContextProcess(void)
 	sts_work_mode	= sts_cfg_nvm.work_mode;
 	sts_service_mask = sts_cfg_nvm.sts_service_mask;
 
-	APP_LOG(TS_OFF, VLEVEL_L, "\r\n ================ STS SERVICE MASK --2 = %d \r\n", sts_service_mask);
-
 #ifdef STS_TMG
 /*
 	volatile uint8_t averageTempThreshold;
diff --git a/decoder.js b/decoder.js
index cbdd6c1..a26b65c 100644
--- a/decoder.js
+++ b/decoder.js
@@ -1,3 +1,8 @@
+// Decode decodes an array of bytes into an object.
+//  - fPort contains the LoRaWAN fPort number
+//  - bytes is an array of bytes, e.g. [225, 230, 255, 0]
+//  - variables contains the device variables e.g. {"calibration": "3.5"} (both the key / value are of type string)
+// The function must return an object, e.g. {"temperature": 22.5}
 function Decode(fPort, data, variables) {
     if (fPort === 13) {
         return [
@@ -21,17 +26,19 @@ function Decode(fPort, data, variables) {
             }
         ]
     }
+    // Heart-beat port with LED state and remaining battery level % only
     else if (fPort === 14) {
         return [
             {
                 led_state: (bytes[0] & 0x7f) === 0 ? "Off" : "On",
-                battery_level: bytes[1] + "%",
+                battery_level: bytes[1] + " %",
             }
         ];
     }
-
+    // fPort: 1, uplink of control command
     else if (fPort === 1) {
 
+        // 0x56, report HW/SW version of node       
         if (bytes[0] === 0x56) {
             return [
                 {
@@ -42,6 +49,7 @@ function Decode(fPort, data, variables) {
                 }
             ];
         }
+        // 0x53, report seft testing result
         else if (bytes[0] === 0x53) {
             return [
                 {
@@ -51,11 +59,44 @@ function Decode(fPort, data, variables) {
                     hw_code: bytes[4],
                     battery_level: bytes[5] + "%",
                     size_value: bytes[6],
+                    average_temp: (bytes[7] + bytes[8] / 100) + "C",
+                    center_temp: (bytes[9] + bytes[10] / 100) + "C",
+                    min_temp: (bytes[11] + bytes[12] / 100) + "C",
+                    max_temp: (bytes[13] + bytes[14] / 100) + "C",
                 }
             ];
 
         }
+        // 0x43, report config of node
+        else if (bytes[0] === 0x43) {
 
+            return [
+                {
+                    mtm_code_1: bytes[1],
+                    mtm_code_2: bytes[2],
+                    sw_code: bytes[3],
+                    hw_code: bytes[4],
+                    uplink: bytes[5],
+                    uplink_unit: bytes[6],
+                    heart_beat_interval: bytes[7],
+                    heart_beat_interval_unit: bytes[8],
+                    work_mode: bytes[9],
+                    service_mask: bytes[10],
+                    size_value: bytes[12],
+                    // key parameters
+                    averageTempThreshold: (bytes[14]) + " C",
+                    // emmisivity for water 0.96, human 0.92
+                    emmisivityThreshold: (bytes[16] / 100),
+                    humanTempThreshold: (bytes[18]) + " C",
+                    waterTempThreshold: (bytes[20] / 10) + " C",
+                }
+
+
+            ];
+
+
+        }
+        // 0x59,0x44 report configed heart-beat interval
         if (bytes[0] === 0x59) {
             if (bytes[1] === 0x44) {
                 return [