
Own fork of MbedSmartRestMain
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
Diff: config/ConfigurationSynchronization.cpp
- Revision:
- 98:e369fc75c000
- Parent:
- 97:9f2de96941c4
- Child:
- 99:47ea098f8a47
--- a/config/ConfigurationSynchronization.cpp Mon Apr 27 13:30:21 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,146 +0,0 @@ -#include <stdio.h> -#include "ConfigurationSynchronization.h" -#include "DeviceMemory.h" -#include "ComposedRecord.h" -#include "CharValue.h" -#include "IntegerValue.h" -#include "logging.h" - -ConfigurationSynchronization::ConfigurationSynchronization( - AbstractSmartRest& client, SmartRestTemplate& tpl, - long& deviceId) : - _init (false), - _changed (false), - _tpl(tpl), - _client(client), - _deviceId(deviceId), - _deviceConfiguration(), - _configurationProperties(_deviceConfiguration) -{ -} - -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("Read device config.\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("Unable to write config.\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)) { - return false; - } - - if (_client.send(record) != SMARTREST_SUCCESS) { - _client.stop(); - return false; - } - - if (_client.receive(received) != SMARTREST_SUCCESS) { - _client.stop(); - return false; - } - _client.stop(); - - if (received.values() != 3) { - aError("Invalid config, %d (3) values received.\n", received.values()); - return false; - } else if (received.value(0).integerValue() != 201) { - aError("updateDeviceObject: Message ID %ld .\n", received.value(0).integerValue()); - return false; - } - return true; -} - -bool ConfigurationSynchronization::loadConfiguration() -{ - char buf[256]; - - if (!::loadConfiguration(buf, sizeof(buf))) { - aError("Load device config.\n"); - return false; - } - if (!_deviceConfiguration.read(buf)) { - aError("Read device config.\n"); - return false; - } - return true; -} - -bool ConfigurationSynchronization::saveConfiguration() -{ - char buf[256]; - - if (!_deviceConfiguration.write(buf, sizeof(buf))) { - aError("Write device config.\n"); - return false; - } - if (!::saveConfiguration(buf)) { - aError("Save device config.\n"); - return false; - } - return true; -}