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

Revision:
101:dbcd3bc51758
Parent:
100:47ea098f8a47
Child:
103:ede6611e064e
--- a/config/ConfigSync.cpp	Thu May 07 13:56:19 2015 +0000
+++ b/config/ConfigSync.cpp	Fri May 08 12:19:57 2015 +0000
@@ -1,143 +1,68 @@
 #include <stdio.h>
-#include "ConfigSync.h"
 #include "DeviceMemory.h"
-#include "ComposedRecord.h"
-#include "CharValue.h"
-#include "IntegerValue.h"
+#include "ConfigSync.h"
+#include "SmartRestConf.h"
 #include "logging.h"
 
-ConfigSync::ConfigSync(
-    AbstractSmartRest& client, SmartRestTemplate& tpl) :
-    _init (false),
-    _changed (false),
-    _tpl(tpl),
-    _client(client),
-    _deviceConfiguration(),
-    _configurationProperties(_deviceConfiguration)
+// default connectivity checking interval [minutes].
+#define INTERVAL_KEY "interval"
+#define DEFAULT_INTERVAL "20"
+
+bool validateConfiguration(Dict& d)
 {
+        return d.get(INTERVAL_KEY);
 }
 
-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()
+size_t ConfigSync::read(char *buf, size_t maxLen, char *line, size_t num)
 {
-    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;
+        static const char *fmt = "130,%ld,%s,%.*s\r\n";
+        int l = 0;
+        if (changed) {
+                changed = false;
+                char s[MAX_ITEM_SIZE*(MAX_KEY_LEN+MAX_VALUE_LEN+2)+1];
+                dict.dump(s);
+                const char *p = dict.get("interval")->value;
+                l = snprintf(buf, maxLen, fmt, deviceID, s, MAX_VALUE_LEN, p);
+                snprintf(line, num, "%s", "Sync Config");
+        }
+        return l;
 }
 
-bool ConfigSync::updateConfiguration(const char *cfg)
+bool ConfigSync::updateConfiguration(const char *buf)
 {
-    if (!_deviceConfiguration.read(cfg)) {
-        aError("Read device config.\n");
-        return false;
-    }
-    
-    if (!_configurationProperties.validateProperties()) {
-        loadConfiguration();
-        return false;
-    }
-    
-    _changed = true;
-    return true;
+        bool b = cp.parse(buf);
+        if (b && validateConfiguration(cp.dict)) {
+                dict = cp.dict;
+                changed = true;
+                saveConfiguration();
+        }
+        return b;
+}
+
+void ConfigSync::resetConfiguration()
+{
+        dict.clear();
+        dict.set(INTERVAL_KEY, DEFAULT_INTERVAL);
+        aDebug("Reset conf\n");
+        changed = true;
 }
 
-bool ConfigSync::updateDeviceObject()
+void ConfigSync::loadConfiguration()
 {
-    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;
+        char buf[(MAX_KEY_LEN+MAX_VALUE_LEN)*MAX_ITEM_SIZE+1];
+        int l = loadConfigFile(buf, sizeof(buf));
+        if (l > 0) {
+                updateConfiguration(buf);
+        }
 }
 
-bool ConfigSync::loadConfiguration()
+void ConfigSync::saveConfiguration() const
 {
-    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;
-}
+        char s[dict.size()*(MAX_KEY_LEN+MAX_VALUE_LEN+2)+1];
+        size_t l = dict.dump(s);
+        if (l) {
+                if (!saveConfigFile(s, l)) {
+                        aError("Save config.\n");
+                }
+        }
+}
\ No newline at end of file