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-27
Revision:
97:9f2de96941c4
Parent:
96:ea056f6be2e8
Child:
98:e369fc75c000

File content as of revision 97:9f2de96941c4:

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

MbedAgent::MbedAgent(GPSI2C& gps, DeviceInfo& deviceInfo):
    _deviceId(0),
    _client(),
    _bootstrap(_client, deviceInfo),
    _integration(_client, _tpl, _deviceId, deviceInfo),
    _configurationSynchronization(_client, _tpl, _deviceId),
    _signalQualityMeasurement(_client, _tpl, _deviceId, deviceInfo),
    _temperatureMeasurement(_client, _tpl, _deviceId, deviceInfo),
    _accelerationMeasurement(_client, _tpl, _deviceId, deviceInfo),
    _analogMeasurement(_client, _tpl, _deviceId, deviceInfo),
    _locationUpdate(_client, _tpl, _deviceId, gps, deviceInfo),
    pool(),
    _operationSupport(_client, _tpl, _deviceId, pool)
{
}

bool MbedAgent::init()
{
    bool flag = true;
    if (!_integration.init()) {
        LCDDisplay::inst().setLines("Integrate init fail");
        flag = false;
    }
    if (!_configurationSynchronization.init()) {
        LCDDisplay::inst().setLines("ConfigSync init fail");
        flag = false;
    }
    if (!_signalQualityMeasurement.init()) {
        LCDDisplay::inst().setLines("Signal init fail");
        flag = false;
    }
    if (!_temperatureMeasurement.init()) {
        LCDDisplay::inst().setLines("Temp init fail");
        flag = false;
    }
    if (!_accelerationMeasurement.init()) {
        LCDDisplay::inst().setLines("Acc init fail");
        flag = false;
    }
    if (!_analogMeasurement.init()) {
        LCDDisplay::inst().setLines("Analog init fail");
        flag = false;
    }
    if (!_locationUpdate.init()) {
        LCDDisplay::inst().setLines("Location init fail");
        flag = false;
    }
    if (!_operationSupport.init()) {
        LCDDisplay::inst().setLines("Operation init fail");
        flag = false;
    }
    return flag;
}

int MbedAgent::run()
{
    // device bootstrapping process
    if (!_bootstrap.setUpCredentials())
        return -1;
    setAuth(_bootstrap.username(), _bootstrap.password());
    aInfo("Set auth: %s:%s (%s)\n", getUsername(), getPassword(), getAuthStr());

    Thread::wait(5000);

    LCDDisplay::inst().setLines("Connect to Cloud", getHost());
    if (!_integration.integrate()) {
        return -2;
    }
    setIdentifier(_client.getIdentifier());
    aInfo("Set id: %s\n", getIdentifier());

    if (!_configurationSynchronization.integrate()) {
        return -3;
    }
    return 0;
}

void MbedAgent::loop()
{
    ReportThread reportThread(pool);
    _operationSupport.run();
    PollThread pollThread(pool, _configurationSynchronization);
    pollThread.setChannel(_deviceId);

    Watchdog wdt;
    wdt.kick(60.0);    // set a 60.0 seconds timeout on watchdog hardware timer
    while (true) {
        _configurationSynchronization.run();
        _analogMeasurement.run();
        _signalQualityMeasurement.run();
        _temperatureMeasurement.run();
        _accelerationMeasurement.run();
        _locationUpdate.run();
//        Thread::wait(30000);
        wdt.kick();    // reset watchdog timer
    }
}