Own fork of MbedSmartRestMain
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
MbedAgent.cpp@90:423177e8a401, 2015-03-20 (annotated)
- Committer:
- xinlei
- Date:
- Fri Mar 20 13:55:21 2015 +0000
- Revision:
- 90:423177e8a401
- Parent:
- 89:0525121f307e
- Child:
- 92:0acd11870c6a
Code refactoring to ease coupling
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" |
xinlei | 72:c5709ae7b193 | 3 | #include "logging.h" |
xinlei | 76:b07effe83fb8 | 4 | #include "watchdog.h" |
Cumulocity | 41:804f6a0bda26 | 5 | |
xinlei | 83:3c8ceb12b773 | 6 | bool lastSensorReadingSent=true; |
xinlei | 83:3c8ceb12b773 | 7 | |
vwochnik | 67:c360a2b2c948 | 8 | MbedAgent::MbedAgent(DeviceIO& io, MDMSerial& mdm, DeviceInfo& deviceInfo, DeviceMemory& deviceMemory) : |
Cumulocity | 42:104746744af8 | 9 | _io(io), |
Cumulocity | 41:804f6a0bda26 | 10 | _mdm(mdm), |
Cumulocity | 41:804f6a0bda26 | 11 | _deviceInfo(deviceInfo), |
vwochnik | 67:c360a2b2c948 | 12 | _deviceMemory(deviceMemory), |
vwochnik | 68:0dc778a16d0d | 13 | _configurationProperties(_deviceConfiguration), |
xinlei | 77:f6717e4eccc4 | 14 | _client(MBED_AGENT_HOST, MBED_AGENT_PORT, MBED_AGENT_DEVICE_IDENTIFIER, mdm), |
vwochnik | 67:c360a2b2c948 | 15 | _bootstrap(_client, _io, _deviceInfo, _deviceMemory), |
Cumulocity | 41:804f6a0bda26 | 16 | _integration(_client, _tpl, _deviceId, _deviceInfo), |
xinlei | 82:ca7430f50b2b | 17 | _displayInfo(), |
vwochnik | 68:0dc778a16d0d | 18 | _configurationSynchronization(_client, _tpl, _deviceId, _deviceMemory, _deviceConfiguration, _configurationProperties), |
xinlei | 82:ca7430f50b2b | 19 | _signalQualityMeasurement(_client, _tpl, _deviceId, _deviceInfo, io, _displayInfo), |
xinlei | 90:423177e8a401 | 20 | _temperatureMeasurement(_client, _tpl, _deviceId, io, deviceInfo, _displayInfo), |
xinlei | 90:423177e8a401 | 21 | _accelerationMeasurement(_client, _tpl, _deviceId, io, deviceInfo, _displayInfo), |
xinlei | 90:423177e8a401 | 22 | _analogMeasurement(_client, _tpl, _deviceId, io, deviceInfo, _displayInfo), |
xinlei | 82:ca7430f50b2b | 23 | _locationUpdate(_client, _tpl, _deviceId, _io.gpsTracker(), io, deviceInfo, _displayInfo), |
xinlei | 82:ca7430f50b2b | 24 | _operationSupport(_client, _tpl, _deviceId, _configurationSynchronization, _io, _displayInfo), |
Cumulocity | 41:804f6a0bda26 | 25 | _deviceId(0) |
Cumulocity | 41:804f6a0bda26 | 26 | { |
Cumulocity | 41:804f6a0bda26 | 27 | } |
Cumulocity | 41:804f6a0bda26 | 28 | |
Cumulocity | 41:804f6a0bda26 | 29 | bool MbedAgent::init() |
Cumulocity | 41:804f6a0bda26 | 30 | { |
xinlei | 71:063c45e99578 | 31 | bool flag = true; |
xinlei | 71:063c45e99578 | 32 | if (!_integration.init()) { |
xinlei | 76:b07effe83fb8 | 33 | aError("Init Device Integration failed.\r\n"); |
xinlei | 71:063c45e99578 | 34 | flag = false; |
xinlei | 71:063c45e99578 | 35 | } |
xinlei | 71:063c45e99578 | 36 | if (!_configurationSynchronization.init()) { |
xinlei | 76:b07effe83fb8 | 37 | aError("Init Configuration Synchronization failed.\r\n"); |
xinlei | 71:063c45e99578 | 38 | flag = false; |
xinlei | 71:063c45e99578 | 39 | } |
xinlei | 71:063c45e99578 | 40 | if (!_signalQualityMeasurement.init()) { |
xinlei | 76:b07effe83fb8 | 41 | aError("Init Signal Quality Measurement failed.\r\n"); |
xinlei | 71:063c45e99578 | 42 | flag = false; |
xinlei | 71:063c45e99578 | 43 | } |
xinlei | 71:063c45e99578 | 44 | if (!_temperatureMeasurement.init()) { |
xinlei | 76:b07effe83fb8 | 45 | aError("Init Temperature Measurement failed.\r\n"); |
xinlei | 71:063c45e99578 | 46 | flag = false; |
Cumulocity | 41:804f6a0bda26 | 47 | } |
xinlei | 71:063c45e99578 | 48 | if (!_accelerationMeasurement.init()) { |
xinlei | 76:b07effe83fb8 | 49 | aError("Init Acceleration Measurement failed.\r\n"); |
xinlei | 71:063c45e99578 | 50 | flag = false; |
xinlei | 71:063c45e99578 | 51 | } |
xinlei | 71:063c45e99578 | 52 | if (!_analogMeasurement.init()) { |
xinlei | 76:b07effe83fb8 | 53 | aError("Init Analog Measurement failed.\r\n"); |
xinlei | 71:063c45e99578 | 54 | flag = false; |
xinlei | 71:063c45e99578 | 55 | } |
xinlei | 71:063c45e99578 | 56 | if (!_locationUpdate.init()) { |
xinlei | 76:b07effe83fb8 | 57 | aError("Init Location Measurement failed.\r\n"); |
xinlei | 71:063c45e99578 | 58 | flag = false; |
xinlei | 71:063c45e99578 | 59 | } |
xinlei | 71:063c45e99578 | 60 | if (!_operationSupport.init()) { |
xinlei | 76:b07effe83fb8 | 61 | aError("Init Operation Support failed.\r\n"); |
xinlei | 71:063c45e99578 | 62 | flag = false; |
xinlei | 71:063c45e99578 | 63 | } |
xinlei | 71:063c45e99578 | 64 | return flag; |
Cumulocity | 41:804f6a0bda26 | 65 | } |
Cumulocity | 41:804f6a0bda26 | 66 | |
Cumulocity | 41:804f6a0bda26 | 67 | bool MbedAgent::run() |
Cumulocity | 41:804f6a0bda26 | 68 | { |
Cumulocity | 41:804f6a0bda26 | 69 | // device bootstrapping process |
vwochnik | 52:8f1370084268 | 70 | if (!_bootstrap.setUpCredentials()) |
Cumulocity | 41:804f6a0bda26 | 71 | return false; |
vwochnik | 52:8f1370084268 | 72 | |
Cumulocity | 46:f6976fd64387 | 73 | Thread::wait(5000); |
Cumulocity | 41:804f6a0bda26 | 74 | |
xinlei | 89:0525121f307e | 75 | _io.lcdPrint("Connect to Cloud", MBED_AGENT_HOST); |
Cumulocity | 41:804f6a0bda26 | 76 | if (!_integration.integrate()) { |
Cumulocity | 41:804f6a0bda26 | 77 | return false; |
Cumulocity | 41:804f6a0bda26 | 78 | } |
Cumulocity | 41:804f6a0bda26 | 79 | |
vwochnik | 68:0dc778a16d0d | 80 | if (!_configurationSynchronization.integrate()) { |
vwochnik | 68:0dc778a16d0d | 81 | return false; |
vwochnik | 68:0dc778a16d0d | 82 | } |
vwochnik | 68:0dc778a16d0d | 83 | |
Cumulocity | 41:804f6a0bda26 | 84 | char status[60]; |
Cumulocity | 41:804f6a0bda26 | 85 | snprintf(status, sizeof(status), "ID: %ld", _deviceId); |
xinlei | 71:063c45e99578 | 86 | _io.lcdPrint("Connected", status); |
Cumulocity | 41:804f6a0bda26 | 87 | |
xinlei | 82:ca7430f50b2b | 88 | char tenant[25]; |
xinlei | 82:ca7430f50b2b | 89 | const char* user= _bootstrap.username(); |
xinlei | 82:ca7430f50b2b | 90 | int len = strchr(user, '/')-user+1+8; // 8: length of "Tenant: " |
xinlei | 82:ca7430f50b2b | 91 | len = len <= 25 ? len : 25; |
xinlei | 82:ca7430f50b2b | 92 | snprintf(tenant, len, "Tenant: %s", user); |
xinlei | 82:ca7430f50b2b | 93 | _displayInfo.setFirstLine(tenant); |
Cumulocity | 41:804f6a0bda26 | 94 | loop(); |
vwochnik | 68:0dc778a16d0d | 95 | |
Cumulocity | 41:804f6a0bda26 | 96 | return true; |
Cumulocity | 41:804f6a0bda26 | 97 | } |
Cumulocity | 41:804f6a0bda26 | 98 | |
Cumulocity | 41:804f6a0bda26 | 99 | void MbedAgent::loop() |
Cumulocity | 41:804f6a0bda26 | 100 | { |
xinlei | 76:b07effe83fb8 | 101 | Watchdog wdt; |
xinlei | 77:f6717e4eccc4 | 102 | DigitalIn joystickUp(A2); |
xinlei | 77:f6717e4eccc4 | 103 | DigitalIn joystickDown(A3); |
xinlei | 77:f6717e4eccc4 | 104 | // DigitalIn joystickLeft(A4); |
xinlei | 77:f6717e4eccc4 | 105 | // AnalogIn joystickRight(A5); |
xinlei | 77:f6717e4eccc4 | 106 | |
xinlei | 76:b07effe83fb8 | 107 | wdt.kick(60.0); // set a 60.0 seconds timeout on watchdog hardware timer |
Cumulocity | 41:804f6a0bda26 | 108 | while (true) { |
xinlei | 77:f6717e4eccc4 | 109 | if (joystickDown) { |
xinlei | 81:4a7761914901 | 110 | setLevel(A_NONE); |
xinlei | 81:4a7761914901 | 111 | _mdm.setDebug(-1); |
xinlei | 77:f6717e4eccc4 | 112 | printf("***Disabled debug mode.***\r\n"); |
xinlei | 77:f6717e4eccc4 | 113 | } else if (joystickUp) { |
xinlei | 77:f6717e4eccc4 | 114 | setLevel(A_DEBUG); |
xinlei | 77:f6717e4eccc4 | 115 | _mdm.setDebug(3); |
xinlei | 77:f6717e4eccc4 | 116 | printf("***Enabled debug mode.***\r\n"); |
xinlei | 77:f6717e4eccc4 | 117 | } |
xinlei | 83:3c8ceb12b773 | 118 | |
xinlei | 78:bf32dfe6c47f | 119 | _configurationSynchronization.run(); |
xinlei | 78:bf32dfe6c47f | 120 | _signalQualityMeasurement.run(); |
xinlei | 78:bf32dfe6c47f | 121 | _temperatureMeasurement.run(); |
xinlei | 78:bf32dfe6c47f | 122 | _analogMeasurement.run(); |
xinlei | 78:bf32dfe6c47f | 123 | _accelerationMeasurement.run(); |
xinlei | 78:bf32dfe6c47f | 124 | _locationUpdate.run(); |
vwochnik | 57:4af5f1bec3a6 | 125 | _operationSupport.run(); |
xinlei | 90:423177e8a401 | 126 | |
xinlei | 76:b07effe83fb8 | 127 | // if ((interval = _configurationProperties.readInterval()) < 0) |
xinlei | 76:b07effe83fb8 | 128 | // break; |
xinlei | 76:b07effe83fb8 | 129 | // |
xinlei | 76:b07effe83fb8 | 130 | // while (timer.read() < interval) { |
xinlei | 76:b07effe83fb8 | 131 | // Thread::yield(); |
xinlei | 76:b07effe83fb8 | 132 | // } |
xinlei | 76:b07effe83fb8 | 133 | wdt.kick(); // reset watchdog timer |
Cumulocity | 41:804f6a0bda26 | 134 | } |
Cumulocity | 41:804f6a0bda26 | 135 | } |