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-10-04
Revision:
171:fb29030d4eaf
Parent:
157:0d79678ed00f
Child:
177:9ec90c8e3ce1

File content as of revision 171:fb29030d4eaf:

/******************************************************************************
 *
 * File:                TimerControl.cpp
 * Desciption:          ICE Timer Control Class implementation
 *
 *****************************************************************************/
#include "TimerControl.h"
#include "mDot.h"
#include "MbedJSONValue.h"
#include "global.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[512];

    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);

    // the pertinents
    controlFile = _controlFile;
    id          = json_value["id"].get<string>();
    output      = json_value["output"].get<string>();
    priority    = atoi(json_value["priority"].get<string>().c_str());
    startTime   = atol(json_value["starttime"].get<string>().c_str());
    duration    = atoi(json_value["duration"].get<string>().c_str());

    return true;
}

//
// method:      start
// description: initialize the control
//
// @param       none
// @return      none
//
void TimerControl::start(void)
{
    currentState = STATE_OFF;
}

//
// method:      timerStart
// description: examine the timestamp to determine if the timer control
//              should be running
//
// @param       none
// @return      true if timer should be running; false otherwise
//
bool TimerControl::timerStart(void)
{
    unsigned long currentTime = time(NULL);

    if ( currentTime < startTime )
        return false;

    if ( currentTime >= startTime && currentTime <= (startTime + duration) ) {
        return true;
    }
    return false;
}
//
// method:      timerStop
// description: determines if a running timer should has reached its duration
//
// @param       none
// @return      true if the timer has expired; false otherwise
//
bool TimerControl::timerStop(void)
{
    if ( time(NULL) >= startTime + duration ) {
        return true;
    }
    return false;
}

//
// method:      update
// description: run the simplified state machine
//
// @param       none
// @return      none
//
TimerError_t TimerControl::update(void)
{
    TimerError_t rc = TIMER_CONTROL_OK;
    
    switch ( this->currentState ) {
        case STATE_OFF:
            if ( this->timerStart() ) {
                currentState = STATE_RUNNING;
                this->startFeed();
            }
            break;
        case STATE_RUNNING:
            if ( this->timerStop() ) {
                currentState = STATE_OFF;
                this->stopFeed();
                this->unregisterControl();
                rc = TIMER_CONTROL_DESTROY;
            }
            break;
        case STATE_DISABLED:
            // not implelmented
        default:
            break;
    }
    return rc;
    
}

//
// method:      startFeed
// description: signal the output thread to start a feed
//
// @param       none
// @return      none
void TimerControl::startFeed(void)
{
    logInfo("%s: %s attempting to start feed on relay %s\n",
            __func__, controlFile.c_str(), output.c_str());

    OutputControlMsg_t *output_mail = OutputMasterMailBox.alloc();
    memset(output_mail, 0, sizeof(OutputControlMsg_t));

    output_mail->action      = ACTION_CONTROL_ON;
    output_mail->controlType = CONTROL_TIMER;
    output_mail->priority    = priority;

    strncpy(output_mail->output_tag, this->output.c_str(), sizeof(output_mail->output_tag)-1);
    strncpy(output_mail->id, this->id.c_str(), sizeof(output_mail->id)-1);
    OutputMasterMailBox.put(output_mail);
}

//
// method:      stopFeed
// description: signal the output thread to stop a feed
//
// @param       none
// @return      none
void TimerControl::stopFeed(void)
{
    logInfo("%s: %s attempting to start feed on relay %s\n",
            __func__, controlFile.c_str(), output.c_str());

    OutputControlMsg_t *output_mail = OutputMasterMailBox.alloc();
    memset(output_mail, 0, sizeof(OutputControlMsg_t));

    output_mail->action      = ACTION_CONTROL_OFF;
    output_mail->controlType = CONTROL_TIMER;
    output_mail->priority    = priority;

    strncpy(output_mail->output_tag, this->output.c_str(), sizeof(output_mail->output_tag)-1);
    strncpy(output_mail->id, this->id.c_str(), sizeof(output_mail->id)-1);
    OutputMasterMailBox.put(output_mail);
}
    
//
// Method:              unregisterControl
// Description:         send OFF indication to Output Master for this control's
//                      relay
//
// @param               none
// @return              none
//
void TimerControl::unregisterControl(void)
{
    logInfo("%s: %s attempting to unregister %s\n",
            __func__, controlFile.c_str());

    OutputControlMsg_t *output_mail = OutputMasterMailBox.alloc();
    memset(output_mail, 0, sizeof(OutputControlMsg_t));

    output_mail->action       = ACTION_CONTROL_UNREGISTER;
    output_mail->controlType  = CONTROL_TIMER;
    output_mail->priority     = priority;
    strncpy(output_mail->output_tag, this->output.c_str(), sizeof(output_mail->output_tag)-1);
    strncpy(output_mail->id, this->id.c_str(), sizeof(output_mail->id)-1);

    OutputMasterMailBox.put(output_mail);
}

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

    printf("\r      controlFile : %s      \n", controlFile.c_str());
    printf("\r               id : %s      \n", id.c_str());
    printf("\r           output : %s      \n", output.c_str());
    printf("\r         priority : %d      \n", priority);
    printf("\r       start time : %lu     \n", startTime);
    printf("\r         duration : %u      \n", duration);
    printf("\r         end time : %lu     \n", startTime + duration);
    printf("\r       expires in : %lu sec \n", (startTime + duration) - time(NULL));
    printf("\r    current State : %s\r\n\r\n", mapper[currentState].c_str());
}