portable version of the cumulocity demo
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
AccelerationMeasurement.cpp@56:2da813cc2f47, 2014-10-03 (annotated)
- Committer:
- ublox
- Date:
- Fri Oct 03 07:54:13 2014 +0000
- Revision:
- 56:2da813cc2f47
- Parent:
- 47:89ae46d5c466
update dependency
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Cumulocity | 41:804f6a0bda26 | 1 | #include "AccelerationMeasurement.h" |
Cumulocity | 41:804f6a0bda26 | 2 | #include "ComposedRecord.h" |
Cumulocity | 41:804f6a0bda26 | 3 | #include "CharValue.h" |
Cumulocity | 41:804f6a0bda26 | 4 | #include "IntegerValue.h" |
Cumulocity | 41:804f6a0bda26 | 5 | #include "FloatValue.h" |
Cumulocity | 41:804f6a0bda26 | 6 | |
Cumulocity | 41:804f6a0bda26 | 7 | AccelerationMeasurement::AccelerationMeasurement(SmartRest& client, SmartRestTemplate& tpl, long& deviceId, MMA7660& sensor) : |
Cumulocity | 41:804f6a0bda26 | 8 | _client(client), |
Cumulocity | 41:804f6a0bda26 | 9 | _tpl(tpl), |
Cumulocity | 41:804f6a0bda26 | 10 | _deviceId(deviceId), |
Cumulocity | 41:804f6a0bda26 | 11 | _sensor(sensor) |
Cumulocity | 41:804f6a0bda26 | 12 | { |
Cumulocity | 41:804f6a0bda26 | 13 | _init = false; |
Cumulocity | 41:804f6a0bda26 | 14 | } |
Cumulocity | 41:804f6a0bda26 | 15 | |
Cumulocity | 41:804f6a0bda26 | 16 | bool AccelerationMeasurement::init() |
Cumulocity | 41:804f6a0bda26 | 17 | { |
Cumulocity | 41:804f6a0bda26 | 18 | if (_init) |
Cumulocity | 41:804f6a0bda26 | 19 | return false; |
Cumulocity | 41:804f6a0bda26 | 20 | |
Cumulocity | 41:804f6a0bda26 | 21 | // Insert measurement |
Cumulocity | 47:89ae46d5c466 | 22 | // USAGE: 106,<DEVICE/ID>,<X>,<Y>,<Z> |
Cumulocity | 41:804f6a0bda26 | 23 | if (!_tpl.add("10,106,POST,/measurement/measurements,application/vnd.com.nsn.cumulocity.measurement+json,application/vnd.com.nsn.cumulocity.measurement+json,%%,NOW UNSIGNED NUMBER NUMBER NUMBER,\"{\"\"time\"\":\"\"%%\"\",\"\"source\"\":{\"\"id\"\":\"\"%%\"\"},\"\"type\"\":\"\"c8y_MotionMeasurement\"\",\"\"c8y_MotionMeasurement\"\":{\"\"x\"\":{\"\"value\"\":%%,\"\"unit\"\":\"\"m^2/s\"\"},\"\"y\"\":{\"\"value\"\":%%,\"\"unit\"\":\"\"m^2/s\"\"},\"\"z\"\":{\"\"value\"\":%%,\"\"unit\"\":\"\"m^2/s\"\"}}}\"\r\n")) |
Cumulocity | 41:804f6a0bda26 | 24 | return false; |
Cumulocity | 41:804f6a0bda26 | 25 | |
Cumulocity | 41:804f6a0bda26 | 26 | _test = _sensor.testConnection(); |
Cumulocity | 41:804f6a0bda26 | 27 | _init = true; |
Cumulocity | 41:804f6a0bda26 | 28 | return true; |
Cumulocity | 41:804f6a0bda26 | 29 | } |
Cumulocity | 41:804f6a0bda26 | 30 | |
Cumulocity | 41:804f6a0bda26 | 31 | bool AccelerationMeasurement::run() |
Cumulocity | 41:804f6a0bda26 | 32 | { |
Cumulocity | 41:804f6a0bda26 | 33 | float data[3] = { 0.0, 0.0, 0.0 }; |
Cumulocity | 41:804f6a0bda26 | 34 | |
Cumulocity | 41:804f6a0bda26 | 35 | if (!_test) |
Cumulocity | 41:804f6a0bda26 | 36 | return false; |
Cumulocity | 41:804f6a0bda26 | 37 | |
Cumulocity | 41:804f6a0bda26 | 38 | _sensor.readData(data); |
Cumulocity | 41:804f6a0bda26 | 39 | |
Cumulocity | 41:804f6a0bda26 | 40 | ComposedRecord record; |
Cumulocity | 41:804f6a0bda26 | 41 | IntegerValue msgId(106); |
Cumulocity | 41:804f6a0bda26 | 42 | IntegerValue devId(_deviceId); |
Cumulocity | 41:804f6a0bda26 | 43 | FloatValue xValue(data[0], 2); |
Cumulocity | 41:804f6a0bda26 | 44 | FloatValue yValue(data[1], 2); |
Cumulocity | 41:804f6a0bda26 | 45 | FloatValue zValue(data[2], 2); |
Cumulocity | 41:804f6a0bda26 | 46 | if ((!record.add(msgId)) || (!record.add(devId)) || (!record.add(xValue)) || (!record.add(yValue)) || (!record.add(zValue))) |
Cumulocity | 41:804f6a0bda26 | 47 | return false; |
Cumulocity | 41:804f6a0bda26 | 48 | |
Cumulocity | 41:804f6a0bda26 | 49 | if (_client.send(record) != SMARTREST_SUCCESS) { |
Cumulocity | 41:804f6a0bda26 | 50 | puts("Signal measurement failed."); |
Cumulocity | 41:804f6a0bda26 | 51 | _client.stop(); |
Cumulocity | 41:804f6a0bda26 | 52 | return false; |
Cumulocity | 41:804f6a0bda26 | 53 | } |
Cumulocity | 41:804f6a0bda26 | 54 | _client.stop(); |
Cumulocity | 41:804f6a0bda26 | 55 | return true; |
Cumulocity | 41:804f6a0bda26 | 56 | } |