
Own fork of MbedSmartRestMain
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
config/ConfigSync.cpp
- Committer:
- xinlei
- Date:
- 2015-05-07
- Revision:
- 98:e369fc75c000
- Child:
- 99:47ea098f8a47
File content as of revision 98:e369fc75c000:
#include <stdio.h> #include "ConfigSync.h" #include "DeviceMemory.h" #include "ComposedRecord.h" #include "CharValue.h" #include "IntegerValue.h" #include "logging.h" ConfigSync::ConfigSync( AbstractSmartRest& client, SmartRestTemplate& tpl, long& deviceId) : _init (false), _changed (false), _tpl(tpl), _client(client), _deviceId(deviceId), _deviceConfiguration(), _configurationProperties(_deviceConfiguration) { } bool ConfigSync::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 ConfigSync::integrate() { if ((!loadConfiguration()) || (!_configurationProperties.validateProperties())) { if ((!_configurationProperties.resetConfiguration()) || (!updateDeviceObject()) || (!saveConfiguration())) return false; } else { if (!updateDeviceObject()) return false; } return true; } bool ConfigSync::run() { if (!_changed) return true; if ((!updateDeviceObject()) || (!saveConfiguration())) return false; _changed = false; return true; } bool ConfigSync::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 ConfigSync::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 ConfigSync::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 ConfigSync::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; }