Own fork of MbedSmartRestMain

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Cumulocity Official

MbedAgent.cpp

Committer:
Cumulocity
Date:
2014-07-24
Revision:
42:104746744af8
Parent:
41:804f6a0bda26
Child:
46:f6976fd64387

File content as of revision 42:104746744af8:

#include "MbedAgent.h"
#include "rtos.h"

MbedAgent::MbedAgent(DeviceIO& io, MDMSerial& mdm, DeviceInfo& deviceInfo) :
    _io(io),
    _mdm(mdm),
    _deviceInfo(deviceInfo),
    _client(MBED_AGENT_HOST, MBED_AGENT_PORT, MBED_AGENT_DEVICE_IDENTIFIER),
    _bootstrap(_client, _mdm, _deviceInfo),
    _integration(_client, _tpl, _deviceId, _deviceInfo),
    _signalQualityMeasurement(_client, _tpl, _deviceId, _deviceInfo),
    _temperatureMeasurement(_client, _tpl, _deviceId, _io.temperatureSensor()),
    _accelerationMeasurement(_client, _tpl, _deviceId, _io.accelerometer()),
    _deviceId(0)
{
}

bool MbedAgent::init()
{
    if ((!_integration.init()) ||
        (!_signalQualityMeasurement.init()) ||
        (!_temperatureMeasurement.init()) ||
        (!_accelerationMeasurement.init())) {
        puts("Initialization failed.");
        return false;
    }
    return true;
}

bool MbedAgent::run()
{
    // device bootstrapping process
    _io.lcdPrint("BOOTSTRAP", _deviceInfo.imei());
    if (!_bootstrap.setUpCredentials()) {
        puts("Could not obtain credentials.");
        return false;
    }

    _io.lcdPrint("INTEGRATION");
    if (!_integration.integrate()) {
        puts("Device integration process failed.");
        return false;
    }
    
    char status[60];
    snprintf(status, sizeof(status), "ID: %ld", _deviceId);
    _io.lcdPrint("INTEGRATED", status);
    puts(status);

    loop();
    return true;
}

void MbedAgent::loop()
{
    Timer timer;
    
    timer.start();
    while (true) {
        timer.reset();
        
        _signalQualityMeasurement.run();
        _temperatureMeasurement.run();
        _accelerationMeasurement.run();
        
        while (timer.read() < MBED_AGENT_INTERVAL) {
            Thread::yield();
        }
    }
}