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:
xinlei
Date:
Mon Apr 13 14:24:58 2015 +0000
Revision:
93:0acd11870c6a
Parent:
92:48069375dffa
v2.1rc1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
xinlei 77:f6717e4eccc4 1 #include <string.h>
vwochnik 62:86a04c5bda18 2 #include "OperationExecutor.h"
vwochnik 62:86a04c5bda18 3 #include "ComposedRecord.h"
vwochnik 62:86a04c5bda18 4 #include "CharValue.h"
vwochnik 62:86a04c5bda18 5 #include "IntegerValue.h"
xinlei 77:f6717e4eccc4 6 #include "logging.h"
vwochnik 62:86a04c5bda18 7
vwochnik 66:31c754c36ed7 8 #define FOUND_RELAY 1
vwochnik 66:31c754c36ed7 9 #define FOUND_MESSAGE 2
vwochnik 68:0dc778a16d0d 10 #define FOUND_CONFIGURATION 3
vwochnik 66:31c754c36ed7 11
xinlei 93:0acd11870c6a 12 OperationExecutor::OperationExecutor(AbstractSmartRest& client, SmartRestTemplate& tpl,
xinlei 93:0acd11870c6a 13 long& deviceId, ConfigurationSynchronization& configurationSynchronization,
xinlei 93:0acd11870c6a 14 LCDDisplay& lcdDisplay) :
xinlei 82:ca7430f50b2b 15 _init(false),
xinlei 82:ca7430f50b2b 16 _deviceId(deviceId),
xinlei 92:48069375dffa 17 _tpl(tpl),
vwochnik 62:86a04c5bda18 18 _client(client),
vwochnik 68:0dc778a16d0d 19 _configurationSynchronization(configurationSynchronization),
xinlei 93:0acd11870c6a 20 _deviceFeedback(),
xinlei 93:0acd11870c6a 21 _lcdDisplay(lcdDisplay)
vwochnik 62:86a04c5bda18 22 {
vwochnik 62:86a04c5bda18 23 }
vwochnik 62:86a04c5bda18 24
vwochnik 62:86a04c5bda18 25 bool OperationExecutor::init()
vwochnik 62:86a04c5bda18 26 {
vwochnik 62:86a04c5bda18 27 if (_init)
vwochnik 62:86a04c5bda18 28 return false;
vwochnik 62:86a04c5bda18 29
vwochnik 64:31a640c32399 30 // Get operation by id
vwochnik 64:31a640c32399 31 // USAGE: 112,<OPERATION/ID>
vwochnik 62:86a04c5bda18 32 if (!_tpl.add("10,112,GET,/devicecontrol/operations/%%,,application/vnd.com.nsn.cumulocity.operation+json,%%,UNSIGNED,\r\n"))
vwochnik 62:86a04c5bda18 33 return false;
vwochnik 62:86a04c5bda18 34
vwochnik 64:31a640c32399 35 // Relay operation response
vwochnik 66:31c754c36ed7 36 // Response: 220,<OPERATION/ID>,<STATUS>
vwochnik 64:31a640c32399 37 if (!_tpl.add("11,220,,\"$.c8y_Relay\",\"$.id\",\"$.c8y_Relay.relayState\"\r\n"))
vwochnik 64:31a640c32399 38 return false;
vwochnik 64:31a640c32399 39
vwochnik 66:31c754c36ed7 40 // Message operation response
vwochnik 66:31c754c36ed7 41 // Response: 221,<OPERATION/ID>,<MESSAGE>
vwochnik 66:31c754c36ed7 42 if (!_tpl.add("11,221,,\"$.c8y_Message\",\"$.id\",\"$.c8y_Message.text\"\r\n"))
vwochnik 66:31c754c36ed7 43 return false;
vwochnik 66:31c754c36ed7 44
vwochnik 68:0dc778a16d0d 45 // Configuration operation response
vwochnik 68:0dc778a16d0d 46 // Response: 222,<OPERATION/ID>,<CONFIGURATION/STRING>
vwochnik 68:0dc778a16d0d 47 if (!_tpl.add("11,222,,\"$.c8y_Configuration\",\"$.id\",\"$.c8y_Configuration.config\"\r\n"))
vwochnik 68:0dc778a16d0d 48 return false;
vwochnik 68:0dc778a16d0d 49
vwochnik 62:86a04c5bda18 50 _init = true;
vwochnik 62:86a04c5bda18 51 return true;
vwochnik 62:86a04c5bda18 52 }
vwochnik 62:86a04c5bda18 53
xinlei 93:0acd11870c6a 54 bool OperationExecutor::executeOperation(ParsedRecord& received)
vwochnik 62:86a04c5bda18 55 {
xinlei 92:48069375dffa 56 uint8_t found = 0;
xinlei 93:0acd11870c6a 57 bool relayState=false;
xinlei 93:0acd11870c6a 58 char message[128];
xinlei 93:0acd11870c6a 59 if (received.values() != 4) {
xinlei 93:0acd11870c6a 60 aError("executeOperation: %u of 4 values received.\n", received.values());
xinlei 93:0acd11870c6a 61 } else if (received.value(0).integerValue() == 220) {
xinlei 93:0acd11870c6a 62 relayState = strcmp("CLOSED", received.value(3).characterValue()) == 0;
xinlei 93:0acd11870c6a 63 found = FOUND_RELAY;
xinlei 93:0acd11870c6a 64 } else if (received.value(0).integerValue() == 221) {
xinlei 93:0acd11870c6a 65 const char *p = received.rawValue(3);
xinlei 93:0acd11870c6a 66 strncpy(message, p, sizeof(message));
xinlei 93:0acd11870c6a 67 message[sizeof(message)-1] = '\0';
xinlei 93:0acd11870c6a 68 found = FOUND_MESSAGE;
xinlei 93:0acd11870c6a 69 } else if (received.value(0).integerValue() == 222) {
xinlei 93:0acd11870c6a 70 strncpy(message, received.value(3).characterValue(), sizeof(message));
xinlei 93:0acd11870c6a 71 message[sizeof(message)-1] = '\0';
xinlei 93:0acd11870c6a 72 found = FOUND_CONFIGURATION;
vwochnik 64:31a640c32399 73 }
vwochnik 64:31a640c32399 74
vwochnik 66:31c754c36ed7 75 switch (found) {
xinlei 92:48069375dffa 76 case FOUND_RELAY: return executeRelayStateUpdate(relayState);
xinlei 92:48069375dffa 77 case FOUND_MESSAGE: return executeMessageDisplay(message);
xinlei 92:48069375dffa 78 case FOUND_CONFIGURATION: return executeUpdateConfiguration(message);
xinlei 93:0acd11870c6a 79 default: return false;
vwochnik 66:31c754c36ed7 80 }
vwochnik 62:86a04c5bda18 81 }
vwochnik 64:31a640c32399 82
vwochnik 64:31a640c32399 83 bool OperationExecutor::executeRelayStateUpdate(bool relayState)
vwochnik 64:31a640c32399 84 {
vwochnik 64:31a640c32399 85 if (relayState)
xinlei 93:0acd11870c6a 86 _deviceFeedback.closeRelay();
vwochnik 64:31a640c32399 87 else
xinlei 93:0acd11870c6a 88 _deviceFeedback.openRelay();
vwochnik 64:31a640c32399 89 return true;
vwochnik 64:31a640c32399 90 }
vwochnik 66:31c754c36ed7 91
vwochnik 66:31c754c36ed7 92 bool OperationExecutor::executeMessageDisplay(const char *message)
vwochnik 66:31c754c36ed7 93 {
xinlei 93:0acd11870c6a 94 _lcdDisplay.setFirstLine(message);
vwochnik 66:31c754c36ed7 95 return true;
vwochnik 66:31c754c36ed7 96 }
vwochnik 68:0dc778a16d0d 97
vwochnik 68:0dc778a16d0d 98 bool OperationExecutor::executeUpdateConfiguration(const char *config)
vwochnik 68:0dc778a16d0d 99 {
vwochnik 68:0dc778a16d0d 100 return _configurationSynchronization.updateConfiguration(config);
vwochnik 68:0dc778a16d0d 101 }