portable version of the cumulocity demo

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Cumulocity Official

Revision:
4:363b4cc49445
Parent:
3:32834ef7cb56
Child:
5:dee05a7c70f9
--- a/main.cpp	Thu Jan 30 11:47:29 2014 +0000
+++ b/main.cpp	Sun Feb 02 16:40:59 2014 +0000
@@ -3,71 +3,169 @@
 #include "UbloxUSBGSMModem.h"
 #include "UbloxUSBCDMAModem.h"
 
-#include "HTTPClient.h"
+#include "MbedSmartRest.h"
+#include "StaticData.h"
+#include "ComposedRecord.h"
+#include "CharValue.h"
+#include "IntegerValue.h"
+
+const char * const serialNumber = "curltest-1234567";
+StaticData srtpl(
+// get device by identity
+// Usage: 100,<SERIAL/NR>
+"10,100,GET,/identity/externalIds/c8y_Serial/%%,,application/vnd.com.nsn.cumulocity.externalId+json,%%,STRING,\r\n"
+// get device id from identity
+// Response: 200,<DEVICE/ID>
+"11,200,\"$.managedObject\",,\"$.id\"\r\n"
+// Create device
+// Usage: 101,<SERIAL/NR>
+"10,101,POST,/inventory/managedObjects,application/vnd.com.nsn.cumulocity.managedObject+json,application/vnd.com.nsn.cumulocity.managedObject+json,%%,STRING,\"{\"\"name\"\":\"\"Curl Test Device\"\",\"\"type\"\":\"\"com_yourcompany?CurlDevice_1.0\"\",\"\"c8y_Hardware\"\":{\"\"revision\"\":\"\"1\"\",\"\"model\"\":\"\"Curl Test Device\"\",\"\"serialNumber\"\":\"\"%%\"\"},\"\"c8y_IsDevice\"\":{}}\"\r\n"
+// Get device id
+// Response: 201,<DEVICE/ID>
+"11,201,,\"$.c8y_IsDevice\",\"$.id\"\r\n"
+// Insert global ID
+// Usage: 102,<DEVICE/ID>,<SERIAL/NR>
+"10,102,POST,/identity/globalIds/%%/externalIds,application/vnd.com.nsn.cumulocity.externalId+json,application/vnd.com.nsn.cumulocity.externalId+json,%%,UNSIGNED STRING,\"{\"\"type\"\":\"\"c8y_Serial\"\",\"\"externalId\"\":\"\"%%\"\"}\"\r\n"
+);
+
+long existing();
+long create();
+bool identify(long deviceId);
 
 C027 c027;
-
-void test(void const*)
-{
-    c027.mdmPower(true);
-    UbloxUSBGSMModem modem; // for LISA-C use the UbloxUSBCDMAModem instead
-    HTTPClient http;
-    char str[512];
-
-    int ret = modem.connect("public4.m2minternet.com"); // eventaully set another apn here
-    if(ret)
-    {
-        printf("Could not connect %d\n", ret);
-        return;
-    }
-    
-    http.basicAuth("vaillant/admin", "klanpi");
-    
-    char *hkey = "X-Id";char *hval = "com_test_device";
-    char *harr[] = {hkey, hval};
-    
-    http.customHeaders(harr, 1);
-    
-    //GET data
-    printf("Trying to fetch page...\n");
-    ret = http.get("http://nocore.info:8888", str, 128);
-    if (!ret) {
-        printf("Page fetched successfully - read %d characters\n", strlen(str));
-        printf("Result: %s\n", str);
-    } else {
-        printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
-    }
-
-    //POST data
-    HTTPMap map;
-    HTTPText text(str, 512);
-    map.put("Hello", "World");
-    map.put("test", "1234");
-    printf("Trying to post data...\n");
-    ret = http.post("http://nocore.info:8888", map, &text);
-    if (!ret) {
-        printf("Executed POST successfully - read %d characters\n", strlen(str));
-        printf("Result: %s\n", str);
-    } else {
-        printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
-    }
-
-    modem.disconnect();
-    c027.mdmPower(false);
-    
-    while(1) {
-    }
-}
-
+MbedSmartRest client("http://developer.cumulocity.com/s", "vaillant/admin", "klanpi", "com_cumulocity_MbedTestDevice_1.0");
+//MbedSmartRest client("http://nocore.info:8888/", "vaillant/admin", "klanpi", "com_cumulocity_MbedTestDevice_1.0");
 
 int main()
 {
-    Thread testTask(test, NULL, osPriorityNormal, 1024 * 4);
-    DigitalOut led(LED); // on rev A you should reasign the signal to A0
-    while(1) {
-        led=!led;
-        Thread::wait(1000);
+    long deviceId;
+
+    c027.mdmPower(true);
+    UbloxUSBGSMModem modem;
+
+    if (modem.connect("public4.m2minternet.com")) {
+        puts("GPRS connection failure.");
+        return 2;
+    }
+
+    puts("Bootstrapping");
+    if (client.bootstrap(srtpl) != SMARTREST_SUCCESS) {
+        puts("Bootstrapping failed.");
+        return 2;
+    }
+
+    if ((deviceId = existing()) == 0) {
+        deviceId = create();
+        if (deviceId != 0)
+            identify(deviceId);
+    }
+
+    printf("Device ID: %ld\r\n", deviceId);
+
+    modem.disconnect();
+    c027.mdmPower(false);    
+    return 0;
+}
+
+long existing()
+{
+    ComposedRecord newMoRec(true); // set copy=true b/c tmp objects
+    ParsedRecord received;
+
+    puts("Checking for device existance...");
+
+    newMoRec.add(IntegerValue(100)).add(CharValue(serialNumber));
+
+    if (client.send(newMoRec) != SMARTREST_SUCCESS) {
+        puts("Send failed.");
+        return 0;
+    }
+
+    if (client.receive(received) != SMARTREST_SUCCESS) {
+        puts("No device found.");
+        return 0;
+    }
+
+    if (received.values() == 0) {
+        puts("Received no values.");
+        return 0;
+    }
+    if (received.value(0).integerValue() == 50) {
+        client.stop();
+        return 0;
+    }
+    
+    if (received.value(0).integerValue() != 200) {
+        puts("Bad response.");
+        return 0;
     }
 
-    return 0;
+    client.stop();
+    return received.value(2).integerValue();
 }
+
+long create()
+{
+    ComposedRecord newMoRec(true); // set copy=true b/c tmp objects
+    ParsedRecord received;
+
+    puts("Creating device...");
+
+    newMoRec.add(IntegerValue(101)).add(CharValue(serialNumber));
+
+    if (client.send(newMoRec) != SMARTREST_SUCCESS) {
+        puts("Send failed.");
+        return 0;
+    }
+
+    if (client.receive(received) != SMARTREST_SUCCESS) {
+        puts("No device found.");
+        return 0;
+    }
+
+    if (received.values() != 3) {
+        puts("Bad received data.");
+        return 0;
+    }
+    
+    if (received.value(0).integerValue() != 201) {
+        puts("Bad received data.");
+        return 0;
+    }
+
+    client.stop();
+    return received.value(2).integerValue();
+}
+
+bool identify(long deviceId)
+{
+    ComposedRecord newMoRec(true); // set copy=true b/c tmp objects
+    ParsedRecord received;
+
+    puts("Adding global identifier...");
+
+    newMoRec.add(IntegerValue(102)).add(IntegerValue(deviceId)).add(CharValue(serialNumber));
+
+    if (client.send(newMoRec) != SMARTREST_SUCCESS) {
+        puts("Sending failed.");
+        return false;
+    }
+
+    if (client.receive(received) != SMARTREST_SUCCESS) {
+        puts("Failed.");
+        return false;
+    }
+
+    if (received.values() != 3) {
+        puts("Received bad data.");
+        return false;
+    }
+    
+    if (received.value(0).integerValue() != 200) {
+        puts("Received bad data.");
+        return false;
+    }
+
+    client.stop();
+    return true;
+}