Own fork of MbedSmartRestMain

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Cumulocity Official

Committer:
xinlei
Date:
Fri Mar 06 11:10:48 2015 +0000
Revision:
83:3c8ceb12b773
Parent:
82:ca7430f50b2b
Child:
85:b49c4cfecc43
DisplayInfo: added second line for storing signal quality.

Who changed what in which revision?

UserRevisionLine numberNew 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 82:ca7430f50b2b 20 _temperatureMeasurement(_client, _tpl, _deviceId, _io.temperatureSensor(), io, deviceInfo, _displayInfo),
xinlei 82:ca7430f50b2b 21 _accelerationMeasurement(_client, _tpl, _deviceId, _io.accelerometer(), io, deviceInfo, _displayInfo),
xinlei 82:ca7430f50b2b 22 _analogMeasurement(_client, _tpl, _deviceId, _io.analog1(), _io.analog2(), 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 71:063c45e99578 75 _io.lcdPrint("Connect to Cloud");
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 83:3c8ceb12b773 102 char signal[25] = {0};
xinlei 77:f6717e4eccc4 103 DigitalIn joystickUp(A2);
xinlei 77:f6717e4eccc4 104 DigitalIn joystickDown(A3);
xinlei 77:f6717e4eccc4 105 // DigitalIn joystickLeft(A4);
xinlei 77:f6717e4eccc4 106 // AnalogIn joystickRight(A5);
xinlei 77:f6717e4eccc4 107
xinlei 76:b07effe83fb8 108 wdt.kick(60.0); // set a 60.0 seconds timeout on watchdog hardware timer
Cumulocity 41:804f6a0bda26 109 while (true) {
xinlei 77:f6717e4eccc4 110 if (joystickDown) {
xinlei 81:4a7761914901 111 setLevel(A_NONE);
xinlei 81:4a7761914901 112 _mdm.setDebug(-1);
xinlei 77:f6717e4eccc4 113 printf("***Disabled debug mode.***\r\n");
xinlei 77:f6717e4eccc4 114 } else if (joystickUp) {
xinlei 77:f6717e4eccc4 115 setLevel(A_DEBUG);
xinlei 77:f6717e4eccc4 116 _mdm.setDebug(3);
xinlei 77:f6717e4eccc4 117 printf("***Enabled debug mode.***\r\n");
xinlei 77:f6717e4eccc4 118 }
xinlei 83:3c8ceb12b773 119 DeviceInfo::SignalQuality *p = _deviceInfo.signalQuality(false);
xinlei 83:3c8ceb12b773 120 if (p && p->rssi)
xinlei 83:3c8ceb12b773 121 snprintf(signal, sizeof(signal), "Network: %d dBm", p->rssi);
xinlei 83:3c8ceb12b773 122 else
xinlei 83:3c8ceb12b773 123 strncpy(signal, "Network: no coverage", sizeof(signal));
xinlei 83:3c8ceb12b773 124 _displayInfo.setSecondLine(signal);
xinlei 83:3c8ceb12b773 125
xinlei 78:bf32dfe6c47f 126 _configurationSynchronization.run();
xinlei 78:bf32dfe6c47f 127 _signalQualityMeasurement.run();
xinlei 78:bf32dfe6c47f 128 _temperatureMeasurement.run();
xinlei 78:bf32dfe6c47f 129 _analogMeasurement.run();
xinlei 78:bf32dfe6c47f 130 _accelerationMeasurement.run();
xinlei 78:bf32dfe6c47f 131 _locationUpdate.run();
vwochnik 57:4af5f1bec3a6 132 _operationSupport.run();
xinlei 77:f6717e4eccc4 133
xinlei 76:b07effe83fb8 134 // if ((interval = _configurationProperties.readInterval()) < 0)
xinlei 76:b07effe83fb8 135 // break;
xinlei 76:b07effe83fb8 136 //
xinlei 76:b07effe83fb8 137 // while (timer.read() < interval) {
xinlei 76:b07effe83fb8 138 // Thread::yield();
xinlei 76:b07effe83fb8 139 // }
xinlei 76:b07effe83fb8 140 wdt.kick(); // reset watchdog timer
Cumulocity 41:804f6a0bda26 141 }
Cumulocity 41:804f6a0bda26 142 }