Erick / Mbed 2 deprecated ICE-F412

Dependencies:   mbed-rtos mbed

ICE-Application/src/ConfigurationHandler/Controls/TimerControl.h

Committer:
jmarkel44
Date:
2017-01-24
Revision:
0:61364762ee0e

File content as of revision 0:61364762ee0e:

/******************************************************************************
 *
 * File:                TimerControl.h
 * Desciption:          ICE Timer Control Class
 *
 *****************************************************************************/
#ifndef TIMERCONTROL_H
#define TIMERCONTROL_H

//#include "ConfigurationHandler.h"
#include <string>
#include <stdio.h>
#include <vector>

typedef enum {
    TIMER_CONTROL_OK,
    TIMER_CONTROL_UNK_STATE,
    TIMER_CONTROL_ERROR,
    TIMER_CONTROL_DESTROY
} TimerControlError_t;


//! Timer Control - implements a timer control.
/// @code
/// #include "TimerControl.h"
///
/// int main(void)
/// {
///     TimerControl *t = new TimerControl();
///     bool rc = t->load(controlFile);
///     if ( rc != true ) {
///         logError("%s: failed to load the manual control\n");
///         delete t;
///         exit(-1);
///     }
///
///     t->start();
///     for ( ;; ) {
///         t->update();
///     }
/// }
/// @endcode
class TimerControl
{
private:
    std::string     controlFile;        // file containing control data
    std::string     id;                 // timer identifier
    std::string     output;             // output to control
    unsigned int    priority;           // control priority
    unsigned int    day;
    unsigned int    startHour;          // start hour (0-23)
    unsigned int    startMin;           // start minute (0-59)
    unsigned int    startSec;           // start second (0-59)
    unsigned int    duration;           // duration in seconds
    unsigned int    week;               // every week, first week, ...

    enum State {
        STATE_INIT,
        STATE_OFF,
        STATE_RUNNING,
        STATE_FINISHED,
        STATE_DISABLED
    };
    State           currentState;

    unsigned long   nextScheduledStartTime;
    unsigned long   startTime;

    bool            isVirtualOutput;

    // validate control file data
    bool validateControlData(const char *buf);

    // copy control file data to object
    void copyControlData(const char *buf);

    // start a feed
    void startFeed(void);

    // stop a feed
    void stopFeed(void);

    // calculate time until start
    unsigned long calcStartTime() const;
    
    const char *displayTime(void);

public:
    TimerControl() {
        currentState = STATE_INIT;
    }
    ~TimerControl() {
        printf("\r%s invoked\n", __func__);
    }
    /// load control data from a JSON configuration file
    bool load(std::string filename);

    /// start a timer control instance
    void start(void);

    /// update a timer control instance
    TimerControlError_t update(void);

    /// unregister a timer control instance from the output thread
    void unregisterControl(void);

    /// display pertinent data of a timer control instance
    void display(void);

    std::string getControlFile(void) const {
        return controlFile;
    }

    std::string getId(void) const {
        return id;
    }

    std::string getOutput(void) const {
        return output;
    }

    unsigned int getPriority(void) const {
        return priority;
    }

    unsigned int getDuration(void) const {
        return duration;
    }

    State getCurrentState(void) const {
        return currentState;
    }
};

#endif