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-03-16
Revision:
89:0525121f307e
Parent:
85:b49c4cfecc43
Child:
90:423177e8a401

File content as of revision 89:0525121f307e:

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

bool lastSensorReadingSent=true;

MbedAgent::MbedAgent(DeviceIO& io, MDMSerial& mdm, DeviceInfo& deviceInfo, DeviceMemory& deviceMemory) :
    _io(io),
    _mdm(mdm),
    _deviceInfo(deviceInfo),
    _deviceMemory(deviceMemory),
    _configurationProperties(_deviceConfiguration),
    _client(MBED_AGENT_HOST, MBED_AGENT_PORT, MBED_AGENT_DEVICE_IDENTIFIER, mdm),
    _bootstrap(_client, _io, _deviceInfo, _deviceMemory),
    _integration(_client, _tpl, _deviceId, _deviceInfo),
    _displayInfo(),
    _configurationSynchronization(_client, _tpl, _deviceId, _deviceMemory, _deviceConfiguration, _configurationProperties),
    _signalQualityMeasurement(_client, _tpl, _deviceId, _deviceInfo, io, _displayInfo),
    _temperatureMeasurement(_client, _tpl, _deviceId, _io.temperatureSensor(), io, deviceInfo, _displayInfo),
    _accelerationMeasurement(_client, _tpl, _deviceId, _io.accelerometer(), io, deviceInfo, _displayInfo),
    _analogMeasurement(_client, _tpl, _deviceId, _io.analog1(), _io.analog2(), io, deviceInfo, _displayInfo),
    _locationUpdate(_client, _tpl, _deviceId, _io.gpsTracker(), io, deviceInfo, _displayInfo),
    _operationSupport(_client, _tpl, _deviceId, _configurationSynchronization, _io, _displayInfo),
    _deviceId(0)
{
}

bool MbedAgent::init()
{
    bool flag = true;
    if (!_integration.init()) {
        aError("Init Device Integration failed.\r\n");
        flag = false;
    }
    if (!_configurationSynchronization.init()) {
        aError("Init Configuration Synchronization failed.\r\n");
        flag = false;
    }
    if (!_signalQualityMeasurement.init()) {
        aError("Init Signal Quality Measurement failed.\r\n");
        flag = false;
    }
    if (!_temperatureMeasurement.init()) {
        aError("Init Temperature Measurement failed.\r\n");
        flag = false;
    }
    if (!_accelerationMeasurement.init()) {
        aError("Init Acceleration Measurement failed.\r\n");
        flag = false;
    }
    if (!_analogMeasurement.init()) {
        aError("Init Analog Measurement failed.\r\n");
        flag = false;
    }
    if (!_locationUpdate.init()) {
        aError("Init Location Measurement failed.\r\n");
        flag = false;
    }
    if (!_operationSupport.init()) {
        aError("Init Operation Support failed.\r\n");
        flag = false;
    }
    return flag;
}

bool MbedAgent::run()
{
    // device bootstrapping process
    if (!_bootstrap.setUpCredentials())
        return false;

    Thread::wait(5000);

    _io.lcdPrint("Connect to Cloud", MBED_AGENT_HOST);
    if (!_integration.integrate()) {
        return false;
    }
    
    if (!_configurationSynchronization.integrate()) {
        return false;
    }
    
    char status[60];
    snprintf(status, sizeof(status), "ID: %ld", _deviceId);
    _io.lcdPrint("Connected", status);

    char tenant[25];
    const char* user= _bootstrap.username();
    int len = strchr(user, '/')-user+1+8;  // 8: length of "Tenant: "
    len = len <= 25 ? len : 25;
    snprintf(tenant, len, "Tenant: %s", user);
    _displayInfo.setFirstLine(tenant);
    loop();

    return true;
}

void MbedAgent::loop()
{
    Watchdog wdt;
    DigitalIn joystickUp(A2);
    DigitalIn joystickDown(A3);
//    DigitalIn joystickLeft(A4);
//    AnalogIn joystickRight(A5);
    
    wdt.kick(60.0);    // set a 60.0 seconds timeout on watchdog hardware timer
    while (true) {
        if (joystickDown) {
            setLevel(A_NONE);
            _mdm.setDebug(-1);
            printf("***Disabled debug mode.***\r\n");
        } else if (joystickUp) {
            setLevel(A_DEBUG);
            _mdm.setDebug(3);
            printf("***Enabled debug mode.***\r\n");
        }

        _configurationSynchronization.run();
        _signalQualityMeasurement.run();
        _temperatureMeasurement.run();
        _analogMeasurement.run();
        _accelerationMeasurement.run();
        _locationUpdate.run();
        _operationSupport.run();
                
//        if ((interval = _configurationProperties.readInterval()) < 0)
//            break;
//
//        while (timer.read() < interval) {
//            Thread::yield();
//        }
        wdt.kick();    // reset watchdog timer
    }
}