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:
Thu Oct 30 14:46:22 2014 +0000
Parent:
65:a62dbef2f924
Child:
67:c360a2b2c948
Commit message:
message on lcd operation

Changed in this revision

DeviceIntegration.cpp Show annotated file Show diff for this revision Revisions of this file
MbedAgent.h Show annotated file Show diff for this revision Revisions of this file
operation/OperationExecutor.cpp Show annotated file Show diff for this revision Revisions of this file
operation/OperationExecutor.h Show annotated file Show diff for this revision Revisions of this file
--- a/DeviceIntegration.cpp	Thu Oct 30 13:38:20 2014 +0000
+++ b/DeviceIntegration.cpp	Thu Oct 30 14:46:22 2014 +0000
@@ -30,7 +30,7 @@
 
     // Create device
     // Usage: 101,<SERIAL/NR>
-    if (!_tpl.add("10,101,POST,/inventory/managedObjects,application/vnd.com.nsn.cumulocity.managedObject+json,application/vnd.com.nsn.cumulocity.managedObject+json,%%,STRING,\"{\"\"name\"\":\"\"Mbed Test Device\"\",\"\"type\"\":\"\"com_ublox_C027_REV-A\"\",\"\"c8y_Hardware\"\":{\"\"revision\"\":\"\"1\"\",\"\"model\"\":\"\"Ublox C027\"\",\"\"serialNumber\"\":\"\"%%\"\"},\"\"c8y_SupportedMeasurements\"\":[\"\"c8y_SignalStrength\"\",\"\"c8y_TemperatureMeasurement\"\",\"\"c8y_AnalogMeasurement\"\",\"\"c8y_MotionMeasurement\"\",\"\"c8y_AnalogMeasurement\"\"],\"\"c8y_RequiredAvailability\"\":{ \"\"responseInterval\"\":15},\"\"c8y_IsDevice\"\":{},\"\"com_cumulocity_model_Agent\"\":{}}\"\r\n"))
+    if (!_tpl.add("10,101,POST,/inventory/managedObjects,application/vnd.com.nsn.cumulocity.managedObject+json,application/vnd.com.nsn.cumulocity.managedObject+json,%%,STRING,\"{\"\"name\"\":\"\"Mbed Test Device\"\",\"\"type\"\":\"\"com_ublox_C027_REV-A\"\",\"\"c8y_Hardware\"\":{\"\"revision\"\":\"\"1\"\",\"\"model\"\":\"\"Ublox C027\"\",\"\"serialNumber\"\":\"\"%%\"\"},\"\"c8y_SupportedMeasurements\"\":[\"\"c8y_SignalStrength\"\",\"\"c8y_TemperatureMeasurement\"\",\"\"c8y_AnalogMeasurement\"\",\"\"c8y_MotionMeasurement\"\",\"\"c8y_AnalogMeasurement\"\"],\"\"c8y_SupportedOperations\"\":[\"\"c8y_Relay\"\",\"\"c8y_Message\"\"],\"\"c8y_RequiredAvailability\"\":{ \"\"responseInterval\"\":15},\"\"c8y_IsDevice\"\":{},\"\"com_cumulocity_model_Agent\"\":{}}\"\r\n"))
         return false;
 
     // Get device id
--- a/MbedAgent.h	Thu Oct 30 13:38:20 2014 +0000
+++ b/MbedAgent.h	Thu Oct 30 14:46:22 2014 +0000
@@ -18,8 +18,8 @@
 
 #define MBED_AGENT_HOST "developer.cumulocity.com"
 #define MBED_AGENT_PORT 80
-#define MBED_AGENT_DEVICE_IDENTIFIER "com_cumulocity_MbedAgent_1.4.1"
-#define MBED_AGENT_INTERVAL 60.0
+#define MBED_AGENT_DEVICE_IDENTIFIER "com_cumulocity_MbedAgent_1.4.2"
+#define MBED_AGENT_INTERVAL 30.0
 
 class MbedAgent
 {
--- a/operation/OperationExecutor.cpp	Thu Oct 30 13:38:20 2014 +0000
+++ b/operation/OperationExecutor.cpp	Thu Oct 30 14:46:22 2014 +0000
@@ -5,6 +5,9 @@
 #include <string.h>
 #include <stdio.h>
 
+#define FOUND_RELAY 1
+#define FOUND_MESSAGE 2
+
 OperationExecutor::OperationExecutor(AbstractSmartRest& client, SmartRestTemplate& tpl, long& deviceId, DeviceIO& io) :
     _client(client),
     _tpl(tpl),
@@ -25,17 +28,22 @@
         return false;
 
     // Relay operation response
-    // Response: 211,<OPERATION/ID>,<STATUS>
+    // Response: 220,<OPERATION/ID>,<STATUS>
     if (!_tpl.add("11,220,,\"$.c8y_Relay\",\"$.id\",\"$.c8y_Relay.relayState\"\r\n"))
         return false;
 
+    // Message operation response
+    // Response: 221,<OPERATION/ID>,<MESSAGE>
+    if (!_tpl.add("11,221,,\"$.c8y_Message\",\"$.id\",\"$.c8y_Message.text\"\r\n"))
+        return false;
+
     _init = true;
     return true;
 }
 
 bool OperationExecutor::executeOperation(OperationStore::Operation& op)
 {
-    uint8_t ret; bool found, relayState;
+    uint8_t ret; uint8_t found; bool relayState; char message[128];
     ComposedRecord record;
     ParsedRecord received;
     
@@ -49,7 +57,7 @@
         return false;
     }
     
-    found = false;
+    found = 0;
     while ((ret = _client.receive(received)) == SMARTREST_SUCCESS) {
         if ((received.values() == 4) &&
             (received.value(0).valueType() == VALUE_INTEGER) &&
@@ -58,16 +66,32 @@
             (received.value(2).integerValue() == op.identifier) &&
             (received.value(3).valueType() == VALUE_CHARACTER)) {
             relayState = (strcmp("CLOSED", received.value(3).characterValue()) == 0);
-            found = true;
+            found = FOUND_RELAY;
+            break;
+        }
+        
+        if ((received.values() == 4) &&
+            (received.value(0).valueType() == VALUE_INTEGER) &&
+            (received.value(0).integerValue() == 221) &&
+            (received.value(2).valueType() == VALUE_INTEGER) &&
+            (received.value(2).integerValue() == op.identifier) &&
+            (received.value(3).valueType() == VALUE_CHARACTER)) {
+            strncpy(message, received.value(3).characterValue(), 128);
+            message[127] = '\0';
+            found = FOUND_MESSAGE;
             break;
         }
     }
     _client.stop();
 
-    if (!found)
+    switch (found) {
+    case FOUND_RELAY:
+        return executeRelayStateUpdate(relayState);
+    case FOUND_MESSAGE:
+        return executeMessageDisplay(message);
+    default:
         return false;
-
-    return executeRelayStateUpdate(relayState);
+    }
 }
 
 bool OperationExecutor::executeRelayStateUpdate(bool relayState)
@@ -78,3 +102,9 @@
         _io.deviceFeedback().openRelay();
     return true;
 }
+
+bool OperationExecutor::executeMessageDisplay(const char *message)
+{
+    _io.lcdPrint(message);
+    return true;
+}
--- a/operation/OperationExecutor.h	Thu Oct 30 13:38:20 2014 +0000
+++ b/operation/OperationExecutor.h	Thu Oct 30 14:46:22 2014 +0000
@@ -16,6 +16,7 @@
     
 protected:
     bool executeRelayStateUpdate(bool relayState);
+    bool executeMessageDisplay(const char *message);
 
 private:
     bool _init;