Erick / Mbed 2 deprecated ICE_BLE_TEST

Dependencies:   NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed

Fork of ICE by Erick

Revision:
115:1558e01d04c6
Parent:
114:c24aebb8b473
Child:
120:539b20bd3816
diff -r c24aebb8b473 -r 1558e01d04c6 src/OutputTask/OutputTask.cpp
--- a/src/OutputTask/OutputTask.cpp	Thu Sep 22 19:38:03 2016 +0000
+++ b/src/OutputTask/OutputTask.cpp	Thu Sep 22 21:19:44 2016 +0000
@@ -8,6 +8,7 @@
 #include "global.h"
 #include "MbedJSONValue.h"
 #include "ModbusMasterApi.h"
+#include "CloudFileSender.h"
 #include <vector>
 #include <string>
 #include <algorithm>
@@ -16,8 +17,8 @@
 static int  createOutput(const char *controlFile);
 static void loadPersistentOutputs(void);
 static void refreshOutputs(void);
-static int  enableOutputReq  (const char* id, unsigned int pri, const char* output);
-static int  disableOutputReq (const char *id, unsigned int pri, const char *output);
+static int  enableOutputReq(OutputControlMsg_t *msg);
+static int  disableOutputReq(OutputControlMsg_t *msg);
 static int  unregisterControl(const char *id, unsigned int pri, const char *output);
 
 // The Output Map
@@ -77,24 +78,24 @@
                     }
                     break;
                 case ACTION_CONTROL_ON:
-                    logInfo("%s is requesting ON control of %s", msg->id, msg->output);
-                    rc = enableOutputReq(msg->id, msg->priority, msg->output);
+                    logInfo("%s is requesting ON control of %s", msg->id, msg->output_tag);
+                    rc = enableOutputReq(msg);
                     if ( rc != 0 ) {
                         logError("%s: failed to enabled output for %s",
                                  __func__, msg->id);
                     }
                     break;
                 case ACTION_CONTROL_OFF:
-                    logInfo("%s is requesting OFF control of %s", msg->id, msg->output);
-                    rc = disableOutputReq(msg->id, msg->priority, msg->output);
+                    logInfo("%s is requesting OFF control of %s", msg->id, msg->output_tag);
+                    rc = disableOutputReq(msg);
                     if ( rc != 0 ) {
                         printf("%s: failed to disabled output for %s",
                                __func__, msg->id);
                     }
                     break;
                 case ACTION_CONTROL_UNREGISTER:
-                    logInfo("%s is requesting its deletion from %s", msg->id, msg->output);
-                    rc = unregisterControl(msg->id, msg->priority, msg->output);
+                    logInfo("%s is requesting its deletion from %s", msg->id, msg->output_tag);
+                    rc = unregisterControl(msg->id, msg->priority, msg->output_tag);
                     if ( rc != 0 ) {
                         printf("%s: failed to unregister control %s",
                                __func__, msg->id);
@@ -113,8 +114,35 @@
     }
 }
 
-void foo(const Control *control)
+void recordEvent(std::string output, const Control *control)
 {
+    
+    EventReasonStruct_t ev;
+    memset(&ev, 0, sizeof(ev));
+    
+    switch ( control->getControlType() ) {
+        case CONTROL_SETPOINT: 
+            ModbusValue input_value;
+            ModbusValue output_value;
+            ev.eventReason = EVENT_REASON_AUTO;
+            strncpy(ev.inputTag, control->getInput().c_str(), sizeof(ev.inputTag));
+            strncpy(ev.outputTag, output.c_str(), sizeof(ev.outputTag));
+            ModbusMasterReadRegister(control->getInput(), &input_value);
+            ModbusMasterReadRegister(output, &output_value);
+            ev.inputValue = input_value.value;
+            ev.outputValue = output_value.value;
+            printf("\rEVENT RECORD\n");
+            printf("\rev.eventReason = %d\n", ev.eventReason);
+            printf("\rev.inputTag    = %s\n", ev.inputTag);
+            printf("\rev.outputTag   = %s\n", ev.outputTag);
+            printf("\rev.inputValue  = %.02f\n", ev.inputValue);
+            printf("\rev.outputValue = %.02f\n", ev.outputValue);
+            EventLoggerApi(ev);
+            break;
+        case CONTROL_MANUAL:
+        default:
+            break;
+    }
 }
 
 /*****************************************************************************
@@ -137,7 +165,7 @@
         } else {
             // a control is tied to this output
             ModbusMasterWriteRegister(pos->first, pos->second.begin()->getState());
-            foo((Control*)pos->second.begin());
+            recordEvent(pos->first, (Control*)pos->second.begin());
             printf("Preparing to send the event log data \n");
         }
     }
@@ -207,36 +235,39 @@
  * @param               args -> not used
  * @return              none
  *****************************************************************************/
-static int enableOutputReq(const char *id, unsigned int priority, const char *output)
+//static int enableOutputReq(const char *id, unsigned int priority, const char *output)
+static int enableOutputReq(OutputControlMsg_t *msg)
 {
     // attempt to find the output in the map
     StringOutputVector_t::iterator pos;
 
-    pos = outputMap.find(output);
+    pos = outputMap.find(msg->output_tag);
     if ( pos == outputMap.end() ) {
         printf("%s: failed to find the designated output %s\n",
-               __func__, output);
+               __func__, msg->output_tag);
         return -1;
     }
 
     if ( pos->second.empty() ) {
         // this is a new request
-        string cid(id);
-        Control c(cid, priority, CONTROL_ON);
+        string cid(msg->id);
+        string input(msg->input_tag);
+        Control c(cid, msg->controlType, input, msg->priority, CONTROL_ON);
         pos->second.push_back(c);
     } else {
         // find this control in the list
         vector<Control>::iterator v;
         for ( v = pos->second.begin(); v != pos->second.end(); ++v ) {
-            if ( strcmp(v->getId().c_str(), id) == 0 )  {
+            if ( strcmp(v->getId().c_str(), msg->id) == 0 )  {
                 v->setState(CONTROL_ON);
                 break;
             }
         }
         if ( v == pos->second.end() ) {
             // this is a new request, so add it and sort the vector
-            string cid(id);
-            Control c(cid, priority, CONTROL_ON);
+            string cid(msg->id);
+            string input(msg->input_tag);
+            Control c(cid, msg->controlType, input, msg->priority, CONTROL_ON);
             pos->second.push_back(c);
             std::sort(pos->second.begin(), pos->second.end());
         }
@@ -252,28 +283,29 @@
  * @param               args -> not used
  * @return              none
  *****************************************************************************/
-static int disableOutputReq(const char *id, unsigned int priority, const char *output)
+static int disableOutputReq(OutputControlMsg_t *msg)
 {
     // attempt to find the output in the map
     StringOutputVector_t::iterator pos;
 
-    pos = outputMap.find(output);
+    pos = outputMap.find(msg->output_tag);
     if ( pos == outputMap.end() ) {
         printf("%s: failed to find the designated output %s\n",
-               __func__, output);
+               __func__, msg->output_tag);
         return -1;
     }
 
     // if the control list is empty, push this control on the list
     if ( pos->second.empty() ) {
-        string cid(id);
-        Control c(cid, priority, CONTROL_OFF);
+        string cid(msg->id);
+        string input(msg->input_tag);
+        Control c(cid, msg->controlType, input, msg->priority, CONTROL_OFF);
         pos->second.push_back(c);
     } else {
         // find this control in the list
         vector<Control>::iterator v;
         for ( v = pos->second.begin(); v != pos->second.end(); ++v ) {
-            if ( strcmp(v->getId().c_str(), id) == 0 )  {
+            if ( strcmp(v->getId().c_str(), msg->id) == 0 )  {
                 v->setState(CONTROL_OFF);
                 break;
             }
@@ -281,8 +313,9 @@
 
         if ( v == pos->second.end() ) {
             // this is a new request, so add it and sort the vector
-            string cid(id);
-            Control c(cid, priority, CONTROL_OFF);
+            string cid(msg->id);
+            string input(msg->input_tag);
+            Control c(cid, msg->controlType, input, msg->priority, CONTROL_OFF);
             pos->second.push_back(c);
             std::sort(pos->second.begin(), pos->second.end());
         }