Erick / Mbed 2 deprecated ICE_BLE_TEST

Dependencies:   NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed

Fork of ICE by Erick

src/ConfigurationHandler/ConfigurationHandler.cpp

Committer:
jmarkel44
Date:
2016-09-13
Revision:
51:66b820f203a5
Parent:
46:4cb96ab2d1c8
Child:
72:3754b352f156

File content as of revision 51:66b820f203a5:

/******************************************************************************
 *
 * File:                ConfigurationHandler.cpp
 * Desciption:          source for the ICE Configuration Handler
 *
 *****************************************************************************/
#include "ConfigurationHandler.h"
#include "global.h"
#include "SetpointControl.h"
#include "TimerControl.h"

StringSetpointMap   setpointTable;          // setpoint control object table
StringTimerMap      timerTable;             // timer control object table
//StringManualMap     manualTable;          // manual control object table

// local function prototypes
static int loadPersistentControls(void);

// local helper functions 
static int createControl(const Message_t *msg);
static int modifyControl(const Message_t *msg);
static int destroyControl(const Message_t *msg);

/*****************************************************************************
 * Function:            ConfigurationHandler()
 * Description:         The ICE Configuration Handler
 *
 * @param               args (unused)
 * @return              none
 *****************************************************************************/
void ConfigurationHandler(void const *args)
{
    UNUSED(args);
    loadPersistentControls();
    osSignalSet(mainThreadId, sig_continue);

    while ( true ) {
        // wait for an event
        osEvent evt = MailBox.get();
        if (evt.status == osEventMail) {
            Message_t *msg = (Message_t*) evt.value.p;

            logInfo("\r%s: mes->action      = %d\n", __func__, msg->action);
            logInfo("\r%s: msg->control     = %d\n", __func__, msg->control);
            logInfo("\r%s: msg->controlFile = %s\n", __func__, msg->controlFile);

            switch ( msg->action ) {
                case ACTION_CREATE: {
                    (void)createControl(msg);
                    break;
                }
                case ACTION_MODIFY: {
                    (void)modifyControl(msg);
                    break;
                }
                case ACTION_DESTROY: {
                    (void)destroyControl(msg);
                    break;
                }
                default:
                    break;
            }

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

/*****************************************************************************
 * Function:            ConfigurationHandler_showControls()
 * Description:         show the controls
 *
 * @param               msg
 * @return              none
 *****************************************************************************/
void ConfigurationHandler_showControls(void)
{
    if ( !timerTable.empty() ) {
        printf("\rTIMER CONTROLS\n");
        StringTimerMap::iterator pos;
        for ( pos = timerTable.begin(); pos != timerTable.end(); ++pos ) {
            printf("\r  control file: %32s\n",
                   pos->second->getControlFile().c_str());
        }
    }
    
    if ( !setpointTable.empty() ) {
        printf("\rSETPOINT CONTROLS\n");
        StringSetpointMap::iterator pos;
        for ( pos = setpointTable.begin();
                pos != setpointTable.end();
                ++pos ) {
            printf("\r      controlFile : %s   \n", pos->second->getControlFile().c_str());
            printf("\r               id : %s   \n", pos->second->getId().c_str());
            printf("\r             name : %s   \n", pos->second->getName().c_str());
            printf("\r         priority : %d   \n", pos->second->getPriority());
            printf("\r            input : %s   \n", pos->second->getInput().c_str());
            printf("\r           output : %s   \n", pos->second->getOutput().c_str());
            printf("\r         prodfact : %0.2f\n", pos->second->getProductFactor());
            printf("\r           halert : %0.2f\n", pos->second->getHighAlert());
            printf("\r           lalert : %0.2f\n", pos->second->getLowAlert());
            printf("\r              hfs : %0.2f\n", pos->second->getHighFailsafe());
            printf("\r              lfs : %0.2f\n", pos->second->getLowFailsafe());
            printf("\r              tol : %0.2f\n", pos->second->getTol());
            printf("\r\n");
            printf("\r      currentState: %u\n",    pos->second->getCurrentState());
            printf("\r\n");
        }
    }
}

/*****************************************************************************
 * Function:            loadPersistentControls()
 * Description:         load persistent controls from flash
 *
 * @param               none
 * @return              none
 *****************************************************************************/
static int loadPersistentControls(void)
{
    static bool loaded = false;

    if ( !loaded ) {        // lazy protection
        // do something
        loaded = true;
    }
    return 0;
}

/*****************************************************************************
 * Function:            createControl()
 * Description:         creates a new control
 *
 * @param               none
 * @return              none
 *****************************************************************************/
static int createControl(const Message_t *msg)
{
    logInfo("\r%s invoked\n", __func__);

    switch (msg->control) {
        case CONTROL_SETPOINT: {
            SetpointControl *setpointControl = new SetpointControl;
            bool rc = setpointControl->load(msg->controlFile);
            if ( rc != true ) {
                logError("%s: failed to load %s\n", __func__, msg->controlFile);
                delete setpointControl;
            } else {
                setpointTable[msg->controlFile] = setpointControl;
                // start the setpoint control 
                setpointControl->start(); 
            }
            break;
        }
        case CONTROL_TIMER: {
            TimerControl *timerControl = new TimerControl;
            bool rc = timerControl->load(msg->controlFile);
            if ( rc != true ) {
                logError("%s: failed to load %s\n", __func__, msg->controlFile);
                delete timerControl;
            } else {
                timerTable[msg->controlFile] = timerControl;
            }
            break;
        }
        case CONTROL_MANUAL: {
#if 0
            ManualControl *manualControl = new ManualControl;
            bool rc = manualControl->load(msg->controlFile);
            if ( rc != true ) {
                logError("%s: failed to load %s\n", __func__, msg->controlFile);
                delete manualControl;
            } else {
                manualTable[msg->controlFile] = manualControl;
            }
#endif
            break;
        }
        case CONTROL_PID:
        case CONTROL_COMPOSITE:
        default:
            logInfo("\r%s: control type %d not implemented yet...\n",
                    __func__, msg->control);
            break;
    }
    return 0;
}

/*****************************************************************************
 * Function:            modifyControl()
 * Description:         modifies a control
 *
 * @param               msg
 * @return              none
 *****************************************************************************/
static int modifyControl(const Message_t *msg)
{
    logInfo("\r%s invoked\n", __func__);

    // TODO: can we delete the current object and start a new one?
    return 0;

}

/*****************************************************************************
 * Function:            destroyControl()
 * Description:         destroys a controls
 *
 * @param               msg
 * @return              none
 *****************************************************************************/
static int destroyControl(const Message_t *msg)
{
    logInfo("\r%s invoked\n", __func__);

    switch ( msg->control ) {
        case CONTROL_SETPOINT: {
            StringSetpointMap::iterator pos;
            pos = setpointTable.find(msg->controlFile);
            if ( pos != setpointTable.end() ) {
                delete (pos->second);
                setpointTable.erase(pos);
            }
            break;
        }
        case CONTROL_TIMER: {
            StringTimerMap::iterator pos;
            pos = timerTable.find(msg->controlFile);
            if ( pos != timerTable.end() ) {
                delete (pos->second);
                timerTable.erase(pos);
            }
            break;
        }
        case CONTROL_MANUAL: {
#if 0
            StringManualMap::iterator pos;
            pos = manualTable.find(msg->controlFile);
            if ( pos != manualTable.end() ) {
                delete (pos->second);
                manualTable.erase(pos);
            }
#endif
            break;
        }
        default:
            break;
    }
    return 0;
}