![](/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
MbedAgent.cpp@41:804f6a0bda26, 2014-07-15 (annotated)
- Committer:
- Cumulocity
- Date:
- Tue Jul 15 12:44:34 2014 +0000
- Revision:
- 41:804f6a0bda26
- Child:
- 42:104746744af8
refactoring of mbed agent
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Cumulocity | 41:804f6a0bda26 | 1 | #include "MbedAgent.h" |
Cumulocity | 41:804f6a0bda26 | 2 | #include "rtos.h" |
Cumulocity | 41:804f6a0bda26 | 3 | |
Cumulocity | 41:804f6a0bda26 | 4 | MbedAgent::MbedAgent(MDMSerial& mdm, DeviceInfo& deviceInfo) : |
Cumulocity | 41:804f6a0bda26 | 5 | _mdm(mdm), |
Cumulocity | 41:804f6a0bda26 | 6 | _deviceInfo(deviceInfo), |
Cumulocity | 41:804f6a0bda26 | 7 | _client(MBED_AGENT_HOST, MBED_AGENT_PORT, MBED_AGENT_DEVICE_IDENTIFIER), |
Cumulocity | 41:804f6a0bda26 | 8 | _bootstrap(_client, _mdm, _deviceInfo), |
Cumulocity | 41:804f6a0bda26 | 9 | _integration(_client, _tpl, _deviceId, _deviceInfo), |
Cumulocity | 41:804f6a0bda26 | 10 | _signalQualityMeasurement(_client, _tpl, _deviceId, _deviceInfo), |
Cumulocity | 41:804f6a0bda26 | 11 | _temperatureMeasurement(_client, _tpl, _deviceId, _io.temperatureSensor()), |
Cumulocity | 41:804f6a0bda26 | 12 | _accelerationMeasurement(_client, _tpl, _deviceId, _io.accelerometer()), |
Cumulocity | 41:804f6a0bda26 | 13 | _deviceId(0) |
Cumulocity | 41:804f6a0bda26 | 14 | { |
Cumulocity | 41:804f6a0bda26 | 15 | } |
Cumulocity | 41:804f6a0bda26 | 16 | |
Cumulocity | 41:804f6a0bda26 | 17 | bool MbedAgent::init() |
Cumulocity | 41:804f6a0bda26 | 18 | { |
Cumulocity | 41:804f6a0bda26 | 19 | if ((!_integration.init()) || |
Cumulocity | 41:804f6a0bda26 | 20 | (!_signalQualityMeasurement.init()) || |
Cumulocity | 41:804f6a0bda26 | 21 | (!_temperatureMeasurement.init()) || |
Cumulocity | 41:804f6a0bda26 | 22 | (!_accelerationMeasurement.init())) { |
Cumulocity | 41:804f6a0bda26 | 23 | puts("Initialization failed."); |
Cumulocity | 41:804f6a0bda26 | 24 | return false; |
Cumulocity | 41:804f6a0bda26 | 25 | } |
Cumulocity | 41:804f6a0bda26 | 26 | return true; |
Cumulocity | 41:804f6a0bda26 | 27 | } |
Cumulocity | 41:804f6a0bda26 | 28 | |
Cumulocity | 41:804f6a0bda26 | 29 | bool MbedAgent::run() |
Cumulocity | 41:804f6a0bda26 | 30 | { |
Cumulocity | 41:804f6a0bda26 | 31 | // device bootstrapping process |
Cumulocity | 41:804f6a0bda26 | 32 | _io.lcdPrint("BOOTSTRAP", _deviceInfo.imei()); |
Cumulocity | 41:804f6a0bda26 | 33 | if (!_bootstrap.setUpCredentials()) { |
Cumulocity | 41:804f6a0bda26 | 34 | puts("Could not obtain credentials."); |
Cumulocity | 41:804f6a0bda26 | 35 | return false; |
Cumulocity | 41:804f6a0bda26 | 36 | } |
Cumulocity | 41:804f6a0bda26 | 37 | |
Cumulocity | 41:804f6a0bda26 | 38 | _io.lcdPrint("INTEGRATION"); |
Cumulocity | 41:804f6a0bda26 | 39 | if (!_integration.integrate()) { |
Cumulocity | 41:804f6a0bda26 | 40 | puts("Device integration process failed."); |
Cumulocity | 41:804f6a0bda26 | 41 | return false; |
Cumulocity | 41:804f6a0bda26 | 42 | } |
Cumulocity | 41:804f6a0bda26 | 43 | |
Cumulocity | 41:804f6a0bda26 | 44 | char status[60]; |
Cumulocity | 41:804f6a0bda26 | 45 | snprintf(status, sizeof(status), "ID: %ld", _deviceId); |
Cumulocity | 41:804f6a0bda26 | 46 | _io.lcdPrint("INTEGRATED", status); |
Cumulocity | 41:804f6a0bda26 | 47 | puts(status); |
Cumulocity | 41:804f6a0bda26 | 48 | |
Cumulocity | 41:804f6a0bda26 | 49 | loop(); |
Cumulocity | 41:804f6a0bda26 | 50 | return true; |
Cumulocity | 41:804f6a0bda26 | 51 | } |
Cumulocity | 41:804f6a0bda26 | 52 | |
Cumulocity | 41:804f6a0bda26 | 53 | void MbedAgent::loop() |
Cumulocity | 41:804f6a0bda26 | 54 | { |
Cumulocity | 41:804f6a0bda26 | 55 | Timer timer; |
Cumulocity | 41:804f6a0bda26 | 56 | |
Cumulocity | 41:804f6a0bda26 | 57 | timer.start(); |
Cumulocity | 41:804f6a0bda26 | 58 | while (true) { |
Cumulocity | 41:804f6a0bda26 | 59 | timer.reset(); |
Cumulocity | 41:804f6a0bda26 | 60 | |
Cumulocity | 41:804f6a0bda26 | 61 | _signalQualityMeasurement.run(); |
Cumulocity | 41:804f6a0bda26 | 62 | _temperatureMeasurement.run(); |
Cumulocity | 41:804f6a0bda26 | 63 | _accelerationMeasurement.run(); |
Cumulocity | 41:804f6a0bda26 | 64 | |
Cumulocity | 41:804f6a0bda26 | 65 | while (timer.read() < MBED_AGENT_INTERVAL) { |
Cumulocity | 41:804f6a0bda26 | 66 | Thread::yield(); |
Cumulocity | 41:804f6a0bda26 | 67 | } |
Cumulocity | 41:804f6a0bda26 | 68 | } |
Cumulocity | 41:804f6a0bda26 | 69 | } |