Erick / Mbed 2 deprecated ICE_BLE_TEST

Dependencies:   NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed

Fork of ICE by Erick

Revision:
133:c871de2d2b90
Parent:
132:45821e189dd0
Child:
136:6ad7ba157b70
--- a/src/ConfigurationHandler/Controls/TimerControl.cpp	Tue Sep 27 12:18:47 2016 +0000
+++ b/src/ConfigurationHandler/Controls/TimerControl.cpp	Tue Sep 27 15:28:59 2016 +0000
@@ -7,6 +7,7 @@
 #include "TimerControl.h"
 #include "mDot.h"
 #include "MbedJSONValue.h"
+#include "global.h"
 #include <string>
 
 extern mDot *GLOBAL_mdot;
@@ -47,13 +48,13 @@
     id          = json_value["id"].get<string>();
     output      = json_value["output"].get<string>();
 
-    // create the schedule 
+    // create the schedule
     Schedule_t s;
     s.priority  = atol(json_value["priority"].get<string>().c_str());
     s.startTime = atol(json_value["starttime"].get<string>().c_str());
     s.duration  = atol(json_value["duration"].get<string>().c_str());
-    
-    // push it on the list 
+
+    // push it on the list
     schedule.push_back(s);
 
     return true;
@@ -62,7 +63,7 @@
 //
 // method:      start
 // description: initialize the control
-// 
+//
 // @param       none
 // @return      none
 //
@@ -84,27 +85,22 @@
     // schedules should be sorted in order, so always check the first
     if ( !schedule.empty() ) {
         unsigned long currentTime = time(NULL);
-        printf("\rDEBUG checking the schedule now=%lu start%lu\n",
-            currentTime, schedule.front().startTime);
-        // does it fit? 
+        // does it fit?
         if ( currentTime < schedule.front().startTime ) {
-            // not ready to run yet
             return false;
         }
         if ( currentTime >= schedule.front().startTime &&
-             currentTime < (schedule.front().startTime + schedule.front().duration) )
-        {
-            printf("START THE FEED!!! current_time = %lu startTime = %lu\n",
-                time(NULL), schedule.front().startTime); 
+                currentTime < (schedule.front().startTime + schedule.front().duration) ) {
+            logInfo("%s signals feed start", __func__);
             return true;
         } else {
             // something is wrong here, so let's toss the schedule away
-            printf("\rERROR: schedule timestamp %lu has expired\n", 
-                schedule.front().startTime);
+            printf("\rERROR: schedule timestamp %lu has expired\n",
+                   schedule.front().startTime);
             schedule.erase(schedule.begin());
         }
     }
-    return false; 
+    return false;
 }
 
 //
@@ -114,16 +110,18 @@
 // @param       none
 // @return      true if the timer has expired; false otherwise
 //
-bool TimerControl::timerStop(void) 
+bool TimerControl::timerStop(void)
 {
-    if ( schedule.front().startTime + schedule.front().duration >= time(NULL) ) {
+    // if current time is greater than start time + feed duration...
+    if ( time(NULL) >= (schedule.front().startTime + schedule.front().duration) ) {
+        logInfo("%s signals a feed stop", __func__);
         return true;
     }
     return false;
 }
 
 //
-// method:      update 
+// method:      update
 // description: run the simplified state machine
 //
 // @param       none
@@ -135,12 +133,13 @@
         case STATE_OFF:
             if ( this->timerStart() ) {
                 currentState = STATE_RUNNING;
+                this->startFeed();
             }
             break;
         case STATE_RUNNING:
             if ( this->timerStop() ) {
                 currentState = STATE_OFF;
-                //TODO: need to remove the schedule from the vector here
+                this->stopFeed();
             }
             break;
         case STATE_DISABLED:
@@ -151,6 +150,52 @@
 }
 
 //
+// method:      startFeed
+// description: signal the output thread to start a feed
+//
+// @param       none
+// @return      none
+void TimerControl::startFeed(void)
+{
+    logInfo("%s: %s attempting to start feed on relay %s\n",
+            __func__, controlFile.c_str(), output.c_str());
+
+    OutputControlMsg_t *output_mail = OutputMasterMailBox.alloc();
+    memset(output_mail, 0, sizeof(OutputControlMsg_t));
+
+    output_mail->action      = ACTION_CONTROL_ON;
+    output_mail->controlType = CONTROL_TIMER;
+    output_mail->priority    = this->schedule.front().priority;
+
+    strncpy(output_mail->output_tag, this->output.c_str(), sizeof(output_mail->output_tag)-1);
+    strncpy(output_mail->id, this->id.c_str(), sizeof(output_mail->id)-1);
+    OutputMasterMailBox.put(output_mail);
+}
+
+//
+// method:      stopFeed
+// description: signal the output thread to stop a feed
+//
+// @param       none
+// @return      none
+void TimerControl::stopFeed(void)
+{
+    logInfo("%s: %s attempting to start feed on relay %s\n",
+            __func__, controlFile.c_str(), output.c_str());
+
+    OutputControlMsg_t *output_mail = OutputMasterMailBox.alloc();
+    memset(output_mail, 0, sizeof(OutputControlMsg_t));
+
+    output_mail->action      = ACTION_CONTROL_OFF;
+    output_mail->controlType = CONTROL_TIMER;
+    output_mail->priority    = this->schedule.front().priority;
+
+    strncpy(output_mail->output_tag, this->output.c_str(), sizeof(output_mail->output_tag)-1);
+    strncpy(output_mail->id, this->id.c_str(), sizeof(output_mail->id)-1);
+    OutputMasterMailBox.put(output_mail);
+}
+
+//
 // methid:      display
 // description: display the elements of this timer control object
 //
@@ -163,7 +208,7 @@
                         "RUNNING",
                         "DISABLED"
                       };
-                      
+
     printf("\r      controlFile : %s   \n", controlFile.c_str());
     printf("\r               id : %s   \n", id.c_str());
     printf("\r           output : %s   \n", output.c_str());
@@ -177,6 +222,6 @@
         printf("\r       start time : %lu\n", pos->startTime);
         printf("\r         duration : %u \n", pos->duration);
     }
-    printf("\r    current State : %u\n", currentState);
+    printf("\r    current State : %s\n", mapper[currentState].c_str());
 
 }
\ No newline at end of file