demo project
Dependencies: AX-12A Dynamixel mbed iothub_client EthernetInterface NTPClient ConfigFile SDFileSystem iothub_amqp_transport mbed-rtos proton-c-mbed wolfSSL
Diff: IothubSerial.cpp
- Revision:
- 17:0dbcbd8587fd
- Parent:
- 13:ffeff9b5e513
--- a/IothubSerial.cpp Tue Jan 19 20:18:23 2016 +0000 +++ b/IothubSerial.cpp Fri Jan 22 01:35:07 2016 +0000 @@ -96,7 +96,77 @@ return -1; } -// try to serialize one or more measurements into the buffer + +// try to serialize a snapshot into the buffer +// return bytes used / -1 if buffer too small / 0 if no data +// current serialization is a json object with time and array per measure +// eg: { "time": "2016-01-23T14:55:02", "temp": [1, 2], "volt": [12.1, 12.2] } +int IothubSerial::MeasureSnapshotToString(MeasureSnapshot& msnap, char* buf, int bufsize) +{ + int slen; + int startlen = bufsize; + + slen = sprintf_s(buf, bufsize, "{"); + if (slen > 0) + { + bufsize -= slen; + buf += slen; + } + else + return -1; + + slen = AddTime(msnap.Created, buf, bufsize); + if (slen > 0) + { + bufsize -= slen; + buf += slen; + } + else + return -1; + + slen = MeasureGroupToString(msnap.Temps, buf, bufsize); + if (slen > 0) + { + bufsize -= slen; + buf += slen; + } + else + return -1; + + slen = MeasureGroupToString(msnap.Positions, buf, bufsize); + if (slen > 0) + { + bufsize -= slen; + buf += slen; + } + else + return -1; + + slen = MeasureGroupToString(msnap.Loads, buf, bufsize); + if (slen > 0) + { + bufsize -= slen; + buf += slen; + } + else + return -1; + + slen = MeasureGroupToString(msnap.Volts, buf, bufsize); + if (slen > 0) + { + bufsize -= slen; + buf += slen; + } + else + return -1; + + // replace final ',' with '}' + *(buf - 1) = '}'; + + return startlen - bufsize; +} + +// try to serialize one or more measurement snapshots into the buffer // 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] }] @@ -106,14 +176,9 @@ bool hasdata = false; bool copydata = false; char* startbuf = buf; - bool settime = false; char* lastcomma = NULL; - time_t secs = 0; - // reserve 1 space for end - bufsize--; - - slen = sprintf_s(buf, bufsize, "[{"); + slen = sprintf_s(buf, bufsize, "["); if (slen > 0) { bufsize -= slen; @@ -125,28 +190,26 @@ if (_hasPending) { hasdata = true; - secs = _pending.Created; - slen = AddTime(secs, buf, bufsize); + slen = MeasureSnapshotToString(_pending, buf, bufsize); if (slen > 0) { bufsize -= slen; buf += slen; - settime = true; } else return -1; // no room for pending record - - slen = MeasureGroupToString(_pending, buf, bufsize); + // add comma + slen = sprintf_s(buf, bufsize, ","); if (slen > 0) { bufsize -= slen; buf += slen; - lastcomma = buf; - _hasPending = false; - copydata = true; } else - return -1; // no room for pending record + return -1; + lastcomma = buf; + _hasPending = false; + copydata = true; } while (!MeasureBuf.empty()) @@ -158,41 +221,26 @@ hasdata = true; _hasPending = true; - if (secs != _pending.Created) - { - if (settime && lastcomma != NULL) - { - *(lastcomma - 1) = '}'; - if (bufsize > 2) - { - strcpy(buf, ",{"); - buf += 2; - bufsize -= 2; - } - } - secs = _pending.Created; - slen = AddTime(secs, buf, bufsize); - if (slen > 0) - { - bufsize -= slen; - buf += slen; - settime = true; - } - else - break; - } - - slen = MeasureGroupToString(_pending, buf, bufsize); + slen = MeasureSnapshotToString(_pending, buf, bufsize); if (slen > 0) { bufsize -= slen; buf += slen; - lastcomma = buf; - _hasPending = false; - copydata = true; } else break; // no room to serialize, leave pending for next message + // add comma + slen = sprintf_s(buf, bufsize, ","); + if (slen > 0) + { + bufsize -= slen; + buf += slen; + } + else + break; + _hasPending = false; + lastcomma = buf; + copydata = true; } if (!hasdata) @@ -201,11 +249,8 @@ if (!copydata) return -1; // have data but buffer too small - // replace final ',' with '}' - *(lastcomma - 1) = '}'; - // this is the extra space we reserved at the start. - *(lastcomma) = ']'; - lastcomma++; + // replace final ',' with ']' + *(lastcomma - 1) = ']'; return lastcomma - startbuf; } @@ -247,7 +292,3 @@ return startlen - bufsize; } -bool IothubSerial::HasMeasureGroup() -{ - return _hasPending; -}