![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Own fork of MbedSmartRestMain
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
DeviceIntegration.cpp@57:4af5f1bec3a6, 2014-10-23 (annotated)
- Committer:
- vwochnik
- Date:
- Thu Oct 23 14:21:26 2014 +0000
- Revision:
- 57:4af5f1bec3a6
- Parent:
- 55:a0f7295ed6b6
- Child:
- 66:31c754c36ed7
Operation support skeleton
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" |
Cumulocity | 41:804f6a0bda26 | 6 | |
vwochnik | 55:a0f7295ed6b6 | 7 | DeviceIntegration::DeviceIntegration(AbstractSmartRest& client, SmartRestTemplate& tpl, long& deviceId, DeviceInfo& deviceInfo) : |
Cumulocity | 41:804f6a0bda26 | 8 | _client(client), |
Cumulocity | 41:804f6a0bda26 | 9 | _tpl(tpl), |
Cumulocity | 41:804f6a0bda26 | 10 | _deviceId(deviceId), |
Cumulocity | 41:804f6a0bda26 | 11 | _deviceInfo(deviceInfo) |
Cumulocity | 41:804f6a0bda26 | 12 | { |
Cumulocity | 41:804f6a0bda26 | 13 | _init = false; |
Cumulocity | 41:804f6a0bda26 | 14 | } |
Cumulocity | 41:804f6a0bda26 | 15 | |
Cumulocity | 41:804f6a0bda26 | 16 | bool DeviceIntegration::init() |
Cumulocity | 41:804f6a0bda26 | 17 | { |
Cumulocity | 41:804f6a0bda26 | 18 | if (_init) |
Cumulocity | 41:804f6a0bda26 | 19 | return false; |
Cumulocity | 41:804f6a0bda26 | 20 | |
Cumulocity | 41:804f6a0bda26 | 21 | // get device by identity |
Cumulocity | 41:804f6a0bda26 | 22 | // Usage: 100,<SERIAL/NR> |
Cumulocity | 41:804f6a0bda26 | 23 | if (!_tpl.add("10,100,GET,/identity/externalIds/c8y_Serial/%%,,application/vnd.com.nsn.cumulocity.externalId+json,%%,STRING,\r\n")) |
Cumulocity | 41:804f6a0bda26 | 24 | return false; |
Cumulocity | 41:804f6a0bda26 | 25 | |
Cumulocity | 41:804f6a0bda26 | 26 | // get device id from identity |
Cumulocity | 41:804f6a0bda26 | 27 | // Response: 200,<DEVICE/ID> |
Cumulocity | 41:804f6a0bda26 | 28 | if (!_tpl.add("11,200,\"$.managedObject\",,\"$.id\"\r\n")) |
Cumulocity | 41:804f6a0bda26 | 29 | return false; |
Cumulocity | 41:804f6a0bda26 | 30 | |
Cumulocity | 41:804f6a0bda26 | 31 | // Create device |
Cumulocity | 41:804f6a0bda26 | 32 | // Usage: 101,<SERIAL/NR> |
vwochnik | 57:4af5f1bec3a6 | 33 | 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_SupportedMeasurements\"\":[\"\"c8y_SignalStrength\"\",\"\"c8y_TemperatureMeasurement\"\",\"\"c8y_AnalogMeasurement\"\",\"\"c8y_MotionMeasurement\"\",\"\"c8y_AnalogMeasurement\"\"],\"\"c8y_RequiredAvailability\"\":{ \"\"responseInterval\"\":15},\"\"c8y_IsDevice\"\":{},\"\"com_cumulocity_model_Agent\"\":{}}\"\r\n")) |
Cumulocity | 41:804f6a0bda26 | 34 | return false; |
Cumulocity | 41:804f6a0bda26 | 35 | |
Cumulocity | 41:804f6a0bda26 | 36 | // Get device id |
Cumulocity | 41:804f6a0bda26 | 37 | // Response: 201,<DEVICE/ID> |
Cumulocity | 41:804f6a0bda26 | 38 | if (!_tpl.add("11,201,,\"$.c8y_IsDevice\",\"$.id\"\r\n")) |
Cumulocity | 41:804f6a0bda26 | 39 | return false; |
Cumulocity | 41:804f6a0bda26 | 40 | |
Cumulocity | 41:804f6a0bda26 | 41 | // Insert global ID |
Cumulocity | 41:804f6a0bda26 | 42 | // Usage: 102,<DEVICE/ID>,<SERIAL/NR> |
Cumulocity | 41:804f6a0bda26 | 43 | 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 | 44 | return false; |
Cumulocity | 41:804f6a0bda26 | 45 | |
Cumulocity | 41:804f6a0bda26 | 46 | // Update IMEI, CellId and iccid |
Cumulocity | 41:804f6a0bda26 | 47 | // Usage: 103,<DEVICE/ID>,<IMEI>,<CELL/ID>,<ICCID> |
Cumulocity | 41:804f6a0bda26 | 48 | 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 | 49 | return false; |
Cumulocity | 41:804f6a0bda26 | 50 | |
Cumulocity | 41:804f6a0bda26 | 51 | _init = true; |
Cumulocity | 41:804f6a0bda26 | 52 | _deviceId = 0; |
Cumulocity | 41:804f6a0bda26 | 53 | return true; |
Cumulocity | 41:804f6a0bda26 | 54 | } |
Cumulocity | 41:804f6a0bda26 | 55 | |
Cumulocity | 41:804f6a0bda26 | 56 | bool DeviceIntegration::integrate() |
Cumulocity | 41:804f6a0bda26 | 57 | { |
Cumulocity | 41:804f6a0bda26 | 58 | if (_deviceId != 0) |
Cumulocity | 41:804f6a0bda26 | 59 | return false; |
Cumulocity | 41:804f6a0bda26 | 60 | |
Cumulocity | 41:804f6a0bda26 | 61 | // template bootstrapping process |
Cumulocity | 41:804f6a0bda26 | 62 | if (_client.bootstrap(_tpl) != SMARTREST_SUCCESS) { |
Cumulocity | 41:804f6a0bda26 | 63 | puts("Template bootstrap failed."); |
Cumulocity | 49:ac0ba9d54ebc | 64 | return false; |
Cumulocity | 41:804f6a0bda26 | 65 | } |
Cumulocity | 41:804f6a0bda26 | 66 | |
vwochnik | 52:8f1370084268 | 67 | if ((!deviceExisting()) && ((!createDevice()) || (!addGlobalIdentifier()))) |
vwochnik | 52:8f1370084268 | 68 | return false; |
Cumulocity | 41:804f6a0bda26 | 69 | |
vwochnik | 52:8f1370084268 | 70 | if (!updateDevice()) |
Cumulocity | 41:804f6a0bda26 | 71 | return false; |
Cumulocity | 41:804f6a0bda26 | 72 | |
Cumulocity | 41:804f6a0bda26 | 73 | return true; |
Cumulocity | 41:804f6a0bda26 | 74 | } |
Cumulocity | 41:804f6a0bda26 | 75 | |
Cumulocity | 41:804f6a0bda26 | 76 | bool DeviceIntegration::deviceExisting() |
Cumulocity | 41:804f6a0bda26 | 77 | { |
Cumulocity | 41:804f6a0bda26 | 78 | ComposedRecord record; |
Cumulocity | 41:804f6a0bda26 | 79 | ParsedRecord received; |
Cumulocity | 41:804f6a0bda26 | 80 | |
Cumulocity | 41:804f6a0bda26 | 81 | IntegerValue msgId(100); |
Cumulocity | 41:804f6a0bda26 | 82 | CharValue imei(_deviceInfo.imei()); |
Cumulocity | 41:804f6a0bda26 | 83 | if ((!record.add(msgId)) || (!record.add(imei))) |
Cumulocity | 41:804f6a0bda26 | 84 | return false; |
Cumulocity | 41:804f6a0bda26 | 85 | |
Cumulocity | 41:804f6a0bda26 | 86 | if (_client.send(record) != SMARTREST_SUCCESS) { |
Cumulocity | 41:804f6a0bda26 | 87 | puts("Send failed."); |
Cumulocity | 41:804f6a0bda26 | 88 | _client.stop(); |
Cumulocity | 41:804f6a0bda26 | 89 | return false; |
Cumulocity | 41:804f6a0bda26 | 90 | } |
Cumulocity | 41:804f6a0bda26 | 91 | |
Cumulocity | 41:804f6a0bda26 | 92 | if (_client.receive(received) != SMARTREST_SUCCESS) { |
Cumulocity | 41:804f6a0bda26 | 93 | puts("No device found."); |
Cumulocity | 41:804f6a0bda26 | 94 | _client.stop(); |
Cumulocity | 41:804f6a0bda26 | 95 | return false; |
Cumulocity | 41:804f6a0bda26 | 96 | } |
Cumulocity | 41:804f6a0bda26 | 97 | _client.stop(); |
Cumulocity | 41:804f6a0bda26 | 98 | |
Cumulocity | 41:804f6a0bda26 | 99 | if (received.values() == 0) { |
Cumulocity | 41:804f6a0bda26 | 100 | puts("Received no values."); |
Cumulocity | 41:804f6a0bda26 | 101 | return false; |
Cumulocity | 41:804f6a0bda26 | 102 | } |
Cumulocity | 41:804f6a0bda26 | 103 | |
Cumulocity | 41:804f6a0bda26 | 104 | if (received.value(0).integerValue() == 50) { |
Cumulocity | 41:804f6a0bda26 | 105 | return false; |
Cumulocity | 41:804f6a0bda26 | 106 | } |
Cumulocity | 41:804f6a0bda26 | 107 | |
Cumulocity | 41:804f6a0bda26 | 108 | if (received.value(0).integerValue() != 200) { |
Cumulocity | 41:804f6a0bda26 | 109 | puts("Bad response."); |
Cumulocity | 41:804f6a0bda26 | 110 | return false; |
Cumulocity | 41:804f6a0bda26 | 111 | } |
Cumulocity | 41:804f6a0bda26 | 112 | |
Cumulocity | 41:804f6a0bda26 | 113 | _deviceId = received.value(2).integerValue(); |
Cumulocity | 41:804f6a0bda26 | 114 | |
Cumulocity | 41:804f6a0bda26 | 115 | return true; |
Cumulocity | 41:804f6a0bda26 | 116 | } |
Cumulocity | 41:804f6a0bda26 | 117 | |
Cumulocity | 41:804f6a0bda26 | 118 | bool DeviceIntegration::createDevice() |
Cumulocity | 41:804f6a0bda26 | 119 | { |
Cumulocity | 41:804f6a0bda26 | 120 | ComposedRecord record; |
Cumulocity | 41:804f6a0bda26 | 121 | ParsedRecord received; |
Cumulocity | 41:804f6a0bda26 | 122 | |
Cumulocity | 41:804f6a0bda26 | 123 | puts("Creating device..."); |
Cumulocity | 41:804f6a0bda26 | 124 | |
Cumulocity | 41:804f6a0bda26 | 125 | IntegerValue msgId(101); |
Cumulocity | 41:804f6a0bda26 | 126 | CharValue imei(_deviceInfo.imei()); |
Cumulocity | 41:804f6a0bda26 | 127 | if ((!record.add(msgId)) || (!record.add(imei))) |
Cumulocity | 41:804f6a0bda26 | 128 | return false; |
Cumulocity | 41:804f6a0bda26 | 129 | |
Cumulocity | 41:804f6a0bda26 | 130 | if (_client.send(record) != SMARTREST_SUCCESS) { |
Cumulocity | 41:804f6a0bda26 | 131 | puts("Send failed."); |
Cumulocity | 41:804f6a0bda26 | 132 | _client.stop(); |
Cumulocity | 41:804f6a0bda26 | 133 | return 0; |
Cumulocity | 41:804f6a0bda26 | 134 | } |
Cumulocity | 41:804f6a0bda26 | 135 | |
Cumulocity | 41:804f6a0bda26 | 136 | if (_client.receive(received) != SMARTREST_SUCCESS) { |
Cumulocity | 41:804f6a0bda26 | 137 | puts("No device found."); |
Cumulocity | 41:804f6a0bda26 | 138 | _client.stop(); |
Cumulocity | 41:804f6a0bda26 | 139 | return false; |
Cumulocity | 41:804f6a0bda26 | 140 | } |
Cumulocity | 41:804f6a0bda26 | 141 | _client.stop(); |
Cumulocity | 41:804f6a0bda26 | 142 | |
Cumulocity | 41:804f6a0bda26 | 143 | if (received.values() != 3) { |
Cumulocity | 41:804f6a0bda26 | 144 | puts("Bad received data."); |
Cumulocity | 41:804f6a0bda26 | 145 | return false; |
Cumulocity | 41:804f6a0bda26 | 146 | } |
Cumulocity | 41:804f6a0bda26 | 147 | |
Cumulocity | 41:804f6a0bda26 | 148 | if (received.value(0).integerValue() != 201) { |
Cumulocity | 41:804f6a0bda26 | 149 | puts("Bad received data."); |
Cumulocity | 41:804f6a0bda26 | 150 | return false; |
Cumulocity | 41:804f6a0bda26 | 151 | } |
Cumulocity | 41:804f6a0bda26 | 152 | |
Cumulocity | 41:804f6a0bda26 | 153 | _deviceId = received.value(2).integerValue(); |
Cumulocity | 41:804f6a0bda26 | 154 | return true; |
Cumulocity | 41:804f6a0bda26 | 155 | } |
Cumulocity | 41:804f6a0bda26 | 156 | |
Cumulocity | 41:804f6a0bda26 | 157 | bool DeviceIntegration::addGlobalIdentifier() |
Cumulocity | 41:804f6a0bda26 | 158 | { |
Cumulocity | 41:804f6a0bda26 | 159 | ComposedRecord record; |
Cumulocity | 41:804f6a0bda26 | 160 | ParsedRecord received; |
Cumulocity | 41:804f6a0bda26 | 161 | |
Cumulocity | 41:804f6a0bda26 | 162 | puts("Adding global identifier..."); |
Cumulocity | 41:804f6a0bda26 | 163 | |
Cumulocity | 41:804f6a0bda26 | 164 | IntegerValue msgId(102); |
Cumulocity | 41:804f6a0bda26 | 165 | IntegerValue deviceId(_deviceId); |
Cumulocity | 41:804f6a0bda26 | 166 | CharValue imei(_deviceInfo.imei()); |
Cumulocity | 41:804f6a0bda26 | 167 | if ((!record.add(msgId)) || (!record.add(deviceId)) || (!record.add(imei))) |
Cumulocity | 41:804f6a0bda26 | 168 | return false; |
Cumulocity | 41:804f6a0bda26 | 169 | |
Cumulocity | 41:804f6a0bda26 | 170 | if (_client.send(record) != SMARTREST_SUCCESS) { |
Cumulocity | 41:804f6a0bda26 | 171 | puts("Sending failed."); |
Cumulocity | 41:804f6a0bda26 | 172 | _client.stop(); |
Cumulocity | 41:804f6a0bda26 | 173 | return false; |
Cumulocity | 41:804f6a0bda26 | 174 | } |
Cumulocity | 41:804f6a0bda26 | 175 | |
Cumulocity | 41:804f6a0bda26 | 176 | if (_client.receive(received) != SMARTREST_SUCCESS) { |
Cumulocity | 41:804f6a0bda26 | 177 | puts("Failed."); |
Cumulocity | 41:804f6a0bda26 | 178 | _client.stop(); |
Cumulocity | 41:804f6a0bda26 | 179 | return false; |
Cumulocity | 41:804f6a0bda26 | 180 | } |
Cumulocity | 41:804f6a0bda26 | 181 | _client.stop(); |
Cumulocity | 41:804f6a0bda26 | 182 | |
Cumulocity | 41:804f6a0bda26 | 183 | if (received.values() != 3) { |
Cumulocity | 41:804f6a0bda26 | 184 | puts("Received bad data."); |
Cumulocity | 41:804f6a0bda26 | 185 | return false; |
Cumulocity | 41:804f6a0bda26 | 186 | } |
Cumulocity | 41:804f6a0bda26 | 187 | |
Cumulocity | 41:804f6a0bda26 | 188 | if (received.value(0).integerValue() != 200) { |
Cumulocity | 41:804f6a0bda26 | 189 | puts("Received bad data."); |
Cumulocity | 41:804f6a0bda26 | 190 | return false; |
Cumulocity | 41:804f6a0bda26 | 191 | } |
Cumulocity | 41:804f6a0bda26 | 192 | |
Cumulocity | 41:804f6a0bda26 | 193 | return true; |
Cumulocity | 41:804f6a0bda26 | 194 | } |
Cumulocity | 41:804f6a0bda26 | 195 | |
Cumulocity | 41:804f6a0bda26 | 196 | bool DeviceIntegration::updateDevice() |
Cumulocity | 41:804f6a0bda26 | 197 | { |
Cumulocity | 41:804f6a0bda26 | 198 | ComposedRecord record; |
Cumulocity | 41:804f6a0bda26 | 199 | ParsedRecord received; |
Cumulocity | 41:804f6a0bda26 | 200 | |
Cumulocity | 41:804f6a0bda26 | 201 | IntegerValue msgId(103); |
Cumulocity | 41:804f6a0bda26 | 202 | IntegerValue deviceId(_deviceId); |
Cumulocity | 41:804f6a0bda26 | 203 | CharValue imei(_deviceInfo.imei()); |
Cumulocity | 41:804f6a0bda26 | 204 | CharValue cellId(_deviceInfo.cellId()); |
Cumulocity | 41:804f6a0bda26 | 205 | CharValue iccid(_deviceInfo.iccid()); |
Cumulocity | 41:804f6a0bda26 | 206 | if ((!record.add(msgId)) || (!record.add(deviceId)) || (!record.add(imei)) || (!record.add(cellId)) || (!record.add(iccid))) |
Cumulocity | 41:804f6a0bda26 | 207 | return false; |
Cumulocity | 41:804f6a0bda26 | 208 | |
Cumulocity | 41:804f6a0bda26 | 209 | if (_client.send(record) != SMARTREST_SUCCESS) { |
Cumulocity | 41:804f6a0bda26 | 210 | puts("Send failed."); |
Cumulocity | 41:804f6a0bda26 | 211 | _client.stop(); |
Cumulocity | 41:804f6a0bda26 | 212 | return false; |
Cumulocity | 41:804f6a0bda26 | 213 | } |
Cumulocity | 41:804f6a0bda26 | 214 | |
Cumulocity | 41:804f6a0bda26 | 215 | if (_client.receive(received) != SMARTREST_SUCCESS) { |
Cumulocity | 41:804f6a0bda26 | 216 | puts("Update failed."); |
Cumulocity | 41:804f6a0bda26 | 217 | _client.stop(); |
Cumulocity | 41:804f6a0bda26 | 218 | return false; |
Cumulocity | 41:804f6a0bda26 | 219 | } |
Cumulocity | 41:804f6a0bda26 | 220 | _client.stop(); |
Cumulocity | 41:804f6a0bda26 | 221 | |
Cumulocity | 41:804f6a0bda26 | 222 | if (received.values() != 3) { |
Cumulocity | 41:804f6a0bda26 | 223 | puts("Bad received data."); |
Cumulocity | 41:804f6a0bda26 | 224 | return false; |
Cumulocity | 41:804f6a0bda26 | 225 | } |
Cumulocity | 41:804f6a0bda26 | 226 | |
Cumulocity | 41:804f6a0bda26 | 227 | if (received.value(0).integerValue() != 201) { |
Cumulocity | 41:804f6a0bda26 | 228 | puts("Bad received data."); |
Cumulocity | 41:804f6a0bda26 | 229 | return false; |
Cumulocity | 41:804f6a0bda26 | 230 | } |
Cumulocity | 41:804f6a0bda26 | 231 | |
Cumulocity | 41:804f6a0bda26 | 232 | return true; |
Cumulocity | 41:804f6a0bda26 | 233 | } |