portable version of the cumulocity demo

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Cumulocity Official

Revision:
47:89ae46d5c466
Child:
49:ac0ba9d54ebc
diff -r f6976fd64387 -r 89ae46d5c466 LocationUpdate.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LocationUpdate.cpp	Thu Jul 24 23:32:53 2014 +0000
@@ -0,0 +1,66 @@
+#include "LocationUpdate.h"
+#include "ComposedRecord.h"
+#include "CharValue.h"
+#include "IntegerValue.h"
+#include "FloatValue.h"
+
+LocationUpdate::LocationUpdate(SmartRest& client, SmartRestTemplate& tpl, long& deviceId, GPSTracker& gpsTracker) :
+    _client(client),
+    _tpl(tpl),
+    _deviceId(deviceId),
+    _gpsTracker(gpsTracker)
+{
+    _init = false;
+}
+
+bool LocationUpdate::init()
+{
+    if (_init)
+        return false;
+    
+    // Insert measurement
+    // USAGE: 108,<DEVICE/ID>,<ALTITUDE>,<LATITUDE>,<LONGITUDE>
+    if (!_tpl.add("10,108,POST,/event/events,application/vnd.com.nsn.cumulocity.event+json,,%%,NOW UNSIGNED NUMBER NUMBER NUMBER,\"{\"\"time\"\":\"\"%%\"\",\"\"source\"\":{\"\"id\"\":\"\"%%\"\"},\"\"type\"\":\"\"c8y_LocationUpdate\"\",\"\"c8y_Position\"\":{\"\"alt\"\":%%,\"\"lat\"\":%%,\"\"lng\"\":%%}}\""))
+        return false;
+
+    _init = true;
+    return true;
+}
+
+bool LocationUpdate::run()
+{
+    GPSTracker::Position position;
+    
+    if (!_gpsTracker.position(&position))
+        return true;
+        
+    puts("Starting measurement sending.");
+    
+    ComposedRecord record;
+    IntegerValue msgId(108);
+    IntegerValue devId(_deviceId);
+    FloatValue altitude(position.altitude, 6);
+    FloatValue latitude(position.latitude, 6);
+    FloatValue longitude(position.longitude, 1);
+    if ((!record.add(msgId)) || (!record.add(devId)) || (!record.add(altitude)) || (!record.add(latitude)) || (!record.add(longitude)))
+        return false;
+
+    if (_client.send(record) != SMARTREST_SUCCESS) {
+        puts("Signal measurement failed.");
+        _client.stop();
+        return false;
+    }
+    
+    ParsedRecord recvd;
+    if (_client.receive(recvd) != SMARTREST_SUCCESS) {
+        puts("Nothing received.");
+        _client.stop();
+        return true;
+    }
+    
+    for (size_t n = 0; n < recvd.values(); n++)
+        puts(recvd.rawValue(n));
+    
+    _client.stop();
+    return true;
+}