Own fork of MbedSmartRestMain

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Cumulocity Official

MbedAgent.cpp

Committer:
xinlei
Date:
2015-04-13
Revision:
92:0acd11870c6a
Parent:
90:423177e8a401
Child:
93:61d44636f020

File content as of revision 92:0acd11870c6a:

#include "MbedAgent.h"
#include "rtos.h"
#include "logging.h"
#include "watchdog.h"

MbedAgent::MbedAgent(GPSI2C& gps, MDMSerial& mdm, LCDDisplay& lcdDisplay,
    DeviceInfo& deviceInfo, DeviceMemory& deviceMemory) :
    _mdm(mdm),
    _deviceInfo(deviceInfo),
    _deviceMemory(deviceMemory),
    _configurationProperties(_deviceConfiguration),
    _client(mdm),
    _bootstrap(_client, lcdDisplay, _deviceInfo, _deviceMemory),
    _integration(_client, _tpl, _deviceId, _deviceInfo),
    _lcdDisplay(lcdDisplay),
    _configurationSynchronization(_client, _tpl, _deviceId, _deviceMemory, _deviceConfiguration, _configurationProperties),
    _signalQualityMeasurement(_client, _tpl, _deviceId, _deviceInfo, _lcdDisplay),
    _temperatureMeasurement(_client, _tpl, _deviceId, deviceInfo, _lcdDisplay),
    _accelerationMeasurement(_client, _tpl, _deviceId, deviceInfo, _lcdDisplay),
    _analogMeasurement(_client, _tpl, _deviceId, deviceInfo, _lcdDisplay),
    _locationUpdate(_client, _tpl, _deviceId, gps, deviceInfo, _lcdDisplay),
    _operationSupport(_client, _tpl, _deviceId, _configurationSynchronization, _lcdDisplay),
    _deviceId(0)
{
}

bool MbedAgent::init()
{
    bool flag = true;
    if (!_integration.init()) {
        _lcdDisplay.setLines("Integration init failure");
        flag = false;
    }
    if (!_configurationSynchronization.init()) {
        _lcdDisplay.setLines("Config Sync init failure");
        flag = false;
    }
    if (!_signalQualityMeasurement.init()) {
        _lcdDisplay.setLines("Signal init failure");
        flag = false;
    }
    if (!_temperatureMeasurement.init()) {
        _lcdDisplay.setLines("Temperature init failure");
        flag = false;
    }
    if (!_accelerationMeasurement.init()) {
        _lcdDisplay.setLines("Acceleration init failure");
        flag = false;
    }
    if (!_analogMeasurement.init()) {
        _lcdDisplay.setLines("Analog init failure");
        flag = false;
    }
    if (!_locationUpdate.init()) {
        _lcdDisplay.setLines("Location init failure");
        flag = false;
    }
    if (!_operationSupport.init()) {
        _lcdDisplay.setLines("Operation init failure");
        flag = false;
    }
    return flag;
}

int MbedAgent::run()
{
    // device bootstrapping process
    if (!_bootstrap.setUpCredentials())
        return -1;

    Thread::wait(5000);

    _lcdDisplay.setLines("Connect to Cloud", getHost());
    if (!_integration.integrate()) {
        return -2;
    }
    
    if (!_configurationSynchronization.integrate()) {
        return -3;
    }
    
    char status[27];
    snprintf(status, sizeof(status), "ID: %ld", _deviceId);
    _lcdDisplay.setLines("Connected", status);

    const char* user= getUsername();
    int len = strchr(user, '/')-user+sizeof("Tenant: ");
    len = len <= 27 ? len : 27;
    snprintf(status, len, "Tenant: %s", user);
    _lcdDisplay.setFirstLine(status);
    return 0;
}

void MbedAgent::loop()
{
    Watchdog wdt;
    wdt.kick(60.0);    // set a 60.0 seconds timeout on watchdog hardware timer

    while (true) {
        _operationSupport.run();
        _configurationSynchronization.run();
        _signalQualityMeasurement.run();
        _temperatureMeasurement.run();
        _accelerationMeasurement.run();
        _analogMeasurement.run();
        _locationUpdate.run();

//        if ((interval = _configurationProperties.readInterval()) < 0)
//            break;
//        while (timer.read() < interval) {
//            Thread::yield();
//        }
        wdt.kick();    // reset watchdog timer
    }
}