portable version of the cumulocity demo
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
LocationUpdate.cpp@47:89ae46d5c466, 2014-07-24 (annotated)
- Committer:
- Cumulocity
- Date:
- Thu Jul 24 23:32:53 2014 +0000
- Revision:
- 47:89ae46d5c466
- Child:
- 49:ac0ba9d54ebc
major update
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Cumulocity | 47:89ae46d5c466 | 1 | #include "LocationUpdate.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 | LocationUpdate::LocationUpdate(SmartRest& client, SmartRestTemplate& tpl, long& deviceId, GPSTracker& gpsTracker) : |
Cumulocity | 47:89ae46d5c466 | 8 | _client(client), |
Cumulocity | 47:89ae46d5c466 | 9 | _tpl(tpl), |
Cumulocity | 47:89ae46d5c466 | 10 | _deviceId(deviceId), |
Cumulocity | 47:89ae46d5c466 | 11 | _gpsTracker(gpsTracker) |
Cumulocity | 47:89ae46d5c466 | 12 | { |
Cumulocity | 47:89ae46d5c466 | 13 | _init = false; |
Cumulocity | 47:89ae46d5c466 | 14 | } |
Cumulocity | 47:89ae46d5c466 | 15 | |
Cumulocity | 47:89ae46d5c466 | 16 | bool LocationUpdate::init() |
Cumulocity | 47:89ae46d5c466 | 17 | { |
Cumulocity | 47:89ae46d5c466 | 18 | if (_init) |
Cumulocity | 47:89ae46d5c466 | 19 | return false; |
Cumulocity | 47:89ae46d5c466 | 20 | |
Cumulocity | 47:89ae46d5c466 | 21 | // Insert measurement |
Cumulocity | 47:89ae46d5c466 | 22 | // USAGE: 108,<DEVICE/ID>,<ALTITUDE>,<LATITUDE>,<LONGITUDE> |
Cumulocity | 47:89ae46d5c466 | 23 | if (!_tpl.add("10,108,POST,/event/events,application/vnd.com.nsn.cumulocity.event+json,,%%,NOW UNSIGNED NUMBER NUMBER NUMBER,\"{\"\"time\"\":\"\"%%\"\",\"\"source\"\":{\"\"id\"\":\"\"%%\"\"},\"\"type\"\":\"\"c8y_LocationUpdate\"\",\"\"c8y_Position\"\":{\"\"alt\"\":%%,\"\"lat\"\":%%,\"\"lng\"\":%%}}\"")) |
Cumulocity | 47:89ae46d5c466 | 24 | return false; |
Cumulocity | 47:89ae46d5c466 | 25 | |
Cumulocity | 47:89ae46d5c466 | 26 | _init = true; |
Cumulocity | 47:89ae46d5c466 | 27 | return true; |
Cumulocity | 47:89ae46d5c466 | 28 | } |
Cumulocity | 47:89ae46d5c466 | 29 | |
Cumulocity | 47:89ae46d5c466 | 30 | bool LocationUpdate::run() |
Cumulocity | 47:89ae46d5c466 | 31 | { |
Cumulocity | 47:89ae46d5c466 | 32 | GPSTracker::Position position; |
Cumulocity | 47:89ae46d5c466 | 33 | |
Cumulocity | 47:89ae46d5c466 | 34 | if (!_gpsTracker.position(&position)) |
Cumulocity | 47:89ae46d5c466 | 35 | return true; |
Cumulocity | 47:89ae46d5c466 | 36 | |
Cumulocity | 47:89ae46d5c466 | 37 | puts("Starting measurement sending."); |
Cumulocity | 47:89ae46d5c466 | 38 | |
Cumulocity | 47:89ae46d5c466 | 39 | ComposedRecord record; |
Cumulocity | 47:89ae46d5c466 | 40 | IntegerValue msgId(108); |
Cumulocity | 47:89ae46d5c466 | 41 | IntegerValue devId(_deviceId); |
Cumulocity | 47:89ae46d5c466 | 42 | FloatValue altitude(position.altitude, 6); |
Cumulocity | 47:89ae46d5c466 | 43 | FloatValue latitude(position.latitude, 6); |
Cumulocity | 47:89ae46d5c466 | 44 | FloatValue longitude(position.longitude, 1); |
Cumulocity | 47:89ae46d5c466 | 45 | if ((!record.add(msgId)) || (!record.add(devId)) || (!record.add(altitude)) || (!record.add(latitude)) || (!record.add(longitude))) |
Cumulocity | 47:89ae46d5c466 | 46 | return false; |
Cumulocity | 47:89ae46d5c466 | 47 | |
Cumulocity | 47:89ae46d5c466 | 48 | if (_client.send(record) != SMARTREST_SUCCESS) { |
Cumulocity | 47:89ae46d5c466 | 49 | puts("Signal measurement failed."); |
Cumulocity | 47:89ae46d5c466 | 50 | _client.stop(); |
Cumulocity | 47:89ae46d5c466 | 51 | return false; |
Cumulocity | 47:89ae46d5c466 | 52 | } |
Cumulocity | 47:89ae46d5c466 | 53 | |
Cumulocity | 47:89ae46d5c466 | 54 | ParsedRecord recvd; |
Cumulocity | 47:89ae46d5c466 | 55 | if (_client.receive(recvd) != SMARTREST_SUCCESS) { |
Cumulocity | 47:89ae46d5c466 | 56 | puts("Nothing received."); |
Cumulocity | 47:89ae46d5c466 | 57 | _client.stop(); |
Cumulocity | 47:89ae46d5c466 | 58 | return true; |
Cumulocity | 47:89ae46d5c466 | 59 | } |
Cumulocity | 47:89ae46d5c466 | 60 | |
Cumulocity | 47:89ae46d5c466 | 61 | for (size_t n = 0; n < recvd.values(); n++) |
Cumulocity | 47:89ae46d5c466 | 62 | puts(recvd.rawValue(n)); |
Cumulocity | 47:89ae46d5c466 | 63 | |
Cumulocity | 47:89ae46d5c466 | 64 | _client.stop(); |
Cumulocity | 47:89ae46d5c466 | 65 | return true; |
Cumulocity | 47:89ae46d5c466 | 66 | } |