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:
- 7:6723f6887d00
- Parent:
- 4:36a4eceb1b7f
- Child:
- 8:d98e2dec0f40
--- a/IothubSerial.cpp Mon Dec 28 17:29:12 2015 +0000 +++ b/IothubSerial.cpp Tue Dec 29 23:31:28 2015 +0000 @@ -90,16 +90,36 @@ return startlen - bufsize; } +int AddTime(time_t seconds, char* buf, int bufsize) +{ + if (bufsize > 32) + { + int slen = strftime(buf, 32, "\"time\": \"%FT%T\",", localtime(&seconds)); + if (slen > 0) + { + return slen; + } + else + return -1; + } + else + return -1; +} + // 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] } +// eg: { "temp": [1, 2], "volt": [12.1, 12.2] } int IothubSerial::MeasureBufToString(char* buf, int bufsize) { int slen; bool hasdata = false; bool copydata = false; - int startlen = bufsize; + char* startbuf = buf; + bool settime = false; + char* lastcomma = NULL; + + time_t secs = 0; if (bufsize > 1) { @@ -118,11 +138,23 @@ if (_hasPending) { hasdata = true; + secs = _pending.Created; + slen = AddTime(secs, buf, bufsize); + if (slen > 0) + { + bufsize -= slen; + buf += slen; + settime = true; + } + else + return -1; // no room for pending record + slen = MeasureGroupToString(_pending, buf, bufsize); if (slen > 0) { bufsize -= slen; buf += slen; + lastcomma = buf; _hasPending = false; copydata = true; } @@ -138,11 +170,37 @@ } 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); if (slen > 0) { bufsize -= slen; buf += slen; + lastcomma = buf; _hasPending = false; copydata = true; } @@ -157,7 +215,41 @@ return -1; // have data but buffer too small // replace final ',' with '}' - *(buf - 1) = '}'; + *(lastcomma - 1) = '}'; + + 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 } +int IothubSerial::AlertBufToString(char* buf, int bufsize) +{ + int slen; + bool hasdata = false; + bool copydata = false; + int startlen = bufsize; + + if (AlertBuf.pop(_pendAlert)) + { + hasdata = true; + slen = sprintf_s(buf, bufsize, "{ \"alerttype\": \"%s\", \"alert\": \"%s\", \"sev\": %d }", _pendAlert.AlertType, _pendAlert.Msg, _pendAlert.Sev); + if (slen > 0) + { + bufsize -= slen; + buf += slen; + copydata = true; + } + } + + if (!hasdata) + return 0; // no data + + if (!copydata) + return -1; // have data but buffer too small + return startlen - bufsize; }