Erick / Mbed 2 deprecated ICE_BLE_TEST

Dependencies:   NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed

Fork of ICE by Erick

src/ConfigurationHandler/Controls/CompositeControl.cpp

Committer:
jmarkel44
Date:
2016-10-14
Revision:
220:dbe21411f962
Parent:
164:7cecd731882e
Child:
221:2a5e9902003c

File content as of revision 220:dbe21411f962:

/******************************************************************************
 *
 * File:                CompositeControl.cpp
 * Desciption:          ICE Composite Control Class implementation
 *
 *****************************************************************************/
#include "CompositeControl.h"
#include "mDot.h"
#include "MbedJSONValue.h"
#include "ModbusMasterApi.h"
#include "global.h"
#include <string>

extern mDot *GLOBAL_mdot;

bool CompositeControl::load(std::string _controlFile)
{
    MbedJSONValue json_value;           // JSON parsing element
    controlFile = _controlFile;

    // open and read from the control file
    mDot::mdot_file file = GLOBAL_mdot->openUserFile(controlFile.c_str(), mDot::FM_RDONLY);
    if ( file.fd < 0 ) {
        logError("%s: failed to open %s\n", __func__, controlFile.c_str());
        return false;
    }

    // read the data into a buffer
    char dataBuf[MAX_FILE_SIZE];

    int bytes_read = GLOBAL_mdot->readUserFile(file, (void *)dataBuf, sizeof(dataBuf));
    if ( bytes_read != sizeof(dataBuf) ) {
        logError("%s: failed to read %d bytes from %s", __func__, sizeof(dataBuf), controlFile.c_str());
        // caller should destroy the object
        return false;
    }

    // close the file
    GLOBAL_mdot->closeUserFile(file);

    parse(json_value, dataBuf);

    if ( !json_value.hasMember("id") ||
            !json_value.hasMember("input") ||
            !json_value.hasMember("ca") ||
            !json_value.hasMember("priority") ||
            !json_value.hasMember("entries") ) {
        printf("\rComposite control is missing expected tags\n");
        return false;
    }

    id                  = json_value["id"].get<string>();
    std::string input   = json_value["input"].get<string>();
    priority            = atoi(json_value["priority"].get<string>().c_str());
    inputs.push_back(input);
    ca = json_value["ca"].get<string>();

    int entries = atoi(json_value["entries"].get<string>().c_str());

    for ( int i = 0; i < entries; ++i ) {
        std::string tag = json_value["outputs"][i]["tag"].get<string>();
        std::string response = json_value["outputs"][i]["response"].get<string>();
        if ( !tag.empty() && !response.empty()  ) {
            OutputElement x = { tag, response };
            outputs.push_back(x);
        } else {
            return false;
        }
    }

    return true;
}

// unregister the control with the output task
void CompositeControl::unregisterControl(void)
{
}

void CompositeControl::display(void)
{
    printf("\r       control file : %s\n", controlFile.c_str());
    printf("\r                 id : %s\n", id.c_str());
    printf("\r           priority : %u\n", priority);
    printf("\r                 ca : %s\n", ca.c_str());
    vector<std::string>::iterator pos;
    for ( pos = inputs.begin(); pos != inputs.end(); ++pos ) {
        printf("\r              input : %s\n", (*pos).c_str());
    }
    vector<OutputElement>::iterator it;
    printf("\r            outputs :\n");
    for ( it = outputs.begin(); it != outputs.end(); ++it ) {
        printf("\r                     tag-> %s\n", (*it).tag.c_str());
        printf("\r                response-> %s\n", (*it).response.c_str());
    }
    printf("\r\n");
}