robot arm demo team / Mbed 2 deprecated RobotArmDemo Featured

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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers IothubSerial.cpp Source File

IothubSerial.cpp

00001 // Copyright (c) Microsoft. All rights reserved.
00002 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
00003 
00004 #include "mbed.h"
00005 #include "IothubSerial.h"
00006 #include "crt_abstractions.h"
00007 
00008 const char* nametemp = "temp";
00009 const char* namevolt = "volt";
00010 const char* namedeg = "rot";
00011 const char* nameload = "load";
00012 
00013 IothubSerial::IothubSerial()
00014 {
00015     _hasPending = false;
00016 }
00017 
00018 // try to serialize the measurements into the buffer
00019 // return bytes used or -1 if buffer too small or other error
00020 // current serialization is a json array with ',' at end
00021 //  eg: temp: [22.0, 23.1, 22.3],
00022 int IothubSerial::MeasureGroupToString(const char* name, MeasureGroup& mg, char* buf, int bufsize)
00023 {
00024     int slen;
00025     int startlen = bufsize;
00026     
00027     slen = sprintf_s(buf, bufsize, "\"%s\": [", name);
00028     if (slen > 0)
00029     {
00030         bufsize -= slen;
00031         buf += slen;
00032     }
00033     else
00034         return -1;
00035        
00036     for (int i = 0; i < mg.NumVals; i++)
00037     {
00038         if (i < mg.NumVals - 1)
00039             slen = sprintf_s(buf, bufsize, "%.2f, ", mg.MeasVals[i]);
00040         else
00041             slen = sprintf_s(buf, bufsize, "%.2f ", mg.MeasVals[i]);
00042         if (slen > 0)
00043         {
00044             bufsize -= slen;
00045             buf += slen;
00046         }
00047         else
00048             return -1;
00049     }
00050     
00051     slen = sprintf_s(buf, bufsize, "],");
00052     if (slen > 0)
00053     {
00054         bufsize -= slen;
00055         buf += slen;
00056     }
00057     else
00058         return -1;
00059     
00060     return startlen - bufsize;
00061 }
00062 
00063 int AddTime(time_t seconds, int ms, char* buf, int bufsize)
00064 {
00065     char tbuf[32];
00066     strftime(tbuf, 32, "%FT%T", localtime(&seconds));
00067     int slen = sprintf_s(buf, bufsize, "\"time\": \"%s.%03d\",", tbuf, ms);
00068     if (slen > 0)
00069     {
00070         return slen;
00071     }
00072     else
00073         return -1;
00074 }
00075 
00076 
00077 // try to serialize a snapshot into the buffer
00078 // return bytes used / -1 if buffer too small / 0 if no data
00079 // current serialization is a json object with time and array per measure
00080 // eg: { "time": "2016-01-23T14:55:02", "temp": [1, 2], "volt": [12.1, 12.2] }
00081 int IothubSerial::MeasureSnapshotToString(MeasureSnapshot& msnap, char* buf, int bufsize)
00082 {
00083     int slen;
00084     int startlen = bufsize;
00085 
00086     slen = sprintf_s(buf, bufsize, "{");
00087     if (slen > 0)
00088     {
00089         bufsize -= slen;
00090         buf += slen;
00091     }
00092     else
00093         return -1;
00094 
00095     slen = AddTime(msnap.Created, msnap.CreatedMs, buf, bufsize);
00096     if (slen > 0)
00097     {
00098         bufsize -= slen;
00099         buf += slen;
00100     }
00101     else
00102         return -1;
00103 
00104     slen = MeasureGroupToString(nametemp, msnap.Temps, buf, bufsize);
00105     if (slen > 0)
00106     {
00107         bufsize -= slen;
00108         buf += slen;
00109     }
00110     else
00111         return -1;
00112 
00113     slen = MeasureGroupToString(namedeg, msnap.Positions, buf, bufsize);
00114     if (slen > 0)
00115     {
00116         bufsize -= slen;
00117         buf += slen;
00118     }
00119     else
00120         return -1;
00121 
00122     slen = MeasureGroupToString(nameload, msnap.Loads, buf, bufsize);
00123     if (slen > 0)
00124     {
00125         bufsize -= slen;
00126         buf += slen;
00127     }
00128     else
00129         return -1;
00130 
00131     slen = MeasureGroupToString(namevolt, msnap.Volts, buf, bufsize);
00132     if (slen > 0)
00133     {
00134         bufsize -= slen;
00135         buf += slen;
00136     }
00137     else
00138         return -1;
00139 
00140     // replace final ',' with '}'
00141     *(buf - 1) = '}';
00142     
00143     return startlen - bufsize;
00144 }
00145 
00146 // try to serialize one or more measurement snapshots into the buffer
00147 // return bytes used / -1 if buffer too small / 0 if no data
00148 // current serialization is a json array of objects with time and array per measure
00149 // eg: [{ "time": "2016-01-23T14:55:02", "temp": [1, 2], "volt": [12.1, 12.2] }]
00150 int IothubSerial::MeasureBufToString(char* buf, int bufsize)
00151 {
00152     int slen;
00153     bool hasdata = false;
00154     bool copydata = false;
00155     char* startbuf = buf;
00156     char* lastcomma = NULL;
00157     
00158     slen = sprintf_s(buf, bufsize, "[");
00159     if (slen > 0)
00160     {
00161         bufsize -= slen;
00162         buf += slen;
00163     }
00164     else
00165         return -1;
00166  
00167     if (_hasPending)
00168     {
00169         hasdata = true;
00170         slen = MeasureSnapshotToString(_pending, buf, bufsize);
00171         if (slen > 0)
00172         {
00173             bufsize -= slen;
00174             buf += slen;
00175         }
00176         else
00177             return -1;          // no room for pending record
00178         // add comma
00179         slen = sprintf_s(buf, bufsize, ",");
00180         if (slen > 0)
00181         {
00182             bufsize -= slen;
00183             buf += slen;
00184         }
00185         else
00186             return -1;
00187         lastcomma = buf;
00188         _hasPending = false;
00189         copydata = true;
00190     }
00191     
00192     while (!MeasureBuf.empty())
00193     {
00194         if (!MeasureBuf.pop(_pending))
00195         {
00196             break;
00197         }
00198         hasdata = true;
00199         _hasPending = true;
00200         
00201         slen = MeasureSnapshotToString(_pending, buf, bufsize);
00202         if (slen > 0)
00203         {
00204             bufsize -= slen;
00205             buf += slen;
00206         }
00207         else
00208             break;              // no room to serialize, leave pending for next message
00209         // add comma
00210         slen = sprintf_s(buf, bufsize, ",");
00211         if (slen > 0)
00212         {
00213             bufsize -= slen;
00214             buf += slen;
00215         }
00216         else
00217             break;
00218         _hasPending = false;
00219         lastcomma = buf;
00220         copydata = true;
00221     }
00222     
00223     if (!hasdata)
00224         return 0;               // no data
00225         
00226     if (!copydata)
00227         return -1;              // have data but buffer too small
00228 
00229     // replace final ',' with ']'
00230     *(lastcomma - 1) = ']';
00231     
00232     return lastcomma - startbuf;
00233 }
00234 
00235 
00236 // try to serialize one or more alerts into the buffer
00237 // return bytes used or -1 if buffer too small or other error. 0 if no data
00238 // Serialize to a json object with time, measurename, value, message, and index of joint
00239 // eg: { "alerttype": "Temperature", "message": "too hot", "measurename", "temp", "index": 2, "value": 79.3, "time": "2016-01-23T14:55:02" }
00240 int IothubSerial::AlertBufToString(char* buf, int bufsize)
00241 {
00242     int slen;
00243     bool hasdata = false;
00244     bool copydata = false;
00245     int startlen = bufsize;
00246  
00247     if (AlertBuf.pop(_pendAlert))
00248     {
00249         char tbuf[32];
00250         (void)strftime(tbuf, 32, "%FT%T", localtime(&_pendAlert.Created));
00251         hasdata = true;
00252         slen = sprintf_s(buf, bufsize, "{ \"alerttype\": \"%s\", \"message\": \"%s\", \"measurename\": \"%s\",  \"index\": %d ,  \"value\": %f, \"time\": \"%s\" }", 
00253                                             _pendAlert.AlertType, _pendAlert.Msg, _pendAlert.MeasureName, _pendAlert.Index, _pendAlert.Value, tbuf);
00254         if (slen > 0)
00255         {
00256             bufsize -= slen;
00257             buf += slen;
00258             copydata = true;
00259         }
00260     }
00261     
00262     if (!hasdata)
00263         return 0;               // no data
00264         
00265     if (!copydata)
00266         return -1;              // have data but buffer too small
00267 
00268 
00269     return startlen - bufsize;
00270 }
00271