portable version of the cumulocity demo

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Cumulocity Official

Committer:
Cumulocity
Date:
Tue Jul 15 12:44:34 2014 +0000
Revision:
41:804f6a0bda26
Child:
47:89ae46d5c466
refactoring of mbed agent

Who changed what in which revision?

UserRevisionLine numberNew 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
Cumulocity 41:804f6a0bda26 7 DeviceIntegration::DeviceIntegration(SmartRest& 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>
Cumulocity 41:804f6a0bda26 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_RequiredAvailability\"\":{ \"\"responseInterval\"\":15},\"\"c8y_IsDevice\"\":{}}\"\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 41:804f6a0bda26 64 return 1;
Cumulocity 41:804f6a0bda26 65 }
Cumulocity 41:804f6a0bda26 66
Cumulocity 41:804f6a0bda26 67 puts("Hello.");
Cumulocity 41:804f6a0bda26 68 if (!deviceExisting()) {
Cumulocity 41:804f6a0bda26 69 puts("Creating device.");
Cumulocity 41:804f6a0bda26 70 if (!createDevice()) {
Cumulocity 41:804f6a0bda26 71 puts("Failed.");
Cumulocity 41:804f6a0bda26 72 return false;
Cumulocity 41:804f6a0bda26 73 }
Cumulocity 41:804f6a0bda26 74 puts("Adding global identifier.");
Cumulocity 41:804f6a0bda26 75 if (!addGlobalIdentifier()) {
Cumulocity 41:804f6a0bda26 76 puts("Failed.");
Cumulocity 41:804f6a0bda26 77 return false;
Cumulocity 41:804f6a0bda26 78 }
Cumulocity 41:804f6a0bda26 79 }
Cumulocity 41:804f6a0bda26 80
Cumulocity 41:804f6a0bda26 81 puts("Updating device data.");
Cumulocity 41:804f6a0bda26 82 if (!updateDevice()) {
Cumulocity 41:804f6a0bda26 83 puts("Failed.");
Cumulocity 41:804f6a0bda26 84 return false;
Cumulocity 41:804f6a0bda26 85 }
Cumulocity 41:804f6a0bda26 86
Cumulocity 41:804f6a0bda26 87 return true;
Cumulocity 41:804f6a0bda26 88 }
Cumulocity 41:804f6a0bda26 89
Cumulocity 41:804f6a0bda26 90 bool DeviceIntegration::deviceExisting()
Cumulocity 41:804f6a0bda26 91 {
Cumulocity 41:804f6a0bda26 92 ComposedRecord record;
Cumulocity 41:804f6a0bda26 93 ParsedRecord received;
Cumulocity 41:804f6a0bda26 94
Cumulocity 41:804f6a0bda26 95 IntegerValue msgId(100);
Cumulocity 41:804f6a0bda26 96 CharValue imei(_deviceInfo.imei());
Cumulocity 41:804f6a0bda26 97 if ((!record.add(msgId)) || (!record.add(imei)))
Cumulocity 41:804f6a0bda26 98 return false;
Cumulocity 41:804f6a0bda26 99
Cumulocity 41:804f6a0bda26 100 if (_client.send(record) != SMARTREST_SUCCESS) {
Cumulocity 41:804f6a0bda26 101 puts("Send failed.");
Cumulocity 41:804f6a0bda26 102 _client.stop();
Cumulocity 41:804f6a0bda26 103 return false;
Cumulocity 41:804f6a0bda26 104 }
Cumulocity 41:804f6a0bda26 105
Cumulocity 41:804f6a0bda26 106 if (_client.receive(received) != SMARTREST_SUCCESS) {
Cumulocity 41:804f6a0bda26 107 puts("No device found.");
Cumulocity 41:804f6a0bda26 108 _client.stop();
Cumulocity 41:804f6a0bda26 109 return false;
Cumulocity 41:804f6a0bda26 110 }
Cumulocity 41:804f6a0bda26 111 _client.stop();
Cumulocity 41:804f6a0bda26 112
Cumulocity 41:804f6a0bda26 113 if (received.values() == 0) {
Cumulocity 41:804f6a0bda26 114 puts("Received no values.");
Cumulocity 41:804f6a0bda26 115 return false;
Cumulocity 41:804f6a0bda26 116 }
Cumulocity 41:804f6a0bda26 117
Cumulocity 41:804f6a0bda26 118 if (received.value(0).integerValue() == 50) {
Cumulocity 41:804f6a0bda26 119 return false;
Cumulocity 41:804f6a0bda26 120 }
Cumulocity 41:804f6a0bda26 121
Cumulocity 41:804f6a0bda26 122 if (received.value(0).integerValue() != 200) {
Cumulocity 41:804f6a0bda26 123 puts("Bad response.");
Cumulocity 41:804f6a0bda26 124 return false;
Cumulocity 41:804f6a0bda26 125 }
Cumulocity 41:804f6a0bda26 126
Cumulocity 41:804f6a0bda26 127 _deviceId = received.value(2).integerValue();
Cumulocity 41:804f6a0bda26 128
Cumulocity 41:804f6a0bda26 129 return true;
Cumulocity 41:804f6a0bda26 130 }
Cumulocity 41:804f6a0bda26 131
Cumulocity 41:804f6a0bda26 132 bool DeviceIntegration::createDevice()
Cumulocity 41:804f6a0bda26 133 {
Cumulocity 41:804f6a0bda26 134 ComposedRecord record;
Cumulocity 41:804f6a0bda26 135 ParsedRecord received;
Cumulocity 41:804f6a0bda26 136
Cumulocity 41:804f6a0bda26 137 puts("Creating device...");
Cumulocity 41:804f6a0bda26 138
Cumulocity 41:804f6a0bda26 139 IntegerValue msgId(101);
Cumulocity 41:804f6a0bda26 140 CharValue imei(_deviceInfo.imei());
Cumulocity 41:804f6a0bda26 141 if ((!record.add(msgId)) || (!record.add(imei)))
Cumulocity 41:804f6a0bda26 142 return false;
Cumulocity 41:804f6a0bda26 143
Cumulocity 41:804f6a0bda26 144 if (_client.send(record) != SMARTREST_SUCCESS) {
Cumulocity 41:804f6a0bda26 145 puts("Send failed.");
Cumulocity 41:804f6a0bda26 146 _client.stop();
Cumulocity 41:804f6a0bda26 147 return 0;
Cumulocity 41:804f6a0bda26 148 }
Cumulocity 41:804f6a0bda26 149
Cumulocity 41:804f6a0bda26 150 if (_client.receive(received) != SMARTREST_SUCCESS) {
Cumulocity 41:804f6a0bda26 151 puts("No device found.");
Cumulocity 41:804f6a0bda26 152 _client.stop();
Cumulocity 41:804f6a0bda26 153 return false;
Cumulocity 41:804f6a0bda26 154 }
Cumulocity 41:804f6a0bda26 155 _client.stop();
Cumulocity 41:804f6a0bda26 156
Cumulocity 41:804f6a0bda26 157 if (received.values() != 3) {
Cumulocity 41:804f6a0bda26 158 puts("Bad received data.");
Cumulocity 41:804f6a0bda26 159 return false;
Cumulocity 41:804f6a0bda26 160 }
Cumulocity 41:804f6a0bda26 161
Cumulocity 41:804f6a0bda26 162 if (received.value(0).integerValue() != 201) {
Cumulocity 41:804f6a0bda26 163 puts("Bad received data.");
Cumulocity 41:804f6a0bda26 164 return false;
Cumulocity 41:804f6a0bda26 165 }
Cumulocity 41:804f6a0bda26 166
Cumulocity 41:804f6a0bda26 167 _deviceId = received.value(2).integerValue();
Cumulocity 41:804f6a0bda26 168 return true;
Cumulocity 41:804f6a0bda26 169 }
Cumulocity 41:804f6a0bda26 170
Cumulocity 41:804f6a0bda26 171 bool DeviceIntegration::addGlobalIdentifier()
Cumulocity 41:804f6a0bda26 172 {
Cumulocity 41:804f6a0bda26 173 ComposedRecord record;
Cumulocity 41:804f6a0bda26 174 ParsedRecord received;
Cumulocity 41:804f6a0bda26 175
Cumulocity 41:804f6a0bda26 176 puts("Adding global identifier...");
Cumulocity 41:804f6a0bda26 177
Cumulocity 41:804f6a0bda26 178 IntegerValue msgId(102);
Cumulocity 41:804f6a0bda26 179 IntegerValue deviceId(_deviceId);
Cumulocity 41:804f6a0bda26 180 CharValue imei(_deviceInfo.imei());
Cumulocity 41:804f6a0bda26 181 if ((!record.add(msgId)) || (!record.add(deviceId)) || (!record.add(imei)))
Cumulocity 41:804f6a0bda26 182 return false;
Cumulocity 41:804f6a0bda26 183
Cumulocity 41:804f6a0bda26 184 if (_client.send(record) != SMARTREST_SUCCESS) {
Cumulocity 41:804f6a0bda26 185 puts("Sending failed.");
Cumulocity 41:804f6a0bda26 186 _client.stop();
Cumulocity 41:804f6a0bda26 187 return false;
Cumulocity 41:804f6a0bda26 188 }
Cumulocity 41:804f6a0bda26 189
Cumulocity 41:804f6a0bda26 190 if (_client.receive(received) != SMARTREST_SUCCESS) {
Cumulocity 41:804f6a0bda26 191 puts("Failed.");
Cumulocity 41:804f6a0bda26 192 _client.stop();
Cumulocity 41:804f6a0bda26 193 return false;
Cumulocity 41:804f6a0bda26 194 }
Cumulocity 41:804f6a0bda26 195 _client.stop();
Cumulocity 41:804f6a0bda26 196
Cumulocity 41:804f6a0bda26 197 if (received.values() != 3) {
Cumulocity 41:804f6a0bda26 198 puts("Received bad data.");
Cumulocity 41:804f6a0bda26 199 return false;
Cumulocity 41:804f6a0bda26 200 }
Cumulocity 41:804f6a0bda26 201
Cumulocity 41:804f6a0bda26 202 if (received.value(0).integerValue() != 200) {
Cumulocity 41:804f6a0bda26 203 puts("Received bad data.");
Cumulocity 41:804f6a0bda26 204 return false;
Cumulocity 41:804f6a0bda26 205 }
Cumulocity 41:804f6a0bda26 206
Cumulocity 41:804f6a0bda26 207 return true;
Cumulocity 41:804f6a0bda26 208 }
Cumulocity 41:804f6a0bda26 209
Cumulocity 41:804f6a0bda26 210 bool DeviceIntegration::updateDevice()
Cumulocity 41:804f6a0bda26 211 {
Cumulocity 41:804f6a0bda26 212 ComposedRecord record;
Cumulocity 41:804f6a0bda26 213 ParsedRecord received;
Cumulocity 41:804f6a0bda26 214
Cumulocity 41:804f6a0bda26 215 IntegerValue msgId(103);
Cumulocity 41:804f6a0bda26 216 IntegerValue deviceId(_deviceId);
Cumulocity 41:804f6a0bda26 217 CharValue imei(_deviceInfo.imei());
Cumulocity 41:804f6a0bda26 218 CharValue cellId(_deviceInfo.cellId());
Cumulocity 41:804f6a0bda26 219 CharValue iccid(_deviceInfo.iccid());
Cumulocity 41:804f6a0bda26 220 if ((!record.add(msgId)) || (!record.add(deviceId)) || (!record.add(imei)) || (!record.add(cellId)) || (!record.add(iccid)))
Cumulocity 41:804f6a0bda26 221 return false;
Cumulocity 41:804f6a0bda26 222
Cumulocity 41:804f6a0bda26 223 if (_client.send(record) != SMARTREST_SUCCESS) {
Cumulocity 41:804f6a0bda26 224 puts("Send failed.");
Cumulocity 41:804f6a0bda26 225 _client.stop();
Cumulocity 41:804f6a0bda26 226 return false;
Cumulocity 41:804f6a0bda26 227 }
Cumulocity 41:804f6a0bda26 228
Cumulocity 41:804f6a0bda26 229 if (_client.receive(received) != SMARTREST_SUCCESS) {
Cumulocity 41:804f6a0bda26 230 puts("Update failed.");
Cumulocity 41:804f6a0bda26 231 _client.stop();
Cumulocity 41:804f6a0bda26 232 return false;
Cumulocity 41:804f6a0bda26 233 }
Cumulocity 41:804f6a0bda26 234 _client.stop();
Cumulocity 41:804f6a0bda26 235
Cumulocity 41:804f6a0bda26 236 if (received.values() != 3) {
Cumulocity 41:804f6a0bda26 237 puts("Bad received data.");
Cumulocity 41:804f6a0bda26 238 return false;
Cumulocity 41:804f6a0bda26 239 }
Cumulocity 41:804f6a0bda26 240
Cumulocity 41:804f6a0bda26 241 if (received.value(0).integerValue() != 201) {
Cumulocity 41:804f6a0bda26 242 puts("Bad received data.");
Cumulocity 41:804f6a0bda26 243 return false;
Cumulocity 41:804f6a0bda26 244 }
Cumulocity 41:804f6a0bda26 245
Cumulocity 41:804f6a0bda26 246 return true;
Cumulocity 41:804f6a0bda26 247 }