demo project

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

Committer:
henryrawas
Date:
Fri Jan 15 22:02:46 2016 +0000
Revision:
13:ffeff9b5e513
Parent:
8:d98e2dec0f40
Child:
17:0dbcbd8587fd
Always test status and send data

Who changed what in which revision?

UserRevisionLine numberNew 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 4:36a4eceb1b7f 99 // try to serialize one or more measurements into the buffer
henryrawas 13:ffeff9b5e513 100 // return bytes used / -1 if buffer too small / 0 if no data
henryrawas 13:ffeff9b5e513 101 // current serialization is a json array of objects with time and array per measure
henryrawas 13:ffeff9b5e513 102 // eg: [{ "time": "2016-01-23T14:55:02", "temp": [1, 2], "volt": [12.1, 12.2] }]
henryrawas 4:36a4eceb1b7f 103 int IothubSerial::MeasureBufToString(char* buf, int bufsize)
henryrawas 4:36a4eceb1b7f 104 {
henryrawas 4:36a4eceb1b7f 105 int slen;
henryrawas 4:36a4eceb1b7f 106 bool hasdata = false;
henryrawas 4:36a4eceb1b7f 107 bool copydata = false;
henryrawas 7:6723f6887d00 108 char* startbuf = buf;
henryrawas 7:6723f6887d00 109 bool settime = false;
henryrawas 7:6723f6887d00 110 char* lastcomma = NULL;
henryrawas 7:6723f6887d00 111
henryrawas 7:6723f6887d00 112 time_t secs = 0;
henryrawas 13:ffeff9b5e513 113 // reserve 1 space for end
henryrawas 13:ffeff9b5e513 114 bufsize--;
henryrawas 4:36a4eceb1b7f 115
henryrawas 13:ffeff9b5e513 116 slen = sprintf_s(buf, bufsize, "[{");
henryrawas 13:ffeff9b5e513 117 if (slen > 0)
henryrawas 4:36a4eceb1b7f 118 {
henryrawas 13:ffeff9b5e513 119 bufsize -= slen;
henryrawas 13:ffeff9b5e513 120 buf += slen;
henryrawas 4:36a4eceb1b7f 121 }
henryrawas 4:36a4eceb1b7f 122 else
henryrawas 4:36a4eceb1b7f 123 return -1;
henryrawas 4:36a4eceb1b7f 124
henryrawas 4:36a4eceb1b7f 125 if (_hasPending)
henryrawas 4:36a4eceb1b7f 126 {
henryrawas 4:36a4eceb1b7f 127 hasdata = true;
henryrawas 7:6723f6887d00 128 secs = _pending.Created;
henryrawas 7:6723f6887d00 129 slen = AddTime(secs, buf, bufsize);
henryrawas 7:6723f6887d00 130 if (slen > 0)
henryrawas 7:6723f6887d00 131 {
henryrawas 7:6723f6887d00 132 bufsize -= slen;
henryrawas 7:6723f6887d00 133 buf += slen;
henryrawas 7:6723f6887d00 134 settime = true;
henryrawas 7:6723f6887d00 135 }
henryrawas 7:6723f6887d00 136 else
henryrawas 7:6723f6887d00 137 return -1; // no room for pending record
henryrawas 7:6723f6887d00 138
henryrawas 4:36a4eceb1b7f 139 slen = MeasureGroupToString(_pending, buf, bufsize);
henryrawas 4:36a4eceb1b7f 140 if (slen > 0)
henryrawas 4:36a4eceb1b7f 141 {
henryrawas 4:36a4eceb1b7f 142 bufsize -= slen;
henryrawas 4:36a4eceb1b7f 143 buf += slen;
henryrawas 7:6723f6887d00 144 lastcomma = buf;
henryrawas 4:36a4eceb1b7f 145 _hasPending = false;
henryrawas 4:36a4eceb1b7f 146 copydata = true;
henryrawas 4:36a4eceb1b7f 147 }
henryrawas 4:36a4eceb1b7f 148 else
henryrawas 4:36a4eceb1b7f 149 return -1; // no room for pending record
henryrawas 4:36a4eceb1b7f 150 }
henryrawas 4:36a4eceb1b7f 151
henryrawas 4:36a4eceb1b7f 152 while (!MeasureBuf.empty())
henryrawas 4:36a4eceb1b7f 153 {
henryrawas 4:36a4eceb1b7f 154 if (!MeasureBuf.pop(_pending))
henryrawas 4:36a4eceb1b7f 155 {
henryrawas 4:36a4eceb1b7f 156 break;
henryrawas 4:36a4eceb1b7f 157 }
henryrawas 4:36a4eceb1b7f 158 hasdata = true;
henryrawas 4:36a4eceb1b7f 159 _hasPending = true;
henryrawas 7:6723f6887d00 160
henryrawas 7:6723f6887d00 161 if (secs != _pending.Created)
henryrawas 7:6723f6887d00 162 {
henryrawas 7:6723f6887d00 163 if (settime && lastcomma != NULL)
henryrawas 7:6723f6887d00 164 {
henryrawas 7:6723f6887d00 165 *(lastcomma - 1) = '}';
henryrawas 7:6723f6887d00 166 if (bufsize > 2)
henryrawas 7:6723f6887d00 167 {
henryrawas 7:6723f6887d00 168 strcpy(buf, ",{");
henryrawas 7:6723f6887d00 169 buf += 2;
henryrawas 7:6723f6887d00 170 bufsize -= 2;
henryrawas 7:6723f6887d00 171 }
henryrawas 7:6723f6887d00 172 }
henryrawas 7:6723f6887d00 173 secs = _pending.Created;
henryrawas 7:6723f6887d00 174 slen = AddTime(secs, buf, bufsize);
henryrawas 7:6723f6887d00 175 if (slen > 0)
henryrawas 7:6723f6887d00 176 {
henryrawas 7:6723f6887d00 177 bufsize -= slen;
henryrawas 7:6723f6887d00 178 buf += slen;
henryrawas 7:6723f6887d00 179 settime = true;
henryrawas 7:6723f6887d00 180 }
henryrawas 7:6723f6887d00 181 else
henryrawas 7:6723f6887d00 182 break;
henryrawas 7:6723f6887d00 183 }
henryrawas 7:6723f6887d00 184
henryrawas 4:36a4eceb1b7f 185 slen = MeasureGroupToString(_pending, buf, bufsize);
henryrawas 4:36a4eceb1b7f 186 if (slen > 0)
henryrawas 4:36a4eceb1b7f 187 {
henryrawas 4:36a4eceb1b7f 188 bufsize -= slen;
henryrawas 4:36a4eceb1b7f 189 buf += slen;
henryrawas 7:6723f6887d00 190 lastcomma = buf;
henryrawas 4:36a4eceb1b7f 191 _hasPending = false;
henryrawas 4:36a4eceb1b7f 192 copydata = true;
henryrawas 4:36a4eceb1b7f 193 }
henryrawas 4:36a4eceb1b7f 194 else
henryrawas 13:ffeff9b5e513 195 break; // no room to serialize, leave pending for next message
henryrawas 4:36a4eceb1b7f 196 }
henryrawas 4:36a4eceb1b7f 197
henryrawas 4:36a4eceb1b7f 198 if (!hasdata)
henryrawas 4:36a4eceb1b7f 199 return 0; // no data
henryrawas 4:36a4eceb1b7f 200
henryrawas 4:36a4eceb1b7f 201 if (!copydata)
henryrawas 4:36a4eceb1b7f 202 return -1; // have data but buffer too small
henryrawas 4:36a4eceb1b7f 203
henryrawas 4:36a4eceb1b7f 204 // replace final ',' with '}'
henryrawas 7:6723f6887d00 205 *(lastcomma - 1) = '}';
henryrawas 13:ffeff9b5e513 206 // this is the extra space we reserved at the start.
henryrawas 13:ffeff9b5e513 207 *(lastcomma) = ']';
henryrawas 13:ffeff9b5e513 208 lastcomma++;
henryrawas 13:ffeff9b5e513 209
henryrawas 7:6723f6887d00 210 return lastcomma - startbuf;
henryrawas 7:6723f6887d00 211 }
henryrawas 7:6723f6887d00 212
henryrawas 7:6723f6887d00 213
henryrawas 7:6723f6887d00 214 // try to serialize one or more alerts into the buffer
henryrawas 7:6723f6887d00 215 // return bytes used or -1 if buffer too small or other error. 0 if no data
henryrawas 13:ffeff9b5e513 216 // Serialize to a json object with time, measurename, value, message, and index of joint
henryrawas 13:ffeff9b5e513 217 // eg: { "alerttype": "Temperature", "message": "too hot", "measurename", "temp", "index": 2, "value": 79.3, "time": "2016-01-23T14:55:02" }
henryrawas 7:6723f6887d00 218 int IothubSerial::AlertBufToString(char* buf, int bufsize)
henryrawas 7:6723f6887d00 219 {
henryrawas 7:6723f6887d00 220 int slen;
henryrawas 7:6723f6887d00 221 bool hasdata = false;
henryrawas 7:6723f6887d00 222 bool copydata = false;
henryrawas 7:6723f6887d00 223 int startlen = bufsize;
henryrawas 7:6723f6887d00 224
henryrawas 7:6723f6887d00 225 if (AlertBuf.pop(_pendAlert))
henryrawas 7:6723f6887d00 226 {
henryrawas 13:ffeff9b5e513 227 char tbuf[32];
henryrawas 13:ffeff9b5e513 228 (void)strftime(tbuf, 32, "%FT%T", localtime(&_pendAlert.Created));
henryrawas 7:6723f6887d00 229 hasdata = true;
henryrawas 13:ffeff9b5e513 230 slen = sprintf_s(buf, bufsize, "{ \"alerttype\": \"%s\", \"message\": \"%s\", \"measurename\": \"%s\", \"index\": %d , \"value\": %f, \"time\": \"%s\" }",
henryrawas 13:ffeff9b5e513 231 _pendAlert.AlertType, _pendAlert.Msg, _pendAlert.MeasureName, _pendAlert.Index, _pendAlert.Value, tbuf);
henryrawas 7:6723f6887d00 232 if (slen > 0)
henryrawas 7:6723f6887d00 233 {
henryrawas 7:6723f6887d00 234 bufsize -= slen;
henryrawas 7:6723f6887d00 235 buf += slen;
henryrawas 7:6723f6887d00 236 copydata = true;
henryrawas 7:6723f6887d00 237 }
henryrawas 7:6723f6887d00 238 }
henryrawas 7:6723f6887d00 239
henryrawas 7:6723f6887d00 240 if (!hasdata)
henryrawas 7:6723f6887d00 241 return 0; // no data
henryrawas 7:6723f6887d00 242
henryrawas 7:6723f6887d00 243 if (!copydata)
henryrawas 7:6723f6887d00 244 return -1; // have data but buffer too small
henryrawas 7:6723f6887d00 245
henryrawas 4:36a4eceb1b7f 246
henryrawas 4:36a4eceb1b7f 247 return startlen - bufsize;
henryrawas 4:36a4eceb1b7f 248 }
henryrawas 4:36a4eceb1b7f 249
henryrawas 4:36a4eceb1b7f 250 bool IothubSerial::HasMeasureGroup()
henryrawas 4:36a4eceb1b7f 251 {
henryrawas 4:36a4eceb1b7f 252 return _hasPending;
henryrawas 4:36a4eceb1b7f 253 }