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