portable version of the cumulocity demo
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
AnalogMeasurement.cpp@47:89ae46d5c466, 2014-07-24 (annotated)
- Committer:
- Cumulocity
- Date:
- Thu Jul 24 23:32:53 2014 +0000
- Revision:
- 47:89ae46d5c466
major update
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Cumulocity | 47:89ae46d5c466 | 1 | #include "AnalogMeasurement.h" |
Cumulocity | 47:89ae46d5c466 | 2 | #include "ComposedRecord.h" |
Cumulocity | 47:89ae46d5c466 | 3 | #include "CharValue.h" |
Cumulocity | 47:89ae46d5c466 | 4 | #include "IntegerValue.h" |
Cumulocity | 47:89ae46d5c466 | 5 | #include "FloatValue.h" |
Cumulocity | 47:89ae46d5c466 | 6 | |
Cumulocity | 47:89ae46d5c466 | 7 | AnalogMeasurement::AnalogMeasurement(SmartRest& client, SmartRestTemplate& tpl, long& deviceId, AnalogIn& analog1, AnalogIn& analog2) : |
Cumulocity | 47:89ae46d5c466 | 8 | _client(client), |
Cumulocity | 47:89ae46d5c466 | 9 | _tpl(tpl), |
Cumulocity | 47:89ae46d5c466 | 10 | _deviceId(deviceId), |
Cumulocity | 47:89ae46d5c466 | 11 | _analog1(analog1), |
Cumulocity | 47:89ae46d5c466 | 12 | _analog2(analog2) |
Cumulocity | 47:89ae46d5c466 | 13 | { |
Cumulocity | 47:89ae46d5c466 | 14 | _init = false; |
Cumulocity | 47:89ae46d5c466 | 15 | } |
Cumulocity | 47:89ae46d5c466 | 16 | |
Cumulocity | 47:89ae46d5c466 | 17 | bool AnalogMeasurement::init() |
Cumulocity | 47:89ae46d5c466 | 18 | { |
Cumulocity | 47:89ae46d5c466 | 19 | if (_init) |
Cumulocity | 47:89ae46d5c466 | 20 | return false; |
Cumulocity | 47:89ae46d5c466 | 21 | |
Cumulocity | 47:89ae46d5c466 | 22 | // Insert measurement |
Cumulocity | 47:89ae46d5c466 | 23 | // USAGE: 107,<DEVICE/ID>,<ANALOG1>,<ANALOG2> |
Cumulocity | 47:89ae46d5c466 | 24 | if (!_tpl.add("10,107,POST,/measurement/measurements,application/vnd.com.nsn.cumulocity.measurement+json,application/vnd.com.nsn.cumulocity.measurement+json,%%,NOW UNSIGNED NUMBER NUMBER,\"{\"\"time\"\":\"\"%%\"\",\"\"source\"\":{\"\"id\"\":\"\"%%\"\"},\"\"type\"\":\"\"c8y_AnalogMeasurement\"\",\"\"c8y_AnalogMeasurement\"\":{\"\"A1\"\":{\"\"value\"\":%%,\"\"unit\"\":\"\"A\"\"},\"\"A2\"\":{\"\"value\"\":%%,\"\"unit\"\":\"\"A\"\"}}}\"\r\n")) |
Cumulocity | 47:89ae46d5c466 | 25 | return false; |
Cumulocity | 47:89ae46d5c466 | 26 | |
Cumulocity | 47:89ae46d5c466 | 27 | _init = true; |
Cumulocity | 47:89ae46d5c466 | 28 | return true; |
Cumulocity | 47:89ae46d5c466 | 29 | } |
Cumulocity | 47:89ae46d5c466 | 30 | |
Cumulocity | 47:89ae46d5c466 | 31 | bool AnalogMeasurement::run() |
Cumulocity | 47:89ae46d5c466 | 32 | { |
Cumulocity | 47:89ae46d5c466 | 33 | ComposedRecord record; |
Cumulocity | 47:89ae46d5c466 | 34 | IntegerValue msgId(107); |
Cumulocity | 47:89ae46d5c466 | 35 | IntegerValue devId(_deviceId); |
Cumulocity | 47:89ae46d5c466 | 36 | FloatValue analog1(_analog1.read(), 1); |
Cumulocity | 47:89ae46d5c466 | 37 | FloatValue analog2(_analog2.read(), 1); |
Cumulocity | 47:89ae46d5c466 | 38 | if ((!record.add(msgId)) || (!record.add(devId)) || (!record.add(analog1)) || (!record.add(analog2))) |
Cumulocity | 47:89ae46d5c466 | 39 | return false; |
Cumulocity | 47:89ae46d5c466 | 40 | |
Cumulocity | 47:89ae46d5c466 | 41 | if (_client.send(record) != SMARTREST_SUCCESS) { |
Cumulocity | 47:89ae46d5c466 | 42 | puts("Signal measurement failed."); |
Cumulocity | 47:89ae46d5c466 | 43 | _client.stop(); |
Cumulocity | 47:89ae46d5c466 | 44 | return false; |
Cumulocity | 47:89ae46d5c466 | 45 | } |
Cumulocity | 47:89ae46d5c466 | 46 | _client.stop(); |
Cumulocity | 47:89ae46d5c466 | 47 | return true; |
Cumulocity | 47:89ae46d5c466 | 48 | } |