portable version of the cumulocity demo
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
TemperatureMeasurement.cpp@56:2da813cc2f47, 2014-10-03 (annotated)
- Committer:
- ublox
- Date:
- Fri Oct 03 07:54:13 2014 +0000
- Revision:
- 56:2da813cc2f47
- Parent:
- 41:804f6a0bda26
update dependency
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Cumulocity | 41:804f6a0bda26 | 1 | #include "TemperatureMeasurement.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 | TemperatureMeasurement::TemperatureMeasurement(SmartRest& client, SmartRestTemplate& tpl, long& deviceId, LM75B& 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 TemperatureMeasurement::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 | 41:804f6a0bda26 | 22 | // USAGE: 105,<DEVICE/ID>,<TEMPERATURE> |
Cumulocity | 41:804f6a0bda26 | 23 | if (!_tpl.add("10,105,POST,/measurement/measurements,application/vnd.com.nsn.cumulocity.measurement+json,application/vnd.com.nsn.cumulocity.measurement+json,%%,NOW UNSIGNED NUMBER,\"{\"\"time\"\":\"\"%%\"\",\"\"source\"\":{\"\"id\"\":\"\"%%\"\"},\"\"type\"\":\"\"c8y_TemperatureMeasurement\"\",\"\"c8y_TemperatureMeasurement\"\":{\"\"T\"\":{\"\"value\"\":%%,\"\"unit\"\":\"\"C\"\"}}}\"\r\n")) |
Cumulocity | 41:804f6a0bda26 | 24 | return false; |
Cumulocity | 41:804f6a0bda26 | 25 | |
Cumulocity | 41:804f6a0bda26 | 26 | _open = _sensor.open(); |
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 TemperatureMeasurement::run() |
Cumulocity | 41:804f6a0bda26 | 32 | { |
Cumulocity | 41:804f6a0bda26 | 33 | if (!_open) |
Cumulocity | 41:804f6a0bda26 | 34 | return false; |
Cumulocity | 41:804f6a0bda26 | 35 | |
Cumulocity | 41:804f6a0bda26 | 36 | ComposedRecord record; |
Cumulocity | 41:804f6a0bda26 | 37 | IntegerValue msgId(105); |
Cumulocity | 41:804f6a0bda26 | 38 | IntegerValue devId(_deviceId); |
Cumulocity | 41:804f6a0bda26 | 39 | FloatValue temperature(_sensor.temp(), 1); |
Cumulocity | 41:804f6a0bda26 | 40 | if ((!record.add(msgId)) || (!record.add(devId)) || (!record.add(temperature))) |
Cumulocity | 41:804f6a0bda26 | 41 | return false; |
Cumulocity | 41:804f6a0bda26 | 42 | |
Cumulocity | 41:804f6a0bda26 | 43 | if (_client.send(record) != SMARTREST_SUCCESS) { |
Cumulocity | 41:804f6a0bda26 | 44 | puts("Signal measurement failed."); |
Cumulocity | 41:804f6a0bda26 | 45 | _client.stop(); |
Cumulocity | 41:804f6a0bda26 | 46 | return false; |
Cumulocity | 41:804f6a0bda26 | 47 | } |
Cumulocity | 41:804f6a0bda26 | 48 | _client.stop(); |
Cumulocity | 41:804f6a0bda26 | 49 | return true; |
Cumulocity | 41:804f6a0bda26 | 50 | } |