#ifndef CONTROLCENTER_H_
#define CONTROLCENTER_H_

#include "gvalue.h"
#include "ProtoInnerCan.h"
#include "record.h"
#include "KeyPad.h"
#include "ProtoC2C.h"

#define ieMSG_FIRE_MAX_COUNT		10000
#define ieMSG_LINKAGE_MAX_COUNT	10000
#define ieMSG_SV_MAX_COUNT		1000
#define ieMSG_FAULT_MAX_COUNT		10000
#define ieMSG_MASK_MAX_COUNT		10000

#define dLINK_INPUT_MAX_COUNT	6000

#define dAddrSdRam_MsgFire 	0xC3C00000
#define dAddrSdRam_MsgLinkage 0xC3C3C000
#define dAddrSdRam_MsgSv 	0xC3C78000
#define dAddrSdRam_MsgFault 	0xC3C7E000
#define dAddrSdRam_MsgMask 	0xC3CBA000

#define dAddrSdRam_DisplayPadBuf	0xC3900000
#define dDiplayPadCtlBufMaxCount	100000

#define dDevFaultNormal			0x01
#define dDevFaultCircuit		0x02
#define dDevFaultBusPad			0x03
#define dDevFaultDirectPad		0x04
#define dDevFaultStroge			0x05
#define dDevFaultExStroge		0x06
#define dDevFaultPowerSuply		0x07

#define dDevFaultPowerSuplyComm	0x08
#define dDevFaultPowerBk		0x09
#define dDevFaultBat			0x0A
#define dDevFaultCommEth		0x0B
#define dDevFaultCommFNet		0x0C
#define dDevFaultCommGraphic		0x0D
#define dDevFaultSys			0x0E

typedef enum{
	ioNone = 0,
	io
}TInnerOperatingCode;

/*typedef union{
	unsigned int D32[4];
	unsigned short D16[8];
	unsigned char D8[16];
	struct{
		unsigned char Second;
		unsigned char Minute;
		unsigned char Hour;	
		unsigned char Day;
		
		unsigned char Month;
		unsigned char Year;
		unsigned char iByte0;		
		unsigned char iByte1;
		
		unsigned char PCtlNum;
		unsigned char P0;
		unsigned char P1;
		unsigned char P2;
		
		unsigned char mType;
		unsigned char dType;
		unsigned char Data0;
		unsigned char Data1;
	}Splite;
}TRecordCommon;*/
 
typedef union{
	unsigned int D32[6];
	unsigned char D8[24];
	struct{
		unsigned char Second;
		unsigned char Minute;
		unsigned char Hour;	
		unsigned char Day;
		
		unsigned char Month;
		unsigned char Year;
		unsigned char iByte0;
		unsigned char State;
		
		unsigned char PCtlNum;
		unsigned char P0;
		unsigned char P1;
		unsigned char P2;
		
		unsigned char mType;
		unsigned char dType;
		unsigned char Data0;
		unsigned char Data1;
		
		unsigned int UserCode;
		unsigned short AreaNum;
		unsigned short DumpShort;
	}Splite;	
}TEventMsg;


typedef struct{
	int Count;
	int WriteIndex;
	int ReadIndex;
	union{
		struct{
			unsigned char P0;
			unsigned char P1;
			unsigned char Dump0;
			unsigned char Dump1;
			int SourceInx;
		}Body;
		unsigned int D32[2];
	}BodyTmp;
}TDPOutputBuf;

typedef struct{
	unsigned int Ava;
	unsigned int Len;
	unsigned char Content[32];
}TPassWord;


class TControlCenter{
	public:
		static const unsigned char LinkInputTypeFire = 1;
		static const unsigned char LinkInputTypeSv = 2;
		static const unsigned char LinkInputTypeHydrant = 3;	
		static const unsigned char LinkInputTypeAvaEdge = 10;
		static const unsigned char LinkInputTypeFireSimulate =11; 
		static const unsigned char LinkInputTypeSvSimulate = 12;
		static const unsigned char LinkInputTypeHydrantSimulate = 13;	
	
	
		static const unsigned char StartTypeSimulate = 1;	
	
		unsigned int ieIsNeedMsgSVCheckCancle;
		unsigned int IsMsgSVOldestOverride;

		short ieRearrangeIndexSv;
		short ieIndexMsgSVOldest;
	
		short ieIndexMsgSV;
		short ieIndexMsgFault;
		short ieIndexMsgStart;
		short ieIndexMsgFeedBack;
	
		unsigned int FeedBackMissCount;
	
		unsigned int IsMsgReseting;
	
		unsigned int ieGot;
		unsigned int SysFreeTick1Ms;
		unsigned int BatShowOrder;
	
		TSys_State_Struct cState;
		
		struct{
			int Count;
			int ActualCount;
			unsigned int Lock;
			unsigned int RequestRead;
			int WriteIndex;
			int ReflashIndex;
			unsigned int isMore;
		}ieMsgFire;
		struct{
			int SatrtHasBennActive;
			int StartCount;
			int MissingCount;
			int FbCount;
			int PumpFbCount;
			int PumpFbLocalCount;
			int VaCount;
			int VaCountOld;
			int BroadcastCount;
			int BroadcastCountOld;
			int IsBroadcastAndVa;
			int Count;
			int HydrantPadCount;
			int HydrantPadCountOld;
			int HydrantPadNewActive;
			int DelayCount;
			int DelayCountOld;
			int StopCount;
			unsigned int SdAddr;
			unsigned int Lock;
			unsigned int RequestRead;
			int ReflashIndex;
			unsigned int isMore;
		}ieMsgLinkage;
		struct{
			int Count;
			TEventMsg Body[ieMSG_SV_MAX_COUNT];
			unsigned int Lock;
			unsigned int RequestRead;
			int ReflashIndex;
		}ieMsgSv;
		struct{
			int Count;
			int VaCount;
			unsigned int SdAddr;
			unsigned int Lock;
			unsigned int RequestRead;
			int ReflashIndex;
		}ieMsgFault;
		struct{
			int Count;
			unsigned int Lock;
			unsigned int RequestRead;
			int ReflashIndex;
			unsigned int isMore;
		}ieMsgMask;
		
		TRecordMaskMsg MaskMsg;
		TRecordCommon aImForceConvert;
		
		TEventMsg MsgReadTmp;
		//TInnerEventMsg MsgWriteTmp;
		TEventMsg MsgExtGet;
		
		
		int BufFireCount;
		int BufLinkageCount;
		int BufSvCount;
		int BufFaultCount;
		unsigned char IsSendResetToPad;
		unsigned char IsSendSelfCheckToPad;
		
		union{
			unsigned char D8[256];
			unsigned int D32[64];
		}ReadWriteData;
		
		int DisplayPadPrcFireIndex;
		TDPOutputBuf	DisplayPadBuf;
		TEventMsg 		DisplayPadMsg;
		struct{
			unsigned char CtlNum;
			unsigned char P0;
			unsigned char P1;
			unsigned char dType;
		}DisplayPadTriggerSource;
		struct{
			unsigned char P0;
			unsigned char P1;
		}DisplayPadPath;
		TDMix DisplayDayTime;
		TDMix DisplayYearMonth;
		unsigned char tData[8];
		unsigned int DisplaySendingWhat;
		
		unsigned int InspecTick;
		unsigned char OnlineBusPadCount;
		unsigned char OnlineBusPad[dHANDCTL_MAX_COUNT];
		unsigned char OnlineDirectPadCount;
		unsigned char OnlineDirectPad[dHANDCTL_MAX_COUNT];
		unsigned char hydrantKpFbHasSend;
		
		TC2CBufBody	NetSendBuf;
		union{
			unsigned int D32[8];
			unsigned char D8[32];
		}DescpBuf;
		
		TPassWord	PassWordII;
		TPassWord	PassWordIII;
		TPassWord	PassWordTemp;
		
		unsigned int PsBatUpDateTick;
		
		struct {
			unsigned int Count;
			unsigned int uCode[dLINK_INPUT_MAX_COUNT];
			unsigned short AreaNum[dLINK_INPUT_MAX_COUNT];
			unsigned char dType[dLINK_INPUT_MAX_COUNT];
			unsigned char AssignType[dLINK_INPUT_MAX_COUNT];
			unsigned char eType[dLINK_INPUT_MAX_COUNT];
		}FiredUserCode;
		unsigned int CrcMask;
		struct{
			unsigned char Mask;
			unsigned char Dump0;
			unsigned char Dump1;
			unsigned char Dump2;
			void Clear(){
				Mask =0;
				Dump0 =0;
				Dump1 =0;
				Dump2 =0;
			}
		}CrcFlag;
	public:
		TControlCenter(){CrcFlag.Clear();}
		void Init();
		void MsgReset();
		void DelayMs(unsigned int aMs);
		
		void FireGetViaPath(unsigned int aIndex);
		void FireMsgClearOne(unsigned int aIndex);
		void FireMsgCopyOne(unsigned int aIndex);
		void FireMsgExtGet(unsigned int aIndex);
		static void sFireGetViaIndex(unsigned int aIndex, TEventMsg *aMsg);
		
		void iePushOneMsgFire(TRecordCommon aIm, unsigned int auC);
		int iePushOneMsgFireSimulate(TRecordCommon aIm, unsigned int auC);
		
		void LinkageGetViaPath(unsigned int aIndex);
		void LinkageMsgClearOne(unsigned int aIndex);
		void LinkageMsgCopyOne(unsigned int aIndex);
		void LinkageMsgExtGet(unsigned int aIndex);
		static void sLinkageGetViaIndex(unsigned int aIndex, TEventMsg *aMsg);
			
		void SvGetViaPath(unsigned int aIndex);
		void SvMsgClearOne(unsigned int aIndex);
		void SvMsgCopyOne(unsigned int aIndex);
		void SvMsgExtGet(unsigned int aIndex);
		static void sSvGetViaIndex(unsigned int aIndex, TEventMsg *aMsg);
			
		void FaultGetViaPath(unsigned int aIndex);
		void FaultMsgClearOne(unsigned int aIndex);
		void FaultMsgCopyOne(unsigned int aIndex);
		void FaultMsgExtGet(unsigned int aIndex);
		static void sFaultGetViaIndex(unsigned int aIndex, TEventMsg *aMsg);
		
		static void sMaskGetViaIndex(unsigned int aIndex, TEventMsg *aMsg);
		
		void LoadCurrentDateTime2RecordCommon(TRecordCommon *pIm);
		void iePushOneMsgLinkageStart		(TRecordCommon aIm);
		void iePushOneMsgLinkageStartCancel	(TRecordCommon aIm);
		void iePushOneMsgLinkageFb		(TRecordCommon aIm);
		void iePushOneMsgLinkageFbCancel	(TRecordCommon aIm);
		
		void ieManualPushOneMsgLinkageStart		(TRecordCommon aIm);
		void ieManualPushOneMsgLinkageStartCancel	(TRecordCommon aIm);
		void ieManualPushOneMsgLinkageFb		(TRecordCommon aIm);
		void ieManualPushOneMsgLinkageFbCancel	(TRecordCommon aIm);
		
		int iePushOneMsgLinkageStartSimulate(TRecordCommon aIm);
		int iePushOneMsgLinkageStartCancelSimulate(TRecordCommon aIm);
		int iePushOneMsgLinkageFbSimulate(TRecordCommon aIm);
		
		void ieMsgLinkageStartDelayUpdate(unsigned int aFullPath, unsigned char aNewTime);
		
		void iePushOneMsgFault(TRecordCommon aIm);
		void iePushOneMsgFaultCancel(TRecordCommon aIm);
		
		unsigned int ieFireMsgConfAddr(unsigned int FullPath);
		void ieLinkageMsgStartConfAddr(unsigned int FullPath);
		void ieLinkageMsgFbConfAddr(unsigned int FullPath);
		unsigned int ieFaultMsgFaultConfAddr(unsigned int FullPath);
		
		void iePushOneMsgSv(TRecordCommon aIm);
		void iePushOneMsgSvCancel(TRecordCommon aIm);
		int iePushOneMsgSvSimulate(TRecordCommon aIm);
		
		void iePushOneMsgMask		(TRecordCommon aIm);
		void iePushOneMsgMaskCancel	(TRecordCommon aIm);
		
		void iePushOneMsg(TRecordCommon aIm, unsigned char aCmd);
		void iePushOneRecord(TRecordCommon aIm, unsigned char aCmd);
		
		void ieManageMsgFire(void);
		void ieManageMsgSv(void);
		void PushInspecRpy(unsigned char Path0, unsigned char Prm);
		
		void DeviceRequest(unsigned char aCmd, unsigned char aReq, unsigned char aPrm);
		
		void InspecDataCheck();
		void InspecTask();
		void TaskFreeRun(void);
		void Task1000Ms(void);
		void Task10Ms(void);
		
		void ieAddDevFault(unsigned char aDevFaultType, unsigned char DevIndex);
		void ieAddDevFaultCancel(unsigned char aDevFaultType, unsigned char DevIndex);
		void ieAddPadFault(unsigned char aP0, unsigned char aDevFaultType);
		void ieAddPadFaultCancel(unsigned char aP0, unsigned char aDevFaultType);
		void ieAddBrdFault(unsigned char aP1, unsigned char aDevFaultType);
		void ieAddBrdFaultCancel(unsigned char aP1, unsigned char aDevFaultType);
		void ieAddBrdFaultLinkageStorage(unsigned short aInx);
		void ieAddBrdFaultCancelLinkageStorage(unsigned short aInx);
		
		void LoadOneFbMiss();
		
		void FireGet4DisplayPadViaIndex(unsigned int aIndex);
		int DipalayPadSendPathType();
		int DipalayPadSendYearMonth();
		int DipalayPadSendDayTime();
		void DisplayPadCheckCircuit(int Ord);
		void DisplayPadCheckByIndex();
		void DisplayPadGoSend();
		void DisplayPadCheckToSend();
		void TaskDiplayPad();
		
		void CheckBatUpdate();
		void ModuleInOutCountDown();
		void CheckFeedBackMiss();
		void CheckDelayStart();
		
		void CheckHydrantKpAction();
		void CheckHydrantKpActionForce();
		void CheckPumpFeedBack();
		
		void WaitChipIdle(unsigned int ChipX);
		void MaskCountSet(int aCnt);
		
		int PassWordII_ReadWrite(int IsRead, TPassWord *pPsw);
		int PassWordIII_ReadWrite(int IsRead, TPassWord *pPsw);
		
		void NwMachineStateClear(unsigned int aCtlNum);
		void NwMachineMsgFireClear(unsigned int aCtlNum);
		void NwMachineMsgLinkageClear(unsigned int aCtlNum);
		void NwMachineMsgSvClear(unsigned int aCtlNum);
		void NwMachineMsgFaultClear(unsigned int aCtlNum);
		
		void CheckSoundFire();
		void CheckSoundLinkage();
		void CheckSoundSv();
		void CheckSoundFault();
		
		void SingleCtlModeSendMaskInf2Crt();
		
		void BroadcastAlternatingUpdate();
		void PrcBroadcastAlternating();
		
		void FireTurnOnVa(void);
		
		static void sSimFireGot(TRecordCommon *aRc);
		static void sSimStartGot(TRecordCommon *aRc);
		static void sSimStartCancelGot(TRecordCommon *aRc);
		static void sSimSvGot(TRecordCommon *aRc);
		static void sSimFeedBackGot(TRecordCommon *aRc);
		
		void RelaySetFireOnOff(unsigned char  IsOnOff);
		void RelaySetFaultOnOff(unsigned char  IsOnOff);
		void RelayTask();
		
		void ProcPortSyncCmd(unsigned char aCmd, unsigned char aP0, unsigned char AnAck);
		void PortSyncTask();
		
		void Task1Ms(void);
};


#endif





