Own fork of MbedSmartRestMain

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Cumulocity Official

Committer:
xinlei
Date:
Wed May 13 12:12:55 2015 +0000
Revision:
106:fc5f25f0e0d5
Parent:
100:dbcd3bc51758
Child:
107:f1ee3e1eb126
OperationSupport revamped

Who changed what in which revision?

UserRevisionLine numberNew contents of line
xinlei 77:f6717e4eccc4 1 #include <string.h>
vwochnik 57:4af5f1bec3a6 2 #include "OperationSupport.h"
xinlei 106:fc5f25f0e0d5 3 //#include "ComposedRecord.h"
xinlei 106:fc5f25f0e0d5 4 //#include "ParsedRecord.h"
xinlei 106:fc5f25f0e0d5 5 //#include "IntegerValue.h"
xinlei 106:fc5f25f0e0d5 6 #include "SmartRestSocket.h"
xinlei 106:fc5f25f0e0d5 7 #include "ControlParser.h"
xinlei 92:0acd11870c6a 8 #include "logging.h"
xinlei 92:0acd11870c6a 9
vwochnik 57:4af5f1bec3a6 10 bool OperationSupport::init()
vwochnik 57:4af5f1bec3a6 11 {
vwochnik 57:4af5f1bec3a6 12 if (_init)
vwochnik 57:4af5f1bec3a6 13 return false;
vwochnik 57:4af5f1bec3a6 14
vwochnik 57:4af5f1bec3a6 15 // Get pending operations
vwochnik 57:4af5f1bec3a6 16 // USAGE: 110,<DEVICE/ID>
vwochnik 57:4af5f1bec3a6 17 if (!_tpl.add("10,110,GET,/devicecontrol/operations?status=PENDING&deviceId=%%&pageSize=100,,application/vnd.com.nsn.cumulocity.operationCollection+json,%%,UNSIGNED,\r\n"))
vwochnik 57:4af5f1bec3a6 18 return false;
vwochnik 57:4af5f1bec3a6 19
vwochnik 57:4af5f1bec3a6 20 // Set operation state
xinlei 92:0acd11870c6a 21 // USAGE: 111,<OPERATION/ID>,<STATE>
vwochnik 58:4cc0ae5a7058 22 if (!_tpl.add("10,111,PUT,/devicecontrol/operations/%%,application/vnd.com.nsn.cumulocity.operation+json,application/vnd.com.nsn.cumulocity.operation+json,%%,UNSIGNED STRING,\"{\"\"status\"\":\"\"%%\"\"}\"\r\n"))
vwochnik 57:4af5f1bec3a6 23 return false;
vwochnik 57:4af5f1bec3a6 24
vwochnik 57:4af5f1bec3a6 25 // Get operations
vwochnik 57:4af5f1bec3a6 26 // Response: 210,<OPERATION/ID>,<STATUS>
vwochnik 57:4af5f1bec3a6 27 if (!_tpl.add("11,210,\"$.operations\",,\"$.id\",\"$.status\"\r\n"))
vwochnik 57:4af5f1bec3a6 28 return false;
vwochnik 57:4af5f1bec3a6 29
vwochnik 57:4af5f1bec3a6 30 // Get operation
vwochnik 57:4af5f1bec3a6 31 // Response: 211,<OPERATION/ID>,<STATUS>
vwochnik 57:4af5f1bec3a6 32 if (!_tpl.add("11,211,,\"$.deviceId\",\"$.id\",\"$.status\"\r\n"))
vwochnik 57:4af5f1bec3a6 33 return false;
vwochnik 63:010bbbb4732a 34
xinlei 93:61d44636f020 35 // Get operation by id
xinlei 93:61d44636f020 36 // USAGE: 112,<OPERATION/ID>
xinlei 93:61d44636f020 37 if (!_tpl.add("10,112,GET,/devicecontrol/operations/%%,,application/vnd.com.nsn.cumulocity.operation+json,%%,UNSIGNED,\r\n"))
xinlei 93:61d44636f020 38 return false;
xinlei 93:61d44636f020 39
xinlei 93:61d44636f020 40 // Relay operation response
xinlei 93:61d44636f020 41 // Response: 220,<OPERATION/ID>,<STATUS>
xinlei 93:61d44636f020 42 if (!_tpl.add("11,220,,\"$.c8y_Relay\",\"$.id\",\"$.c8y_Relay.relayState\"\r\n"))
xinlei 93:61d44636f020 43 return false;
xinlei 93:61d44636f020 44
xinlei 93:61d44636f020 45 // Message operation response
xinlei 93:61d44636f020 46 // Response: 221,<OPERATION/ID>,<MESSAGE>
xinlei 93:61d44636f020 47 if (!_tpl.add("11,221,,\"$.c8y_Message\",\"$.id\",\"$.c8y_Message.text\"\r\n"))
xinlei 93:61d44636f020 48 return false;
xinlei 93:61d44636f020 49
xinlei 93:61d44636f020 50 // Configuration operation response
xinlei 93:61d44636f020 51 // Response: 222,<OPERATION/ID>,<CONFIGURATION/STRING>
xinlei 93:61d44636f020 52 if (!_tpl.add("11,222,,\"$.c8y_Configuration\",\"$.id\",\"$.c8y_Configuration.config\"\r\n"))
vwochnik 63:010bbbb4732a 53 return false;
vwochnik 57:4af5f1bec3a6 54
vwochnik 57:4af5f1bec3a6 55 _init = true;
vwochnik 57:4af5f1bec3a6 56 return true;
vwochnik 57:4af5f1bec3a6 57 }
vwochnik 57:4af5f1bec3a6 58
xinlei 106:fc5f25f0e0d5 59 bool OperationSupport::executePendingOperations()
vwochnik 63:010bbbb4732a 60 {
xinlei 106:fc5f25f0e0d5 61 char buf[SMARTREST_SIZE];
xinlei 106:fc5f25f0e0d5 62 char buf2[SMARTREST_BODY_SIZE];
xinlei 106:fc5f25f0e0d5 63 SmartRestSocket sock;
xinlei 106:fc5f25f0e0d5 64 int l = snprintf(buf2, sizeof(buf2), "110,%ld\r\n", deviceID);
xinlei 106:fc5f25f0e0d5 65 l = snprintf(buf, sizeof(buf), fmtSmartRest, "/s", l, buf2);
xinlei 106:fc5f25f0e0d5 66 l = sock.sendAndReceive(buf, l, sizeof(buf));
xinlei 106:fc5f25f0e0d5 67 if (l < 0) {
xinlei 106:fc5f25f0e0d5 68 aError("Retrieve pending operations\n");
xinlei 106:fc5f25f0e0d5 69 return false;
vwochnik 61:15719dbe8820 70 }
xinlei 106:fc5f25f0e0d5 71 const size_t N = 10;
xinlei 106:fc5f25f0e0d5 72 long opl[N];
xinlei 106:fc5f25f0e0d5 73 size_t i = 0;
xinlei 106:fc5f25f0e0d5 74 for (const char* p = skipHTTPHeader(buf); i<N && *p;) {
xinlei 106:fc5f25f0e0d5 75 long id = 0;
xinlei 106:fc5f25f0e0d5 76 int c = 0;
xinlei 106:fc5f25f0e0d5 77 int n = sscanf(p, "210,1,%ld,PENDING\r\n%n", &id, &c);
xinlei 106:fc5f25f0e0d5 78 if (n == 1) {
xinlei 106:fc5f25f0e0d5 79 opl[i++] = id;
xinlei 106:fc5f25f0e0d5 80 p += c;
xinlei 106:fc5f25f0e0d5 81 } else
xinlei 106:fc5f25f0e0d5 82 break;
xinlei 106:fc5f25f0e0d5 83 }
xinlei 106:fc5f25f0e0d5 84 if (i >= N)
xinlei 106:fc5f25f0e0d5 85 aWarning("Over %u pending operations.\n", N);
xinlei 106:fc5f25f0e0d5 86 ControlParser cp(opool, conf);
xinlei 106:fc5f25f0e0d5 87 bool flag = true;
xinlei 106:fc5f25f0e0d5 88 l = 0;
xinlei 106:fc5f25f0e0d5 89 for (size_t j = 0; j < i; ++j) {
xinlei 106:fc5f25f0e0d5 90 l += snprintf(buf2+l, sizeof(buf2)-l, "112,%ld\r\n", opl[j]);
xinlei 106:fc5f25f0e0d5 91 }
xinlei 106:fc5f25f0e0d5 92 l = snprintf(buf, sizeof(buf), fmtSmartRest, "/s", l, buf2);
xinlei 106:fc5f25f0e0d5 93 l = sock.sendAndReceive(buf, l, sizeof(buf));
xinlei 106:fc5f25f0e0d5 94 if (l >= 0)
xinlei 106:fc5f25f0e0d5 95 cp.parse(buf);
xinlei 106:fc5f25f0e0d5 96 else
xinlei 106:fc5f25f0e0d5 97 flag = false;
xinlei 106:fc5f25f0e0d5 98 return flag;
xinlei 93:61d44636f020 99 }