demo project
Dependencies: AX-12A Dynamixel mbed iothub_client EthernetInterface NTPClient ConfigFile SDFileSystem iothub_amqp_transport mbed-rtos proton-c-mbed wolfSSL
IothubSerial.cpp
- Committer:
- henryrawas
- Date:
- 2016-01-15
- Revision:
- 13:ffeff9b5e513
- Parent:
- 8:d98e2dec0f40
- Child:
- 17:0dbcbd8587fd
File content as of revision 13:ffeff9b5e513:
#include "mbed.h" #include "IothubSerial.h" #include "crt_abstractions.h" const char* nametemp = "temp"; const char* namevolt = "volt"; const char* namedeg = "rot"; const char* nameload = "load"; IothubSerial::IothubSerial() { _hasPending = false; } // try to serialize the measurements into the buffer // return bytes used or -1 if buffer too small or other error // current serialization is a json array with ',' at end // eg: temp: [22.0, 23.1, 22.3], int IothubSerial::MeasureGroupToString(MeasureGroup& mg, char* buf, int bufsize) { const char* name; int slen; int startlen = bufsize; switch (mg.MeasId) { case NM_Temperature: name = nametemp; break; case NM_Voltage: name = namevolt; break; case NM_Degrees: name = namedeg; break; case NM_Load: name = nameload; break; default: return -1; } slen = sprintf_s(buf, bufsize, "\"%s\": [", name); if (slen > 0) { 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; buf += slen; } else return -1; } slen = sprintf_s(buf, bufsize, "],"); if (slen > 0) { bufsize -= slen; buf += slen; } else return -1; 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 / -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; 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, "[{"); if (slen > 0) { bufsize -= slen; buf += slen; } else return -1; 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; } else return -1; // no room for pending record } while (!MeasureBuf.empty()) { if (!MeasureBuf.pop(_pending)) { break; } 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; } else break; // no room to serialize, leave pending for next message } if (!hasdata) return 0; // no data 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++; 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 // 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; bool hasdata = false; bool copydata = false; int startlen = bufsize; 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\", \"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; buf += slen; copydata = true; } } if (!hasdata) return 0; // no data if (!copydata) return -1; // have data but buffer too small return startlen - bufsize; } bool IothubSerial::HasMeasureGroup() { return _hasPending; }