Cumulocity Official / Mbed 2 deprecated MbedSmartRestMain Featured

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Vincent Wochnik

Committer:
xinlei
Date:
Fri Mar 06 11:38:15 2015 +0000
Revision:
86:b49c4cfecc43
Parent:
85:5dc5a50e4b06
Child:
92:48069375dffa
bugfix: fixed LCD display showing signal quality behind real signal measuring.

Who changed what in which revision?

UserRevisionLine numberNew 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 82:ca7430f50b2b 14 DeviceIO& io, DeviceInfo& deviceInfo, DisplayInfo& displayInfo) :
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 82:ca7430f50b2b 21 _displayInfo(displayInfo)
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 {
xinlei 85:5dc5a50e4b06 51 extern bool lastSensorReadingSent;
xinlei 77:f6717e4eccc4 52 GPSTracker::Position position;
Cumulocity 49:ac0ba9d54ebc 53 if (!_gpsTracker.position(&position)) {
xinlei 72:c5709ae7b193 54 aError("No GPS data available.\r\n");
xinlei 74:ca3001991fdc 55 return false;
Cumulocity 49:ac0ba9d54ebc 56 }
xinlei 86:b49c4cfecc43 57 float data[3] = { position.altitude, position.latitude, position.longitude };
xinlei 77:f6717e4eccc4 58
xinlei 71:063c45e99578 59 if (abs(oldValues[0]-data[0]) <= abs(oldValues[0])*THRESHOLD_PERCENT_LOC &&
xinlei 71:063c45e99578 60 abs(oldValues[1]-data[1]) <= abs(oldValues[1])*THRESHOLD_PERCENT_LOC &&
xinlei 71:063c45e99578 61 abs(oldValues[2]-data[2]) <= abs(oldValues[2])*THRESHOLD_PERCENT_LOC) {
xinlei 71:063c45e99578 62 if (sendingTimer.read() < TIME_LIMIT_LOC) {
xinlei 85:5dc5a50e4b06 63 if (lastSensorReadingSent) {
xinlei 85:5dc5a50e4b06 64 _io.lcdPrint(_displayInfo.getFirstLine(), _displayInfo.getSecondLine());
xinlei 85:5dc5a50e4b06 65 lastSensorReadingSent=false;
xinlei 85:5dc5a50e4b06 66 }
xinlei 73:313975bfec96 67 aDebug("Similar location readings found, no sending!\r\n");
xinlei 71:063c45e99578 68 return true;
xinlei 71:063c45e99578 69 } else {
xinlei 77:f6717e4eccc4 70 aDebug("Location sensor timed out at %f s, a sending is forced.\r\n", sendingTimer.read());
xinlei 71:063c45e99578 71 }
xinlei 71:063c45e99578 72 }
xinlei 73:313975bfec96 73 char status[27] = {0};
xinlei 73:313975bfec96 74 snprintf(status, 27, "Sending GPS %.1f,%.1f,%.1f", data[0], data[1], data[2]);
xinlei 84:3c8ceb12b773 75 _io.lcdPrint(_displayInfo.getFirstLine(), _displayInfo.getSecondLine(), status);
Cumulocity 47:89ae46d5c466 76
Cumulocity 49:ac0ba9d54ebc 77 Aggregator aggregator;
Cumulocity 49:ac0ba9d54ebc 78 ComposedRecord record1, record2;
Cumulocity 49:ac0ba9d54ebc 79 IntegerValue msgId1(108);
Cumulocity 49:ac0ba9d54ebc 80 IntegerValue msgId2(109);
Cumulocity 47:89ae46d5c466 81 IntegerValue devId(_deviceId);
Cumulocity 49:ac0ba9d54ebc 82 FloatValue altitude(position.altitude, 2);
Cumulocity 47:89ae46d5c466 83 FloatValue latitude(position.latitude, 6);
Cumulocity 49:ac0ba9d54ebc 84 FloatValue longitude(position.longitude, 6);
Cumulocity 49:ac0ba9d54ebc 85 if ((!record1.add(msgId1)) || (!record1.add(devId)) || (!record1.add(altitude)) || (!record1.add(latitude)) || (!record1.add(longitude)))
Cumulocity 49:ac0ba9d54ebc 86 return false;
Cumulocity 49:ac0ba9d54ebc 87 if ((!record2.add(msgId2)) || (!record2.add(devId)) || (!record2.add(altitude)) || (!record2.add(latitude)) || (!record2.add(longitude)))
Cumulocity 49:ac0ba9d54ebc 88 return false;
Cumulocity 49:ac0ba9d54ebc 89 if ((!aggregator.add(record1)) || (!aggregator.add(record2)))
Cumulocity 47:89ae46d5c466 90 return false;
Cumulocity 47:89ae46d5c466 91
xinlei 73:313975bfec96 92 float t_start = sendingTimer.read();
Cumulocity 49:ac0ba9d54ebc 93 if (_client.send(aggregator) != SMARTREST_SUCCESS) {
xinlei 76:b07effe83fb8 94 aWarning("Sending GPS readings failed.\r\n");
Cumulocity 47:89ae46d5c466 95 _client.stop();
Cumulocity 47:89ae46d5c466 96 return false;
Cumulocity 47:89ae46d5c466 97 }
xinlei 73:313975bfec96 98 float t_end = sendingTimer.read();
Cumulocity 47:89ae46d5c466 99 _client.stop();
xinlei 74:ca3001991fdc 100 aInfo("GPS readings sent in %.1f.\r\n", t_end-t_start);
xinlei 71:063c45e99578 101 oldValues[0] = data[0];
xinlei 71:063c45e99578 102 oldValues[1] = data[1];
xinlei 71:063c45e99578 103 oldValues[2] = data[2];
xinlei 71:063c45e99578 104 sendingTimer.reset();
xinlei 85:5dc5a50e4b06 105 lastSensorReadingSent=true;
Cumulocity 47:89ae46d5c466 106 return true;
Cumulocity 47:89ae46d5c466 107 }