// SemiE10.h: interface for the CSemiE10 class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_SEMIE10_H__EA674164_EC86_401C_A8B8_6AC49BE92642__INCLUDED_)
#define AFX_SEMIE10_H__EA674164_EC86_401C_A8B8_6AC49BE92642__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "NonScheduledTime.h"
#include "OperationsTime.h"
#include "Timer.h"

struct StTime
{
    StTime()
    {
        csTimeName = nullptr;
        dTime      = 0.0;
    }

    const char *csTimeName;
    double dTime;
};

struct StRamTime
{
    StRamTime()
    {
        dTime = 0.0;
    }

    CString csTimeName;
    double dTime;
};

struct StInterrupt
{
    StInterrupt()
    {
        dInterruptTime = 0.0;
    }

    CString csInterruptName;
    double dInterruptTime;
};

struct StLong
{
    StLong()
    {
        lCount = 0;
    }

    CString csTimeName;
    long lCount;
};

struct StOleDateTime
{
    StOleDateTime()
    {
    }

    CString csTimeName;
    COleDateTime odtDateTime;
};

struct StSemiE10
{
    enum
    {
        MTBA,                // Mean Time between Assist
        MTBF,                // Mean Time between Failure
        MTBI,                // Mean Time between Interrupts
        MTTR,                // Mean Time between Repair
        MAX_RAM_MEASUREMENT, // Maximun Reliablity, Availability, Maintainability Measurement
    };

    enum
    {
        UNIT_CNT,
        ASSIST_CNT,
        FAILURE_CNT,
        JAM_CNT,
        UPH, // Unit per hour
        MAX_CNT,
    };

    enum
    {
        START_LOT_TIME,
        MAX_OLE_DATE_TIME,
    };

    enum
    {
        ASSIST_TIME,
        FAILURE_TIME,
        MAX_INTERRUPT,
    };

    enum
    {
        EQUIP_SUBSTATE,
        EQUIP_STATE,
        RAM_TIME,
        INTERRUPT,
        CNT,
        OLE_DATE_TIME,
    };

    StTime stTime[CEquipState::MAX_EQUIPMENT_SUBSTATE];
    StTime stEquipStateTime[CEquipState::MAX_EQUIP_STATE];
    StRamTime stRamTime[MAX_RAM_MEASUREMENT];

    StInterrupt stInterrupt[MAX_INTERRUPT];
    StLong stLong[MAX_CNT];
    StOleDateTime stOleDateTime[MAX_OLE_DATE_TIME];
};

#ifdef  BUILD_MCCTRDLL
#define     MCCTRDLLCLASS   __declspec(dllexport)
#else
#define     MCCTRDLLCLASS   __declspec(dllimport)
#endif

class MCCTRDLLCLASS CSemiE10
// class CSemiE10
{
public:
//  default constructor
    CSemiE10();

//  default destructor
    virtual ~CSemiE10();

//  read save time to file time interval
    void ReadSaveTimeInterval();

//  machine jam start jam timer and stop the rest of the timer
    void McJam();

//  Open lot reset all timer
    void OpenLot();

//  Machine running start regular production timer and stop the rest of the timer
    void StartMc();

//  Machine stopped start stop machine timer and stop the rest of the timer
    void StopMc();

//  start No material timer and stop the rest of the timer.
//  Return true if no material not started before
//  Return false if no material start before
    bool NoMaterial();

//  Close Lot stop all timer
    void CloseLot();

//  machine shutdown or startup
    void ShutdownStartup();

//  check whether it is failure or assist
    void CheckAssistOrFailure();

//  set uptime and downtime equipment state
    void SetEquipState(int iEvt, int iState);

//  Start all equipment state time. Possible to start all equipment state?
    void StartAllEquipState();

//  Stop all equipment state time
    void StopAllEquipState();

//  Reset all equipment state time
    void ResetAllEquipState();

//  get equipment substate time
    double GetEquipSubStateTime(int iEvt);

//  get total uptime and down equipment state
    double GetTotalTime();

//  get non scheduled time
    double GetNonScheduledTime();

//  get operations time
    double GetOperationsTime();

//  get Uptime
    double GetUptime();

//  get engineering time
    double GetEngineeringTime();

//  get manufacturing time
    double GetManufacturingTime();

//  get productive time
    double GetProductiveTime();

//  get standby time
    double GetStandbyTime();

//  get downtime
    double GetDowntime();

//  get unscheduled downtime
    double GetUnscheduledDowntime();

//  get scheduled downtime
    double GetScheduledDowntime();

//  get Mean Time between Assist
    double GetMTBA();

//  get Mean Time between Failure
    double GetMTBF();

//  get Mean Time between Interrupts
    double GetMTBI();

//  get MTTR
    double GetMTTR();

//  get UPH
    long GetUPH();

//  Jam Reset
    void JamReset();

//  Set jam count
    void SetJamCnt(long lJamCnt);

//  increament jam count
    void IncJamCnt(long lJamCnt);

//  get jam count
    long GetJamCnt();

//  get assist count
    long GetAssistCnt();

//  get failure count
    long GetFailureCnt();

//  Get Assist time in mins
    double GetAssistTime();

//  get failure time in mins
    double GetFailureTime();

//  save equipment substate time periodically to file
    void SaveTimeInfo();

//  get all SemiE10 timing
    StSemiE10 GetCurrentTimeInfo();

/*
   // get mtta
    LONGLONG GetMTTA();
 */

protected:
//  check if lot has been closed. If lot open, read all equipment substate time from runtime.ini
    void CheckLotClosed();

//  calculate jam count
    void CalJamCnt(StLong stLong[StSemiE10::MAX_CNT]);

//  Calculate Equipment State Time
    void CalEquipStateTime();

//  Calculate RAM Time
    void CalRamTime();

    CNonScheduledTime m_nonScheduledTime; // non scheduled time

    StSemiE10 stSemiE10;

private:
//  write jam info to file
    void WriteJamInfoToFile();

//  write RAM infomation to file
    void WriteRamInfoToFile();

//  write Equipment state time to file
    void WriteEquStateToFile();

//  read equipment timing
    void ReadEquTiming();

//  reset jam count and write to file
    void ResetJamCnt();

//  reset RAM Time and write to file
    void ResetRamTime();

//  reset RAM and write to file
    void ResetEquTime();

    double Round(double value);

    CTimer m_SaveInfoTimer;

    COperationsTime m_operationsTime;  // operations time

    long m_lJamCnt;                    // jam count
    long m_lPrevJamCnt;                // previous jam count
    long m_lAssistFailureCnt;          // assist and failure counter
    long m_lAssistCnt;                 // assist counter
    long m_lFailureCnt;                // failure counter
    long m_lPrevAssistCnt;             // previous assist counter
    long m_lPrevFailureCnt;            // previous failure counter

    double m_dPrevMtba;                // previous mtba before write to runtime.ini
    double m_dPrevMtbf;                // previous mtbf before write to runtime.ini
    double m_dPrevMtbi;                // previous mtbi before write to runtime.ini
    double m_dPrevMttr;                // previous mttr before write to runtime.ini

    double m_dPrevTolTime;             // previous Total Time
    double m_dPrevNonScheduleTime;     // previous non scheduled time
    double m_dPrevOperationsTime;      // previous operations time
    double m_dPrevUptime;              // previous uptime
    double m_dPrevEngineeringtime;     // previous Engineering Time
    double m_dPrevManufaturingTime;    // previous Manufacturing Time
    double m_dPrevProductiveTime;      // previous productive time
    double m_dPrevStandbyTime;         // previous standby time
    double m_dPrevDownTime;            // previous downtime
    double m_dPrevUnscheduledDowntime; // previous Unscheduled Downtime
    double m_dPrevScheduleTime;        // previous scheduled downtime

    long m_lPrevUPH;                   // previous uph before write to runtime.ini

    bool m_bLotOpened;                 // flag to keep track of lot open status

    LONGLONG m_llJamStartTime;         // for record jam start time
    LONGLONG m_llFailureTime;          // Failure time
    LONGLONG m_llAssistTime;           // Assist time
    LONGLONG m_llPrevFailureTime;      // previous Failure time
    LONGLONG m_llPrevAssistTime;       // previous Assist time

    double m_dFailureTime;             // jam time above this time will be failure

    CIniFile m_runtimeIni;
    bool     m_bTraceData;             // if m_bTraceData = trace data to vc output window
};

#endif // !defined(AFX_SEMIE10_H__EA674164_EC86_401C_A8B8_6AC49BE92642__INCLUDED_)
