![](/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
Diff: MbedAgent.cpp
- Revision:
- 100:dbcd3bc51758
- Parent:
- 99:47ea098f8a47
- Child:
- 105:c61f0d62b625
--- a/MbedAgent.cpp Thu May 07 13:56:19 2015 +0000 +++ b/MbedAgent.cpp Fri May 08 12:19:57 2015 +0000 @@ -1,13 +1,12 @@ #include "MbedAgent.h" -#include "LCDDisplay.h" #include "logging.h" #include "watchdog.h" MbedAgent::MbedAgent(DeviceInfo& deviceInfo): _client(), tpl(), _bootstrap(_client, deviceInfo), - _integration(_client, _tpl, deviceInfo), lcdThirdLineBlank(true), - signal(), temp(), poti(), gps(), acc(), conf(), sock(), - pool(), _operationSupport(_client, _tpl, pool) + _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; @@ -20,6 +19,42 @@ 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; @@ -28,7 +63,12 @@ LCDDisplay::inst().setLines("Operation init fail"); flag = false; } - return flag; + for (size_t i = 0; i < N; ++i) { + if (!reporters[i]->init()) { + aError("Init %s", reporters[i]->name()); + } + } + return flag; } int MbedAgent::run() @@ -46,31 +86,39 @@ return -2; } setX_ID(_client.getIdentifier()); - aInfo("Set id: %s\n", srX_ID); + aInfo("Set X-ID: %s\n", srX_ID); - if (!_configSync.integrate()) { - return -3; - } return 0; } void MbedAgent::loop() { -// ReportThread reportThread(pool); + ReportThread reportThread(pool); _operationSupport.run(); -// PollThread pollThread(pool, _configSync); -// pollThread.setChannel(_deviceId); + PollThread pollThread(pool, conf); + pollThread.setChannel(deviceID); Watchdog wdt; - wdt.kick(60.0); // set a 60.0 seconds timeout on watchdog hardware timer + wdt.kick(60.0); // set a 60.0 seconds watchdog while (true) { - _configSync.run(); -// _analogMeasurement.run(); -// _signalQualityMeasurement.run(); -// _temperatureMeasurement.run(); -// _accelerationMeasurement.run(); -// _locationUpdate.run(); -// Thread::wait(30000); + 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 } } \ No newline at end of file