Official reference client implementation for Cumulocity SmartREST on u-blox C027.
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
measurement/LocationUpdate.cpp@71:063c45e99578, 2015-02-16 (annotated)
- Committer:
- xinlei
- Date:
- Mon Feb 16 09:17:30 2015 +0000
- Revision:
- 71:063c45e99578
- Parent:
- 58:4cc0ae5a7058
- Child:
- 72:c5709ae7b193
Various minor bug fixes
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Cumulocity | 47:89ae46d5c466 | 1 | #include "LocationUpdate.h" |
Cumulocity | 49:ac0ba9d54ebc | 2 | #include "Aggregator.h" |
Cumulocity | 47:89ae46d5c466 | 3 | #include "ComposedRecord.h" |
Cumulocity | 47:89ae46d5c466 | 4 | #include "CharValue.h" |
Cumulocity | 47:89ae46d5c466 | 5 | #include "IntegerValue.h" |
Cumulocity | 47:89ae46d5c466 | 6 | #include "FloatValue.h" |
Cumulocity | 47:89ae46d5c466 | 7 | |
xinlei | 71:063c45e99578 | 8 | #define THRESHOLD_PERCENT_LOC 0.05 // Percentage cut-off for avoiding sending similar acceleration sensor data. |
xinlei | 71:063c45e99578 | 9 | #define TIME_LIMIT_LOC 900 // Time interval for forcing a sending even if acceleration sensor readings are constantly similar. |
xinlei | 71:063c45e99578 | 10 | |
vwochnik | 55:a0f7295ed6b6 | 11 | LocationUpdate::LocationUpdate(AbstractSmartRest& client, SmartRestTemplate& tpl, long& deviceId, GPSTracker& gpsTracker) : |
Cumulocity | 47:89ae46d5c466 | 12 | _client(client), |
Cumulocity | 47:89ae46d5c466 | 13 | _tpl(tpl), |
Cumulocity | 47:89ae46d5c466 | 14 | _deviceId(deviceId), |
Cumulocity | 47:89ae46d5c466 | 15 | _gpsTracker(gpsTracker) |
Cumulocity | 47:89ae46d5c466 | 16 | { |
Cumulocity | 47:89ae46d5c466 | 17 | _init = false; |
xinlei | 71:063c45e99578 | 18 | oldValues[0] = 0; |
xinlei | 71:063c45e99578 | 19 | oldValues[1] = 0; |
xinlei | 71:063c45e99578 | 20 | oldValues[2] = 0; |
xinlei | 71:063c45e99578 | 21 | sendingTimer.start(); |
Cumulocity | 47:89ae46d5c466 | 22 | } |
Cumulocity | 47:89ae46d5c466 | 23 | |
Cumulocity | 47:89ae46d5c466 | 24 | bool LocationUpdate::init() |
Cumulocity | 47:89ae46d5c466 | 25 | { |
Cumulocity | 47:89ae46d5c466 | 26 | if (_init) |
Cumulocity | 47:89ae46d5c466 | 27 | return false; |
Cumulocity | 47:89ae46d5c466 | 28 | |
Cumulocity | 49:ac0ba9d54ebc | 29 | // Update device position |
Cumulocity | 47:89ae46d5c466 | 30 | // USAGE: 108,<DEVICE/ID>,<ALTITUDE>,<LATITUDE>,<LONGITUDE> |
Cumulocity | 49:ac0ba9d54ebc | 31 | if (!_tpl.add("10,108,PUT,/inventory/managedObjects/%%,application/vnd.com.nsn.cumulocity.managedObject+json,application/vnd.com.nsn.cumulocity.managedObject+json,%%,UNSIGNED NUMBER NUMBER NUMBER,\"{\"\"c8y_Position\"\":{\"\"alt\"\":%%,\"\"lat\"\":%%,\"\"lng\"\":%%},\"\"c8y_MotionTracking\"\":{\"\"active\"\":true}}\"\r\n")) |
Cumulocity | 49:ac0ba9d54ebc | 32 | return false; |
Cumulocity | 49:ac0ba9d54ebc | 33 | |
Cumulocity | 49:ac0ba9d54ebc | 34 | // Insert measurement |
Cumulocity | 49:ac0ba9d54ebc | 35 | // USAGE: 109,<DEVICE/ID>,<ALTITUDE>,<LATITUDE>,<LONGITUDE> |
vwochnik | 58:4cc0ae5a7058 | 36 | if (!_tpl.add("10,109,POST,/event/events,application/vnd.com.nsn.cumulocity.event+json,application/vnd.com.nsn.cumulocity.event+json,%%,NOW UNSIGNED NUMBER NUMBER NUMBER,\"{\"\"time\"\":\"\"%%\"\",\"\"source\"\":{\"\"id\"\":\"\"%%\"\"},\"\"type\"\":\"\"c8y_LocationUpdate\"\",\"\"text\"\":\"\"Mbed location update\"\",\"\"c8y_Position\"\":{\"\"alt\"\":%%,\"\"lat\"\":%%,\"\"lng\"\":%%}}\"\r\n")) |
Cumulocity | 47:89ae46d5c466 | 37 | return false; |
Cumulocity | 47:89ae46d5c466 | 38 | |
Cumulocity | 47:89ae46d5c466 | 39 | _init = true; |
Cumulocity | 47:89ae46d5c466 | 40 | return true; |
Cumulocity | 47:89ae46d5c466 | 41 | } |
Cumulocity | 47:89ae46d5c466 | 42 | |
Cumulocity | 47:89ae46d5c466 | 43 | bool LocationUpdate::run() |
Cumulocity | 47:89ae46d5c466 | 44 | { |
Cumulocity | 47:89ae46d5c466 | 45 | GPSTracker::Position position; |
Cumulocity | 47:89ae46d5c466 | 46 | |
Cumulocity | 49:ac0ba9d54ebc | 47 | if (!_gpsTracker.position(&position)) { |
Cumulocity | 49:ac0ba9d54ebc | 48 | puts("No GPS data available."); |
Cumulocity | 47:89ae46d5c466 | 49 | return true; |
Cumulocity | 49:ac0ba9d54ebc | 50 | } |
xinlei | 71:063c45e99578 | 51 | float data[3] = { 0.0, 0.0, 0.0 }; |
xinlei | 71:063c45e99578 | 52 | data[0] = position.altitude; |
xinlei | 71:063c45e99578 | 53 | data[1] = position.latitude; |
xinlei | 71:063c45e99578 | 54 | data[2] = position.longitude; |
xinlei | 71:063c45e99578 | 55 | if (abs(oldValues[0]-data[0]) <= abs(oldValues[0])*THRESHOLD_PERCENT_LOC && |
xinlei | 71:063c45e99578 | 56 | abs(oldValues[1]-data[1]) <= abs(oldValues[1])*THRESHOLD_PERCENT_LOC && |
xinlei | 71:063c45e99578 | 57 | abs(oldValues[2]-data[2]) <= abs(oldValues[2])*THRESHOLD_PERCENT_LOC) { |
xinlei | 71:063c45e99578 | 58 | if (sendingTimer.read() < TIME_LIMIT_LOC) { |
xinlei | 71:063c45e99578 | 59 | printf("Similar location readings found, no sending!\r\n"); |
xinlei | 71:063c45e99578 | 60 | return true; |
xinlei | 71:063c45e99578 | 61 | } else { |
xinlei | 71:063c45e99578 | 62 | printf("Sending timer of location sensor timed out at %f s, a sending is forced.\r\n", sendingTimer.read()); |
xinlei | 71:063c45e99578 | 63 | } |
xinlei | 71:063c45e99578 | 64 | } |
Cumulocity | 47:89ae46d5c466 | 65 | |
Cumulocity | 47:89ae46d5c466 | 66 | puts("Starting measurement sending."); |
Cumulocity | 49:ac0ba9d54ebc | 67 | Aggregator aggregator; |
Cumulocity | 49:ac0ba9d54ebc | 68 | ComposedRecord record1, record2; |
Cumulocity | 49:ac0ba9d54ebc | 69 | IntegerValue msgId1(108); |
Cumulocity | 49:ac0ba9d54ebc | 70 | IntegerValue msgId2(109); |
Cumulocity | 47:89ae46d5c466 | 71 | IntegerValue devId(_deviceId); |
Cumulocity | 49:ac0ba9d54ebc | 72 | FloatValue altitude(position.altitude, 2); |
Cumulocity | 47:89ae46d5c466 | 73 | FloatValue latitude(position.latitude, 6); |
Cumulocity | 49:ac0ba9d54ebc | 74 | FloatValue longitude(position.longitude, 6); |
Cumulocity | 49:ac0ba9d54ebc | 75 | if ((!record1.add(msgId1)) || (!record1.add(devId)) || (!record1.add(altitude)) || (!record1.add(latitude)) || (!record1.add(longitude))) |
Cumulocity | 49:ac0ba9d54ebc | 76 | return false; |
Cumulocity | 49:ac0ba9d54ebc | 77 | if ((!record2.add(msgId2)) || (!record2.add(devId)) || (!record2.add(altitude)) || (!record2.add(latitude)) || (!record2.add(longitude))) |
Cumulocity | 49:ac0ba9d54ebc | 78 | return false; |
Cumulocity | 49:ac0ba9d54ebc | 79 | if ((!aggregator.add(record1)) || (!aggregator.add(record2))) |
Cumulocity | 47:89ae46d5c466 | 80 | return false; |
Cumulocity | 47:89ae46d5c466 | 81 | |
Cumulocity | 49:ac0ba9d54ebc | 82 | puts("Sending GPS measurement."); |
Cumulocity | 49:ac0ba9d54ebc | 83 | if (_client.send(aggregator) != SMARTREST_SUCCESS) { |
Cumulocity | 47:89ae46d5c466 | 84 | puts("Signal measurement failed."); |
Cumulocity | 47:89ae46d5c466 | 85 | _client.stop(); |
Cumulocity | 47:89ae46d5c466 | 86 | return false; |
Cumulocity | 47:89ae46d5c466 | 87 | } |
Cumulocity | 49:ac0ba9d54ebc | 88 | |
Cumulocity | 47:89ae46d5c466 | 89 | _client.stop(); |
xinlei | 71:063c45e99578 | 90 | oldValues[0] = data[0]; |
xinlei | 71:063c45e99578 | 91 | oldValues[1] = data[1]; |
xinlei | 71:063c45e99578 | 92 | oldValues[2] = data[2]; |
xinlei | 71:063c45e99578 | 93 | sendingTimer.reset(); |
xinlei | 71:063c45e99578 | 94 | printf("Location readings sent.\r\n"); |
Cumulocity | 47:89ae46d5c466 | 95 | return true; |
Cumulocity | 47:89ae46d5c466 | 96 | } |