
Own fork of MbedSmartRestMain
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
measurement/LocationUpdate.cpp@98:e369fc75c000, 2015-05-07 (annotated)
- Committer:
- xinlei
- Date:
- Thu May 07 09:57:55 2015 +0000
- Revision:
- 98:e369fc75c000
- Parent:
- 95:5dfdc8568e9f
- Child:
- 99:47ea098f8a47
prepare for v2.1rc3.
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 | 95:5dfdc8568e9f | 7 | #include "LCDDisplay.h" |
xinlei | 72:c5709ae7b193 | 8 | #include "logging.h" |
Cumulocity | 47:89ae46d5c466 | 9 | |
xinlei | 71:063c45e99578 | 10 | #define THRESHOLD_PERCENT_LOC 0.05 // Percentage cut-off for avoiding sending similar acceleration sensor data. |
xinlei | 73:313975bfec96 | 11 | // Time interval for forcing a sending even if analog sensor readings are constantly similar (in seconds). |
xinlei | 73:313975bfec96 | 12 | #define TIME_LIMIT_LOC 900 |
xinlei | 71:063c45e99578 | 13 | |
xinlei | 92:0acd11870c6a | 14 | LocationUpdate::LocationUpdate(AbstractSmartRest& client, SmartRestTemplate& tpl, |
xinlei | 98:e369fc75c000 | 15 | long& deviceId, DeviceInfo& deviceInfo) : |
xinlei | 91:48069375dffa | 16 | _deviceId(deviceId), |
Cumulocity | 47:89ae46d5c466 | 17 | _tpl(tpl), |
xinlei | 91:48069375dffa | 18 | _client(client), |
xinlei | 98:e369fc75c000 | 19 | _gpsTracker(), |
xinlei | 95:5dfdc8568e9f | 20 | _deviceInfo(deviceInfo) |
Cumulocity | 47:89ae46d5c466 | 21 | { |
Cumulocity | 47:89ae46d5c466 | 22 | _init = false; |
xinlei | 71:063c45e99578 | 23 | oldValues[0] = 0; |
xinlei | 71:063c45e99578 | 24 | oldValues[1] = 0; |
xinlei | 71:063c45e99578 | 25 | oldValues[2] = 0; |
xinlei | 93:61d44636f020 | 26 | t_start = time(NULL); |
Cumulocity | 47:89ae46d5c466 | 27 | } |
Cumulocity | 47:89ae46d5c466 | 28 | |
Cumulocity | 47:89ae46d5c466 | 29 | bool LocationUpdate::init() |
Cumulocity | 47:89ae46d5c466 | 30 | { |
Cumulocity | 47:89ae46d5c466 | 31 | if (_init) |
Cumulocity | 47:89ae46d5c466 | 32 | return false; |
Cumulocity | 47:89ae46d5c466 | 33 | |
Cumulocity | 49:ac0ba9d54ebc | 34 | // Update device position |
Cumulocity | 47:89ae46d5c466 | 35 | // USAGE: 108,<DEVICE/ID>,<ALTITUDE>,<LATITUDE>,<LONGITUDE> |
Cumulocity | 49:ac0ba9d54ebc | 36 | 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 | 37 | return false; |
Cumulocity | 49:ac0ba9d54ebc | 38 | |
Cumulocity | 49:ac0ba9d54ebc | 39 | // Insert measurement |
Cumulocity | 49:ac0ba9d54ebc | 40 | // USAGE: 109,<DEVICE/ID>,<ALTITUDE>,<LATITUDE>,<LONGITUDE> |
vwochnik | 58:4cc0ae5a7058 | 41 | 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 | 42 | return false; |
Cumulocity | 47:89ae46d5c466 | 43 | |
Cumulocity | 47:89ae46d5c466 | 44 | _init = true; |
Cumulocity | 47:89ae46d5c466 | 45 | return true; |
Cumulocity | 47:89ae46d5c466 | 46 | } |
Cumulocity | 47:89ae46d5c466 | 47 | |
Cumulocity | 47:89ae46d5c466 | 48 | bool LocationUpdate::run() |
Cumulocity | 47:89ae46d5c466 | 49 | { |
xinlei | 77:f6717e4eccc4 | 50 | GPSTracker::Position position; |
Cumulocity | 49:ac0ba9d54ebc | 51 | if (!_gpsTracker.position(&position)) { |
xinlei | 74:ca3001991fdc | 52 | return false; |
Cumulocity | 49:ac0ba9d54ebc | 53 | } |
xinlei | 85:b49c4cfecc43 | 54 | float data[3] = { position.altitude, position.latitude, position.longitude }; |
xinlei | 77:f6717e4eccc4 | 55 | |
xinlei | 71:063c45e99578 | 56 | if (abs(oldValues[0]-data[0]) <= abs(oldValues[0])*THRESHOLD_PERCENT_LOC && |
xinlei | 71:063c45e99578 | 57 | abs(oldValues[1]-data[1]) <= abs(oldValues[1])*THRESHOLD_PERCENT_LOC && |
xinlei | 71:063c45e99578 | 58 | abs(oldValues[2]-data[2]) <= abs(oldValues[2])*THRESHOLD_PERCENT_LOC) { |
xinlei | 93:61d44636f020 | 59 | time_t t_interval = time(NULL) - t_start; |
xinlei | 93:61d44636f020 | 60 | if (t_interval < TIME_LIMIT_LOC) { |
xinlei | 95:5dfdc8568e9f | 61 | if (strcmp(LCDDisplay::inst().getThirdLine(), "")) { |
xinlei | 95:5dfdc8568e9f | 62 | LCDDisplay::inst().setThirdLine(""); |
xinlei | 84:5dc5a50e4b06 | 63 | } |
xinlei | 71:063c45e99578 | 64 | return true; |
xinlei | 71:063c45e99578 | 65 | } else { |
xinlei | 93:61d44636f020 | 66 | aDebug("GPS: Timeout at %d s.\n", t_interval); |
xinlei | 71:063c45e99578 | 67 | } |
xinlei | 71:063c45e99578 | 68 | } |
xinlei | 73:313975bfec96 | 69 | char status[27] = {0}; |
xinlei | 92:0acd11870c6a | 70 | snprintf(status, 27, "Send GPS %.1f,%.1f,%.1f", data[0], data[1], data[2]); |
xinlei | 95:5dfdc8568e9f | 71 | LCDDisplay::inst().setThirdLine(status); |
Cumulocity | 47:89ae46d5c466 | 72 | |
Cumulocity | 49:ac0ba9d54ebc | 73 | Aggregator aggregator; |
Cumulocity | 49:ac0ba9d54ebc | 74 | ComposedRecord record1, record2; |
Cumulocity | 49:ac0ba9d54ebc | 75 | IntegerValue msgId1(108); |
Cumulocity | 49:ac0ba9d54ebc | 76 | IntegerValue msgId2(109); |
Cumulocity | 47:89ae46d5c466 | 77 | IntegerValue devId(_deviceId); |
Cumulocity | 49:ac0ba9d54ebc | 78 | FloatValue altitude(position.altitude, 2); |
Cumulocity | 47:89ae46d5c466 | 79 | FloatValue latitude(position.latitude, 6); |
Cumulocity | 49:ac0ba9d54ebc | 80 | FloatValue longitude(position.longitude, 6); |
Cumulocity | 49:ac0ba9d54ebc | 81 | if ((!record1.add(msgId1)) || (!record1.add(devId)) || (!record1.add(altitude)) || (!record1.add(latitude)) || (!record1.add(longitude))) |
Cumulocity | 49:ac0ba9d54ebc | 82 | return false; |
Cumulocity | 49:ac0ba9d54ebc | 83 | if ((!record2.add(msgId2)) || (!record2.add(devId)) || (!record2.add(altitude)) || (!record2.add(latitude)) || (!record2.add(longitude))) |
Cumulocity | 49:ac0ba9d54ebc | 84 | return false; |
Cumulocity | 49:ac0ba9d54ebc | 85 | if ((!aggregator.add(record1)) || (!aggregator.add(record2))) |
Cumulocity | 47:89ae46d5c466 | 86 | return false; |
Cumulocity | 47:89ae46d5c466 | 87 | |
xinlei | 93:61d44636f020 | 88 | time_t t0 = time(NULL); |
Cumulocity | 49:ac0ba9d54ebc | 89 | if (_client.send(aggregator) != SMARTREST_SUCCESS) { |
xinlei | 92:0acd11870c6a | 90 | aWarning("GPS: Send failed.\n"); |
Cumulocity | 47:89ae46d5c466 | 91 | _client.stop(); |
Cumulocity | 47:89ae46d5c466 | 92 | return false; |
Cumulocity | 47:89ae46d5c466 | 93 | } |
xinlei | 93:61d44636f020 | 94 | time_t t1 = time(NULL); |
Cumulocity | 47:89ae46d5c466 | 95 | _client.stop(); |
xinlei | 93:61d44636f020 | 96 | aDebug("GPS: Sent in %d s.\n", t1-t0); |
xinlei | 71:063c45e99578 | 97 | oldValues[0] = data[0]; |
xinlei | 71:063c45e99578 | 98 | oldValues[1] = data[1]; |
xinlei | 71:063c45e99578 | 99 | oldValues[2] = data[2]; |
xinlei | 93:61d44636f020 | 100 | t_start = time(NULL); |
Cumulocity | 47:89ae46d5c466 | 101 | return true; |
Cumulocity | 47:89ae46d5c466 | 102 | } |