// GPVision.h: interface for the CGPVision class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_GPVISION_H__4C9E405C_B8DB_4228_9B5A_6C4C0C5EC7B2__INCLUDED_)
#define AFX_GPVISION_H__4C9E405C_B8DB_4228_9B5A_6C4C0C5EC7B2__INCLUDED_

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

#include "VisionNW.h"
#include "Utility.h"

#ifdef  BUILD_COMMDLL
#define     COMMDLL_DLLCLASS    __declspec(dllexport)
#else
#define     COMMDLL_DLLCLASS    __declspec(dllimport)
#endif

#define     END_TERMINATOR  '\r'

#define MAX_GP_STATION  20

enum NEXT_SOT
{
    REF_AUTO_CALIBRATION=2,
    RESOLUTION_AUTO_CALIBRATION,
    MANUAL_TEACH,
    INSPECTION,
};

enum TYPE_ID
{
    TYPE1 =1,
    TYPE2,
    TYPE3,
    TYPE4,
    TYPE5,
    TYPE6,
    TYPE7,
};

struct stGPDoubleDataResult
{
    double dResult1;
    double dResult2;
    double dResult3;
    double dResult4;
};

class CScanner;

class COMMDLL_DLLCLASS CGPVision : public CVisionNW
{
public:
// Gary 15N0v11, V1.12.01
// feature to enable/disable multiple app display. Used along with SwitchAppWindow
    bool MultipleAppWindow(bool bEnable, DWORD dwTimeout = 5000);

    bool    SwitchAppWindow(int nStationNo, DWORD dwTimeout = 5000);
    bool    IsLotOpen();
    bool    WaitInTeach(int nStationNo, DWORD dwTimeout);
    bool    WaitResultReady(int nStationNo, DWORD dwTimeout);
    bool    WaitImageTaken(int nStationNo, DWORD dwTimeout);
    bool    CameraLiveCapture(int nStationNo, bool bOn = true);
    bool    IsTeachingValid(int nStationNo, int nTypeID);
    bool    GetResult(int nStationNo, CString &csResult);
    CString GetReceiveData();
    bool    Send(CString csMsg);
    bool    IsConnectedToServer();
    void    Disconnect();
    bool    Connect(int nPortNo, CString csIPAddress, bool bServer = true);
// static void StaticReceiveComm(const char *cpcBuffer, int nSize);
    void StaticReceiveComm(const char *cpcBuffer, int nSize);
    bool SetShiftID(CString csShiftID);
    bool SetNetUPH(long lNetUPH);
    bool SetGrossUPH(long lGrossUPH);
    bool SetMachineName(CString csName);
    bool SetOperatorID(CString csOperatorID);
    bool SetLotSize(long lLotSize);
    bool IsTeach(int nStationNo);
    bool IsRun();
    bool IsAck();
    bool Stop();
    bool Run();
    bool DataEnd();
    bool DataStart();
    bool SetLotNumber(CString csLotNumber);
    bool SetPackageID(long lPackageID);
    bool GetResult(int nStationNo, stGPDoubleDataResult &st_dResult);
    bool CheckResultReady(int nStationNo);
    bool CloseLot();
    bool OpenLot(int nPackageID, DWORD dwTimeout = 5000);
    bool CheckImageTaken(int nStationNo);
    bool StartInspect(int nStationNo, int nNextSOT = 0, int nTypeID = 0);
    CGPVision();
    virtual ~CGPVision();

    virtual void ReceiveComm(const char *cpcBuffer, int nSize);

    HANDLE GetResultReadyHandle(int nStationNo);
    HANDLE GetImageTakenHandle(int nStationNo);

// private:
protected:
    bool SendCommand(CString csCommand, bool bWaitAck = false, DWORD dwTimeout = 5000);
    bool GetStatus();
    bool SendMsg(CString csMsg, DWORD dwTimeout, bool bWaitAck = false);

    HANDLE               m_hImageTaken[MAX_GP_STATION];
    HANDLE               m_hInTeach[MAX_GP_STATION];
    CMutex               m_GPVisionMutex;
    HANDLE               m_hAck;
    HANDLE               m_hStatusReady;
    HANDLE               m_hResultReady[MAX_GP_STATION];
    HANDLE               m_hTeachValidReady[MAX_GP_STATION];
    stGPDoubleDataResult m_stResult[MAX_GP_STATION];
    CStringArray         m_csaResult;

    bool m_bRunMode;
    char m_cReceiveData[MAXSOCKBUF];
    bool m_bTeachingValid;
    bool m_bLotOpened;
    char m_cRcvData[MAXSOCKBUF];

    static bool m_bInitBefore;
};

#endif // !defined(AFX_GPVISION_H__4C9E405C_B8DB_4228_9B5A_6C4C0C5EC7B2__INCLUDED_)
