Erick / Mbed 2 deprecated ICE_BLE_TEST

Dependencies:   NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed

Fork of ICE by Erick

src/ConfigurationHandler/Controls/ManualControl.cpp

Committer:
jmarkel44
Date:
2016-09-23
Revision:
121:650205ffa656
Parent:
115:1558e01d04c6
Child:
131:a290a3934132

File content as of revision 121:650205ffa656:

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

extern mDot *GLOBAL_mdot;

// Method:          load
// Description:     open the configuration file and assign data to the
//                  setpoint control object
//
// @param           controlFile -> name of the control file
// @return          true if data is assigned; false on error

bool ManualControl::load(string _controlFile)
{
    MbedJSONValue json_value;
    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 )
        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());
        // caller should destroy the object
        return false;
    }

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

    // parse the json data
    parse(json_value, dataBuf);

    id              = json_value     ["id"].get<string>();
    output          = json_value     ["output"].get<string>();
    type            = atoi(json_value["type"].get<string>().c_str());
    priority        = atoi(json_value["priority"].get<string>().c_str());
    duration        = atoi(json_value["duration"].get<string>().c_str());
    setpoint        = atof(json_value["setpoint"].get<string>().c_str());
    state           = atoi(json_value["state"].get<string>().c_str());
    percent         = atoi(json_value["percent"].get<string>().c_str());

    return true;
}

// Method:          start
// Description:     start the manual control
//
// @param           none
// @return          none

void ManualControl::start(void)
{
    currentState = STATE_STARTUP;

}

// Method:          update
// Description:
//
// @param           none
// @return          none

int ManualControl::update(void)
{
    int rc = 0;
    switch ( this->currentState ) {
        case STATE_STARTUP:
            if ( state ) {
                this->currentState = STATE_CONTROL_ON;
            } else {
                this->currentState = STATE_CONTROL_OFF;
            }
            this->triggerOutput();
            break;
        case STATE_CONTROL_ON:
            if ( !state ) {
                this->currentState = STATE_CONTROL_OFF;
                this->triggerOutput();
            }
            break;
        case STATE_CONTROL_OFF:
            if ( state ) {
                this->currentState = STATE_CONTROL_ON;
                this->triggerOutput();
            }
            break;
        default:
            logError("%s unknown state %d\n", __func__, this->currentState);
            rc = -1;
            break;
    }
    return rc;
}

// Method:              unregisterControl
// Description:         unregister this control with the output master
//
// @param               none
// @return              none
int ManualControl::unregisterControl(void)
{
    logInfo("%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->controlType = CONTROL_MANUAL;
    output_mail->action      = ACTION_CONTROL_UNREGISTER;
    output_mail->priority    = this->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);
    return 0;
}

int ManualControl::triggerOutput(void)
{
    printf("%s: %s attempting to manually turn %s relay %s\n",
           __func__, controlFile.c_str(), (state) ? "ON" : "OFF", id.c_str());

    OutputControlMsg_t *output_mail = OutputMasterMailBox.alloc();
    memset(output_mail, 0, sizeof(OutputControlMsg_t));
    
    output_mail->controlType    = CONTROL_MANUAL;
    output_mail->action         = (state) ? ACTION_CONTROL_ON : ACTION_CONTROL_OFF;
    output_mail->priority       = this->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);
    return 0;
}

void ManualControl::display(void)
{
    string mapper[] = { "STARTUP",
                        "CONTROL_ON",
                        "CONTROL_OFF"
                      };

    printf("\r      controlFile : %s   \n", controlFile.c_str());
    printf("\r               id : %s   \n", id.c_str());
    printf("\r             type : %u   \n", type);
    printf("\r         priority : %u   \n", priority);
    printf("\r         duration : %u   \n", duration);
    printf("\r            state : %u   \n", state);
    printf("\r          percent : %u   \n", percent);
    printf("\r      currentState: %s   \n", mapper[currentState].c_str());

    printf("\r\n");

}