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-05-07
Revision:
99:47ea098f8a47
Parent:
98:e369fc75c000
Child:
100:dbcd3bc51758

File content as of revision 99:47ea098f8a47:

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

MbedAgent::MbedAgent(DeviceInfo& deviceInfo):
    _client(), tpl(), _bootstrap(_client, deviceInfo), 
    _integration(_client, _tpl, deviceInfo), lcdThirdLineBlank(true), 
    signal(), temp(), poti(), gps(), acc(), conf(), sock(),
    pool(), _operationSupport(_client, _tpl, pool)
{
    reporters[0] = &signal;
    reporters[1] = &temp;
    reporters[2] = &poti;
    reporters[3] = &gps;
    reporters[4] = &acc;
    reporters[5] = &conf;
}

bool MbedAgent::init()
{
    bool flag = true;
    if (!_integration.init()) {
        LCDDisplay::inst().setLines("Integrate 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", srUsername, srPassword, srAuthStr);

    Thread::wait(2000);

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

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

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

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