Erick / Mbed 2 deprecated ICE_BLE_TEST

Dependencies:   NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed

Fork of ICE by Erick

src/ConfigurationHandler/Controls/TimerControl.cpp

Committer:
jmarkel44
Date:
2016-09-26
Revision:
131:a290a3934132
Parent:
128:534bf29132f8
Child:
132:45821e189dd0

File content as of revision 131:a290a3934132:

/******************************************************************************
 *
 * File:                TimerControl.cpp
 * Desciption:          ICE Timer Control Class implementation
 *
 *****************************************************************************/
#include "TimerControl.h"
#include "mDot.h"
#include "MbedJSONValue.h"
#include <string>

extern mDot *GLOBAL_mdot;

//
// method:      load
// description: load the pertinents from the control file
//
// @param       _controlFile
// @return      true if loaded; false otherwise
//
bool TimerControl::load(string _controlFile)
{
    MbedJSONValue json_value;           // json parsing element

    // try to open the control file
    mDot::mdot_file file = GLOBAL_mdot->openUserFile(_controlFile.c_str(), mDot::FM_RDONLY);
    if ( file.fd < 0 ) {
        return false;
    }

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

    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());
        return false;
    }

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

    parse(json_value, dataBuf);

    controlFile = _controlFile;
    id          = json_value     ["id"].get<string>();
    priority    = atoi(json_value["priority"].get<string>().c_str());
    dow         = atoi(json_value["dow"].get<string>().c_str());
    startw      = atoi(json_value["startw"].get<string>().c_str());
    output      = json_value     ["output"].get<string>();
    startHour   = atoi(json_value["startHour"].get<string>().c_str());
    startMinute = atoi(json_value["startMinute"].get<string>().c_str());
    duration    = atoi(json_value["duration"].get<string>().c_str());

    return true;
}

void TimerControl::start(void)
{
    currentState = STATE_OFF;
}

bool TimerControl::feedCheck(void)
{
    return false;
}

void TimerControl::update(void)
{
    switch ( this->currentState ) {
        case STATE_OFF:
            if ( this->feedCheck() ) {
                currentState = STATE_RUNNING;
            }
            break;
        case STATE_RUNNING:
            if ( !this->feedCheck() ) {
                currentState = STATE_OFF;
            }
            break;
        case STATE_DISABLED:
            // not implelmented
        default:
            break;
    }
}

//
// methid:      display
// description: display the elements of this timer control object
//
// @param       none
// @return
void TimerControl::display(void)
{
    char daysOfWeek[32];
    memset(daysOfWeek, 0, sizeof(daysOfWeek));
    string mapper[] = { "OFF",
                        "RUNNING",
                        "DISABLED"
                      };

    sprintf(daysOfWeek, "%s %s %s %s %s %s %s",
            (dow & DAY_SUNDAY_MASK)    ? "Su" : "  ",
            (dow & DAY_MONDAY_MASK)    ? "M"  : " ",
            (dow & DAY_TUESDAY_MASK)   ? "Tu" : "  ",
            (dow & DAY_WEDNESDAY_MASK) ? "W"  : " ",
            (dow & DAY_THURSDAY_MASK)  ? "Th" : "  ",
            (dow & DAY_FRIDAY_MASK)    ? "F"  : " ",
            (dow & DAY_SATURDAY_MASK)  ? "Sa" : "  ");

    printf("\r      controlFile : %s   \n", controlFile.c_str());
    printf("\r               id : %s   \n", id.c_str());
    printf("\r         priority : %d   \n", priority);
    printf("\r              dow : %s   \n", daysOfWeek);
    printf("\r           startw : %u   \n", startw);
    printf("\r           output : %s   \n", output.c_str());
    printf("\r    starting hour : %u   \n", startHour);
    printf("\r  starting minute : %u   \n", startMinute);
    printf("\r         duration : %u   \n", duration);
    printf("\r\n");
    printf("\r     current state: %s    \n", mapper[currentState].c_str());
    printf("\r\n");
    
    //GetNextScheduledFeed();
}