#ifndef DEVICE_H_
#define DEVICE_H_

#include "gType.h"
#include "my_string.h"
#include "gvalue.h"
#include "record.h"
#include "ProtoC2C.h"

typedef union{
	unsigned char MemBytes[16];
	unsigned char MemD32[4];
	struct{
		unsigned char Addr;
		unsigned char CfgBits;
		unsigned short AreaEx[3];
		TUserCode	UcList[2];
	};
}TDisplayPadSetting;

typedef union{
	unsigned int D32[8];
	unsigned char D8[32];
	struct{
		unsigned int Sn;
		unsigned char dType;
		unsigned char Id;
		unsigned short Ver;
		
		unsigned int UserCode;
	}Common;
	
	struct{
		unsigned int Sn;
		unsigned char dType;
		unsigned char Id;
		unsigned short Ver;
		
		unsigned int UserCode;
		
		unsigned char OnlineCount;
		unsigned char ResistedCount;
		unsigned short Voltage;
		
		unsigned short Current;
		unsigned char Tempe;
		unsigned char MotherBoardID;
		
		unsigned char FBCZero;
		unsigned char FBCFull;
		unsigned char FBVZero;
		unsigned char FBVFULL;		
		
		unsigned char Flag;
	}Port;
	struct{
		unsigned int Sn;
		unsigned char dType;
		unsigned char Id;
		unsigned short Ver;
		
		unsigned int UserCode;
		
		unsigned char OnlineCount;
		unsigned char ResistedCount;
		unsigned short Voltage;
		
		unsigned short Current;
		unsigned short Tempe;

		unsigned char Flag;
	}HandCtl;
	struct{
		unsigned int Sn;
		unsigned char dType;
		unsigned char Id;
		unsigned short Ver;
		
		unsigned int UserCode;
		
		unsigned char OnlineCount;
		unsigned char ResistedCount;
		unsigned short Voltage;
		
		unsigned short Current;
		unsigned short Tempe;
		
		unsigned char Flag;
	}DirectCtl;
	struct{
		unsigned int Sn;
		unsigned char dType;
		unsigned char Id;
		unsigned short Ver;
		
		unsigned int UserCode;
		
		unsigned char OnlineCount;
		unsigned char ResistedCount;
		
		unsigned short Voltage;
		unsigned short Current;
		unsigned short Tempe;
		
		unsigned char PortOnlineList[8];
		unsigned char PortCarryList[8];
		
		unsigned char Flag;
	}MotherBoard;
	struct{
		unsigned int Sn;
		unsigned char dType;
		unsigned char Id;
		unsigned short Ver;
		
		unsigned int UserCode;
		
		unsigned char iLinkType;
		unsigned char oLinkType;
		
		unsigned short iLinkSpeed;
		unsigned short oLinkSpeed;

		unsigned char LinkCount;
		unsigned char EnableBit;	//0x01:En1; 0x02:En2; 0x04:En3; 0x08:En4;
		
		unsigned char Flag;
	}CommBoard;
	struct{
		unsigned int Sn;
		unsigned char dType;
		unsigned char Id;
		unsigned short Ver;
		
		unsigned int UserCode;
		
		unsigned char iLinkType;
		unsigned char oLinkType;	//0x01=CAN 0x02=RS485 0x04=Eth
		
		unsigned short iLinkSpeed;
		unsigned short oLinkSpeed;

		unsigned char LinkCount;
		unsigned char EnableBit;	//0x01:En1; 0x02:En2; 0x04:En3; 0x08:En4;
		
		unsigned char Flag;
	}MultiCommBoard;
}TRootDeviceData;

class TRootDevice{
	public:
		TRootDeviceData fData;
		float fVoltage;
		float fCurrent;
		unsigned int DymState;
	public:
		TRootDevice(void){
			unsigned int i;
			for(i=0;i<8;i++)	fData.D32[i] = 0;
		}
		void GetDescp(TMyString *StrGot);
		void SetDescp(TMyString *StrPut);
		
		void PrintProperty	(TMyString *Str0, TMyString *Str1, TMyString *Str2);		//UserCode  ID dType
		void PrintAttribute	(TMyString *Str0, TMyString *Str1);					//CarryCount  Ver
		void PrintDynamicsData	(TMyString *Str0, TMyString *Str1, TMyString *Str2, TMyString *Str3, TMyString *Str4, TMyString *Str5, TMyString *Str6, TMyString *Str7);
		void PrintAllPrm		(TMyString *StrKey[12], TMyString *StrVal[12]);
		void PrintActiveCount(TMyString *Str0);
		void PrintCarryCount(TMyString *Str0);
		void PrintPortIDnActiveCount	(TMyString *Str0, TMyString *Str1);
};

class TPort : public TRootDevice{
	public:
		static const unsigned char DisplayPadByAreaModeBit = 0x80;
		static const unsigned char DisplayPadUsingUcEx0 = 0x01;
		static const unsigned char DisplayPadUsingUcEx1 = 0x02;
		static const unsigned char DisplayPadUsingAreaEx0 = 0x04;
		static const unsigned char DisplayPadUsingAreaEx1 = 0x08;
		static const unsigned char DisplayPadUsingAreaEx2 = 0x10;
	
		static const unsigned char ModulaInputAsFeedBackBit = 0x01;
		static const unsigned char MouilaInputAsFireBit	= 0x02;
		static const unsigned char MouilaInputAsSvBit	= 0x04;
		
		static const unsigned char MouilaInOutAsFeedBackBit	= 0x01;
		static const unsigned char MouilaInOutAsFireBit		= 0x02;
		static const unsigned char MouilaInOutAsSvBit		= 0x04;
		static const unsigned char MouilaInOutAsPushDrvBit	= 0x08;
	
		static const unsigned char VaAsLinkageBit	= 0x01;
		static const unsigned char VaSoundOnBit	= 0x02;
		static const unsigned char VaVisionOnBit	= 0x04;
		static const unsigned char VaSoundTypeBits= 0xF0;
	
		static TDisplayPadSetting tmpDisplayPadSet[dDISPLAYPAD_MAX_COUNT];
	public:
		unsigned int LastUpdataTime;
		unsigned char ExistTable[256];
		unsigned char dTypeTable[256];
		unsigned char StateTable[256];
		unsigned char LnkRdyGo[256];
		unsigned char DisplayPadAddr[dDISPLAYPAD_MAX_COUNT];
		unsigned int  DisplayPadLastDayTime[dDISPLAYPAD_MAX_COUNT];
		unsigned int  DisplayPadLastYearMonth[dDISPLAYPAD_MAX_COUNT];
		unsigned char NeedFeedBackAddr[256];
		unsigned char HasSetLightOn[256];
		unsigned int DelayTick[256];
		unsigned char DelayToDoEventType[256];
		unsigned char AssignType[256];
		unsigned char FuncSetByte[256];
		TUserCode	UcList[256];
		unsigned short Area[256];
	
		unsigned int NeedFeedBackCount;
		unsigned char DisplayPadCount;
		unsigned char IsIncludePump;
		unsigned char Dump0;
		unsigned char Dump1;
		
		unsigned char SyncRegisterReqStart;
		unsigned char SyncMaskReqStart;
		unsigned char SyncRegisterTick;
		unsigned char SyncMaskTick;
		unsigned char SyncRegisterOnGo;
		unsigned char SyncMaskOnGo;
		unsigned char SyncRegisterResult;
		unsigned char SyncMaskResult;
		unsigned char SyncRegisterHasReportError;
		unsigned char SyncMaskHasReportError;
	
		unsigned int UserCodeMin;
		unsigned int UserCodeMax;
		
		TDisplayPadSetting DisplayPadSet[dDISPLAYPAD_MAX_COUNT];
	
		TCanHeaderID aIde;
		unsigned char Data[8];
	
		TRecordCommon aIm;
		
		unsigned char Num;
		
		unsigned int CrcDevType;
		unsigned int CrcAuxId;
		unsigned int CrcAssignedType;
		unsigned int CrcSignalType;
		unsigned int CrcArea;
		unsigned int CrcDisplayPad;
		unsigned int CrcRealAssignedType;
		unsigned int CrcRealSignalType;
		struct{
			unsigned char DevType;
			unsigned char AuxId;
			unsigned char AssignedType;
			unsigned char SignalType;
			unsigned char Area;
			unsigned char CrcDisplayPad;
			void Clear(){
				DevType=0;
				AuxId=0;
				AssignedType=0;
				SignalType=0;
				Area=0;
				CrcDisplayPad =0;
			}
		}CrcFlag;
		
		//UserCodeMatch	= SDRAM  dAddrSdRam_4UserCodeMatch
	public:
		TPort(void){
			unsigned int i;
			for(i=0;i<8;i++)	fData.D32[i] = 0;
			for(i=0;i<256;i++){
				StateTable[i] = 0;
				LnkRdyGo[i] =0;
				HasSetLightOn[i] =0;
				DelayToDoEventType[i] =0;
			}
			SyncRegisterReqStart =0;
			SyncMaskReqStart =0;
			SyncRegisterOnGo =0;
			SyncRegisterHasReportError =0;
			SyncMaskOnGo =0;
			SyncMaskHasReportError =0;
			CrcFlag.Clear();
		}
		void Init(void){
			unsigned int i;
			for(i=0;i<8;i++)	fData.D32[i] = 0;
			for(i=0;i<256;i++){
				StateTable[i] = 0;
				DelayTick[i] =0;
				LnkRdyGo[i] =0;
				HasSetLightOn[i] =0;
				DelayToDoEventType[i] =0;
			}
			SyncRegisterReqStart =0;
			SyncMaskReqStart =0;
			SyncRegisterOnGo =0;
			SyncRegisterHasReportError =0;
			SyncMaskOnGo =0;
			SyncMaskHasReportError =0;
			for(i=0; i<dDISPLAYPAD_MAX_COUNT; i++)DisplayPadLastDayTime[i] = 0;
			for(i=0; i<dDISPLAYPAD_MAX_COUNT; i++)DisplayPadLastYearMonth[i] = 0;
			CrcFlag.Clear();
		}
		void AssignByData(unsigned int *da);
		
		void ResetCallBack();
		
		void InterLinkGoStartDev(unsigned char aCtlNum, unsigned char aP0, unsigned char aP1, unsigned char aType);
		void AddRemoveMsgDelayStart(unsigned int IsAdd, unsigned char aP0, unsigned char aP1, unsigned char aType);
		
		void SortDisplayPad();
		void SetOneDisplayPadSetting(unsigned char *pByte);
		
		void AddRemoveMsgDelayStartSimulate(unsigned int IsAdd, unsigned char aP0, unsigned char aP1, unsigned char aType);
		
		static void sRpyRegDataSyncReqStart(class TPort *pPort);
		static void sRpyMaskDataSyncReqStart(class TPort *pPort);
		static void sSendRegDataSync(class TPort *pPort);
		static void sSnedMaskDataSync(class TPort *pPort);
		
		static void sRecallCrcFaultMsg(unsigned char aPort);
		
		void SyncTaskIntv1000Ms();
};

class THandCtlBoard : public TRootDevice{
	public:
		unsigned char KeyList[dHANDCTL_KEY_MAX_COUNT];
		unsigned char KeyHasConfig[dHANDCTL_KEY_MAX_COUNT];
		unsigned char KeyState[dHANDCTL_KEY_MAX_COUNT];
		unsigned char KeyFbRequst[dHANDCTL_KEY_MAX_COUNT];
		unsigned char KeyFbState[dHANDCTL_KEY_MAX_COUNT];
		unsigned char LnkRdyGo[dHANDCTL_KEY_MAX_COUNT];
		TUserCode	UcList[dHANDCTL_KEY_MAX_COUNT];
		unsigned char AssignType[dHANDCTL_KEY_MAX_COUNT];
		unsigned char IsMatchAreaAssign[dHANDCTL_KEY_MAX_COUNT];
		unsigned short Area[dHANDCTL_KEY_MAX_COUNT];
		unsigned char tData[8];
	
		unsigned char PadHasConfig;
		unsigned char PadFbRequst;
		unsigned char KeyCount;
		unsigned char PadIsProhibit;
		unsigned int Crc;
	
		unsigned char CrcFlag;
	
		TUserCodeStr UcListNotAnd[dHANDCTL_KEY_MAX_COUNT];
	public:
		THandCtlBoard(void){
			unsigned int i;
			for(i=0;i<8;i++)  fData.D32[i]=0;
			CrcFlag =0;
		}
		void Init(void){
			unsigned int i;
			PadIsProhibit = 1;
			PadHasConfig =0;
			PadFbRequst =0;
			for(i=0;i<8;i++)  fData.D32[i]=0;
			for(i=0; i<dHANDCTL_KEY_MAX_COUNT; i++){
				KeyList[i] = 0;
				KeyHasConfig[i] =0;
				KeyState[i] =0;
				KeyFbRequst[i] =0;
				KeyFbState[i] =0;
			}
			for(i=0; i<dHANDCTL_KEY_MAX_COUNT; i++){
				KeyList[i] = 0xA5;
			}
			CrcFlag =0;
		}
		void AssignByData(unsigned int *da);
		void SetOutputStateViaUserCode(unsigned char aPadNum, unsigned char aKeyNum, unsigned char aDType, unsigned char aCtlNum, unsigned int aDoStop);
		void SetOutputStateViaAreaNum(unsigned char aPadNum, unsigned char aKeyNum, unsigned char aDType, unsigned char aCtlNum, unsigned int aDoStop);
		void KeyAction(unsigned char aCtlNum, unsigned char aPadNum, unsigned char aKeyNum);
		void KeyPress(unsigned char aCtlNum, unsigned char aPadNum, unsigned char aKeyNum);
		void CheckKeyLnkRdyGo(unsigned char aCtlNum, unsigned char aPadNum);
		void CheckKeyState(unsigned char aCtlNum, unsigned char aPadNum);
		void SendPadCfg2Pad(unsigned char aPadNum);
		
		void ResetCallBack();
		static void sRecallCrcFaultMsg(unsigned char aPad);
};

class TDirectCtlBoard : public TRootDevice{
	public:
		unsigned char StartDelay[32];
		unsigned char OutputState[32];
		unsigned char FeedbackState[32];
		unsigned char FaultState[32];
		unsigned char LnkRdyGo[32];
		unsigned char LnkRdyGoOld[32];
		unsigned char FeedBackMiss[32];

		unsigned char IsRegisted[32];
		unsigned char IsCheckError[32];
		unsigned char IsPump[32];
		unsigned char IsProhibitLinkage[32];
		unsigned char IsOutputState[32];
	
		unsigned char PadHasConfig;
		unsigned char PadIsProhibit;
		unsigned char IsIncludePump;
		unsigned char ManualAllow;
		unsigned char NodeCount;
	
		unsigned char DelayToDoEventType[32];
		unsigned int DelayTick[32];
	
		unsigned int UserCodeMin;
		unsigned int UserCodeMax;
	
		TUserCode	UcList[32];
		TUserCodeStr UcFastMatch[32];
		unsigned short Area[32];
		unsigned char AssignType[32];
		
		union{
			unsigned int D32[2];
			unsigned char D8[8];
		}tData;
		
		TC2CBufBody	aC2cNetBuf;
		
		TRecordCommon aIm;
		static TRecordCommon sIm;
		
		unsigned int CrcConfig;
		unsigned int CrcAuxId;
		unsigned int CrcAssignedType;
		unsigned int CrcArea;
		
		struct{
			unsigned char Config;
			unsigned char AuxId;
			unsigned char AssignedType;
			unsigned char Area;
			void Clear(){
				Config =0;
				AuxId =0;
				AssignedType =0;
				Area =0;
			}
		}CrcFlag;
	public:
		TDirectCtlBoard(void){
			unsigned int i;
			for(i=0;i<8;i++)  fData.D32[i]=0;
			CrcFlag.Clear();
		}
		void Init(void){
			unsigned int i;
			for(i=0;i<8;i++)  fData.D32[i]=0;
			CrcFlag.Clear();
		}
		void AssignByData(unsigned int *da);
		void aImAddDateTime();
		void dUploadAllowState(unsigned char aCtlNum, unsigned char aP0, unsigned char aSta);
		void dUploadNodeState(unsigned char aCtlNum, unsigned char aP0, unsigned char aNodeNum, unsigned char aSta);
		void Request(unsigned char aCtlNum, unsigned char aCmd, unsigned char *Data);
		void Send2MasterMachineEventTime();
		static void sForceCheckHydrantKpAction();
		static void sNwPutLnkState(unsigned char aCmd, unsigned char *Data);
		
		void SendPadCfg2Pad(unsigned char aPadNum);
		void ResetCallBack();
		
		void InterLinkGoStartNode(unsigned char aCtlNum, unsigned char aP0, unsigned char aP1);
		void AddRemoveMsgDelayStart(unsigned int IsAdd, unsigned char aP0, unsigned char aP1);
		
		void GetUserCodeAreaNumDevType(unsigned char aP0, unsigned char aP1, unsigned int *pUc, unsigned short *pAreaNum, unsigned char *pDType);
		
		void AddRemoveMsgDelayStartSimulate(unsigned int IsAdd, unsigned char aP0, unsigned char aP1);
		void AddStartStopSimulate(unsigned int IsStart, unsigned char aP0, unsigned char aP1);
		
		static void sRecallCrcFaultMsg(unsigned char aPad);
};

class TMotherBoard : public TRootDevice{
	public:
		unsigned int Exist[8];
		unsigned int OffLine[8];
		unsigned int NoReportTick[8];
		unsigned int ErrorHasReport[8];
		unsigned int LastUpdataTime;
		struct{
			unsigned char dType;
			unsigned char Id;
		}DevList[8];
		unsigned char ReqQue;
		unsigned char Flag;
	public:
		TMotherBoard(void){
			unsigned int i;
			for(i=0;i<8;i++){
				Exist[i]=0;
				OffLine[i] =0;
				NoReportTick[i] =0;
				ErrorHasReport[i] =0;
			}
		}
		void Init(void){
			unsigned int i;
			for(i=0;i<8;i++){
				Exist[i]=0;
				OffLine[i] =0;
				NoReportTick[i] =0;
				ErrorHasReport[i] =0;
			}
		}
		void AssignByData(unsigned int *da);
		void CheckOnlineTask();
		void ResetCallBack();
};

class TCommBoard : public TRootDevice{
	public:
		unsigned int LastUpdataTime;
		unsigned char ReqQue;
		unsigned char Flag;
	public:
		TCommBoard(void){
			unsigned int i;
			for(i=0;i<4;i++)  fData.D32[i]=0;
		}
		void Init(void){
			unsigned int i;
			for(i=0;i<8;i++)  fData.D32[i]=0;
		}
		void AssignByData(unsigned int *da);	
};

class TDevPrinter{
	public:
		int sOffline;
		int sNoPaper;
	
	public:
		TDevPrinter(){
			sOffline = 0;
			sNoPaper = 0;
		}
		void Init(){
			sOffline = 0;
			sNoPaper = 0;
		}
		void AddRemoveMsgFaultOffline(int IsAdd);
		void AddRemoveMsgFaultNoPaper(int IsAdd);
		
		void SetState(int aOffline, int aNoPaper);
		void Task1000Ms();
};

class TGeneralDev{
	public:
		unsigned int LcdCountDownTick;
		unsigned int LcdScreenSaving;
	public:
		TGeneralDev(){}
		void Init(){
			LcdCountDownTick = 100;
			LcdScreenSaving = 0;
		}
		void LcdBlackLightSetOnStartUp();
		void LcdBlackLightSetOn();
		void LcdBlackLightSetOff();
		void Task1000Ms();
};


#endif


