Official reference client implementation for Cumulocity SmartREST on u-blox C027.

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Vincent Wochnik

operation/OperationExecutor.cpp

Committer:
xinlei
Date:
2015-04-13
Revision:
93:0acd11870c6a
Parent:
92:48069375dffa

File content as of revision 93:0acd11870c6a:

#include <string.h>
#include "OperationExecutor.h"
#include "ComposedRecord.h"
#include "CharValue.h"
#include "IntegerValue.h"
#include "logging.h"

#define FOUND_RELAY 1
#define FOUND_MESSAGE 2
#define FOUND_CONFIGURATION 3

OperationExecutor::OperationExecutor(AbstractSmartRest& client, SmartRestTemplate& tpl,
    long& deviceId, ConfigurationSynchronization& configurationSynchronization, 
    LCDDisplay& lcdDisplay) :
    _init(false),
    _deviceId(deviceId),
    _tpl(tpl),
    _client(client),
    _configurationSynchronization(configurationSynchronization),
    _deviceFeedback(),
    _lcdDisplay(lcdDisplay)
{
}

bool OperationExecutor::init()
{
    if (_init)
        return false;
    
    // Get operation by id
    // USAGE: 112,<OPERATION/ID>
    if (!_tpl.add("10,112,GET,/devicecontrol/operations/%%,,application/vnd.com.nsn.cumulocity.operation+json,%%,UNSIGNED,\r\n"))
        return false;

    // Relay operation response
    // Response: 220,<OPERATION/ID>,<STATUS>
    if (!_tpl.add("11,220,,\"$.c8y_Relay\",\"$.id\",\"$.c8y_Relay.relayState\"\r\n"))
        return false;

    // Message operation response
    // Response: 221,<OPERATION/ID>,<MESSAGE>
    if (!_tpl.add("11,221,,\"$.c8y_Message\",\"$.id\",\"$.c8y_Message.text\"\r\n"))
        return false;

    // Configuration operation response
    // Response: 222,<OPERATION/ID>,<CONFIGURATION/STRING>
    if (!_tpl.add("11,222,,\"$.c8y_Configuration\",\"$.id\",\"$.c8y_Configuration.config\"\r\n"))
        return false;

    _init = true;
    return true;
}

bool OperationExecutor::executeOperation(ParsedRecord& received)
{
    uint8_t found = 0;
    bool relayState=false;
    char message[128];
    if (received.values() != 4) {
        aError("executeOperation: %u of 4 values received.\n", received.values());
    } else if (received.value(0).integerValue() == 220) {
        relayState = strcmp("CLOSED", received.value(3).characterValue()) == 0;
        found = FOUND_RELAY;
    } else if (received.value(0).integerValue() == 221) {
        const char *p = received.rawValue(3);
        strncpy(message, p, sizeof(message));
        message[sizeof(message)-1] = '\0';
        found = FOUND_MESSAGE;
    } else if (received.value(0).integerValue() == 222) {
        strncpy(message, received.value(3).characterValue(), sizeof(message));
        message[sizeof(message)-1] = '\0';
        found = FOUND_CONFIGURATION;
    }

    switch (found) {
    case FOUND_RELAY:         return executeRelayStateUpdate(relayState);
    case FOUND_MESSAGE:       return executeMessageDisplay(message);
    case FOUND_CONFIGURATION: return executeUpdateConfiguration(message);
    default:                  return false;
    }
}

bool OperationExecutor::executeRelayStateUpdate(bool relayState)
{
    if (relayState)
        _deviceFeedback.closeRelay();
    else
        _deviceFeedback.openRelay();
    return true;
}

bool OperationExecutor::executeMessageDisplay(const char *message)
{
    _lcdDisplay.setFirstLine(message);
    return true;
}

bool OperationExecutor::executeUpdateConfiguration(const char *config)
{
    return _configurationSynchronization.updateConfiguration(config);
}