demo project

Dependencies:   AX-12A Dynamixel mbed iothub_client EthernetInterface NTPClient ConfigFile SDFileSystem iothub_amqp_transport mbed-rtos proton-c-mbed wolfSSL

Revision:
13:ffeff9b5e513
Parent:
8:d98e2dec0f40
Child:
17:0dbcbd8587fd
--- a/IothubSerial.cpp	Thu Jan 07 17:31:23 2016 +0000
+++ b/IothubSerial.cpp	Fri Jan 15 22:02:46 2016 +0000
@@ -2,10 +2,10 @@
 #include "IothubSerial.h"
 #include "crt_abstractions.h"
 
-char* nametemp = "temp";
-char* namevolt = "volt";
-char* namedeg = "rot";
-char* nameload = "load";
+const char* nametemp = "temp";
+const char* namevolt = "volt";
+const char* namedeg = "rot";
+const char* nameload = "load";
 
 IothubSerial::IothubSerial()
 {
@@ -18,7 +18,7 @@
 //  eg: temp: [22.0, 23.1, 22.3],
 int IothubSerial::MeasureGroupToString(MeasureGroup& mg, char* buf, int bufsize)
 {
-    char* name;
+    const char* name;
     int slen;
     int startlen = bufsize;
     
@@ -44,9 +44,21 @@
             return -1;
     }
     
-    if (bufsize > strlen(name) + 5)
+    slen = sprintf_s(buf, bufsize, "\"%s\": [", name);
+    if (slen > 0)
     {
-        slen = sprintf_s(buf, bufsize, "\"%s\": [", name);
+        bufsize -= slen;
+        buf += slen;
+    }
+    else
+        return -1;
+       
+    for (int i = 0; i < mg.NumVals; i++)
+    {
+        if (i < mg.NumVals - 1)
+            slen = sprintf_s(buf, bufsize, "%.2f, ", mg.MeasVals[i]);
+        else
+            slen = sprintf_s(buf, bufsize, "%.2f ", mg.MeasVals[i]);
         if (slen > 0)
         {
             bufsize -= slen;
@@ -55,43 +67,16 @@
         else
             return -1;
     }
-    else
-        return -1;
-        
-    for (int i = 0; i < mg.NumVals; i++)
+    
+    slen = sprintf_s(buf, bufsize, "],");
+    if (slen > 0)
     {
-        if (bufsize > 9)
-        {
-            if (i < mg.NumVals - 1)
-                slen = sprintf_s(buf, bufsize, "%7.2f, ", mg.MeasVals[i]);
-            else
-                slen = sprintf_s(buf, bufsize, "%7.2f ", mg.MeasVals[i]);
-            if (slen > 0)
-            {
-                bufsize -= slen;
-                buf += slen;
-            }
-            else
-                return -1;
-        }
-        else
-            return -1;
-    }
-    
-    if (bufsize > 2)
-    {
-        slen = sprintf_s(buf, bufsize, "],");
-        if (slen > 0)
-        {
-            bufsize -= slen;
-            buf += slen;
-        }
-        else
-            return -1;
+        bufsize -= slen;
+        buf += slen;
     }
     else
         return -1;
-        
+    
     return startlen - bufsize;
 }
 
@@ -112,9 +97,9 @@
 }
 
 // try to serialize one or more measurements into the buffer
-// return bytes used or -1 if buffer too small or other error. 0 if no data
-// current serialization is a json object with entry per measure
-// eg: { "temp": [1, 2], "volt": [12.1, 12.2] }
+// return bytes used / -1 if buffer too small / 0 if no data
+// current serialization is a json array of objects with time and array per measure
+// eg: [{ "time": "2016-01-23T14:55:02", "temp": [1, 2], "volt": [12.1, 12.2] }]
 int IothubSerial::MeasureBufToString(char* buf, int bufsize)
 {
     int slen;
@@ -125,17 +110,14 @@
     char* lastcomma = NULL;
     
     time_t secs = 0;
+    // reserve 1 space for end
+    bufsize--;
     
-    if (bufsize > 1)
+    slen = sprintf_s(buf, bufsize, "[{");
+    if (slen > 0)
     {
-        slen = sprintf_s(buf, bufsize, "{");
-        if (slen > 0)
-        {
-            bufsize -= slen;
-            buf += slen;
-        }
-        else
-            return -1;
+        bufsize -= slen;
+        buf += slen;
     }
     else
         return -1;
@@ -210,7 +192,7 @@
             copydata = true;
         }
         else
-            break;              // no room to serialize, leave pending for mext message
+            break;              // no room to serialize, leave pending for next message
     }
     
     if (!hasdata)
@@ -221,15 +203,18 @@
 
     // replace final ',' with '}'
     *(lastcomma - 1) = '}';
-
+    // this is the extra space we reserved at the start.
+    *(lastcomma) = ']';
+    lastcomma++;
+    
     return lastcomma - startbuf;
 }
 
 
 // try to serialize one or more alerts into the buffer
 // return bytes used or -1 if buffer too small or other error. 0 if no data
-// current serialization is a json object
-// eg: { "alert": "msg", "sev": n }
+// Serialize to a json object with time, measurename, value, message, and index of joint
+// eg: { "alerttype": "Temperature", "message": "too hot", "measurename", "temp", "index": 2, "value": 79.3, "time": "2016-01-23T14:55:02" }
 int IothubSerial::AlertBufToString(char* buf, int bufsize)
 {
     int slen;
@@ -239,8 +224,11 @@
  
     if (AlertBuf.pop(_pendAlert))
     {
+        char tbuf[32];
+        (void)strftime(tbuf, 32, "%FT%T", localtime(&_pendAlert.Created));
         hasdata = true;
-        slen = sprintf_s(buf, bufsize, "{ \"alerttype\": \"%s\", \"alert\": \"%s\", \"sev\": %d }", _pendAlert.AlertType, _pendAlert.Msg, _pendAlert.Sev);
+        slen = sprintf_s(buf, bufsize, "{ \"alerttype\": \"%s\", \"message\": \"%s\", \"measurename\": \"%s\",  \"index\": %d ,  \"value\": %f, \"time\": \"%s\" }", 
+                                            _pendAlert.AlertType, _pendAlert.Msg, _pendAlert.MeasureName, _pendAlert.Index, _pendAlert.Value, tbuf);
         if (slen > 0)
         {
             bufsize -= slen;