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:
107:fc5f25f0e0d5
Parent:
101:dbcd3bc51758
Child:
108:f1ee3e1eb126
--- a/operation/OperationSupport.cpp	Mon May 11 18:10:44 2015 +0000
+++ b/operation/OperationSupport.cpp	Wed May 13 12:12:55 2015 +0000
@@ -1,9 +1,10 @@
 #include <string.h>
 #include "OperationSupport.h"
-#include "ComposedRecord.h"
-#include "ParsedRecord.h"
-#include "IntegerValue.h"
-#include "SmartRestConf.h"
+//#include "ComposedRecord.h"
+//#include "ParsedRecord.h"
+//#include "IntegerValue.h"
+#include "SmartRestSocket.h"
+#include "ControlParser.h"
 #include "logging.h"
 
 bool OperationSupport::init()
@@ -55,101 +56,44 @@
     return true;
 }
 
-bool OperationSupport::run()
-{
-    if (_firstRun) {
-        _firstRun = false;
-        bool b = requestPendingOperations();
-        return b;
-    } else {
-        return true;
-    }
-}
-
-bool OperationSupport::executePendingOperation(Operation& op)
-{
-    ComposedRecord r;
-    if (!r.add(IntegerValue(112)) || !r.add(IntegerValue(op.identifier))) {
-        return false;
-    } else if (_client.send(r) != SMARTREST_SUCCESS) {
-        _client.stop();
-        return false;
-    }
-    ParsedRecord p;
-    bool b = true;
-    while (_client.receive(p) == SMARTREST_SUCCESS) {
-        if (p.values() >= 3 &&
-            p.value(0).integerValue() >= 220 &&
-            p.value(0).integerValue() <= 222 &&
-            p.value(2).valueType() == VALUE_INTEGER) {
-            Operation* op1 = opool.alloc();
-            op1->identifier = p.value(2).integerValue();
-//            bool ret = _executor.executeOperation(p);
-            bool ret = true;
-            op1->state = ret ? OPERATION_SUCCESSFUL : OPERATION_FAILED;
-            opool.put(op1);
-        }
-    }
-    _client.stop();
-    return b;
-}
-
-bool OperationSupport::requestPendingOperations()
+bool OperationSupport::executePendingOperations()
 {
-    IntegerValue msgId(110);
-    ComposedRecord record;
-
-    if (!record.add(msgId) || !record.add(IntegerValue(deviceID)))
-        return false;
-    else if (_client.send(record) != SMARTREST_SUCCESS) {
-        _client.stop();
-        return false;
-    }
-
-    uint8_t ret;
-    ParsedRecord received;
-    Operation opl[10];
-    size_t c = 0;
-    while ((ret=_client.receive(received)) == SMARTREST_SUCCESS) {
-        if (c < 10 && operationFromRecord(received, opl[c])) {
-            ++c;
-        } else {
-            aWarning("Ignored pending operation after 10.\n");
-            break;
+        char buf[SMARTREST_SIZE];
+        char buf2[SMARTREST_BODY_SIZE];
+        SmartRestSocket sock;
+        int l = snprintf(buf2, sizeof(buf2), "110,%ld\r\n", deviceID);
+        l = snprintf(buf, sizeof(buf), fmtSmartRest, "/s", l, buf2);
+        l = sock.sendAndReceive(buf, l, sizeof(buf));
+        if (l < 0) {
+                aError("Retrieve pending operations\n");
+                return false;
         }
-    }
-    _client.stop();
-
-    for (size_t i = 0; i < c; ++i) {
-        Operation* op = opool.alloc();
-        op->identifier = opl[i].identifier;
-        op->state = OPERATION_EXECUTING;
-        opool.put(op);
-        executePendingOperation(opl[i]);
-    }
-    return (ret == SMARTREST_END_OF_RESPONSE || ret == SMARTREST_CONNECTION_CLOSED);
-}
-
-bool OperationSupport::operationFromRecord(ParsedRecord& received, Operation& op)
-{
-    if ((received.values() < 4) ||
-        (received.value(0).valueType() != VALUE_INTEGER) ||
-//        (received.value(0).integerValue() != 211) ||
-        (received.value(2).valueType() != VALUE_INTEGER) ||
-        (received.value(3).valueType() != VALUE_CHARACTER))
-        return false;
-
-    op.identifier = received.value(2).integerValue();
-    const char *tmp = received.value(3).characterValue();
-    if (strcmp(tmp, strExecuting) == 0)
-        op.state = OPERATION_EXECUTING;
-    else if (strcmp(tmp, strSuccessful) == 0)
-        op.state = OPERATION_SUCCESSFUL;
-    else if (strcmp(tmp, strFailed) == 0)
-        op.state = OPERATION_FAILED;
-    else if (strcmp(tmp, strPending) == 0)
-        op.state = OPERATION_PENDING;
-    else
-        return false;
-    return true;
+        const size_t N = 10;
+        long opl[N];
+        size_t i = 0;
+        for (const char* p = skipHTTPHeader(buf); i<N && *p;) {
+                long id = 0;
+                int c = 0;
+                int n = sscanf(p, "210,1,%ld,PENDING\r\n%n", &id, &c);
+                if (n == 1) {
+                        opl[i++] = id;
+                        p += c;
+                } else
+                        break;
+        }
+        if (i >= N)
+                aWarning("Over %u pending operations.\n", N);
+        ControlParser cp(opool, conf);
+        bool flag = true;
+        l = 0;
+        for (size_t j = 0; j < i; ++j) {
+                l += snprintf(buf2+l, sizeof(buf2)-l, "112,%ld\r\n", opl[j]);
+        }
+        l = snprintf(buf, sizeof(buf), fmtSmartRest, "/s", l, buf2);
+        l = sock.sendAndReceive(buf, l, sizeof(buf));
+        if (l >= 0)
+                cp.parse(buf);
+        else
+                flag = false;
+        return flag;
 }
\ No newline at end of file