Send file data through D7A Action Protocol demo.

Dependencies:   modem_ref_helper

Revision:
7:612f4cb8d8bf
Parent:
6:d4512d8f5dff
Child:
9:ef574b88e854
--- a/main.cpp	Thu Sep 21 10:29:37 2017 +0000
+++ b/main.cpp	Thu Dec 21 15:24:36 2017 +0000
@@ -6,38 +6,52 @@
 #include "files.h"
 #include "sensor.h"
 
+#define MIN_REPORT_PERIOD   (10) // Seconds
+
 Semaphore modem_ready(0);
 sensor_config_t g_light_config;
 Queue<void, 8> g_file_modified;
 
 uint8_t g_main_id;
 
+static bool report_ok(uint32_t last_report_time)
+{
+    // Do not send a report if it's been less than MIN_REPORT_PERIOD since the last report
+    if ((last_report_time/1000) < MIN_REPORT_PERIOD)
+    {
+        PRINT("Report Skipped, next in %ds min\n", MIN_REPORT_PERIOD - (last_report_time/1000));
+        return false;
+    }
+    
+    return true;
+}
+
 // Check parameters to see if data should be send
-static bool report_needed(sensor_config_t* cfg, int32_t value, int32_t last_value, uint32_t last_report_time)
+static bool report_needed(sensor_config_t* config, int32_t value, int32_t last_value, uint32_t last_report_time, uint8_t user_id)
 {
-    switch (cfg->report_type)
+    switch (config->report_type)
     {
         case REPORT_ALWAYS:
             // Send a report at each measure
-            IPRINT("Report always\r\n");
-            return true;
+            PRINT("Report[%d] always\r\n", user_id);
+            return report_ok(last_report_time);
         case REPORT_ON_DIFFERENCE:
             // Send a report when the difference between the last reported measure and the current mesure is greater than max_diff
-            if (abs(last_value - value) >= cfg->max_diff && cfg->max_diff)
+            if (abs(last_value - value) >= config->max_diff && config->max_diff)
             {
-                IPRINT("Report on difference (last:%d new:%d max_diff:%d)\r\n", last_value, value, cfg->max_diff);
-                return true;
+                PRINT("Report[%d] on difference (last:%d new:%d max_diff:%d)\r\n", user_id, last_value, value, config->max_diff);
+                return report_ok(last_report_time);
             }
             break;
         case REPORT_ON_THRESHOLD:
             // Send a report when crossing a threshold
-            if (   (value >= cfg->threshold_high && last_value < cfg->threshold_high)
-                || (value <= cfg->threshold_low  && last_value > cfg->threshold_low)
-                || (value < cfg->threshold_high  && last_value >= cfg->threshold_high)
-                || (value > cfg->threshold_low   && last_value <= cfg->threshold_low))
+            if (   (value >= config->threshold_high && last_value < config->threshold_high)
+                || (value <= config->threshold_low  && last_value > config->threshold_low)
+                || (value < config->threshold_high  && last_value >= config->threshold_high)
+                || (value > config->threshold_low   && last_value <= config->threshold_low))
             {
-                IPRINT("Rerport on threshold (last:%d new:%d th:%d tl:%d)\r\n", last_value, value, cfg->threshold_high, cfg->threshold_low);
-                return true;
+                PRINT("Report[%d] on threshold (last:%d new:%d th:%d tl:%d)\r\n", user_id, last_value, value, config->threshold_high, config->threshold_low);
+                return report_ok(last_report_time);
             }
             break;
         default:
@@ -45,10 +59,10 @@
     }
     
     // Send a report if it's been more than max_period since the last report
-    if (((last_report_time/1000) >= cfg->max_period) && cfg->max_period)
+    if (((last_report_time/1000) >= config->max_period) && config->max_period)
     {
-        IPRINT("Report on period (max_period:%d time:%d)\r\n", cfg->max_period, last_report_time);
-        return true;
+        PRINT("Report[%d] on period (max_period:%d time:%d)\r\n", user_id, config->max_period, last_report_time);
+        return report_ok(last_report_time);
     }
 
     return false;
@@ -73,7 +87,7 @@
         
         PRINT("Light %d\r\n", light_level);
                 
-        if (report_needed(&g_light_config, light_level, light_level_old, last_report_time))
+        if (report_needed(&g_light_config, light_level, light_level_old, last_report_time, g_main_id))
         {
             PRINT("Light report %d\r\n", light_level);