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

Committer:
vwochnik
Date:
Fri Oct 24 15:00:36 2014 +0000
Revision:
60:3c822f97fc73
Parent:
59:f96be79feccd
Child:
61:15719dbe8820
operation thread + funcs

Who changed what in which revision?

UserRevisionLine numberNew 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 60:3c822f97fc73 6 #include <stdio.h>
vwochnik 57:4af5f1bec3a6 7
vwochnik 59:f96be79feccd 8 CharValue aOperationStatePending("PENDING");
vwochnik 59:f96be79feccd 9 CharValue aOperationStateExecuting("EXECUTING");
vwochnik 59:f96be79feccd 10 CharValue aOperationStateSuccessful("SUCCESSFUL");
vwochnik 59:f96be79feccd 11 CharValue aOperationStateFailed("FAILED");
vwochnik 59:f96be79feccd 12
vwochnik 57:4af5f1bec3a6 13 OperationSupport::OperationSupport(AbstractSmartRest& client, SmartRestTemplate& tpl, long& deviceId) :
vwochnik 57:4af5f1bec3a6 14 _client(client),
vwochnik 57:4af5f1bec3a6 15 _tpl(tpl),
vwochnik 60:3c822f97fc73 16 _deviceId(deviceId),
vwochnik 60:3c822f97fc73 17 _thread(OperationSupport::thread_func, this)
vwochnik 57:4af5f1bec3a6 18 {
vwochnik 57:4af5f1bec3a6 19 _init = false;
vwochnik 57:4af5f1bec3a6 20 }
vwochnik 57:4af5f1bec3a6 21
vwochnik 57:4af5f1bec3a6 22 bool OperationSupport::init()
vwochnik 57:4af5f1bec3a6 23 {
vwochnik 57:4af5f1bec3a6 24 if (_init)
vwochnik 57:4af5f1bec3a6 25 return false;
vwochnik 57:4af5f1bec3a6 26
vwochnik 57:4af5f1bec3a6 27 // Get pending operations
vwochnik 57:4af5f1bec3a6 28 // USAGE: 110,<DEVICE/ID>
vwochnik 57:4af5f1bec3a6 29 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 30 return false;
vwochnik 57:4af5f1bec3a6 31
vwochnik 57:4af5f1bec3a6 32 // Set operation state
vwochnik 57:4af5f1bec3a6 33 // USAGE: 110,<OPERATION/ID>,<STATE>
vwochnik 58:4cc0ae5a7058 34 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 35 return false;
vwochnik 57:4af5f1bec3a6 36
vwochnik 57:4af5f1bec3a6 37 // Get operations
vwochnik 57:4af5f1bec3a6 38 // Response: 210,<OPERATION/ID>,<STATUS>
vwochnik 57:4af5f1bec3a6 39 if (!_tpl.add("11,210,\"$.operations\",,\"$.id\",\"$.status\"\r\n"))
vwochnik 57:4af5f1bec3a6 40 return false;
vwochnik 57:4af5f1bec3a6 41
vwochnik 57:4af5f1bec3a6 42 // Get operation
vwochnik 57:4af5f1bec3a6 43 // Response: 211,<OPERATION/ID>,<STATUS>
vwochnik 57:4af5f1bec3a6 44 if (!_tpl.add("11,211,,\"$.deviceId\",\"$.id\",\"$.status\"\r\n"))
vwochnik 57:4af5f1bec3a6 45 return false;
vwochnik 57:4af5f1bec3a6 46
vwochnik 57:4af5f1bec3a6 47 _init = true;
vwochnik 57:4af5f1bec3a6 48 return true;
vwochnik 57:4af5f1bec3a6 49 }
vwochnik 57:4af5f1bec3a6 50
vwochnik 57:4af5f1bec3a6 51 bool OperationSupport::run()
vwochnik 57:4af5f1bec3a6 52 {
vwochnik 60:3c822f97fc73 53 uint8_t ret;
vwochnik 60:3c822f97fc73 54 OperationStore::Operation ops[100];
vwochnik 60:3c822f97fc73 55 size_t nops;
vwochnik 60:3c822f97fc73 56
vwochnik 59:f96be79feccd 57 ComposedRecord record;
vwochnik 59:f96be79feccd 58 ParsedRecord received;
vwochnik 59:f96be79feccd 59
vwochnik 59:f96be79feccd 60 IntegerValue msgId(110);
vwochnik 59:f96be79feccd 61 IntegerValue deviceId(_deviceId);
vwochnik 59:f96be79feccd 62 if ((!record.add(msgId)) || (!record.add(deviceId)))
vwochnik 59:f96be79feccd 63 return false;
vwochnik 60:3c822f97fc73 64
vwochnik 60:3c822f97fc73 65 puts("Operation support.");
vwochnik 59:f96be79feccd 66 if (_client.send(record) != SMARTREST_SUCCESS) {
vwochnik 59:f96be79feccd 67 _client.stop();
vwochnik 59:f96be79feccd 68 return false;
vwochnik 59:f96be79feccd 69 }
vwochnik 60:3c822f97fc73 70
vwochnik 60:3c822f97fc73 71 nops = 0;
vwochnik 60:3c822f97fc73 72 while ((ret = _client.receive(received)) == SMARTREST_SUCCESS) {
vwochnik 60:3c822f97fc73 73 puts("Received operation.");
vwochnik 60:3c822f97fc73 74 if (!operationFromRecord(received, ops[nops++]))
vwochnik 60:3c822f97fc73 75 puts("Operation conversion failed.");
vwochnik 60:3c822f97fc73 76 }
vwochnik 59:f96be79feccd 77 _client.stop();
vwochnik 60:3c822f97fc73 78
vwochnik 60:3c822f97fc73 79 if ((ret != SMARTREST_END_OF_RESPONSE) &&
vwochnik 60:3c822f97fc73 80 (ret != SMARTREST_CONNECTION_CLOSED)) {
vwochnik 60:3c822f97fc73 81 puts("Failed receive.");
vwochnik 60:3c822f97fc73 82 return false;
vwochnik 60:3c822f97fc73 83 }
vwochnik 59:f96be79feccd 84
vwochnik 60:3c822f97fc73 85 for (size_t i = 0; i < nops; i++) {
vwochnik 60:3c822f97fc73 86 ops[i].state = STATE_SUCCESSFUL;
vwochnik 60:3c822f97fc73 87 if (!updateOperation(ops[i]))
vwochnik 60:3c822f97fc73 88 puts("Operation update failed.");
vwochnik 60:3c822f97fc73 89 }
vwochnik 60:3c822f97fc73 90
vwochnik 60:3c822f97fc73 91 return true;
vwochnik 57:4af5f1bec3a6 92 }
vwochnik 59:f96be79feccd 93
vwochnik 60:3c822f97fc73 94 bool OperationSupport::operationFromRecord(ParsedRecord& received, OperationStore::Operation& op)
vwochnik 59:f96be79feccd 95 {
vwochnik 59:f96be79feccd 96 const char *tmp;
vwochnik 59:f96be79feccd 97
vwochnik 59:f96be79feccd 98 if ((received.values() < 4) ||
vwochnik 59:f96be79feccd 99 (received.value(0).valueType() != VALUE_INTEGER) ||
vwochnik 60:3c822f97fc73 100 (received.value(0).integerValue() != 210) ||
vwochnik 59:f96be79feccd 101 (received.value(2).valueType() != VALUE_INTEGER) ||
vwochnik 59:f96be79feccd 102 (received.value(3).valueType() != VALUE_CHARACTER))
vwochnik 59:f96be79feccd 103 return false;
vwochnik 59:f96be79feccd 104
vwochnik 60:3c822f97fc73 105 op.identifier = received.value(2).integerValue();
vwochnik 59:f96be79feccd 106 tmp = received.value(3).characterValue();
vwochnik 59:f96be79feccd 107 if (strcmp(tmp, "EXECUTING") == 0)
vwochnik 59:f96be79feccd 108 op.state = STATE_EXECUTING;
vwochnik 59:f96be79feccd 109 else if (strcmp(tmp, "SUCCESSFUL") == 0)
vwochnik 59:f96be79feccd 110 op.state = STATE_SUCCESSFUL;
vwochnik 59:f96be79feccd 111 else if (strcmp(tmp, "FAILED") == 0)
vwochnik 59:f96be79feccd 112 op.state = STATE_FAILED;
vwochnik 59:f96be79feccd 113 else
vwochnik 59:f96be79feccd 114 op.state = STATE_PENDING;
vwochnik 59:f96be79feccd 115
vwochnik 59:f96be79feccd 116 return true;
vwochnik 59:f96be79feccd 117 }
vwochnik 59:f96be79feccd 118
vwochnik 59:f96be79feccd 119 bool OperationSupport::updateOperation(OperationStore::Operation& op)
vwochnik 59:f96be79feccd 120 {
vwochnik 59:f96be79feccd 121 ComposedRecord record;
vwochnik 59:f96be79feccd 122 ParsedRecord received;
vwochnik 59:f96be79feccd 123
vwochnik 59:f96be79feccd 124 IntegerValue msgId(111);
vwochnik 59:f96be79feccd 125 IntegerValue operationId(op.identifier);
vwochnik 59:f96be79feccd 126 if ((!record.add(msgId)) || (!record.add(operationId)) ||
vwochnik 59:f96be79feccd 127 (!record.add(operationStateValue(op))))
vwochnik 59:f96be79feccd 128 return false;
vwochnik 59:f96be79feccd 129
vwochnik 59:f96be79feccd 130 if (_client.send(record) != SMARTREST_SUCCESS) {
vwochnik 59:f96be79feccd 131 _client.stop();
vwochnik 59:f96be79feccd 132 return false;
vwochnik 59:f96be79feccd 133 }
vwochnik 59:f96be79feccd 134
vwochnik 59:f96be79feccd 135 if (_client.receive(received) != SMARTREST_SUCCESS) {
vwochnik 59:f96be79feccd 136 _client.stop();
vwochnik 59:f96be79feccd 137 return false;
vwochnik 59:f96be79feccd 138 }
vwochnik 59:f96be79feccd 139
vwochnik 59:f96be79feccd 140 _client.stop();
vwochnik 59:f96be79feccd 141
vwochnik 59:f96be79feccd 142 //TODO: assertions
vwochnik 59:f96be79feccd 143 return true;
vwochnik 59:f96be79feccd 144 }
vwochnik 59:f96be79feccd 145
vwochnik 59:f96be79feccd 146 CharValue& OperationSupport::operationStateValue(OperationStore::Operation& op)
vwochnik 59:f96be79feccd 147 {
vwochnik 59:f96be79feccd 148 switch (op.state) {
vwochnik 59:f96be79feccd 149 case STATE_EXECUTING:
vwochnik 59:f96be79feccd 150 return aOperationStateExecuting;
vwochnik 59:f96be79feccd 151 case STATE_SUCCESSFUL:
vwochnik 59:f96be79feccd 152 return aOperationStateSuccessful;
vwochnik 59:f96be79feccd 153 case STATE_FAILED:
vwochnik 59:f96be79feccd 154 return aOperationStateFailed;
vwochnik 59:f96be79feccd 155 default:
vwochnik 59:f96be79feccd 156 return aOperationStatePending;
vwochnik 59:f96be79feccd 157 }
vwochnik 59:f96be79feccd 158 }
vwochnik 60:3c822f97fc73 159
vwochnik 60:3c822f97fc73 160 void OperationSupport::thread()
vwochnik 60:3c822f97fc73 161 {
vwochnik 60:3c822f97fc73 162 while (!_init)
vwochnik 60:3c822f97fc73 163 Thread::yield();
vwochnik 60:3c822f97fc73 164
vwochnik 60:3c822f97fc73 165
vwochnik 60:3c822f97fc73 166 }
vwochnik 60:3c822f97fc73 167
vwochnik 60:3c822f97fc73 168 void OperationSupport::thread_func(void const *arg)
vwochnik 60:3c822f97fc73 169 {
vwochnik 60:3c822f97fc73 170 OperationSupport *that;
vwochnik 60:3c822f97fc73 171 that = (OperationSupport*)arg;
vwochnik 60:3c822f97fc73 172 that->thread();
vwochnik 60:3c822f97fc73 173 }