Own fork of MbedSmartRestMain
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
DeviceIntegration.cpp@91:48069375dffa, 2015-03-20 (annotated)
- Committer:
- xinlei
- Date:
- Fri Mar 20 14:27:10 2015 +0000
- Revision:
- 91:48069375dffa
- Parent:
- 77:f6717e4eccc4
- Child:
- 92:0acd11870c6a
Get rid of all annoying warning messages.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Cumulocity | 41:804f6a0bda26 | 1 | #include "DeviceIntegration.h" |
Cumulocity | 41:804f6a0bda26 | 2 | #include <stdio.h> |
Cumulocity | 41:804f6a0bda26 | 3 | #include "ComposedRecord.h" |
Cumulocity | 41:804f6a0bda26 | 4 | #include "CharValue.h" |
Cumulocity | 41:804f6a0bda26 | 5 | #include "IntegerValue.h" |
xinlei | 72:c5709ae7b193 | 6 | #include "logging.h" |
Cumulocity | 41:804f6a0bda26 | 7 | |
xinlei | 91:48069375dffa | 8 | DeviceIntegration::DeviceIntegration(AbstractSmartRest& client, SmartRestTemplate& tpl, |
xinlei | 91:48069375dffa | 9 | long& deviceId, DeviceInfo& deviceInfo) : |
xinlei | 91:48069375dffa | 10 | _deviceId(deviceId), |
xinlei | 91:48069375dffa | 11 | _tpl(tpl), |
Cumulocity | 41:804f6a0bda26 | 12 | _client(client), |
Cumulocity | 41:804f6a0bda26 | 13 | _deviceInfo(deviceInfo) |
Cumulocity | 41:804f6a0bda26 | 14 | { |
Cumulocity | 41:804f6a0bda26 | 15 | _init = false; |
Cumulocity | 41:804f6a0bda26 | 16 | } |
Cumulocity | 41:804f6a0bda26 | 17 | |
Cumulocity | 41:804f6a0bda26 | 18 | bool DeviceIntegration::init() |
Cumulocity | 41:804f6a0bda26 | 19 | { |
Cumulocity | 41:804f6a0bda26 | 20 | if (_init) |
Cumulocity | 41:804f6a0bda26 | 21 | return false; |
Cumulocity | 41:804f6a0bda26 | 22 | |
Cumulocity | 41:804f6a0bda26 | 23 | // get device by identity |
Cumulocity | 41:804f6a0bda26 | 24 | // Usage: 100,<SERIAL/NR> |
Cumulocity | 41:804f6a0bda26 | 25 | if (!_tpl.add("10,100,GET,/identity/externalIds/c8y_Serial/%%,,application/vnd.com.nsn.cumulocity.externalId+json,%%,STRING,\r\n")) |
Cumulocity | 41:804f6a0bda26 | 26 | return false; |
Cumulocity | 41:804f6a0bda26 | 27 | |
Cumulocity | 41:804f6a0bda26 | 28 | // get device id from identity |
Cumulocity | 41:804f6a0bda26 | 29 | // Response: 200,<DEVICE/ID> |
Cumulocity | 41:804f6a0bda26 | 30 | if (!_tpl.add("11,200,\"$.managedObject\",,\"$.id\"\r\n")) |
Cumulocity | 41:804f6a0bda26 | 31 | return false; |
Cumulocity | 41:804f6a0bda26 | 32 | |
Cumulocity | 41:804f6a0bda26 | 33 | // Create device |
Cumulocity | 41:804f6a0bda26 | 34 | // Usage: 101,<SERIAL/NR> |
vwochnik | 69:237c01eb36c2 | 35 | if (!_tpl.add("10,101,POST,/inventory/managedObjects,application/vnd.com.nsn.cumulocity.managedObject+json,application/vnd.com.nsn.cumulocity.managedObject+json,%%,STRING,\"{\"\"name\"\":\"\"Mbed Test Device\"\",\"\"type\"\":\"\"com_ublox_C027_REV-A\"\",\"\"c8y_Hardware\"\":{\"\"revision\"\":\"\"1\"\",\"\"model\"\":\"\"Ublox C027\"\",\"\"serialNumber\"\":\"\"%%\"\"},\"\"c8y_SupportedOperations\"\":[\"\"c8y_Relay\"\",\"\"c8y_Configuration\"\",\"\"c8y_Message\"\"],\"\"c8y_IsDevice\"\":{},\"\"com_cumulocity_model_Agent\"\":{}}\"\r\n")) |
Cumulocity | 41:804f6a0bda26 | 36 | return false; |
Cumulocity | 41:804f6a0bda26 | 37 | |
Cumulocity | 41:804f6a0bda26 | 38 | // Get device id |
Cumulocity | 41:804f6a0bda26 | 39 | // Response: 201,<DEVICE/ID> |
Cumulocity | 41:804f6a0bda26 | 40 | if (!_tpl.add("11,201,,\"$.c8y_IsDevice\",\"$.id\"\r\n")) |
Cumulocity | 41:804f6a0bda26 | 41 | return false; |
Cumulocity | 41:804f6a0bda26 | 42 | |
Cumulocity | 41:804f6a0bda26 | 43 | // Insert global ID |
Cumulocity | 41:804f6a0bda26 | 44 | // Usage: 102,<DEVICE/ID>,<SERIAL/NR> |
Cumulocity | 41:804f6a0bda26 | 45 | if (!_tpl.add("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")) |
Cumulocity | 41:804f6a0bda26 | 46 | return false; |
Cumulocity | 41:804f6a0bda26 | 47 | |
Cumulocity | 41:804f6a0bda26 | 48 | // Update IMEI, CellId and iccid |
Cumulocity | 41:804f6a0bda26 | 49 | // Usage: 103,<DEVICE/ID>,<IMEI>,<CELL/ID>,<ICCID> |
Cumulocity | 41:804f6a0bda26 | 50 | if (!_tpl.add("10,103,PUT,/inventory/managedObjects/%%,application/vnd.com.nsn.cumulocity.managedObject+json,application/vnd.com.nsn.cumulocity.managedObject+json,%%,UNSIGNED STRING STRING STRING,\"{\"\"c8y_Mobile\"\":{\"\"imei\"\":\"\"%%\"\",\"\"cellId\"\":\"\"%%\"\",\"\"iccid\"\":\"\"%%\"\"}}\"\r\n")) |
Cumulocity | 41:804f6a0bda26 | 51 | return false; |
Cumulocity | 41:804f6a0bda26 | 52 | |
Cumulocity | 41:804f6a0bda26 | 53 | _init = true; |
Cumulocity | 41:804f6a0bda26 | 54 | _deviceId = 0; |
Cumulocity | 41:804f6a0bda26 | 55 | return true; |
Cumulocity | 41:804f6a0bda26 | 56 | } |
Cumulocity | 41:804f6a0bda26 | 57 | |
Cumulocity | 41:804f6a0bda26 | 58 | bool DeviceIntegration::integrate() |
Cumulocity | 41:804f6a0bda26 | 59 | { |
Cumulocity | 41:804f6a0bda26 | 60 | if (_deviceId != 0) |
Cumulocity | 41:804f6a0bda26 | 61 | return false; |
Cumulocity | 41:804f6a0bda26 | 62 | |
Cumulocity | 41:804f6a0bda26 | 63 | // template bootstrapping process |
Cumulocity | 41:804f6a0bda26 | 64 | if (_client.bootstrap(_tpl) != SMARTREST_SUCCESS) { |
xinlei | 72:c5709ae7b193 | 65 | aError("Template bootstrap failed.\r\n"); |
Cumulocity | 49:ac0ba9d54ebc | 66 | return false; |
Cumulocity | 41:804f6a0bda26 | 67 | } |
Cumulocity | 41:804f6a0bda26 | 68 | |
vwochnik | 52:8f1370084268 | 69 | if ((!deviceExisting()) && ((!createDevice()) || (!addGlobalIdentifier()))) |
vwochnik | 52:8f1370084268 | 70 | return false; |
Cumulocity | 41:804f6a0bda26 | 71 | |
vwochnik | 52:8f1370084268 | 72 | if (!updateDevice()) |
Cumulocity | 41:804f6a0bda26 | 73 | return false; |
Cumulocity | 41:804f6a0bda26 | 74 | |
Cumulocity | 41:804f6a0bda26 | 75 | return true; |
Cumulocity | 41:804f6a0bda26 | 76 | } |
Cumulocity | 41:804f6a0bda26 | 77 | |
Cumulocity | 41:804f6a0bda26 | 78 | bool DeviceIntegration::deviceExisting() |
Cumulocity | 41:804f6a0bda26 | 79 | { |
Cumulocity | 41:804f6a0bda26 | 80 | ComposedRecord record; |
Cumulocity | 41:804f6a0bda26 | 81 | ParsedRecord received; |
Cumulocity | 41:804f6a0bda26 | 82 | |
Cumulocity | 41:804f6a0bda26 | 83 | IntegerValue msgId(100); |
Cumulocity | 41:804f6a0bda26 | 84 | CharValue imei(_deviceInfo.imei()); |
Cumulocity | 41:804f6a0bda26 | 85 | if ((!record.add(msgId)) || (!record.add(imei))) |
Cumulocity | 41:804f6a0bda26 | 86 | return false; |
Cumulocity | 41:804f6a0bda26 | 87 | |
Cumulocity | 41:804f6a0bda26 | 88 | if (_client.send(record) != SMARTREST_SUCCESS) { |
xinlei | 77:f6717e4eccc4 | 89 | aWarning("Send failed.\r\n"); |
Cumulocity | 41:804f6a0bda26 | 90 | _client.stop(); |
Cumulocity | 41:804f6a0bda26 | 91 | return false; |
Cumulocity | 41:804f6a0bda26 | 92 | } |
Cumulocity | 41:804f6a0bda26 | 93 | |
Cumulocity | 41:804f6a0bda26 | 94 | if (_client.receive(received) != SMARTREST_SUCCESS) { |
xinlei | 77:f6717e4eccc4 | 95 | aError("No device found.\r\n"); |
Cumulocity | 41:804f6a0bda26 | 96 | _client.stop(); |
Cumulocity | 41:804f6a0bda26 | 97 | return false; |
Cumulocity | 41:804f6a0bda26 | 98 | } |
Cumulocity | 41:804f6a0bda26 | 99 | _client.stop(); |
Cumulocity | 41:804f6a0bda26 | 100 | |
Cumulocity | 41:804f6a0bda26 | 101 | if (received.values() == 0) { |
xinlei | 77:f6717e4eccc4 | 102 | aError("Received no values.\r\n"); |
Cumulocity | 41:804f6a0bda26 | 103 | return false; |
Cumulocity | 41:804f6a0bda26 | 104 | } |
Cumulocity | 41:804f6a0bda26 | 105 | |
Cumulocity | 41:804f6a0bda26 | 106 | if (received.value(0).integerValue() == 50) { |
Cumulocity | 41:804f6a0bda26 | 107 | return false; |
Cumulocity | 41:804f6a0bda26 | 108 | } |
Cumulocity | 41:804f6a0bda26 | 109 | |
Cumulocity | 41:804f6a0bda26 | 110 | if (received.value(0).integerValue() != 200) { |
xinlei | 77:f6717e4eccc4 | 111 | aError("Bad response.\r\n"); |
Cumulocity | 41:804f6a0bda26 | 112 | return false; |
Cumulocity | 41:804f6a0bda26 | 113 | } |
Cumulocity | 41:804f6a0bda26 | 114 | |
Cumulocity | 41:804f6a0bda26 | 115 | _deviceId = received.value(2).integerValue(); |
Cumulocity | 41:804f6a0bda26 | 116 | |
Cumulocity | 41:804f6a0bda26 | 117 | return true; |
Cumulocity | 41:804f6a0bda26 | 118 | } |
Cumulocity | 41:804f6a0bda26 | 119 | |
Cumulocity | 41:804f6a0bda26 | 120 | bool DeviceIntegration::createDevice() |
Cumulocity | 41:804f6a0bda26 | 121 | { |
Cumulocity | 41:804f6a0bda26 | 122 | ComposedRecord record; |
Cumulocity | 41:804f6a0bda26 | 123 | ParsedRecord received; |
Cumulocity | 41:804f6a0bda26 | 124 | |
xinlei | 77:f6717e4eccc4 | 125 | aInfo("Creating device...\r\n"); |
Cumulocity | 41:804f6a0bda26 | 126 | |
Cumulocity | 41:804f6a0bda26 | 127 | IntegerValue msgId(101); |
Cumulocity | 41:804f6a0bda26 | 128 | CharValue imei(_deviceInfo.imei()); |
Cumulocity | 41:804f6a0bda26 | 129 | if ((!record.add(msgId)) || (!record.add(imei))) |
Cumulocity | 41:804f6a0bda26 | 130 | return false; |
Cumulocity | 41:804f6a0bda26 | 131 | |
Cumulocity | 41:804f6a0bda26 | 132 | if (_client.send(record) != SMARTREST_SUCCESS) { |
xinlei | 77:f6717e4eccc4 | 133 | aError("Failed to sending data when creating device.\r\n"); |
Cumulocity | 41:804f6a0bda26 | 134 | _client.stop(); |
Cumulocity | 41:804f6a0bda26 | 135 | return 0; |
Cumulocity | 41:804f6a0bda26 | 136 | } |
Cumulocity | 41:804f6a0bda26 | 137 | |
Cumulocity | 41:804f6a0bda26 | 138 | if (_client.receive(received) != SMARTREST_SUCCESS) { |
xinlei | 77:f6717e4eccc4 | 139 | aError("No device found.\r\n"); |
Cumulocity | 41:804f6a0bda26 | 140 | _client.stop(); |
Cumulocity | 41:804f6a0bda26 | 141 | return false; |
Cumulocity | 41:804f6a0bda26 | 142 | } |
Cumulocity | 41:804f6a0bda26 | 143 | _client.stop(); |
Cumulocity | 41:804f6a0bda26 | 144 | |
Cumulocity | 41:804f6a0bda26 | 145 | if (received.values() != 3) { |
xinlei | 77:f6717e4eccc4 | 146 | aError("Received incomplete data when creating device.\r\n"); |
Cumulocity | 41:804f6a0bda26 | 147 | return false; |
Cumulocity | 41:804f6a0bda26 | 148 | } |
Cumulocity | 41:804f6a0bda26 | 149 | |
Cumulocity | 41:804f6a0bda26 | 150 | if (received.value(0).integerValue() != 201) { |
xinlei | 77:f6717e4eccc4 | 151 | aError("Received invalid data when creating device.\r\n"); |
Cumulocity | 41:804f6a0bda26 | 152 | return false; |
Cumulocity | 41:804f6a0bda26 | 153 | } |
Cumulocity | 41:804f6a0bda26 | 154 | |
Cumulocity | 41:804f6a0bda26 | 155 | _deviceId = received.value(2).integerValue(); |
Cumulocity | 41:804f6a0bda26 | 156 | return true; |
Cumulocity | 41:804f6a0bda26 | 157 | } |
Cumulocity | 41:804f6a0bda26 | 158 | |
Cumulocity | 41:804f6a0bda26 | 159 | bool DeviceIntegration::addGlobalIdentifier() |
Cumulocity | 41:804f6a0bda26 | 160 | { |
Cumulocity | 41:804f6a0bda26 | 161 | ComposedRecord record; |
Cumulocity | 41:804f6a0bda26 | 162 | ParsedRecord received; |
Cumulocity | 41:804f6a0bda26 | 163 | |
xinlei | 77:f6717e4eccc4 | 164 | aInfo("Adding global identifier...\r\n"); |
Cumulocity | 41:804f6a0bda26 | 165 | |
Cumulocity | 41:804f6a0bda26 | 166 | IntegerValue msgId(102); |
Cumulocity | 41:804f6a0bda26 | 167 | IntegerValue deviceId(_deviceId); |
Cumulocity | 41:804f6a0bda26 | 168 | CharValue imei(_deviceInfo.imei()); |
Cumulocity | 41:804f6a0bda26 | 169 | if ((!record.add(msgId)) || (!record.add(deviceId)) || (!record.add(imei))) |
Cumulocity | 41:804f6a0bda26 | 170 | return false; |
Cumulocity | 41:804f6a0bda26 | 171 | |
Cumulocity | 41:804f6a0bda26 | 172 | if (_client.send(record) != SMARTREST_SUCCESS) { |
xinlei | 77:f6717e4eccc4 | 173 | aWarning("Adding global identifier failed.\r\n"); |
Cumulocity | 41:804f6a0bda26 | 174 | _client.stop(); |
Cumulocity | 41:804f6a0bda26 | 175 | return false; |
Cumulocity | 41:804f6a0bda26 | 176 | } |
Cumulocity | 41:804f6a0bda26 | 177 | |
Cumulocity | 41:804f6a0bda26 | 178 | if (_client.receive(received) != SMARTREST_SUCCESS) { |
xinlei | 77:f6717e4eccc4 | 179 | aError("Received data failed when adding global identifier.\r\n"); |
Cumulocity | 41:804f6a0bda26 | 180 | _client.stop(); |
Cumulocity | 41:804f6a0bda26 | 181 | return false; |
Cumulocity | 41:804f6a0bda26 | 182 | } |
Cumulocity | 41:804f6a0bda26 | 183 | _client.stop(); |
Cumulocity | 41:804f6a0bda26 | 184 | |
Cumulocity | 41:804f6a0bda26 | 185 | if (received.values() != 3) { |
xinlei | 77:f6717e4eccc4 | 186 | aError("Received incomplete data when adding global identifier.\r\n"); |
Cumulocity | 41:804f6a0bda26 | 187 | return false; |
Cumulocity | 41:804f6a0bda26 | 188 | } |
Cumulocity | 41:804f6a0bda26 | 189 | |
Cumulocity | 41:804f6a0bda26 | 190 | if (received.value(0).integerValue() != 200) { |
xinlei | 77:f6717e4eccc4 | 191 | aError("Received invalid data when adding global identifier.\r\n"); |
Cumulocity | 41:804f6a0bda26 | 192 | return false; |
Cumulocity | 41:804f6a0bda26 | 193 | } |
Cumulocity | 41:804f6a0bda26 | 194 | |
Cumulocity | 41:804f6a0bda26 | 195 | return true; |
Cumulocity | 41:804f6a0bda26 | 196 | } |
Cumulocity | 41:804f6a0bda26 | 197 | |
Cumulocity | 41:804f6a0bda26 | 198 | bool DeviceIntegration::updateDevice() |
Cumulocity | 41:804f6a0bda26 | 199 | { |
Cumulocity | 41:804f6a0bda26 | 200 | ComposedRecord record; |
Cumulocity | 41:804f6a0bda26 | 201 | ParsedRecord received; |
Cumulocity | 41:804f6a0bda26 | 202 | |
Cumulocity | 41:804f6a0bda26 | 203 | IntegerValue msgId(103); |
Cumulocity | 41:804f6a0bda26 | 204 | IntegerValue deviceId(_deviceId); |
Cumulocity | 41:804f6a0bda26 | 205 | CharValue imei(_deviceInfo.imei()); |
Cumulocity | 41:804f6a0bda26 | 206 | CharValue cellId(_deviceInfo.cellId()); |
Cumulocity | 41:804f6a0bda26 | 207 | CharValue iccid(_deviceInfo.iccid()); |
Cumulocity | 41:804f6a0bda26 | 208 | if ((!record.add(msgId)) || (!record.add(deviceId)) || (!record.add(imei)) || (!record.add(cellId)) || (!record.add(iccid))) |
Cumulocity | 41:804f6a0bda26 | 209 | return false; |
Cumulocity | 41:804f6a0bda26 | 210 | |
Cumulocity | 41:804f6a0bda26 | 211 | if (_client.send(record) != SMARTREST_SUCCESS) { |
xinlei | 77:f6717e4eccc4 | 212 | aError("Sending data failed when updating device.\r\n"); |
Cumulocity | 41:804f6a0bda26 | 213 | _client.stop(); |
Cumulocity | 41:804f6a0bda26 | 214 | return false; |
Cumulocity | 41:804f6a0bda26 | 215 | } |
Cumulocity | 41:804f6a0bda26 | 216 | |
Cumulocity | 41:804f6a0bda26 | 217 | if (_client.receive(received) != SMARTREST_SUCCESS) { |
xinlei | 77:f6717e4eccc4 | 218 | aError("Updating device failed.\r\n"); |
Cumulocity | 41:804f6a0bda26 | 219 | _client.stop(); |
Cumulocity | 41:804f6a0bda26 | 220 | return false; |
Cumulocity | 41:804f6a0bda26 | 221 | } |
Cumulocity | 41:804f6a0bda26 | 222 | _client.stop(); |
Cumulocity | 41:804f6a0bda26 | 223 | |
Cumulocity | 41:804f6a0bda26 | 224 | if (received.values() != 3) { |
xinlei | 77:f6717e4eccc4 | 225 | aError("Received incomplete data when updating device.\r\n"); |
Cumulocity | 41:804f6a0bda26 | 226 | return false; |
Cumulocity | 41:804f6a0bda26 | 227 | } |
Cumulocity | 41:804f6a0bda26 | 228 | |
Cumulocity | 41:804f6a0bda26 | 229 | if (received.value(0).integerValue() != 201) { |
xinlei | 77:f6717e4eccc4 | 230 | aError("Received invalid data when updating device.\r\n"); |
Cumulocity | 41:804f6a0bda26 | 231 | return false; |
Cumulocity | 41:804f6a0bda26 | 232 | } |
Cumulocity | 41:804f6a0bda26 | 233 | |
Cumulocity | 41:804f6a0bda26 | 234 | return true; |
Cumulocity | 41:804f6a0bda26 | 235 | } |