#ifndef SECSMANAGER_H
#define SECSMANAGER_H

#include "Group.h"

#include <QMutex>
#include <set>
#include "logic_eap.h"
class SecsManager
{
public:
	static SecsManager& getInstance();
    enum SecsAlarm{
		UndefineAlarm=40000,
        AxisError=40001,
        RobotError ,
        LaserError ,
        StripError ,
        GasError   ,
        VaccumError,
        CameraError,
    };

public:
    SecsManager();
	~SecsManager();
public:
    int InitConnect();
	void UninitConnect();

	bool SendEvent(_EventID eventID);
	bool SendAlarm(_ALAM_ID alarmID);
	bool SendAlarmClear(_ALAM_ID alarmID);
	void ClearAllAlarm();
    bool SwitchCommEnable(bool enable);
    bool SwitchCtrlMode(CONTROL_MODE mode);
	bool SetVariable(_SVID svid, const std::string newValue);
	bool OnSetConstVariable(_ECID ecid, const std::string newValue);

	std::string GetCommStateStr(COMM_STATE State);
    std::string GetCtrlStateStr(CONTROL_STATE State);
	//收到终端消息
	void OnReciveTerminalMsg(std::string s);
	//HOST获取变量值
	const char* SecsVarValue(int id);
	//HOST获取常量值
	const char* SecsVarConstValue(int cId);
	//HOST获取Recipe参数值
	const char* SecsPPFormatValue(PP_FORMAT_CMD pp_cmd, const char* pCmd);

    void setCurCommState(const COMM_STATE &CurCommState);
    void setCurCtrlState(const CONTROL_STATE &CurCtrlState);
    int getControlMode();
	bool getCommEnable();

	CONTROL_STATE getCurCtrlState() const;
    COMM_STATE getCurCommState() const;
private:
    // SECS
    CSecsEquip* m_pSecs;

	//当前的通信状态
    COMM_STATE                  m_CurCommState;
	//当前的控制状态
	CONTROL_STATE              m_CurCtrlState;
	QMutex m_Mutex;
    std::set<int> m_TriggerAlarm;
};

extern SecsManager g_SecsManager;

// 状态改变
void OnStateChangeProc(CSecsEquip*, const char* varname, int nState);

// 远程
REMOTE_REPLY OnRemoteProc(CSecsEquip* pSH, REMOTE_DATA* rd);
void OnRemoteProcEx(CSecsEquip* pSH, std::string pReplyMailbox, REMOTE_DATA rd);

// 消息
TERMINAL_REPLY OnTerminalProc(CSecsEquip* pSH, TERMINAL_DATA td);

// 常量改变
void OnConstantChangeProc(CSecsEquip* pSH, int nID, const char* value);

// 变量数据
const char* OnVarValueProc(CSecsEquip* pSH, int var);
// CONST 变量数据
const char* OnConstVarValueProc(CSecsEquip* pSH, int var);

// 时间
void OnHostClockProc(CSecsEquip*, const char* second_offset, SYSTEMTIME time);

// PP格式化数据,获取值
void OnPPFormatValueProc(CSecsEquip*, PP_FORMAT_HANDLE* pPpFormat);

// PP列表
std::list<std::string> OnPPNameListProc(CSecsEquip*);

// PP数据获取
// 数据需要释放
// 不会出现多线程同时调用该函数
void OnSecsPPEventProc(CSecsEquip*, PP_HANDLE_DATA* pPPHandleData);

#endif // SECSMANAGER_H
