Erick / Mbed 2 deprecated ICE_BLE_TEST

Dependencies:   NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed

Fork of ICE by Erick

src/OutputTask/OutputTask.cpp

Committer:
jmarkel44
Date:
2016-09-16
Revision:
67:49f266601d83
Parent:
66:db1425574b58
Child:
70:7427f4959201

File content as of revision 67:49f266601d83:

#include "OutputTask.h"
#include "global.h"
#include "MbedJSONValue.h"

static int  createOutput(const char *controlFile);
static void loadPersistentOutputs(void);

typedef std::map<string, string> StringOutputVector_t;

StringOutputVector_t outputMap;

void OutputTask(void const *args)
{
    UNUSED(args);
    
    printf("\r%s has started...\n", __func__);

    loadPersistentOutputs();

    while (true) {
        // wait for an event
        osEvent evt = OutputMasterMailBox.get();
        if (evt.status == osEventMail) {
            OutputControlMsg_t *msg = (OutputControlMsg_t*) evt.value.p;
            printf("\r%s received message from someone...\n", __func__);
            printf("\rmsg->relay    = %s\n", msg->output.c_str());
            printf("\rmsg->state    = %s\n", msg->state == ON ? "ON" : "OFF");
            printf("\rmsg->priority = %u\n", msg->priority);

            switch ( msg->action ) {
                case ACTION_NEW:
                    // read the file and and create an output entry
                    (void) createOutput(msg->controlFile);
                    break;
                case ACTION_CONTROL_REQ:
                default:
                    break;
            }

            // free the message
            OutputMasterMailBox.free(msg);
        }
    }
}

void DisplayOutputs(void)
{
    StringOutputVector_t::iterator pos;

    for ( pos = outputMap.begin(); pos != outputMap.end(); ++pos ) {
        printf("\r  [%s] [ %s]\n", pos->first.c_str(), pos->second.c_str());
    }
    printf("\r\n");
}

static int createOutput(const char *controlFile)
{
    char dataBuf[1024];
    int status = GLOBAL_mdot->readUserFile(controlFile, (void *)dataBuf, sizeof(dataBuf));
    if ( status != true ) {
        logError("%s failed to read %s", __func__, controlFile);
        return -1;
    }

    MbedJSONValue json_value;
    parse(json_value, dataBuf);

    // extract the relay information
    string id = json_value["id"].get<string>();

    // maps shouldnt' allow duplicates
    outputMap[id] = "Null for now";

    return 0;
}

static void loadPersistentOutputs(void)
{
    bool status;
    MbedJSONValue json_value;

    std::vector<mDot::mdot_file> file_list = GLOBAL_mdot->listUserFiles();
    
    for (std::vector<mDot::mdot_file>::iterator i = file_list.begin(); i != file_list.end(); ++i) {
        if( strncmp( i->name, OUTPUT_STR, strlen(OUTPUT_STR)) == 0 ) {

            logInfo("%s: FOUND OUTPUT FILE: %s", __func__, i->name);

            char scratchBuf[1024];

            status = GLOBAL_mdot->readUserFile(i->name, scratchBuf, 1024);
            if( status != true ) {
                logInfo("(%d)read file failed, status=%d", __LINE__, status);
            } else {
                logInfo("(%d)Read File SUCCESS: %s", __LINE__, scratchBuf );
            }

            parse( json_value, scratchBuf );

            string id = json_value["id"].get<string>();
            printf("\r%s id = %s\n", __func__, id.c_str());
            outputMap[id] = "Null for now";
        }
    }
}