#ifndef EXRECORD_H_
#define EXRECORD_H_

#include "gType.h"
#include "TDateTime.h"
#include "ProtoFecBus.h"
#include "record.h"

#define EXRECORDBUFDEEP 	1024
//#define dExRecordCmdIdf		0xB7
//#define dExRecordReport		0xA6

typedef enum
{
	exNoCmd							= (unsigned char)0x00,//没有
	
	exHeartBeat					= (unsigned char)0x20,//心跳回复	
	
	exGetModle					= (unsigned char)0x40,//读取型号	
	exGetVer						= (unsigned char)0x50,//读取版本	
	exSetCtlMsg 				= (unsigned char)0x51,//写入连接控制器信息	
	exGetCtlMsg 				= (unsigned char)0x52,//读取连接控制器信息	
	exGetErrState 			= (unsigned char)0x53,//查询存储器故障状态	
		
	exGetTotalRecordNum = (unsigned char)0x60,//读取总信息数量	
	exWriteRecord 			= (unsigned char)0x61,//写入信息	
	exReadRecordOrder 	= (unsigned char)0x62,//读取第几条信息	
	exClearRecord 			= (unsigned char)0x63,//清空信息
}exCmdTypeDef;

typedef enum
{
	exErrCom						= (unsigned char)0x01,//通讯故障	离线
	exErrSave						= (unsigned char)0x02,//存储器故障
	exErrComSave				= (unsigned char)0x04,//记录通讯故障
}exErrTypeDef;

typedef union{
	unsigned int D32[10];
	unsigned char D8[40];
	struct{
		unsigned char Cmd;
		unsigned char Len;
		//unsigned short Order;
		
		unsigned char Data[38];
	}DS;
}TMemComBufBody;

typedef union{
	unsigned int D32[8];
	unsigned char D8[32];
	struct{
		unsigned char MsgType ;//信息类型
		unsigned char CtlAddr;
    unsigned short CtlType;
		
		unsigned char Pid[12];
		
		unsigned char CtlNum ;//控制器编号（1字节）
		unsigned char UnitNum ;//单元编号（1字节）
		unsigned char DeviceNum ; //设备编号（1字节）
		unsigned char ChannelNum ; //通道编号（1字节）
		
		unsigned short DeviceCode ; //设备类型代码（2字节）
		unsigned short EventCode ; //事件代码（2字节）
		
		TFecStateCode StateCode ; //状态代码（2字节）
		unsigned char Year ;//年（1字节）
		unsigned char Mon;//月（1字节）
		
		unsigned char Day;//日（1字节）
		unsigned char Hour;//时（1字节）
		unsigned char Min;//分（1字节）
		unsigned char Sec;//秒（1字节）
	}Data;
}TMemBufBody;

class TexRecord{
	public:
		unsigned int RxGetFlag;
		TMemComBufBody RxMsg;
	
		unsigned int TxSetFlag;
		TMemComBufBody TxMsg;
	
		unsigned int GetVerFlag;
		unsigned int GetModleFlag;
	
		unsigned int InRecordIndex;
		unsigned int OutRecordIndex;
		TMemBufBody ExRecordBufTemp;
		TMemBufBody ExRecordBuf[EXRECORDBUFDEEP];
	
		struct{
			unsigned char State;
			unsigned char AckOk;
			unsigned char OnGoing;
			unsigned char Order;
			unsigned char RetryTimes;
			unsigned char RetryFLAG;
			unsigned short TimeOutTick;
		}OnTransmite;
		
		union{
			unsigned int D32;
			struct{
				unsigned char OffLine;
				unsigned char ReadWriteFail;
				unsigned char Rsv0;
				unsigned char Rsv1;
			}Body;
		}ErrState, ErrReport;
		
		unsigned int OnlineTick;
		unsigned int ReadyFlag;
	
	public:
		TexRecord(){};
		void Init();
		
		void getExRecordTime(TRecordCommon aRC);
		void getExRecordStateCode(unsigned short StateCode);
		void setExRecordStateCode(unsigned short StateCode);
			
			
		void SetStateCodeAll();
		void SetStateCodeAutoManual();
		void SetStateCodePsOnActive();
		void SetStateCodePsError();
		void SetStateCodeFire();
		void SetStateCodeStart();
		void SetStateCodeFeedBack();
		void SetStateCodeSv();	
		void SetStateCodeFault();	
		void SetStateCodeMask();			
			
		void getExRecordDeviceCode(unsigned char dType);
			
		void getExRecordEpFaultEvent(unsigned char aType, unsigned char State);
			
		void getExRecordFaultBrdDeviceCode(unsigned char aType);
		void getExRecordFaultBrdEventCode(unsigned char aType, unsigned char State);
		void getExRecordFaultDevDeviceCode(unsigned char aType);
		void getExRecordFaultDevEventCode(unsigned char aType, unsigned char State);
			
		void oBufInsert();
		void oBufInsert(TMemComBufBody aNsBuf);
		void iBufInsert();
		void iBufProcess();
		void oBufProcess();
			
		void iRecordBufInsert(unsigned char * pdata);
		
		void iExRecordFireInsert(TRecordCommon aRC);
		void iExRecordFaultInsert(TRecordCommon aRC);
		
		void iExRecordLinkageInsert(TRecordCommon aRC);
		void iExRecordOperateInsert(TRecordCommon aRC);
		void iExRecordSvInsert(TRecordCommon aRC);
			
		void oCmdInsert();
		
		void Task10Ms();
		void Task1000Ms();
};























#endif


