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

Files at this revision

API Documentation at this revision

Comitter:
vwochnik
Date:
Fri Oct 24 11:30:43 2014 +0000
Parent:
58:4cc0ae5a7058
Child:
60:3c822f97fc73
Commit message:
operation support

Changed in this revision

operation/OperationStore.h Show annotated file Show diff for this revision Revisions of this file
operation/OperationSupport.cpp Show annotated file Show diff for this revision Revisions of this file
operation/OperationSupport.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/operation/OperationStore.h	Fri Oct 24 11:30:43 2014 +0000
@@ -0,0 +1,19 @@
+#ifndef OPERATIONSTORE_H
+#define OPERATIONSTORE_H
+
+#define STATE_PENDING 0
+#define STATE_EXECUTING 1
+#define STATE_SUCCESSFUL 2
+#define STATE_FAILED 3
+
+class OperationStore
+{
+public:
+    struct Operation {
+        unsigned long identifier;
+        const char *description;
+        uint8_t state;
+    };
+};
+
+#endif
\ No newline at end of file
--- a/operation/OperationSupport.cpp	Fri Oct 24 08:55:17 2014 +0000
+++ b/operation/OperationSupport.cpp	Fri Oct 24 11:30:43 2014 +0000
@@ -1,8 +1,14 @@
 #include "OperationSupport.h"
+#include <string.h>
 #include "ComposedRecord.h"
 #include "CharValue.h"
 #include "IntegerValue.h"
 
+CharValue aOperationStatePending("PENDING");
+CharValue aOperationStateExecuting("EXECUTING");
+CharValue aOperationStateSuccessful("SUCCESSFUL");
+CharValue aOperationStateFailed("FAILED");
+
 OperationSupport::OperationSupport(AbstractSmartRest& client, SmartRestTemplate& tpl, long& deviceId) :
     _client(client),
     _tpl(tpl),
@@ -42,4 +48,85 @@
 
 bool OperationSupport::run()
 {
+    ComposedRecord record;
+    ParsedRecord received;
+    
+    IntegerValue msgId(110);
+    IntegerValue deviceId(_deviceId);
+    if ((!record.add(msgId)) || (!record.add(deviceId)))
+        return false;
+        
+    if (_client.send(record) != SMARTREST_SUCCESS) {
+        _client.stop();
+        return false;
+    }
+    _client.stop();
+    
 }
+
+bool operationFromRecord(ParsedRecord& received, OperationStore::Operation& op)
+{
+    const char *tmp;
+
+    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;
+    
+    tmp = received.value(3).characterValue();
+    op.identifier = received.value(2).integerValue();
+
+    if (strcmp(tmp, "EXECUTING") == 0)
+        op.state = STATE_EXECUTING;
+    else if (strcmp(tmp, "SUCCESSFUL") == 0)
+        op.state = STATE_SUCCESSFUL;
+    else if (strcmp(tmp, "FAILED") == 0)
+        op.state = STATE_FAILED;
+    else
+        op.state = STATE_PENDING;
+    
+    return true;
+}
+
+bool OperationSupport::updateOperation(OperationStore::Operation& op)
+{
+    ComposedRecord record;
+    ParsedRecord received;
+    
+    IntegerValue msgId(111);
+    IntegerValue operationId(op.identifier);
+    if ((!record.add(msgId)) || (!record.add(operationId)) ||
+        (!record.add(operationStateValue(op))))
+        return false;
+    
+    if (_client.send(record) != SMARTREST_SUCCESS) {
+        _client.stop();
+        return false;
+    }
+    
+    if (_client.receive(received) != SMARTREST_SUCCESS) {
+        _client.stop();
+        return false;
+    }
+    
+    _client.stop();
+    
+    //TODO: assertions
+    return true;
+}
+
+CharValue& OperationSupport::operationStateValue(OperationStore::Operation& op)
+{
+    switch (op.state) {
+    case STATE_EXECUTING:
+        return aOperationStateExecuting;
+    case STATE_SUCCESSFUL:
+        return aOperationStateSuccessful;
+    case STATE_FAILED:
+        return aOperationStateFailed;
+    default:
+        return aOperationStatePending;
+    }
+}
--- a/operation/OperationSupport.h	Fri Oct 24 08:55:17 2014 +0000
+++ b/operation/OperationSupport.h	Fri Oct 24 11:30:43 2014 +0000
@@ -3,6 +3,10 @@
 
 #include "AbstractSmartRest.h"
 #include "SmartRestTemplate.h"
+#include "ComposedRecord.h"
+#include "CharValue.h"
+#include "OperationStore.h"
+#include "ParsedRecord.h"
 
 class OperationSupport
 {
@@ -12,6 +16,11 @@
     bool init();
     bool run();
 
+protected:
+    bool operationFromRecord(ParsedRecord&, OperationStore::Operation&);
+    bool updateOperation(OperationStore::Operation&);
+    CharValue& operationStateValue(OperationStore::Operation&);
+
 private:
     bool _init;
     long& _deviceId;
@@ -19,4 +28,9 @@
     AbstractSmartRest& _client;
 };
 
+extern CharValue aOperationStatePending;
+extern CharValue aOperationStateExecuting;
+extern CharValue aOperationStateSuccessful;
+extern CharValue aOperationStateFailed;
+
 #endif
\ No newline at end of file