Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
measurement/LocationUpdate.cpp
- Committer:
- xinlei
- Date:
- 2015-02-16
- Revision:
- 72:c5709ae7b193
- Parent:
- 71:063c45e99578
- Child:
- 73:313975bfec96
File content as of revision 72:c5709ae7b193:
#include "LocationUpdate.h"
#include "Aggregator.h"
#include "ComposedRecord.h"
#include "CharValue.h"
#include "IntegerValue.h"
#include "FloatValue.h"
#include "logging.h"
#define THRESHOLD_PERCENT_LOC 0.05 // Percentage cut-off for avoiding sending similar acceleration sensor data.
#define TIME_LIMIT_LOC 900 // Time interval for forcing a sending even if acceleration sensor readings are constantly similar.
LocationUpdate::LocationUpdate(AbstractSmartRest& client, SmartRestTemplate& tpl, long& deviceId, GPSTracker& gpsTracker) :
_client(client),
_tpl(tpl),
_deviceId(deviceId),
_gpsTracker(gpsTracker)
{
_init = false;
oldValues[0] = 0;
oldValues[1] = 0;
oldValues[2] = 0;
sendingTimer.start();
}
bool LocationUpdate::init()
{
if (_init)
return false;
// Update device position
// USAGE: 108,<DEVICE/ID>,<ALTITUDE>,<LATITUDE>,<LONGITUDE>
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"))
return false;
// Insert measurement
// USAGE: 109,<DEVICE/ID>,<ALTITUDE>,<LATITUDE>,<LONGITUDE>
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"))
return false;
_init = true;
return true;
}
bool LocationUpdate::run()
{
GPSTracker::Position position;
if (!_gpsTracker.position(&position)) {
aError("No GPS data available.\r\n");
return true;
}
float data[3] = { 0.0, 0.0, 0.0 };
data[0] = position.altitude;
data[1] = position.latitude;
data[2] = position.longitude;
if (abs(oldValues[0]-data[0]) <= abs(oldValues[0])*THRESHOLD_PERCENT_LOC &&
abs(oldValues[1]-data[1]) <= abs(oldValues[1])*THRESHOLD_PERCENT_LOC &&
abs(oldValues[2]-data[2]) <= abs(oldValues[2])*THRESHOLD_PERCENT_LOC) {
if (sendingTimer.read() < TIME_LIMIT_LOC) {
aInfo("Similar location readings found, no sending!\r\n");
return true;
} else {
aInfo("Sending timer of location sensor timed out at %f s, a sending is forced.\r\n", sendingTimer.read());
}
}
Aggregator aggregator;
ComposedRecord record1, record2;
IntegerValue msgId1(108);
IntegerValue msgId2(109);
IntegerValue devId(_deviceId);
FloatValue altitude(position.altitude, 2);
FloatValue latitude(position.latitude, 6);
FloatValue longitude(position.longitude, 6);
if ((!record1.add(msgId1)) || (!record1.add(devId)) || (!record1.add(altitude)) || (!record1.add(latitude)) || (!record1.add(longitude)))
return false;
if ((!record2.add(msgId2)) || (!record2.add(devId)) || (!record2.add(altitude)) || (!record2.add(latitude)) || (!record2.add(longitude)))
return false;
if ((!aggregator.add(record1)) || (!aggregator.add(record2)))
return false;
if (_client.send(aggregator) != SMARTREST_SUCCESS) {
aError("Signal measurement failed.\r\n");
_client.stop();
return false;
}
_client.stop();
oldValues[0] = data[0];
oldValues[1] = data[1];
oldValues[2] = data[2];
sendingTimer.reset();
aInfo("Location readings sent.\r\n");
return true;
}

Cumulocity