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
operation/OperationSupport.cpp@59:f96be79feccd, 2014-10-24 (annotated)
- Committer:
- vwochnik
- Date:
- Fri Oct 24 11:30:43 2014 +0000
- Revision:
- 59:f96be79feccd
- Parent:
- 58:4cc0ae5a7058
- Child:
- 60:3c822f97fc73
operation support
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vwochnik | 57:4af5f1bec3a6 | 1 | #include "OperationSupport.h" |
vwochnik | 59:f96be79feccd | 2 | #include <string.h> |
vwochnik | 57:4af5f1bec3a6 | 3 | #include "ComposedRecord.h" |
vwochnik | 57:4af5f1bec3a6 | 4 | #include "CharValue.h" |
vwochnik | 57:4af5f1bec3a6 | 5 | #include "IntegerValue.h" |
vwochnik | 57:4af5f1bec3a6 | 6 | |
vwochnik | 59:f96be79feccd | 7 | CharValue aOperationStatePending("PENDING"); |
vwochnik | 59:f96be79feccd | 8 | CharValue aOperationStateExecuting("EXECUTING"); |
vwochnik | 59:f96be79feccd | 9 | CharValue aOperationStateSuccessful("SUCCESSFUL"); |
vwochnik | 59:f96be79feccd | 10 | CharValue aOperationStateFailed("FAILED"); |
vwochnik | 59:f96be79feccd | 11 | |
vwochnik | 57:4af5f1bec3a6 | 12 | OperationSupport::OperationSupport(AbstractSmartRest& client, SmartRestTemplate& tpl, long& deviceId) : |
vwochnik | 57:4af5f1bec3a6 | 13 | _client(client), |
vwochnik | 57:4af5f1bec3a6 | 14 | _tpl(tpl), |
vwochnik | 57:4af5f1bec3a6 | 15 | _deviceId(deviceId) |
vwochnik | 57:4af5f1bec3a6 | 16 | { |
vwochnik | 57:4af5f1bec3a6 | 17 | _init = false; |
vwochnik | 57:4af5f1bec3a6 | 18 | } |
vwochnik | 57:4af5f1bec3a6 | 19 | |
vwochnik | 57:4af5f1bec3a6 | 20 | bool OperationSupport::init() |
vwochnik | 57:4af5f1bec3a6 | 21 | { |
vwochnik | 57:4af5f1bec3a6 | 22 | if (_init) |
vwochnik | 57:4af5f1bec3a6 | 23 | return false; |
vwochnik | 57:4af5f1bec3a6 | 24 | |
vwochnik | 57:4af5f1bec3a6 | 25 | // Get pending operations |
vwochnik | 57:4af5f1bec3a6 | 26 | // USAGE: 110,<DEVICE/ID> |
vwochnik | 57:4af5f1bec3a6 | 27 | 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 | 28 | return false; |
vwochnik | 57:4af5f1bec3a6 | 29 | |
vwochnik | 57:4af5f1bec3a6 | 30 | // Set operation state |
vwochnik | 57:4af5f1bec3a6 | 31 | // USAGE: 110,<OPERATION/ID>,<STATE> |
vwochnik | 58:4cc0ae5a7058 | 32 | 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 | 33 | return false; |
vwochnik | 57:4af5f1bec3a6 | 34 | |
vwochnik | 57:4af5f1bec3a6 | 35 | // Get operations |
vwochnik | 57:4af5f1bec3a6 | 36 | // Response: 210,<OPERATION/ID>,<STATUS> |
vwochnik | 57:4af5f1bec3a6 | 37 | if (!_tpl.add("11,210,\"$.operations\",,\"$.id\",\"$.status\"\r\n")) |
vwochnik | 57:4af5f1bec3a6 | 38 | return false; |
vwochnik | 57:4af5f1bec3a6 | 39 | |
vwochnik | 57:4af5f1bec3a6 | 40 | // Get operation |
vwochnik | 57:4af5f1bec3a6 | 41 | // Response: 211,<OPERATION/ID>,<STATUS> |
vwochnik | 57:4af5f1bec3a6 | 42 | if (!_tpl.add("11,211,,\"$.deviceId\",\"$.id\",\"$.status\"\r\n")) |
vwochnik | 57:4af5f1bec3a6 | 43 | return false; |
vwochnik | 57:4af5f1bec3a6 | 44 | |
vwochnik | 57:4af5f1bec3a6 | 45 | _init = true; |
vwochnik | 57:4af5f1bec3a6 | 46 | return true; |
vwochnik | 57:4af5f1bec3a6 | 47 | } |
vwochnik | 57:4af5f1bec3a6 | 48 | |
vwochnik | 57:4af5f1bec3a6 | 49 | bool OperationSupport::run() |
vwochnik | 57:4af5f1bec3a6 | 50 | { |
vwochnik | 59:f96be79feccd | 51 | ComposedRecord record; |
vwochnik | 59:f96be79feccd | 52 | ParsedRecord received; |
vwochnik | 59:f96be79feccd | 53 | |
vwochnik | 59:f96be79feccd | 54 | IntegerValue msgId(110); |
vwochnik | 59:f96be79feccd | 55 | IntegerValue deviceId(_deviceId); |
vwochnik | 59:f96be79feccd | 56 | if ((!record.add(msgId)) || (!record.add(deviceId))) |
vwochnik | 59:f96be79feccd | 57 | return false; |
vwochnik | 59:f96be79feccd | 58 | |
vwochnik | 59:f96be79feccd | 59 | if (_client.send(record) != SMARTREST_SUCCESS) { |
vwochnik | 59:f96be79feccd | 60 | _client.stop(); |
vwochnik | 59:f96be79feccd | 61 | return false; |
vwochnik | 59:f96be79feccd | 62 | } |
vwochnik | 59:f96be79feccd | 63 | _client.stop(); |
vwochnik | 59:f96be79feccd | 64 | |
vwochnik | 57:4af5f1bec3a6 | 65 | } |
vwochnik | 59:f96be79feccd | 66 | |
vwochnik | 59:f96be79feccd | 67 | bool operationFromRecord(ParsedRecord& received, OperationStore::Operation& op) |
vwochnik | 59:f96be79feccd | 68 | { |
vwochnik | 59:f96be79feccd | 69 | const char *tmp; |
vwochnik | 59:f96be79feccd | 70 | |
vwochnik | 59:f96be79feccd | 71 | if ((received.values() < 4) || |
vwochnik | 59:f96be79feccd | 72 | (received.value(0).valueType() != VALUE_INTEGER) || |
vwochnik | 59:f96be79feccd | 73 | (received.value(0).integerValue() != 211) || |
vwochnik | 59:f96be79feccd | 74 | (received.value(2).valueType() != VALUE_INTEGER) || |
vwochnik | 59:f96be79feccd | 75 | (received.value(3).valueType() != VALUE_CHARACTER)) |
vwochnik | 59:f96be79feccd | 76 | return false; |
vwochnik | 59:f96be79feccd | 77 | |
vwochnik | 59:f96be79feccd | 78 | tmp = received.value(3).characterValue(); |
vwochnik | 59:f96be79feccd | 79 | op.identifier = received.value(2).integerValue(); |
vwochnik | 59:f96be79feccd | 80 | |
vwochnik | 59:f96be79feccd | 81 | if (strcmp(tmp, "EXECUTING") == 0) |
vwochnik | 59:f96be79feccd | 82 | op.state = STATE_EXECUTING; |
vwochnik | 59:f96be79feccd | 83 | else if (strcmp(tmp, "SUCCESSFUL") == 0) |
vwochnik | 59:f96be79feccd | 84 | op.state = STATE_SUCCESSFUL; |
vwochnik | 59:f96be79feccd | 85 | else if (strcmp(tmp, "FAILED") == 0) |
vwochnik | 59:f96be79feccd | 86 | op.state = STATE_FAILED; |
vwochnik | 59:f96be79feccd | 87 | else |
vwochnik | 59:f96be79feccd | 88 | op.state = STATE_PENDING; |
vwochnik | 59:f96be79feccd | 89 | |
vwochnik | 59:f96be79feccd | 90 | return true; |
vwochnik | 59:f96be79feccd | 91 | } |
vwochnik | 59:f96be79feccd | 92 | |
vwochnik | 59:f96be79feccd | 93 | bool OperationSupport::updateOperation(OperationStore::Operation& op) |
vwochnik | 59:f96be79feccd | 94 | { |
vwochnik | 59:f96be79feccd | 95 | ComposedRecord record; |
vwochnik | 59:f96be79feccd | 96 | ParsedRecord received; |
vwochnik | 59:f96be79feccd | 97 | |
vwochnik | 59:f96be79feccd | 98 | IntegerValue msgId(111); |
vwochnik | 59:f96be79feccd | 99 | IntegerValue operationId(op.identifier); |
vwochnik | 59:f96be79feccd | 100 | if ((!record.add(msgId)) || (!record.add(operationId)) || |
vwochnik | 59:f96be79feccd | 101 | (!record.add(operationStateValue(op)))) |
vwochnik | 59:f96be79feccd | 102 | return false; |
vwochnik | 59:f96be79feccd | 103 | |
vwochnik | 59:f96be79feccd | 104 | if (_client.send(record) != SMARTREST_SUCCESS) { |
vwochnik | 59:f96be79feccd | 105 | _client.stop(); |
vwochnik | 59:f96be79feccd | 106 | return false; |
vwochnik | 59:f96be79feccd | 107 | } |
vwochnik | 59:f96be79feccd | 108 | |
vwochnik | 59:f96be79feccd | 109 | if (_client.receive(received) != SMARTREST_SUCCESS) { |
vwochnik | 59:f96be79feccd | 110 | _client.stop(); |
vwochnik | 59:f96be79feccd | 111 | return false; |
vwochnik | 59:f96be79feccd | 112 | } |
vwochnik | 59:f96be79feccd | 113 | |
vwochnik | 59:f96be79feccd | 114 | _client.stop(); |
vwochnik | 59:f96be79feccd | 115 | |
vwochnik | 59:f96be79feccd | 116 | //TODO: assertions |
vwochnik | 59:f96be79feccd | 117 | return true; |
vwochnik | 59:f96be79feccd | 118 | } |
vwochnik | 59:f96be79feccd | 119 | |
vwochnik | 59:f96be79feccd | 120 | CharValue& OperationSupport::operationStateValue(OperationStore::Operation& op) |
vwochnik | 59:f96be79feccd | 121 | { |
vwochnik | 59:f96be79feccd | 122 | switch (op.state) { |
vwochnik | 59:f96be79feccd | 123 | case STATE_EXECUTING: |
vwochnik | 59:f96be79feccd | 124 | return aOperationStateExecuting; |
vwochnik | 59:f96be79feccd | 125 | case STATE_SUCCESSFUL: |
vwochnik | 59:f96be79feccd | 126 | return aOperationStateSuccessful; |
vwochnik | 59:f96be79feccd | 127 | case STATE_FAILED: |
vwochnik | 59:f96be79feccd | 128 | return aOperationStateFailed; |
vwochnik | 59:f96be79feccd | 129 | default: |
vwochnik | 59:f96be79feccd | 130 | return aOperationStatePending; |
vwochnik | 59:f96be79feccd | 131 | } |
vwochnik | 59:f96be79feccd | 132 | } |