SmartREST client reference implementation for the u-blox C027 mbed compatible device.

Dependencies:   C027 C027_Support mbed mbed-rtos MbedSmartRest LM75B MMA7660 C12832

Fork of MbedSmartRestTest by Vincent Wochnik

program.cpp

Committer:
vwochnik
Date:
2014-02-12
Revision:
10:d5cd4a7ada7b
Parent:
9:5e182f6e5e9b
Child:
11:6f8f70a6dd46

File content as of revision 10:d5cd4a7ada7b:

#include "common.h"
#include "StaticData.h"
#include "ComposedRecord.h"
#include "CharValue.h"
#include "IntegerValue.h"
#include "FloatValue.h"

long existing();
long create();
bool identify(long deviceId);
bool update(long deviceId);
void loop(long deviceId);
bool measurement(long deviceId, double rssi, int ber);

StaticData srtpl(
// get device by identity
// Usage: 100,<SERIAL/NR>
"10,100,GET,/identity/externalIds/c8y_Serial/%%,,application/vnd.com.nsn.cumulocity.externalId+json,%%,STRING,\r\n"
// get device id from identity
// Response: 200,<DEVICE/ID>
"11,200,\"$.managedObject\",,\"$.id\"\r\n"
// Create device
// Usage: 101,<SERIAL/NR>
"10,101,POST,/inventory/managedObjects,application/vnd.com.nsn.cumulocity.managedObject+json,application/vnd.com.nsn.cumulocity.managedObject+json,%%,STRING,\"{\"\"name\"\":\"\"Mbed Test Device\"\",\"\"type\"\":\"\"com_ublox_C027_REV-A\"\",\"\"c8y_Hardware\"\":{\"\"revision\"\":\"\"1\"\",\"\"model\"\":\"\"Ublox C027\"\",\"\"serialNumber\"\":\"\"%%\"\"},\"\"c8y_SupportedMeasurements\"\":[\"\"c8y_SignalStrength\"\"],\"\"c8y_RequiredAvailability\"\":{ \"\"responseInterval\"\":15},\"\"c8y_IsDevice\"\":{}}\"\r\n"
// Get device id
// Response: 201,<DEVICE/ID>
"11,201,,\"$.c8y_IsDevice\",\"$.id\"\r\n"
// Insert global ID
// Usage: 102,<DEVICE/ID>,<SERIAL/NR>
"10,102,POST,/identity/globalIds/%%/externalIds,application/vnd.com.nsn.cumulocity.externalId+json,application/vnd.com.nsn.cumulocity.externalId+json,%%,UNSIGNED STRING,\"{\"\"type\"\":\"\"c8y_Serial\"\",\"\"externalId\"\":\"\"%%\"\"}\"\r\n"
// Update IMEI, CellId and iccid
// Usage: 103,<DEVICE/ID>,<IMEI>,<CELL/ID>,<ICCID>
"10,103,PUT,/inventory/managedObjects/%%,application/vnd.com.nsn.cumulocity.managedObject+json,application/vnd.com.nsn.cumulocity.managedObject+json,%%,UNSIGNED STRING STRING STRING,\"{\"\"c8y_Mobile\"\":{\"\"imei\"\":\"\"%%\"\",\"\"cellId\"\":\"\"%%\"\",\"\"iccid\"\":\"\"%%\"\"}}\"\r\n"
// Insert measurement
// USAGE: 104,<DEVICE/ID>,<RSSI>,<BER>
"10,104,POST,/measurement/measurements,application/vnd.com.nsn.cumulocity.measurement+json,application/vnd.com.nsn.cumulocity.measurement+json,%%,NOW UNSIGNED NUMBER UNSIGNED,\"{\"\"time\"\":\"\"%%\"\",\"\"source\"\":{\"\"id\"\":\"\"%%\"\"},\"\"type\"\":\"\"c8y_SignalStrength\"\",\"\"c8y_SignalStrength\"\":{\"\"rssi\"\":{\"\"value\"\":%%,\"\"unit\"\":\"\"dBm\"\"},\"\"ber\"\":{\"\"value\"\":%%,\"\"unit\"\":\"\"%\"\"}}}\"\r\n"
);

const char * const serialNumber = "ublox-a123bd";
float interval = 120.0; // send measurements every two minutes
MbedSmartRest client("http://developer.cumulocity.com/s", "vaillant/admin", "klanpi", "com_u-blox_C027_REV-A_0.7");
//MbedSmartRest client("http://nocore.info:8888/", "vaillant/admin", "klanpi", "com_cumulocity_MbedTestDevice_2.0");

int program(void)
{
    long deviceId; Timer timer;

    puts("Hello!");

    puts("Bootstrapping");
    if (client.bootstrap(srtpl) != SMARTREST_SUCCESS) {
        puts("Bootstrapping failed.");
        return 2;
    }
    
    puts("Starting action...");
    
    if ((deviceId = existing()) == 0) {
        deviceId = create();
        if (deviceId == 0)
            return 1;
        if (!identify(deviceId))
            return 2;
    }
    if (!update(deviceId))
        return 3;

    printf("Device ID: %ld\r\n", deviceId);

    timer.start();
    while (true) {
        timer.reset();
        loop(deviceId);

        // block remaining number of seconds
        while (timer.read() < interval);
    }
}

long existing()
{
    ComposedRecord newMoRec(true); // set copy=true b/c tmp objects
    ParsedRecord received;

    puts("Checking for device existance...");

    newMoRec.add(IntegerValue(100)).add(CharValue(serialNumber));

    if (client.send(newMoRec) != SMARTREST_SUCCESS) {
        puts("Send failed.");
        client.stop();
        return 0;
    }

    if (client.receive(received) != SMARTREST_SUCCESS) {
        puts("No device found.");
        client.stop();
        return 0;
    }

    if (received.values() == 0) {
        puts("Received no values.");
        client.stop();
        return 0;
    }

    if (received.value(0).integerValue() == 50) {
        client.stop();
        return 0;
    }
    
    if (received.value(0).integerValue() != 200) {
        puts("Bad response.");
        client.stop();
        return 0;
    }

    client.stop();
    return received.value(2).integerValue();
}

long create()
{
    ComposedRecord newMoRec(true); // set copy=true b/c tmp objects
    ParsedRecord received;

    puts("Creating device...");

    newMoRec.add(IntegerValue(101)).add(CharValue(serialNumber));

    if (client.send(newMoRec) != SMARTREST_SUCCESS) {
        puts("Send failed.");
        client.stop();
        return 0;
    }

    if (client.receive(received) != SMARTREST_SUCCESS) {
        puts("No device found.");
        client.stop();
        return 0;
    }

    if (received.values() != 3) {
        puts("Bad received data.");
        client.stop();
        return 0;
    }
    
    if (received.value(0).integerValue() != 201) {
        puts("Bad received data.");
        client.stop();
        return 0;
    }

    client.stop();
    return received.value(2).integerValue();
}

bool identify(long deviceId)
{
    ComposedRecord newMoRec(true); // set copy=true b/c tmp objects
    ParsedRecord received;

    puts("Adding global identifier...");

    newMoRec.add(IntegerValue(102)).add(IntegerValue(deviceId)).add(CharValue(serialNumber));

    if (client.send(newMoRec) != SMARTREST_SUCCESS) {
        puts("Sending failed.");
        client.stop();
        return false;
    }

    if (client.receive(received) != SMARTREST_SUCCESS) {
        puts("Failed.");
        client.stop();
        return false;
    }

    if (received.values() != 3) {
        puts("Received bad data.");
        client.stop();
        return false;
    }
    
    if (received.value(0).integerValue() != 200) {
        puts("Received bad data.");
        client.stop();
        return false;
    }

    client.stop();
    return true;
}

bool update(long deviceId)
{
    ComposedRecord newMoRec(true); // set copy=true b/c tmp objects
    ParsedRecord received;

    puts("Updating device data...");

    newMoRec.add(IntegerValue(103)).add(IntegerValue(deviceId)).add(CharValue(imei())).add(CharValue(cellId())).add(CharValue(iccid()));

    if (client.send(newMoRec) != SMARTREST_SUCCESS) {
        puts("Send failed.");
        client.stop();
        return false;
    }

    if (client.receive(received) != SMARTREST_SUCCESS) {
        puts("Update failed.");
        client.stop();
        return false;
    }

    if (received.values() != 3) {
        puts("Bad received data.");
        client.stop();
        return false;
    }
    
    if (received.value(0).integerValue() != 201) {
        puts("Bad received data.");
        client.stop();
        return false;
    }

    client.stop();
    
    return true;
}

void loop(long deviceId)
{
    sigq_t *signal = signalQuality();
    measurement(deviceId, signal->rssi, signal->ber);
}

bool measurement(long deviceId, double rssi, int ber)
{
    ComposedRecord newMoRec(true); // set copy=true b/c tmp objects
    
    puts("Creating measurement...");

    newMoRec.add(IntegerValue(104)).add(IntegerValue(deviceId)).add(FloatValue(rssi, 0)).add(IntegerValue(ber));

    if (client.send(newMoRec) != SMARTREST_SUCCESS) {
        puts("Send failed.");
        client.stop();
        return false;
    }

    client.stop();
    return true;
}