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:
Tue Mar 03 14:10:09 2015 +0000
Revision:
77:f6717e4eccc4
Parent:
76:b07effe83fb8
Child:
82:ca7430f50b2b
Working device push, DNS caching, change logging level when running and more.

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 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 {
xinlei 77:f6717e4eccc4 51 GPSTracker::Position position;
Cumulocity 49:ac0ba9d54ebc 52 if (!_gpsTracker.position(&position)) {
xinlei 72:c5709ae7b193 53 aError("No GPS data available.\r\n");
xinlei 74:ca3001991fdc 54 return false;
Cumulocity 49:ac0ba9d54ebc 55 }
xinlei 77:f6717e4eccc4 56 static char tenant[25];
xinlei 77:f6717e4eccc4 57 static bool firstTime = true;
xinlei 77:f6717e4eccc4 58 if (firstTime) {
xinlei 77:f6717e4eccc4 59 firstTime = false;
xinlei 77:f6717e4eccc4 60 const char* user= _bootstrap.username();
xinlei 77:f6717e4eccc4 61 int len = strchr(user, '/')-user+1+8; // 8: length of "Tenant: "
xinlei 77:f6717e4eccc4 62 len = len <= 25 ? len : 25;
xinlei 77:f6717e4eccc4 63 snprintf(tenant, len, "Tenant: %s", user);
xinlei 77:f6717e4eccc4 64 }
xinlei 71:063c45e99578 65 float data[3] = { 0.0, 0.0, 0.0 };
xinlei 71:063c45e99578 66 data[0] = position.altitude;
xinlei 71:063c45e99578 67 data[1] = position.latitude;
xinlei 71:063c45e99578 68 data[2] = position.longitude;
xinlei 77:f6717e4eccc4 69 DeviceInfo::SignalQuality *p = _deviceInfo.signalQuality(false);
xinlei 73:313975bfec96 70 char signal[25] = {0};
xinlei 77:f6717e4eccc4 71 if (p && p->rssi)
xinlei 77:f6717e4eccc4 72 snprintf(signal, sizeof(signal), "Network: %d dBm", p->rssi);
xinlei 74:ca3001991fdc 73 else
xinlei 77:f6717e4eccc4 74 strncpy(signal, "Network: no coverage", sizeof(signal));
xinlei 77:f6717e4eccc4 75
xinlei 71:063c45e99578 76 if (abs(oldValues[0]-data[0]) <= abs(oldValues[0])*THRESHOLD_PERCENT_LOC &&
xinlei 71:063c45e99578 77 abs(oldValues[1]-data[1]) <= abs(oldValues[1])*THRESHOLD_PERCENT_LOC &&
xinlei 71:063c45e99578 78 abs(oldValues[2]-data[2]) <= abs(oldValues[2])*THRESHOLD_PERCENT_LOC) {
xinlei 71:063c45e99578 79 if (sendingTimer.read() < TIME_LIMIT_LOC) {
xinlei 74:ca3001991fdc 80 _io.lcdPrint(tenant, signal);
xinlei 73:313975bfec96 81 aDebug("Similar location readings found, no sending!\r\n");
xinlei 71:063c45e99578 82 return true;
xinlei 71:063c45e99578 83 } else {
xinlei 77:f6717e4eccc4 84 aDebug("Location sensor timed out at %f s, a sending is forced.\r\n", sendingTimer.read());
xinlei 71:063c45e99578 85 }
xinlei 71:063c45e99578 86 }
xinlei 73:313975bfec96 87 char status[27] = {0};
xinlei 73:313975bfec96 88 snprintf(status, 27, "Sending GPS %.1f,%.1f,%.1f", data[0], data[1], data[2]);
xinlei 73:313975bfec96 89 _io.lcdPrint(tenant, signal, status);
Cumulocity 47:89ae46d5c466 90
Cumulocity 49:ac0ba9d54ebc 91 Aggregator aggregator;
Cumulocity 49:ac0ba9d54ebc 92 ComposedRecord record1, record2;
Cumulocity 49:ac0ba9d54ebc 93 IntegerValue msgId1(108);
Cumulocity 49:ac0ba9d54ebc 94 IntegerValue msgId2(109);
Cumulocity 47:89ae46d5c466 95 IntegerValue devId(_deviceId);
Cumulocity 49:ac0ba9d54ebc 96 FloatValue altitude(position.altitude, 2);
Cumulocity 47:89ae46d5c466 97 FloatValue latitude(position.latitude, 6);
Cumulocity 49:ac0ba9d54ebc 98 FloatValue longitude(position.longitude, 6);
Cumulocity 49:ac0ba9d54ebc 99 if ((!record1.add(msgId1)) || (!record1.add(devId)) || (!record1.add(altitude)) || (!record1.add(latitude)) || (!record1.add(longitude)))
Cumulocity 49:ac0ba9d54ebc 100 return false;
Cumulocity 49:ac0ba9d54ebc 101 if ((!record2.add(msgId2)) || (!record2.add(devId)) || (!record2.add(altitude)) || (!record2.add(latitude)) || (!record2.add(longitude)))
Cumulocity 49:ac0ba9d54ebc 102 return false;
Cumulocity 49:ac0ba9d54ebc 103 if ((!aggregator.add(record1)) || (!aggregator.add(record2)))
Cumulocity 47:89ae46d5c466 104 return false;
Cumulocity 47:89ae46d5c466 105
xinlei 73:313975bfec96 106 float t_start = sendingTimer.read();
Cumulocity 49:ac0ba9d54ebc 107 if (_client.send(aggregator) != SMARTREST_SUCCESS) {
xinlei 76:b07effe83fb8 108 aWarning("Sending GPS readings failed.\r\n");
Cumulocity 47:89ae46d5c466 109 _client.stop();
Cumulocity 47:89ae46d5c466 110 return false;
Cumulocity 47:89ae46d5c466 111 }
xinlei 73:313975bfec96 112 float t_end = sendingTimer.read();
Cumulocity 47:89ae46d5c466 113 _client.stop();
xinlei 74:ca3001991fdc 114 aInfo("GPS readings sent in %.1f.\r\n", t_end-t_start);
xinlei 71:063c45e99578 115 oldValues[0] = data[0];
xinlei 71:063c45e99578 116 oldValues[1] = data[1];
xinlei 71:063c45e99578 117 oldValues[2] = data[2];
xinlei 71:063c45e99578 118 sendingTimer.reset();
Cumulocity 47:89ae46d5c466 119 return true;
Cumulocity 47:89ae46d5c466 120 }