Erick / Mbed 2 deprecated ICE-F412

Dependencies:   mbed-rtos mbed

ICE-Application/src/ConfigurationHandler/Algorithms/CompositeAlgorithm.cpp

Committer:
jmarkel44
Date:
2017-01-24
Revision:
0:61364762ee0e

File content as of revision 0:61364762ee0e:

/******************************************************************************
 *
 * File:                CompositeAlgorithm.cpp
 * Desciption:          source for the ICE Composite Algorithm
 *
 *****************************************************************************/
#include "global.h"
#include "cJSON.h"
#include "ICELog.h"
#include "CompositeAlgorithm.h"
//#include "../add-ons/MTSLog/MTSLog.h"

//
// method:          load
// description:     load the pertinents from the control file
//
// @param[in]       _controlFile : name of the control file
// @param[out]      none
// @return          none
//
bool CompositeAlgorithm::load(const std::string _controlFile)
{
    controlFile = _controlFile;

    // read the data into a buffer
    char dataBuf[MAX_FILE_SIZE];
    bool rc = GLOBAL_mdot->readUserFile(controlFile.c_str(), (void *)dataBuf, sizeof(dataBuf));
    if ( rc != true ) {
        logError("%s: failed to read %s", __func__, controlFile.c_str());
        // caller should destroy the object
        return false;
    }

    // parse the JSON data
    cJSON * root = cJSON_Parse(dataBuf);

    if ( !cJSON_HasObjectItem(root, "id") ||
            !cJSON_HasObjectItem(root, "opr") ||
            !cJSON_HasObjectItem(root, "op")  ||
            !cJSON_HasObjectItem(root, "true") ||
            !cJSON_HasObjectItem(root, "false") ) {
        logError("%s: control file is missing expected tags", __func__);
        cJSON_Delete(root);
        return false;
    }

    id          = cJSON_GetObjectItem(root, "id")->valuestring;
    opr         = cJSON_GetObjectItem(root, "opr")->valuestring;
    op          = cJSON_GetObjectItem(root, "op")->valuestring;
    resultTrue  = cJSON_GetObjectItem(root, "true")->valuestring;
    resultFalse = cJSON_GetObjectItem(root, "false")->valuestring;

    cJSON_Delete(root);
    return true;
}

//
// method:              validateControlData
// description:         validate the JSON formatted string
//
// @param[in]           buf -> JSON formatted string
// @param[out]          none
// @return              true if valid; false otherwise
//
bool CompositeAlgorithm::validateControlData(const char *buf)
{
    bool rc = true;
    cJSON * root = cJSON_Parse(buf);

    if ( !cJSON_HasObjectItem(root, "id") ||
            !cJSON_HasObjectItem(root, "opr") ||
            !cJSON_HasObjectItem(root, "op")  ||
            !cJSON_HasObjectItem(root, "true") ||
            !cJSON_HasObjectItem(root, "false") ) {
        logError("%s: control file is missing expected tags", __func__);
        rc = false;
    }
    cJSON_Delete(root);
    return rc;
}

//
// method:              copyControlData
// description:         copy JSON data to control data
//
// @param[in]           buf -> JSON formatted string
// @param[out]          none
// @return              
void CompositeAlgorithm::copyControlData(const char *buf)
{
    cJSON * root = cJSON_Parse(buf);
    
    id          = cJSON_GetObjectItem(root, "id")->valuestring;
    opr         = cJSON_GetObjectItem(root, "opr")->valuestring;
    op          = cJSON_GetObjectItem(root, "op")->valuestring;
    resultTrue  = cJSON_GetObjectItem(root, "true")->valuestring;
    resultFalse = cJSON_GetObjectItem(root, "false")->valuestring;

    cJSON_Delete(root);
}

//
// method:          display
// description:     display the pertinents
//
// @param[in]       none
// @param[out]      none
// @return          none
//
void CompositeAlgorithm::display(void)
{
    printf("\r          id : %s\n", id.c_str());
    printf("\r         opr : %s\n", opr.c_str());
    printf("\r          op : %s\n", op.c_str());
    printf("\r        true : %s\n", resultTrue.c_str());
    printf("\r       false : %s\n", resultFalse.c_str());
    printf("\r\n");
}