Own fork of MbedSmartRestMain
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
DeviceIntegration.cpp
- Committer:
- xinlei
- Date:
- 2015-03-20
- Revision:
- 91:48069375dffa
- Parent:
- 77:f6717e4eccc4
- Child:
- 92:0acd11870c6a
File content as of revision 91:48069375dffa:
#include "DeviceIntegration.h" #include <stdio.h> #include "ComposedRecord.h" #include "CharValue.h" #include "IntegerValue.h" #include "logging.h" DeviceIntegration::DeviceIntegration(AbstractSmartRest& client, SmartRestTemplate& tpl, long& deviceId, DeviceInfo& deviceInfo) : _deviceId(deviceId), _tpl(tpl), _client(client), _deviceInfo(deviceInfo) { _init = false; } bool DeviceIntegration::init() { if (_init) return false; // get device by identity // Usage: 100,<SERIAL/NR> if (!_tpl.add("10,100,GET,/identity/externalIds/c8y_Serial/%%,,application/vnd.com.nsn.cumulocity.externalId+json,%%,STRING,\r\n")) return false; // get device id from identity // Response: 200,<DEVICE/ID> if (!_tpl.add("11,200,\"$.managedObject\",,\"$.id\"\r\n")) return false; // Create device // Usage: 101,<SERIAL/NR> 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")) return false; // Get device id // Response: 201,<DEVICE/ID> if (!_tpl.add("11,201,,\"$.c8y_IsDevice\",\"$.id\"\r\n")) return false; // Insert global ID // Usage: 102,<DEVICE/ID>,<SERIAL/NR> 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")) return false; // Update IMEI, CellId and iccid // Usage: 103,<DEVICE/ID>,<IMEI>,<CELL/ID>,<ICCID> 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")) return false; _init = true; _deviceId = 0; return true; } bool DeviceIntegration::integrate() { if (_deviceId != 0) return false; // template bootstrapping process if (_client.bootstrap(_tpl) != SMARTREST_SUCCESS) { aError("Template bootstrap failed.\r\n"); return false; } if ((!deviceExisting()) && ((!createDevice()) || (!addGlobalIdentifier()))) return false; if (!updateDevice()) return false; return true; } bool DeviceIntegration::deviceExisting() { ComposedRecord record; ParsedRecord received; IntegerValue msgId(100); CharValue imei(_deviceInfo.imei()); if ((!record.add(msgId)) || (!record.add(imei))) return false; if (_client.send(record) != SMARTREST_SUCCESS) { aWarning("Send failed.\r\n"); _client.stop(); return false; } if (_client.receive(received) != SMARTREST_SUCCESS) { aError("No device found.\r\n"); _client.stop(); return false; } _client.stop(); if (received.values() == 0) { aError("Received no values.\r\n"); return false; } if (received.value(0).integerValue() == 50) { return false; } if (received.value(0).integerValue() != 200) { aError("Bad response.\r\n"); return false; } _deviceId = received.value(2).integerValue(); return true; } bool DeviceIntegration::createDevice() { ComposedRecord record; ParsedRecord received; aInfo("Creating device...\r\n"); IntegerValue msgId(101); CharValue imei(_deviceInfo.imei()); if ((!record.add(msgId)) || (!record.add(imei))) return false; if (_client.send(record) != SMARTREST_SUCCESS) { aError("Failed to sending data when creating device.\r\n"); _client.stop(); return 0; } if (_client.receive(received) != SMARTREST_SUCCESS) { aError("No device found.\r\n"); _client.stop(); return false; } _client.stop(); if (received.values() != 3) { aError("Received incomplete data when creating device.\r\n"); return false; } if (received.value(0).integerValue() != 201) { aError("Received invalid data when creating device.\r\n"); return false; } _deviceId = received.value(2).integerValue(); return true; } bool DeviceIntegration::addGlobalIdentifier() { ComposedRecord record; ParsedRecord received; aInfo("Adding global identifier...\r\n"); IntegerValue msgId(102); IntegerValue deviceId(_deviceId); CharValue imei(_deviceInfo.imei()); if ((!record.add(msgId)) || (!record.add(deviceId)) || (!record.add(imei))) return false; if (_client.send(record) != SMARTREST_SUCCESS) { aWarning("Adding global identifier failed.\r\n"); _client.stop(); return false; } if (_client.receive(received) != SMARTREST_SUCCESS) { aError("Received data failed when adding global identifier.\r\n"); _client.stop(); return false; } _client.stop(); if (received.values() != 3) { aError("Received incomplete data when adding global identifier.\r\n"); return false; } if (received.value(0).integerValue() != 200) { aError("Received invalid data when adding global identifier.\r\n"); return false; } return true; } bool DeviceIntegration::updateDevice() { ComposedRecord record; ParsedRecord received; IntegerValue msgId(103); IntegerValue deviceId(_deviceId); CharValue imei(_deviceInfo.imei()); CharValue cellId(_deviceInfo.cellId()); CharValue iccid(_deviceInfo.iccid()); if ((!record.add(msgId)) || (!record.add(deviceId)) || (!record.add(imei)) || (!record.add(cellId)) || (!record.add(iccid))) return false; if (_client.send(record) != SMARTREST_SUCCESS) { aError("Sending data failed when updating device.\r\n"); _client.stop(); return false; } if (_client.receive(received) != SMARTREST_SUCCESS) { aError("Updating device failed.\r\n"); _client.stop(); return false; } _client.stop(); if (received.values() != 3) { aError("Received incomplete data when updating device.\r\n"); return false; } if (received.value(0).integerValue() != 201) { aError("Received invalid data when updating device.\r\n"); return false; } return true; }