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
MbedAgent.cpp
- Committer:
- xinlei
- Date:
- 2015-05-08
- Revision:
- 101:dbcd3bc51758
- Parent:
- 100:47ea098f8a47
- Child:
- 106:c61f0d62b625
File content as of revision 101:dbcd3bc51758:
#include "MbedAgent.h" #include "logging.h" #include "watchdog.h" MbedAgent::MbedAgent(DeviceInfo& deviceInfo): _client(), tpl(), _bootstrap(_client, deviceInfo), _integration(_client, tpl, deviceInfo), lcdThirdLineBlank(true), signal(deviceInfo), temp(), poti(), gps(), acc(), conf(), sock(), pool(), _operationSupport(_client, tpl, pool) { reporters[0] = &signal; reporters[1] = &temp; reporters[2] = &poti; reporters[3] = &gps; reporters[4] = &acc; reporters[5] = &conf; } bool MbedAgent::init() { bool flag = true; // Insert measurement Acceleration // USAGE: 106,<DEVICE/ID>,<X>,<Y>,<Z> if (!tpl.add("10,106,POST,/measurement/measurements,application/vnd.com.nsn.cumulocity.measurement+json,application/vnd.com.nsn.cumulocity.measurement+json,%%,NOW UNSIGNED NUMBER NUMBER NUMBER,\"{\"\"time\"\":\"\"%%\"\",\"\"source\"\":{\"\"id\"\":\"\"%%\"\"},\"\"type\"\":\"\"c8y_MotionMeasurement\"\",\"\"c8y_MotionMeasurement\"\":{\"\"x\"\":{\"\"value\"\":%%,\"\"unit\"\":\"\"m/s^2\"\"},\"\"y\"\":{\"\"value\"\":%%,\"\"unit\"\":\"\"m/s^2\"\"},\"\"z\"\":{\"\"value\"\":%%,\"\"unit\"\":\"\"m/s^2\"\"}}}\"\r\n")) flag = false; // Insert measurement Potentiometer // USAGE: 107,<DEVICE/ID>,<ANALOG1>,<ANALOG2> if (!tpl.add("10,107,POST,/measurement/measurements,application/vnd.com.nsn.cumulocity.measurement+json,application/vnd.com.nsn.cumulocity.measurement+json,%%,NOW UNSIGNED NUMBER NUMBER,\"{\"\"time\"\":\"\"%%\"\",\"\"source\"\":{\"\"id\"\":\"\"%%\"\"},\"\"type\"\":\"\"c8y_AnalogMeasurement\"\",\"\"c8y_AnalogMeasurement\"\":{\"\"A1\"\":{\"\"value\"\":%%,\"\"unit\"\":\"\"A\"\"},\"\"A2\"\":{\"\"value\"\":%%,\"\"unit\"\":\"\"A\"\"}}}\"\r\n")) flag = false; // Update device position // USAGE: 108,<DEVICE/ID>,<ALTITUDE>,<LATITUDE>,<LONGITUDE> if (!tpl.add("10,108,PUT,/inventory/managedObjects/%%,application/vnd.com.nsn.cumulocity.managedObject+json,application/vnd.com.nsn.cumulocity.managedObject+json,%%,UNSIGNED NUMBER NUMBER NUMBER,\"{\"\"c8y_Position\"\":{\"\"alt\"\":%%,\"\"lat\"\":%%,\"\"lng\"\":%%},\"\"c8y_MotionTracking\"\":{\"\"active\"\":true}}\"\r\n")) flag = false; // Insert measurement Location // USAGE: 109,<DEVICE/ID>,<ALTITUDE>,<LATITUDE>,<LONGITUDE> if (!tpl.add("10,109,POST,/event/events,application/vnd.com.nsn.cumulocity.event+json,application/vnd.com.nsn.cumulocity.event+json,%%,NOW UNSIGNED NUMBER NUMBER NUMBER,\"{\"\"time\"\":\"\"%%\"\",\"\"source\"\":{\"\"id\"\":\"\"%%\"\"},\"\"type\"\":\"\"c8y_LocationUpdate\"\",\"\"text\"\":\"\"Mbed location update\"\",\"\"c8y_Position\"\":{\"\"alt\"\":%%,\"\"lat\"\":%%,\"\"lng\"\":%%}}\"\r\n")) flag = false; // Insert measurement Signal Quality // USAGE: 104,<DEVICE/ID>,<RSSI>,<BER> if (!tpl.add("10,104,POST,/measurement/measurements,application/vnd.com.nsn.cumulocity.measurement+json,application/vnd.com.nsn.cumulocity.measurement+json,%%,NOW UNSIGNED NUMBER UNSIGNED,\"{\"\"time\"\":\"\"%%\"\",\"\"source\"\":{\"\"id\"\":\"\"%%\"\"},\"\"type\"\":\"\"c8y_SignalStrength\"\",\"\"c8y_SignalStrength\"\":{\"\"rssi\"\":{\"\"value\"\":%%,\"\"unit\"\":\"\"dBm\"\"},\"\"ber\"\":{\"\"value\"\":%%,\"\"unit\"\":\"\"%\"\"}}}\"\r\n")) flag = false; // Insert measurement Temperature // USAGE: 105,<DEVICE/ID>,<TEMPERATURE> if (!tpl.add("10,105,POST,/measurement/measurements,application/vnd.com.nsn.cumulocity.measurement+json,application/vnd.com.nsn.cumulocity.measurement+json,%%,NOW UNSIGNED NUMBER,\"{\"\"time\"\":\"\"%%\"\",\"\"source\"\":{\"\"id\"\":\"\"%%\"\"},\"\"type\"\":\"\"c8y_TemperatureMeasurement\"\",\"\"c8y_TemperatureMeasurement\"\":{\"\"T\"\":{\"\"value\"\":%%,\"\"unit\"\":\"\"C\"\"}}}\"\r\n")) flag = false; // Update Configuration // Usage: 130,<DEVICE/ID>,<CONFIG/STRING>,<RESPONSIBILITY> if (!tpl.add("10,130,PUT,/inventory/managedObjects/%%,application/vnd.com.nsn.cumulocity.managedObject+json,application/vnd.com.nsn.cumulocity.managedObject+json,%%,UNSIGNED STRING NUMBER,\"{\"\"c8y_Configuration\"\":{\"\"config\"\":\"\"%%\"\"},\"\"c8y_RequiredAvailability\"\":{ \"\"responseInterval\"\":%%}}\"\r\n")) flag = false; if (!_integration.init()) { LCDDisplay::inst().setLines("Integrate init fail"); flag = false; } if (!_operationSupport.init()) { LCDDisplay::inst().setLines("Operation init fail"); flag = false; } for (size_t i = 0; i < N; ++i) { if (!reporters[i]->init()) { aError("Init %s", reporters[i]->name()); } } return flag; } int MbedAgent::run() { // device bootstrapping process if (!_bootstrap.setUpCredentials()) return -1; setAuth(_bootstrap.username(), _bootstrap.password()); aInfo("Set auth: %s:%s (%s)\n", srUsername, srPassword, srAuthStr); Thread::wait(2000); LCDDisplay::inst().setLines("Connect to Cloud", srHost); if (!_integration.integrate()) { return -2; } setX_ID(_client.getIdentifier()); aInfo("Set X-ID: %s\n", srX_ID); return 0; } void MbedAgent::loop() { ReportThread reportThread(pool); _operationSupport.run(); PollThread pollThread(pool, conf); pollThread.setChannel(deviceID); Watchdog wdt; wdt.kick(60.0); // set a 60.0 seconds watchdog while (true) { for (size_t i = 0; i < N; ++i) { // if (reporters[i] == &conf) { size_t l = reporters[i]->read(buf2, SMARRESTBODY_SIZE, status, DISPLAY_LEN); bool b = l; if (b) { // Refresh LCD display needed LCDDisplay::inst().setThirdLine(status); } else if (!lcdThirdLineBlank && !b) { // Clear LCD display needed LCDDisplay::inst().setThirdLine(""); } lcdThirdLineBlank = !b; if (b) { int l2 = snprintf(buf, SMARTREST_SIZE, fmtSmartRest, "/s", l, buf2); l2 = sock.sendOnly(buf, l2); if (l2 < 0) aError(status); } // } } wdt.kick(); // reset watchdog timer } }