Own fork of MbedSmartRestMain

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Cumulocity Official

Committer:
vwochnik
Date:
Sun Nov 30 19:34:49 2014 +0000
Revision:
67:c360a2b2c948
Parent:
65:a62dbef2f924
Child:
68:0dc778a16d0d
refactor credentials persistence, add factory reset upon fire press button

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"
Cumulocity 41:804f6a0bda26 3
vwochnik 67:c360a2b2c948 4 MbedAgent::MbedAgent(DeviceIO& io, MDMSerial& mdm, DeviceInfo& deviceInfo, DeviceMemory& deviceMemory) :
Cumulocity 42:104746744af8 5 _io(io),
Cumulocity 41:804f6a0bda26 6 _mdm(mdm),
Cumulocity 41:804f6a0bda26 7 _deviceInfo(deviceInfo),
vwochnik 67:c360a2b2c948 8 _deviceMemory(deviceMemory),
Cumulocity 41:804f6a0bda26 9 _client(MBED_AGENT_HOST, MBED_AGENT_PORT, MBED_AGENT_DEVICE_IDENTIFIER),
vwochnik 67:c360a2b2c948 10 _bootstrap(_client, _io, _deviceInfo, _deviceMemory),
Cumulocity 41:804f6a0bda26 11 _integration(_client, _tpl, _deviceId, _deviceInfo),
Cumulocity 41:804f6a0bda26 12 _signalQualityMeasurement(_client, _tpl, _deviceId, _deviceInfo),
Cumulocity 41:804f6a0bda26 13 _temperatureMeasurement(_client, _tpl, _deviceId, _io.temperatureSensor()),
Cumulocity 41:804f6a0bda26 14 _accelerationMeasurement(_client, _tpl, _deviceId, _io.accelerometer()),
Cumulocity 47:89ae46d5c466 15 _analogMeasurement(_client, _tpl, _deviceId, _io.analog1(), _io.analog2()),
Cumulocity 47:89ae46d5c466 16 _locationUpdate(_client, _tpl, _deviceId, _io.gpsTracker()),
vwochnik 65:a62dbef2f924 17 _operationSupport(_client, _tpl, _deviceId, _io),
Cumulocity 41:804f6a0bda26 18 _deviceId(0)
Cumulocity 41:804f6a0bda26 19 {
Cumulocity 41:804f6a0bda26 20 }
Cumulocity 41:804f6a0bda26 21
Cumulocity 41:804f6a0bda26 22 bool MbedAgent::init()
Cumulocity 41:804f6a0bda26 23 {
Cumulocity 41:804f6a0bda26 24 if ((!_integration.init()) ||
Cumulocity 41:804f6a0bda26 25 (!_signalQualityMeasurement.init()) ||
Cumulocity 41:804f6a0bda26 26 (!_temperatureMeasurement.init()) ||
Cumulocity 47:89ae46d5c466 27 (!_accelerationMeasurement.init()) ||
Cumulocity 47:89ae46d5c466 28 (!_analogMeasurement.init()) ||
vwochnik 57:4af5f1bec3a6 29 (!_locationUpdate.init()) ||
vwochnik 57:4af5f1bec3a6 30 (!_operationSupport.init())) {
Cumulocity 41:804f6a0bda26 31 puts("Initialization failed.");
Cumulocity 41:804f6a0bda26 32 return false;
Cumulocity 41:804f6a0bda26 33 }
Cumulocity 41:804f6a0bda26 34 return true;
Cumulocity 41:804f6a0bda26 35 }
Cumulocity 41:804f6a0bda26 36
Cumulocity 41:804f6a0bda26 37 bool MbedAgent::run()
Cumulocity 41:804f6a0bda26 38 {
Cumulocity 41:804f6a0bda26 39 // device bootstrapping process
vwochnik 52:8f1370084268 40 if (!_bootstrap.setUpCredentials())
Cumulocity 41:804f6a0bda26 41 return false;
vwochnik 52:8f1370084268 42
Cumulocity 46:f6976fd64387 43 Thread::wait(5000);
Cumulocity 41:804f6a0bda26 44
Cumulocity 41:804f6a0bda26 45 _io.lcdPrint("INTEGRATION");
Cumulocity 41:804f6a0bda26 46 if (!_integration.integrate()) {
Cumulocity 41:804f6a0bda26 47 return false;
Cumulocity 41:804f6a0bda26 48 }
Cumulocity 41:804f6a0bda26 49
Cumulocity 41:804f6a0bda26 50 char status[60];
Cumulocity 41:804f6a0bda26 51 snprintf(status, sizeof(status), "ID: %ld", _deviceId);
Cumulocity 41:804f6a0bda26 52 _io.lcdPrint("INTEGRATED", status);
Cumulocity 41:804f6a0bda26 53
Cumulocity 41:804f6a0bda26 54 loop();
Cumulocity 41:804f6a0bda26 55 return true;
Cumulocity 41:804f6a0bda26 56 }
Cumulocity 41:804f6a0bda26 57
Cumulocity 41:804f6a0bda26 58 void MbedAgent::loop()
Cumulocity 41:804f6a0bda26 59 {
Cumulocity 41:804f6a0bda26 60 Timer timer;
Cumulocity 41:804f6a0bda26 61
Cumulocity 41:804f6a0bda26 62 timer.start();
Cumulocity 41:804f6a0bda26 63 while (true) {
Cumulocity 41:804f6a0bda26 64 timer.reset();
Cumulocity 41:804f6a0bda26 65
vwochnik 65:a62dbef2f924 66 _signalQualityMeasurement.run();
Cumulocity 41:804f6a0bda26 67 _temperatureMeasurement.run();
Cumulocity 41:804f6a0bda26 68 _accelerationMeasurement.run();
Cumulocity 47:89ae46d5c466 69 _analogMeasurement.run();
vwochnik 65:a62dbef2f924 70 _locationUpdate.run();
vwochnik 57:4af5f1bec3a6 71 _operationSupport.run();
Cumulocity 41:804f6a0bda26 72
vwochnik 65:a62dbef2f924 73 while (timer.read() < MBED_AGENT_INTERVAL) {
Cumulocity 41:804f6a0bda26 74 Thread::yield();
Cumulocity 41:804f6a0bda26 75 }
Cumulocity 41:804f6a0bda26 76 }
Cumulocity 41:804f6a0bda26 77 }