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

config/ConfigurationSynchronization.cpp

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

File content as of revision 93:0acd11870c6a:

#include <stdio.h>
#include "ConfigurationSynchronization.h"
#include "ComposedRecord.h"
#include "CharValue.h"
#include "IntegerValue.h"
#include "logging.h"

ConfigurationSynchronization::ConfigurationSynchronization(AbstractSmartRest& client, SmartRestTemplate& tpl, long& deviceId, DeviceMemory& deviceMemory, DeviceConfiguration& deviceConfiguration, ConfigurationProperties& configurationProperties) :
    _deviceId(deviceId),
    _tpl(tpl),
    _client(client),
    _deviceMemory(deviceMemory),
    _deviceConfiguration(deviceConfiguration),
    _configurationProperties(configurationProperties)
{
    _init = false;
    _changed = false;
}

bool ConfigurationSynchronization::init()
{
    if (_init)
        return false;
    
    // Update Configuration
    // Usage: 130,<DEVICE/ID>,<CONFIG/STRING>,<RESPONSIBILITY>
    if (!_tpl.add("10,130,PUT,/inventory/managedObjects/%%,application/vnd.com.nsn.cumulocity.managedObject+json,application/vnd.com.nsn.cumulocity.managedObject+json,%%,UNSIGNED STRING NUMBER,\"{\"\"c8y_Configuration\"\":{\"\"config\"\":\"\"%%\"\"},\"\"c8y_RequiredAvailability\"\":{ \"\"responseInterval\"\":%%}}\"\r\n"))
        return false;

    _init = true;
    return true;
}

bool ConfigurationSynchronization::integrate()
{
    if ((!loadConfiguration()) || (!_configurationProperties.validateProperties())) {
        if ((!_configurationProperties.resetConfiguration()) || (!updateDeviceObject()) || (!saveConfiguration()))
            return false;
    } else {
        if (!updateDeviceObject())
            return false;
    }

    return true;
}

bool ConfigurationSynchronization::run()
{
    if (!_changed)
        return true;

    if ((!updateDeviceObject()) || (!saveConfiguration()))
        return false;
    
    _changed = false;
    return true;
}

bool ConfigurationSynchronization::updateConfiguration(const char *cfg)
{
    if (!_deviceConfiguration.read(cfg)) {
        aError("updateConfiguration: Unable to read device configuration!\r\n");
        return false;
    }
    
    if (!_configurationProperties.validateProperties()) {
        loadConfiguration();
        return false;
    }
    
    _changed = true;
    return true;
}

bool ConfigurationSynchronization::updateDeviceObject()
{
    char buf[256];
    
    if (!_deviceConfiguration.write(buf, sizeof(buf))) {
        aError("updateDeviceObject: Unable to write device configuration!\r\n");
        return false;
    }

    ComposedRecord record;
    ParsedRecord received;

    IntegerValue msgId(130);
    IntegerValue deviceId(_deviceId);
    CharValue config(buf);
    IntegerValue responsibility(_configurationProperties.readInterval());
    if (!record.add(msgId) || !record.add(deviceId) || !record.add(config) || !record.add(responsibility)) {
        aWarning("updateDeviceObject: Adding record failed!\r\n");
        return false;
    }
    
    if (_client.send(record) != SMARTREST_SUCCESS) {
        aWarning("Sending configuration failed.\r\n");
        _client.stop();
        return false;
    }
    
    if (_client.receive(received) != SMARTREST_SUCCESS) {
        aWarning("Receiving configuration failed.\r\n");
        _client.stop();
        return false;
    }
    _client.stop();

    if (received.values() != 3) {
        aError("Invalid configuration received, expected 3 values, but %d received.\r\n", received.values());
        return false;
    } else if (received.value(0).integerValue() != 201) {
        aError("updateDeviceObject: Unexpected message ID %ld received.\r\n", received.value(0).integerValue());
        return false;
    }
    return true;
}

bool ConfigurationSynchronization::loadConfiguration()
{
    char buf[256];
    
    if (!_deviceMemory.loadConfiguration(buf, sizeof(buf))) {
        aError("loadConfiguration: Unable to load device configuration!\r\n");
        return false;
    }
    if (!_deviceConfiguration.read(buf)) {
        aError("loadConfiguration: Unable to read device configuration!\r\n");
        return false;
    }
    return true;
}

bool ConfigurationSynchronization::saveConfiguration()
{
    char buf[256];
    
    if (!_deviceConfiguration.write(buf, sizeof(buf))) {
        aError("saveConfiguration: Unable to write device configuration!\r\n");
        return false;
    }
    if (!_deviceMemory.saveConfiguration(buf)) {
        aError("saveConfiguration: Unable to save device configuration!\r\n");
        return false;
    }
    return true;
}