demo project
Dependencies: AX-12A Dynamixel mbed iothub_client EthernetInterface NTPClient ConfigFile SDFileSystem iothub_amqp_transport mbed-rtos proton-c-mbed wolfSSL
IothubSerial.cpp@17:0dbcbd8587fd, 2016-01-22 (annotated)
- Committer:
- henryrawas
- Date:
- Fri Jan 22 01:35:07 2016 +0000
- Revision:
- 17:0dbcbd8587fd
- Parent:
- 13:ffeff9b5e513
add all measures to circular buffer at once. Handle alert command
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
henryrawas | 4:36a4eceb1b7f | 1 | #include "mbed.h" |
henryrawas | 4:36a4eceb1b7f | 2 | #include "IothubSerial.h" |
henryrawas | 4:36a4eceb1b7f | 3 | #include "crt_abstractions.h" |
henryrawas | 4:36a4eceb1b7f | 4 | |
henryrawas | 13:ffeff9b5e513 | 5 | const char* nametemp = "temp"; |
henryrawas | 13:ffeff9b5e513 | 6 | const char* namevolt = "volt"; |
henryrawas | 13:ffeff9b5e513 | 7 | const char* namedeg = "rot"; |
henryrawas | 13:ffeff9b5e513 | 8 | const char* nameload = "load"; |
henryrawas | 4:36a4eceb1b7f | 9 | |
henryrawas | 4:36a4eceb1b7f | 10 | IothubSerial::IothubSerial() |
henryrawas | 4:36a4eceb1b7f | 11 | { |
henryrawas | 4:36a4eceb1b7f | 12 | _hasPending = false; |
henryrawas | 4:36a4eceb1b7f | 13 | } |
henryrawas | 4:36a4eceb1b7f | 14 | |
henryrawas | 4:36a4eceb1b7f | 15 | // try to serialize the measurements into the buffer |
henryrawas | 4:36a4eceb1b7f | 16 | // return bytes used or -1 if buffer too small or other error |
henryrawas | 4:36a4eceb1b7f | 17 | // current serialization is a json array with ',' at end |
henryrawas | 4:36a4eceb1b7f | 18 | // eg: temp: [22.0, 23.1, 22.3], |
henryrawas | 4:36a4eceb1b7f | 19 | int IothubSerial::MeasureGroupToString(MeasureGroup& mg, char* buf, int bufsize) |
henryrawas | 4:36a4eceb1b7f | 20 | { |
henryrawas | 13:ffeff9b5e513 | 21 | const char* name; |
henryrawas | 4:36a4eceb1b7f | 22 | int slen; |
henryrawas | 4:36a4eceb1b7f | 23 | int startlen = bufsize; |
henryrawas | 4:36a4eceb1b7f | 24 | |
henryrawas | 4:36a4eceb1b7f | 25 | switch (mg.MeasId) |
henryrawas | 4:36a4eceb1b7f | 26 | { |
henryrawas | 4:36a4eceb1b7f | 27 | case NM_Temperature: |
henryrawas | 4:36a4eceb1b7f | 28 | name = nametemp; |
henryrawas | 4:36a4eceb1b7f | 29 | break; |
henryrawas | 4:36a4eceb1b7f | 30 | |
henryrawas | 4:36a4eceb1b7f | 31 | case NM_Voltage: |
henryrawas | 4:36a4eceb1b7f | 32 | name = namevolt; |
henryrawas | 4:36a4eceb1b7f | 33 | break; |
henryrawas | 4:36a4eceb1b7f | 34 | |
henryrawas | 4:36a4eceb1b7f | 35 | case NM_Degrees: |
henryrawas | 4:36a4eceb1b7f | 36 | name = namedeg; |
henryrawas | 4:36a4eceb1b7f | 37 | break; |
henryrawas | 4:36a4eceb1b7f | 38 | |
henryrawas | 8:d98e2dec0f40 | 39 | case NM_Load: |
henryrawas | 8:d98e2dec0f40 | 40 | name = nameload; |
henryrawas | 8:d98e2dec0f40 | 41 | break; |
henryrawas | 8:d98e2dec0f40 | 42 | |
henryrawas | 4:36a4eceb1b7f | 43 | default: |
henryrawas | 4:36a4eceb1b7f | 44 | return -1; |
henryrawas | 4:36a4eceb1b7f | 45 | } |
henryrawas | 4:36a4eceb1b7f | 46 | |
henryrawas | 13:ffeff9b5e513 | 47 | slen = sprintf_s(buf, bufsize, "\"%s\": [", name); |
henryrawas | 13:ffeff9b5e513 | 48 | if (slen > 0) |
henryrawas | 4:36a4eceb1b7f | 49 | { |
henryrawas | 13:ffeff9b5e513 | 50 | bufsize -= slen; |
henryrawas | 13:ffeff9b5e513 | 51 | buf += slen; |
henryrawas | 13:ffeff9b5e513 | 52 | } |
henryrawas | 13:ffeff9b5e513 | 53 | else |
henryrawas | 13:ffeff9b5e513 | 54 | return -1; |
henryrawas | 13:ffeff9b5e513 | 55 | |
henryrawas | 13:ffeff9b5e513 | 56 | for (int i = 0; i < mg.NumVals; i++) |
henryrawas | 13:ffeff9b5e513 | 57 | { |
henryrawas | 13:ffeff9b5e513 | 58 | if (i < mg.NumVals - 1) |
henryrawas | 13:ffeff9b5e513 | 59 | slen = sprintf_s(buf, bufsize, "%.2f, ", mg.MeasVals[i]); |
henryrawas | 13:ffeff9b5e513 | 60 | else |
henryrawas | 13:ffeff9b5e513 | 61 | slen = sprintf_s(buf, bufsize, "%.2f ", mg.MeasVals[i]); |
henryrawas | 4:36a4eceb1b7f | 62 | if (slen > 0) |
henryrawas | 4:36a4eceb1b7f | 63 | { |
henryrawas | 4:36a4eceb1b7f | 64 | bufsize -= slen; |
henryrawas | 4:36a4eceb1b7f | 65 | buf += slen; |
henryrawas | 4:36a4eceb1b7f | 66 | } |
henryrawas | 4:36a4eceb1b7f | 67 | else |
henryrawas | 4:36a4eceb1b7f | 68 | return -1; |
henryrawas | 4:36a4eceb1b7f | 69 | } |
henryrawas | 13:ffeff9b5e513 | 70 | |
henryrawas | 13:ffeff9b5e513 | 71 | slen = sprintf_s(buf, bufsize, "],"); |
henryrawas | 13:ffeff9b5e513 | 72 | if (slen > 0) |
henryrawas | 4:36a4eceb1b7f | 73 | { |
henryrawas | 13:ffeff9b5e513 | 74 | bufsize -= slen; |
henryrawas | 13:ffeff9b5e513 | 75 | buf += slen; |
henryrawas | 4:36a4eceb1b7f | 76 | } |
henryrawas | 4:36a4eceb1b7f | 77 | else |
henryrawas | 4:36a4eceb1b7f | 78 | return -1; |
henryrawas | 13:ffeff9b5e513 | 79 | |
henryrawas | 4:36a4eceb1b7f | 80 | return startlen - bufsize; |
henryrawas | 4:36a4eceb1b7f | 81 | } |
henryrawas | 4:36a4eceb1b7f | 82 | |
henryrawas | 7:6723f6887d00 | 83 | int AddTime(time_t seconds, char* buf, int bufsize) |
henryrawas | 7:6723f6887d00 | 84 | { |
henryrawas | 7:6723f6887d00 | 85 | if (bufsize > 32) |
henryrawas | 7:6723f6887d00 | 86 | { |
henryrawas | 7:6723f6887d00 | 87 | int slen = strftime(buf, 32, "\"time\": \"%FT%T\",", localtime(&seconds)); |
henryrawas | 7:6723f6887d00 | 88 | if (slen > 0) |
henryrawas | 7:6723f6887d00 | 89 | { |
henryrawas | 7:6723f6887d00 | 90 | return slen; |
henryrawas | 7:6723f6887d00 | 91 | } |
henryrawas | 7:6723f6887d00 | 92 | else |
henryrawas | 7:6723f6887d00 | 93 | return -1; |
henryrawas | 7:6723f6887d00 | 94 | } |
henryrawas | 7:6723f6887d00 | 95 | else |
henryrawas | 7:6723f6887d00 | 96 | return -1; |
henryrawas | 7:6723f6887d00 | 97 | } |
henryrawas | 7:6723f6887d00 | 98 | |
henryrawas | 17:0dbcbd8587fd | 99 | |
henryrawas | 17:0dbcbd8587fd | 100 | // try to serialize a snapshot into the buffer |
henryrawas | 17:0dbcbd8587fd | 101 | // return bytes used / -1 if buffer too small / 0 if no data |
henryrawas | 17:0dbcbd8587fd | 102 | // current serialization is a json object with time and array per measure |
henryrawas | 17:0dbcbd8587fd | 103 | // eg: { "time": "2016-01-23T14:55:02", "temp": [1, 2], "volt": [12.1, 12.2] } |
henryrawas | 17:0dbcbd8587fd | 104 | int IothubSerial::MeasureSnapshotToString(MeasureSnapshot& msnap, char* buf, int bufsize) |
henryrawas | 17:0dbcbd8587fd | 105 | { |
henryrawas | 17:0dbcbd8587fd | 106 | int slen; |
henryrawas | 17:0dbcbd8587fd | 107 | int startlen = bufsize; |
henryrawas | 17:0dbcbd8587fd | 108 | |
henryrawas | 17:0dbcbd8587fd | 109 | slen = sprintf_s(buf, bufsize, "{"); |
henryrawas | 17:0dbcbd8587fd | 110 | if (slen > 0) |
henryrawas | 17:0dbcbd8587fd | 111 | { |
henryrawas | 17:0dbcbd8587fd | 112 | bufsize -= slen; |
henryrawas | 17:0dbcbd8587fd | 113 | buf += slen; |
henryrawas | 17:0dbcbd8587fd | 114 | } |
henryrawas | 17:0dbcbd8587fd | 115 | else |
henryrawas | 17:0dbcbd8587fd | 116 | return -1; |
henryrawas | 17:0dbcbd8587fd | 117 | |
henryrawas | 17:0dbcbd8587fd | 118 | slen = AddTime(msnap.Created, buf, bufsize); |
henryrawas | 17:0dbcbd8587fd | 119 | if (slen > 0) |
henryrawas | 17:0dbcbd8587fd | 120 | { |
henryrawas | 17:0dbcbd8587fd | 121 | bufsize -= slen; |
henryrawas | 17:0dbcbd8587fd | 122 | buf += slen; |
henryrawas | 17:0dbcbd8587fd | 123 | } |
henryrawas | 17:0dbcbd8587fd | 124 | else |
henryrawas | 17:0dbcbd8587fd | 125 | return -1; |
henryrawas | 17:0dbcbd8587fd | 126 | |
henryrawas | 17:0dbcbd8587fd | 127 | slen = MeasureGroupToString(msnap.Temps, buf, bufsize); |
henryrawas | 17:0dbcbd8587fd | 128 | if (slen > 0) |
henryrawas | 17:0dbcbd8587fd | 129 | { |
henryrawas | 17:0dbcbd8587fd | 130 | bufsize -= slen; |
henryrawas | 17:0dbcbd8587fd | 131 | buf += slen; |
henryrawas | 17:0dbcbd8587fd | 132 | } |
henryrawas | 17:0dbcbd8587fd | 133 | else |
henryrawas | 17:0dbcbd8587fd | 134 | return -1; |
henryrawas | 17:0dbcbd8587fd | 135 | |
henryrawas | 17:0dbcbd8587fd | 136 | slen = MeasureGroupToString(msnap.Positions, buf, bufsize); |
henryrawas | 17:0dbcbd8587fd | 137 | if (slen > 0) |
henryrawas | 17:0dbcbd8587fd | 138 | { |
henryrawas | 17:0dbcbd8587fd | 139 | bufsize -= slen; |
henryrawas | 17:0dbcbd8587fd | 140 | buf += slen; |
henryrawas | 17:0dbcbd8587fd | 141 | } |
henryrawas | 17:0dbcbd8587fd | 142 | else |
henryrawas | 17:0dbcbd8587fd | 143 | return -1; |
henryrawas | 17:0dbcbd8587fd | 144 | |
henryrawas | 17:0dbcbd8587fd | 145 | slen = MeasureGroupToString(msnap.Loads, buf, bufsize); |
henryrawas | 17:0dbcbd8587fd | 146 | if (slen > 0) |
henryrawas | 17:0dbcbd8587fd | 147 | { |
henryrawas | 17:0dbcbd8587fd | 148 | bufsize -= slen; |
henryrawas | 17:0dbcbd8587fd | 149 | buf += slen; |
henryrawas | 17:0dbcbd8587fd | 150 | } |
henryrawas | 17:0dbcbd8587fd | 151 | else |
henryrawas | 17:0dbcbd8587fd | 152 | return -1; |
henryrawas | 17:0dbcbd8587fd | 153 | |
henryrawas | 17:0dbcbd8587fd | 154 | slen = MeasureGroupToString(msnap.Volts, buf, bufsize); |
henryrawas | 17:0dbcbd8587fd | 155 | if (slen > 0) |
henryrawas | 17:0dbcbd8587fd | 156 | { |
henryrawas | 17:0dbcbd8587fd | 157 | bufsize -= slen; |
henryrawas | 17:0dbcbd8587fd | 158 | buf += slen; |
henryrawas | 17:0dbcbd8587fd | 159 | } |
henryrawas | 17:0dbcbd8587fd | 160 | else |
henryrawas | 17:0dbcbd8587fd | 161 | return -1; |
henryrawas | 17:0dbcbd8587fd | 162 | |
henryrawas | 17:0dbcbd8587fd | 163 | // replace final ',' with '}' |
henryrawas | 17:0dbcbd8587fd | 164 | *(buf - 1) = '}'; |
henryrawas | 17:0dbcbd8587fd | 165 | |
henryrawas | 17:0dbcbd8587fd | 166 | return startlen - bufsize; |
henryrawas | 17:0dbcbd8587fd | 167 | } |
henryrawas | 17:0dbcbd8587fd | 168 | |
henryrawas | 17:0dbcbd8587fd | 169 | // try to serialize one or more measurement snapshots into the buffer |
henryrawas | 13:ffeff9b5e513 | 170 | // return bytes used / -1 if buffer too small / 0 if no data |
henryrawas | 13:ffeff9b5e513 | 171 | // current serialization is a json array of objects with time and array per measure |
henryrawas | 13:ffeff9b5e513 | 172 | // eg: [{ "time": "2016-01-23T14:55:02", "temp": [1, 2], "volt": [12.1, 12.2] }] |
henryrawas | 4:36a4eceb1b7f | 173 | int IothubSerial::MeasureBufToString(char* buf, int bufsize) |
henryrawas | 4:36a4eceb1b7f | 174 | { |
henryrawas | 4:36a4eceb1b7f | 175 | int slen; |
henryrawas | 4:36a4eceb1b7f | 176 | bool hasdata = false; |
henryrawas | 4:36a4eceb1b7f | 177 | bool copydata = false; |
henryrawas | 7:6723f6887d00 | 178 | char* startbuf = buf; |
henryrawas | 7:6723f6887d00 | 179 | char* lastcomma = NULL; |
henryrawas | 7:6723f6887d00 | 180 | |
henryrawas | 17:0dbcbd8587fd | 181 | slen = sprintf_s(buf, bufsize, "["); |
henryrawas | 13:ffeff9b5e513 | 182 | if (slen > 0) |
henryrawas | 4:36a4eceb1b7f | 183 | { |
henryrawas | 13:ffeff9b5e513 | 184 | bufsize -= slen; |
henryrawas | 13:ffeff9b5e513 | 185 | buf += slen; |
henryrawas | 4:36a4eceb1b7f | 186 | } |
henryrawas | 4:36a4eceb1b7f | 187 | else |
henryrawas | 4:36a4eceb1b7f | 188 | return -1; |
henryrawas | 4:36a4eceb1b7f | 189 | |
henryrawas | 4:36a4eceb1b7f | 190 | if (_hasPending) |
henryrawas | 4:36a4eceb1b7f | 191 | { |
henryrawas | 4:36a4eceb1b7f | 192 | hasdata = true; |
henryrawas | 17:0dbcbd8587fd | 193 | slen = MeasureSnapshotToString(_pending, buf, bufsize); |
henryrawas | 7:6723f6887d00 | 194 | if (slen > 0) |
henryrawas | 7:6723f6887d00 | 195 | { |
henryrawas | 7:6723f6887d00 | 196 | bufsize -= slen; |
henryrawas | 7:6723f6887d00 | 197 | buf += slen; |
henryrawas | 7:6723f6887d00 | 198 | } |
henryrawas | 7:6723f6887d00 | 199 | else |
henryrawas | 7:6723f6887d00 | 200 | return -1; // no room for pending record |
henryrawas | 17:0dbcbd8587fd | 201 | // add comma |
henryrawas | 17:0dbcbd8587fd | 202 | slen = sprintf_s(buf, bufsize, ","); |
henryrawas | 4:36a4eceb1b7f | 203 | if (slen > 0) |
henryrawas | 4:36a4eceb1b7f | 204 | { |
henryrawas | 4:36a4eceb1b7f | 205 | bufsize -= slen; |
henryrawas | 4:36a4eceb1b7f | 206 | buf += slen; |
henryrawas | 4:36a4eceb1b7f | 207 | } |
henryrawas | 4:36a4eceb1b7f | 208 | else |
henryrawas | 17:0dbcbd8587fd | 209 | return -1; |
henryrawas | 17:0dbcbd8587fd | 210 | lastcomma = buf; |
henryrawas | 17:0dbcbd8587fd | 211 | _hasPending = false; |
henryrawas | 17:0dbcbd8587fd | 212 | copydata = true; |
henryrawas | 4:36a4eceb1b7f | 213 | } |
henryrawas | 4:36a4eceb1b7f | 214 | |
henryrawas | 4:36a4eceb1b7f | 215 | while (!MeasureBuf.empty()) |
henryrawas | 4:36a4eceb1b7f | 216 | { |
henryrawas | 4:36a4eceb1b7f | 217 | if (!MeasureBuf.pop(_pending)) |
henryrawas | 4:36a4eceb1b7f | 218 | { |
henryrawas | 4:36a4eceb1b7f | 219 | break; |
henryrawas | 4:36a4eceb1b7f | 220 | } |
henryrawas | 4:36a4eceb1b7f | 221 | hasdata = true; |
henryrawas | 4:36a4eceb1b7f | 222 | _hasPending = true; |
henryrawas | 7:6723f6887d00 | 223 | |
henryrawas | 17:0dbcbd8587fd | 224 | slen = MeasureSnapshotToString(_pending, buf, bufsize); |
henryrawas | 4:36a4eceb1b7f | 225 | if (slen > 0) |
henryrawas | 4:36a4eceb1b7f | 226 | { |
henryrawas | 4:36a4eceb1b7f | 227 | bufsize -= slen; |
henryrawas | 4:36a4eceb1b7f | 228 | buf += slen; |
henryrawas | 4:36a4eceb1b7f | 229 | } |
henryrawas | 4:36a4eceb1b7f | 230 | else |
henryrawas | 13:ffeff9b5e513 | 231 | break; // no room to serialize, leave pending for next message |
henryrawas | 17:0dbcbd8587fd | 232 | // add comma |
henryrawas | 17:0dbcbd8587fd | 233 | slen = sprintf_s(buf, bufsize, ","); |
henryrawas | 17:0dbcbd8587fd | 234 | if (slen > 0) |
henryrawas | 17:0dbcbd8587fd | 235 | { |
henryrawas | 17:0dbcbd8587fd | 236 | bufsize -= slen; |
henryrawas | 17:0dbcbd8587fd | 237 | buf += slen; |
henryrawas | 17:0dbcbd8587fd | 238 | } |
henryrawas | 17:0dbcbd8587fd | 239 | else |
henryrawas | 17:0dbcbd8587fd | 240 | break; |
henryrawas | 17:0dbcbd8587fd | 241 | _hasPending = false; |
henryrawas | 17:0dbcbd8587fd | 242 | lastcomma = buf; |
henryrawas | 17:0dbcbd8587fd | 243 | copydata = true; |
henryrawas | 4:36a4eceb1b7f | 244 | } |
henryrawas | 4:36a4eceb1b7f | 245 | |
henryrawas | 4:36a4eceb1b7f | 246 | if (!hasdata) |
henryrawas | 4:36a4eceb1b7f | 247 | return 0; // no data |
henryrawas | 4:36a4eceb1b7f | 248 | |
henryrawas | 4:36a4eceb1b7f | 249 | if (!copydata) |
henryrawas | 4:36a4eceb1b7f | 250 | return -1; // have data but buffer too small |
henryrawas | 4:36a4eceb1b7f | 251 | |
henryrawas | 17:0dbcbd8587fd | 252 | // replace final ',' with ']' |
henryrawas | 17:0dbcbd8587fd | 253 | *(lastcomma - 1) = ']'; |
henryrawas | 13:ffeff9b5e513 | 254 | |
henryrawas | 7:6723f6887d00 | 255 | return lastcomma - startbuf; |
henryrawas | 7:6723f6887d00 | 256 | } |
henryrawas | 7:6723f6887d00 | 257 | |
henryrawas | 7:6723f6887d00 | 258 | |
henryrawas | 7:6723f6887d00 | 259 | // try to serialize one or more alerts into the buffer |
henryrawas | 7:6723f6887d00 | 260 | // return bytes used or -1 if buffer too small or other error. 0 if no data |
henryrawas | 13:ffeff9b5e513 | 261 | // Serialize to a json object with time, measurename, value, message, and index of joint |
henryrawas | 13:ffeff9b5e513 | 262 | // eg: { "alerttype": "Temperature", "message": "too hot", "measurename", "temp", "index": 2, "value": 79.3, "time": "2016-01-23T14:55:02" } |
henryrawas | 7:6723f6887d00 | 263 | int IothubSerial::AlertBufToString(char* buf, int bufsize) |
henryrawas | 7:6723f6887d00 | 264 | { |
henryrawas | 7:6723f6887d00 | 265 | int slen; |
henryrawas | 7:6723f6887d00 | 266 | bool hasdata = false; |
henryrawas | 7:6723f6887d00 | 267 | bool copydata = false; |
henryrawas | 7:6723f6887d00 | 268 | int startlen = bufsize; |
henryrawas | 7:6723f6887d00 | 269 | |
henryrawas | 7:6723f6887d00 | 270 | if (AlertBuf.pop(_pendAlert)) |
henryrawas | 7:6723f6887d00 | 271 | { |
henryrawas | 13:ffeff9b5e513 | 272 | char tbuf[32]; |
henryrawas | 13:ffeff9b5e513 | 273 | (void)strftime(tbuf, 32, "%FT%T", localtime(&_pendAlert.Created)); |
henryrawas | 7:6723f6887d00 | 274 | hasdata = true; |
henryrawas | 13:ffeff9b5e513 | 275 | slen = sprintf_s(buf, bufsize, "{ \"alerttype\": \"%s\", \"message\": \"%s\", \"measurename\": \"%s\", \"index\": %d , \"value\": %f, \"time\": \"%s\" }", |
henryrawas | 13:ffeff9b5e513 | 276 | _pendAlert.AlertType, _pendAlert.Msg, _pendAlert.MeasureName, _pendAlert.Index, _pendAlert.Value, tbuf); |
henryrawas | 7:6723f6887d00 | 277 | if (slen > 0) |
henryrawas | 7:6723f6887d00 | 278 | { |
henryrawas | 7:6723f6887d00 | 279 | bufsize -= slen; |
henryrawas | 7:6723f6887d00 | 280 | buf += slen; |
henryrawas | 7:6723f6887d00 | 281 | copydata = true; |
henryrawas | 7:6723f6887d00 | 282 | } |
henryrawas | 7:6723f6887d00 | 283 | } |
henryrawas | 7:6723f6887d00 | 284 | |
henryrawas | 7:6723f6887d00 | 285 | if (!hasdata) |
henryrawas | 7:6723f6887d00 | 286 | return 0; // no data |
henryrawas | 7:6723f6887d00 | 287 | |
henryrawas | 7:6723f6887d00 | 288 | if (!copydata) |
henryrawas | 7:6723f6887d00 | 289 | return -1; // have data but buffer too small |
henryrawas | 7:6723f6887d00 | 290 | |
henryrawas | 4:36a4eceb1b7f | 291 | |
henryrawas | 4:36a4eceb1b7f | 292 | return startlen - bufsize; |
henryrawas | 4:36a4eceb1b7f | 293 | } |
henryrawas | 4:36a4eceb1b7f | 294 |