portable version of the cumulocity demo

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Cumulocity Official

LocationUpdate.cpp

Committer:
Cumulocity
Date:
2014-07-24
Revision:
47:89ae46d5c466
Child:
49:ac0ba9d54ebc

File content as of revision 47:89ae46d5c466:

#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;
}