
Own fork of MbedSmartRestMain
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
measurement/LocationUpdate.cpp@73:313975bfec96, 2015-02-17 (annotated)
- Committer:
- xinlei
- Date:
- Tue Feb 17 16:31:30 2015 +0000
- Revision:
- 73:313975bfec96
- Parent:
- 72:c5709ae7b193
- Child:
- 74:ca3001991fdc
measurements: displaying concrete information on LCD display, timing of sending operation.
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" |
xinlei | 72:c5709ae7b193 | 7 | #include "logging.h" |
Cumulocity | 47:89ae46d5c466 | 8 | |
xinlei | 71:063c45e99578 | 9 | #define THRESHOLD_PERCENT_LOC 0.05 // Percentage cut-off for avoiding sending similar acceleration sensor data. |
xinlei | 73:313975bfec96 | 10 | // Time interval for forcing a sending even if analog sensor readings are constantly similar (in seconds). |
xinlei | 73:313975bfec96 | 11 | #define TIME_LIMIT_LOC 900 |
xinlei | 71:063c45e99578 | 12 | |
xinlei | 73:313975bfec96 | 13 | LocationUpdate::LocationUpdate(AbstractSmartRest& client, SmartRestTemplate& tpl, long& deviceId, GPSTracker& gpsTracker, |
xinlei | 73:313975bfec96 | 14 | DeviceIO& io, DeviceInfo& deviceInfo, DeviceBootstrap& bootstrap) : |
Cumulocity | 47:89ae46d5c466 | 15 | _client(client), |
Cumulocity | 47:89ae46d5c466 | 16 | _tpl(tpl), |
Cumulocity | 47:89ae46d5c466 | 17 | _deviceId(deviceId), |
xinlei | 73:313975bfec96 | 18 | _gpsTracker(gpsTracker), |
xinlei | 73:313975bfec96 | 19 | _io(io), |
xinlei | 73:313975bfec96 | 20 | _deviceInfo(deviceInfo), |
xinlei | 73:313975bfec96 | 21 | _bootstrap(bootstrap) |
Cumulocity | 47:89ae46d5c466 | 22 | { |
Cumulocity | 47:89ae46d5c466 | 23 | _init = false; |
xinlei | 71:063c45e99578 | 24 | oldValues[0] = 0; |
xinlei | 71:063c45e99578 | 25 | oldValues[1] = 0; |
xinlei | 71:063c45e99578 | 26 | oldValues[2] = 0; |
xinlei | 71:063c45e99578 | 27 | sendingTimer.start(); |
Cumulocity | 47:89ae46d5c466 | 28 | } |
Cumulocity | 47:89ae46d5c466 | 29 | |
Cumulocity | 47:89ae46d5c466 | 30 | bool LocationUpdate::init() |
Cumulocity | 47:89ae46d5c466 | 31 | { |
Cumulocity | 47:89ae46d5c466 | 32 | if (_init) |
Cumulocity | 47:89ae46d5c466 | 33 | return false; |
Cumulocity | 47:89ae46d5c466 | 34 | |
Cumulocity | 49:ac0ba9d54ebc | 35 | // Update device position |
Cumulocity | 47:89ae46d5c466 | 36 | // USAGE: 108,<DEVICE/ID>,<ALTITUDE>,<LATITUDE>,<LONGITUDE> |
Cumulocity | 49:ac0ba9d54ebc | 37 | 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 | 38 | return false; |
Cumulocity | 49:ac0ba9d54ebc | 39 | |
Cumulocity | 49:ac0ba9d54ebc | 40 | // Insert measurement |
Cumulocity | 49:ac0ba9d54ebc | 41 | // USAGE: 109,<DEVICE/ID>,<ALTITUDE>,<LATITUDE>,<LONGITUDE> |
vwochnik | 58:4cc0ae5a7058 | 42 | 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 | 43 | return false; |
Cumulocity | 47:89ae46d5c466 | 44 | |
Cumulocity | 47:89ae46d5c466 | 45 | _init = true; |
Cumulocity | 47:89ae46d5c466 | 46 | return true; |
Cumulocity | 47:89ae46d5c466 | 47 | } |
Cumulocity | 47:89ae46d5c466 | 48 | |
Cumulocity | 47:89ae46d5c466 | 49 | bool LocationUpdate::run() |
Cumulocity | 47:89ae46d5c466 | 50 | { |
Cumulocity | 47:89ae46d5c466 | 51 | GPSTracker::Position position; |
Cumulocity | 47:89ae46d5c466 | 52 | |
Cumulocity | 49:ac0ba9d54ebc | 53 | if (!_gpsTracker.position(&position)) { |
xinlei | 72:c5709ae7b193 | 54 | aError("No GPS data available.\r\n"); |
Cumulocity | 47:89ae46d5c466 | 55 | return true; |
Cumulocity | 49:ac0ba9d54ebc | 56 | } |
xinlei | 71:063c45e99578 | 57 | float data[3] = { 0.0, 0.0, 0.0 }; |
xinlei | 71:063c45e99578 | 58 | data[0] = position.altitude; |
xinlei | 71:063c45e99578 | 59 | data[1] = position.latitude; |
xinlei | 71:063c45e99578 | 60 | data[2] = position.longitude; |
xinlei | 73:313975bfec96 | 61 | char tenant[25] = {0}; |
xinlei | 73:313975bfec96 | 62 | snprintf(tenant, 25, "Tenant: %s", _bootstrap.username()); |
xinlei | 73:313975bfec96 | 63 | char signal[25] = {0}; |
xinlei | 73:313975bfec96 | 64 | snprintf(signal, 25, "Network: %d dBm", _deviceInfo.signalQuality()->rssi); |
xinlei | 71:063c45e99578 | 65 | if (abs(oldValues[0]-data[0]) <= abs(oldValues[0])*THRESHOLD_PERCENT_LOC && |
xinlei | 71:063c45e99578 | 66 | abs(oldValues[1]-data[1]) <= abs(oldValues[1])*THRESHOLD_PERCENT_LOC && |
xinlei | 71:063c45e99578 | 67 | abs(oldValues[2]-data[2]) <= abs(oldValues[2])*THRESHOLD_PERCENT_LOC) { |
xinlei | 71:063c45e99578 | 68 | if (sendingTimer.read() < TIME_LIMIT_LOC) { |
xinlei | 73:313975bfec96 | 69 | aDebug("Similar location readings found, no sending!\r\n"); |
xinlei | 73:313975bfec96 | 70 | _io.lcdPrint(tenant, signal); |
xinlei | 71:063c45e99578 | 71 | return true; |
xinlei | 71:063c45e99578 | 72 | } else { |
xinlei | 73:313975bfec96 | 73 | aDebug("Sending timer of location sensor timed out at %f s, a sending is forced.\r\n", sendingTimer.read()); |
xinlei | 71:063c45e99578 | 74 | } |
xinlei | 71:063c45e99578 | 75 | } |
xinlei | 73:313975bfec96 | 76 | char status[27] = {0}; |
xinlei | 73:313975bfec96 | 77 | snprintf(status, 27, "Sending GPS %.1f,%.1f,%.1f", data[0], data[1], data[2]); |
xinlei | 73:313975bfec96 | 78 | _io.lcdPrint(tenant, signal, status); |
Cumulocity | 47:89ae46d5c466 | 79 | |
Cumulocity | 49:ac0ba9d54ebc | 80 | Aggregator aggregator; |
Cumulocity | 49:ac0ba9d54ebc | 81 | ComposedRecord record1, record2; |
Cumulocity | 49:ac0ba9d54ebc | 82 | IntegerValue msgId1(108); |
Cumulocity | 49:ac0ba9d54ebc | 83 | IntegerValue msgId2(109); |
Cumulocity | 47:89ae46d5c466 | 84 | IntegerValue devId(_deviceId); |
Cumulocity | 49:ac0ba9d54ebc | 85 | FloatValue altitude(position.altitude, 2); |
Cumulocity | 47:89ae46d5c466 | 86 | FloatValue latitude(position.latitude, 6); |
Cumulocity | 49:ac0ba9d54ebc | 87 | FloatValue longitude(position.longitude, 6); |
Cumulocity | 49:ac0ba9d54ebc | 88 | if ((!record1.add(msgId1)) || (!record1.add(devId)) || (!record1.add(altitude)) || (!record1.add(latitude)) || (!record1.add(longitude))) |
Cumulocity | 49:ac0ba9d54ebc | 89 | return false; |
Cumulocity | 49:ac0ba9d54ebc | 90 | if ((!record2.add(msgId2)) || (!record2.add(devId)) || (!record2.add(altitude)) || (!record2.add(latitude)) || (!record2.add(longitude))) |
Cumulocity | 49:ac0ba9d54ebc | 91 | return false; |
Cumulocity | 49:ac0ba9d54ebc | 92 | if ((!aggregator.add(record1)) || (!aggregator.add(record2))) |
Cumulocity | 47:89ae46d5c466 | 93 | return false; |
Cumulocity | 47:89ae46d5c466 | 94 | |
xinlei | 73:313975bfec96 | 95 | float t_start = sendingTimer.read(); |
Cumulocity | 49:ac0ba9d54ebc | 96 | if (_client.send(aggregator) != SMARTREST_SUCCESS) { |
xinlei | 72:c5709ae7b193 | 97 | aError("Signal measurement failed.\r\n"); |
Cumulocity | 47:89ae46d5c466 | 98 | _client.stop(); |
Cumulocity | 47:89ae46d5c466 | 99 | return false; |
Cumulocity | 47:89ae46d5c466 | 100 | } |
xinlei | 73:313975bfec96 | 101 | float t_end = sendingTimer.read(); |
Cumulocity | 47:89ae46d5c466 | 102 | _client.stop(); |
xinlei | 73:313975bfec96 | 103 | aInfo("Acceleration readings sent in %.1f.\r\n", t_end-t_start); |
xinlei | 71:063c45e99578 | 104 | oldValues[0] = data[0]; |
xinlei | 71:063c45e99578 | 105 | oldValues[1] = data[1]; |
xinlei | 71:063c45e99578 | 106 | oldValues[2] = data[2]; |
xinlei | 71:063c45e99578 | 107 | sendingTimer.reset(); |
Cumulocity | 47:89ae46d5c466 | 108 | return true; |
Cumulocity | 47:89ae46d5c466 | 109 | } |