// RSLaser.h: interface for the CRSLaser class.
//
//////////////////////////////////////////////////////////////////////
#pragma once

#include <string>

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

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

enum LOT_TYPE
{
    LOT,
    LOT2,
    LOT3
};

enum
{
    LASER_MARK_TIMEOUT,
    LASER_MARK_COMPLETE,
    LASER_MARK_IN_PROGRESS,
    LASER_MARK_ALARM,
};

class COMMDLL_DLLCLASS CRSLaser : public CVisionNW
{
public:
    HANDLE       GetMarkingInProgressHandle();
    HANDLE       GetAlarmHandle();
    int          WaitMarkInProgress(DWORD dwTimeout);
    int          IsAlarm(DWORD dwTimeout);
    int          WaitMarkComplete(DWORD dwTimeout);
    std::string  GetReceiveData();
    bool         Send(std::string strMsg);
    bool         MeasurePower(DWORD dwTimeout);
    virtual void ReceiveComm(const char *cpcBuffer, int nSize);
    bool         SetSetting(std::string strSetting, int nRequired, DWORD dwTimeout);
    bool         SetDeviceID(int nOriginLocation, std::string strDeviceIDData, DWORD dwTimeout);
    bool         SetBin(int nOriginLocation, std::string strBinData, DWORD dwTimeout);
    bool         SetMatrixPosition(double dX, double dY, double dAngle, DWORD dwTimeout);
    bool         SetPositionAndOccupation(std::string strMsg, DWORD dwTimeout);
    bool         SetPart(int nPartNo, DWORD dwTimeout);
    bool         SetOccupation(std::string strMsg, DWORD dwTimeout);
    double       GetPowerValue(DWORD dwTimeout);
    int          IsPowerAutoMode(DWORD dwTimeout);
    int          IsPowerOK(DWORD dwTimeout);
    std::string  GetLayoutFile(DWORD dwTimeout);
    std::string  GetLotID(DWORD dwTimeout);
    bool         GetAlarm(int &nAlarmNo, std::string &csAlarmMsg, DWORD dwTimeout);
    int          IsMarkingEnd(DWORD dwTimeout);
    int          IsJobEnd(DWORD dwTimeout);
    int          IsSystemReady(DWORD dwTimeout);
    bool         StopMark();
    bool         StartMark();
    bool         CancelJob();
    bool         LoadJob();
    bool         SetVariable(std::string strVariableName, std::string csData, DWORD dwTimeout);
    bool         SetLotFile(std::string strLotFilename, DWORD dwTimeout);
    bool         SetLayout(std::string csLotNo, long lLotQty, std::string csLayoutFilename, std::string csVariableData, int nLotType, DWORD dwTimeout);
    bool         Connect(int nPortNo, std::string csIPAddress, bool bServer = TRUE);
    void         Disconnect();
    bool         IsClientConnected();
    bool         IsServerActive();
    bool         IsConnectedToServer();
    CRSLaser();
    virtual ~CRSLaser();

    HANDLE GetMarkingEndHandle();

protected:
    bool GetPowerStatus(DWORD dwTimeout);
    bool GetNextData(char *Token, char** context);
    bool SendMsg(std::string csMsg, DWORD dwTimeout);
    bool DecodeStatus(char *Token, char** context);
    bool GetStatus(std::string csStatusName, DWORD dwTimeout);

    HANDLE m_hAck;
    HANDLE m_hNack;
    HANDLE m_hStatusReady;
    HANDLE m_hSystemReady;
    HANDLE m_hJobEnd;
    HANDLE m_hMarkingEnd;
    HANDLE m_hMarkingInProgress;
    HANDLE m_hPowerStatusReady;
    HANDLE m_hAlarm;

    bool    m_bAlarm;
    std::string m_strAlarmMsg;
    int     m_nAlarmNo;

    std::string m_strLotID;
    std::string m_strLayoutFilename;
    double  m_dPower;
    bool    m_bPowerOK;
    bool    m_nPowerAutoMode;

    char m_cReceiveData[4096];
    bool m_bWaitForTerminator2;

private:
    int GetMsgCase(std::string csMsg);
};


