#include "ControlCenter.h"
#include "RuntimeData.h"
#include "Runtime.h"
#include "record.h"

extern "C"{
	#include "user_norflash.h"
	#include "user_eeprom.h"
	#include "fdcan_task.h"
	#include "relay_task.h"
	#include "uart_key_drv.h"
}

void TControlCenter::DelayMs(unsigned int aMs)
{
	volatile unsigned int mTick;
	volatile unsigned int Tick;
	for(Tick=0; Tick<aMs; Tick++){
		for(mTick=0; mTick<160000; mTick++);
		sys_error_led_task();
	}
}

void TControlCenter::NwMachineStateClear(unsigned int aCtlNum)
{
	ResetOneNetworkEpState(aCtlNum);
}

void TControlCenter::NwMachineMsgFireClear(unsigned int aCtlNum)
{
	int i,Cnt, FirstNullIndex, WritePst;
	unsigned char aP0, aP1, EpSta;
	
	if( (aCtlNum == 0) || (aCtlNum == MainCtl.fData.Split.MyNum)  )return;
	
	ieMsgFire.RequestRead =0;
	ieMsgFire.Lock = 1;
	Cnt = 0;
	i =0;
	
	FirstNullIndex = ieMSG_FIRE_MAX_COUNT +1000;
	Cnt = 0;
	for(i=0; i<ieMSG_FIRE_MAX_COUNT; i++){
		//To Find Dual, And Clear it
		FireGetViaPath(i);
		if(MsgReadTmp.D32[2] == 0){
			if(FirstNullIndex > ieMSG_FIRE_MAX_COUNT)FirstNullIndex = i;
		}else
		if( MsgReadTmp.Splite.PCtlNum == aCtlNum ){
				FireMsgClearOne(i);
				if(FirstNullIndex > ieMSG_FIRE_MAX_COUNT){
					FirstNullIndex = i;
				}
		}else{
			if(FirstNullIndex < ieMSG_FIRE_MAX_COUNT){
				FireMsgCopyOne(FirstNullIndex);
				FireMsgClearOne(i);
				FirstNullIndex++;
			}
			Cnt++;
		}
	}
	
	ieMsgFire.Count = Cnt;
	ieMsgFire.WriteIndex = Cnt;
	ieMsgFire.ReflashIndex++;
	
	ieMsgFire.Lock = 0;
	
	if(IsMute == 0){
		if(ieMsgFire.Count == 0){
			if( (ieMsgLinkage.Count > 0) && (AlarmSoundWhat > 0) ){
				if(IsMuteLnk == 0)AlarmSoundWhat = 1;
			}
			if( (ieMsgSv.Count > 0) && (AlarmSoundWhat > 1) ){
				if(IsMuteSv == 0)AlarmSoundWhat = 2;
			}
			if( (ieMsgFault.Count > 0) && (AlarmSoundWhat > 2) ){
				if(IsMuteFault == 0)AlarmSoundWhat = 3;
			}
		}
	}
	
	ieGot = 1;
}


void TControlCenter::NwMachineMsgLinkageClear(unsigned int aCtlNum)
{
	int i,Cnt, FirstNullIndex, WritePst;
	int aStartCount, aFbCount, aMsCount, aVaCount, aHP;
	unsigned char aP0, aP1, EpSta;
	int aDelayCount, aStopCount;
	
	if( (aCtlNum == 0) || (aCtlNum == MainCtl.fData.Split.MyNum)  )return;
	
	ieMsgLinkage.RequestRead =0;
	ieMsgLinkage.Lock = 1;
	Cnt = 0;
	i =0;
	
	FirstNullIndex = ieMSG_LINKAGE_MAX_COUNT +1000;
	Cnt = 0;
	aStartCount = 0;
	aFbCount = 0;
	aMsCount = 0;
	aVaCount = 0;
	aHP =0;
	aDelayCount = 0;
	aStopCount = 0;
	
	for(i=0; i<ieMSG_LINKAGE_MAX_COUNT; i++){
		//To Find Dual, And Clear it
		LinkageGetViaPath(i);
		if(MsgReadTmp.D32[2] == 0){
			if(FirstNullIndex > ieMSG_LINKAGE_MAX_COUNT)FirstNullIndex = i;
		}else
		if( MsgReadTmp.Splite.PCtlNum == aCtlNum ){
				LinkageMsgClearOne(i);
				if(FirstNullIndex > ieMSG_LINKAGE_MAX_COUNT){
					FirstNullIndex = i;
				}
		}else{
			if(FirstNullIndex < ieMSG_LINKAGE_MAX_COUNT){
				LinkageMsgCopyOne(FirstNullIndex);
				LinkageMsgClearOne(i);
				FirstNullIndex++;
			}
			Cnt++;
			if(MsgReadTmp.Splite.mType == etLinkageStart){
				if( (MsgReadTmp.Splite.Data0 != static_cast<unsigned char>(0x09)) && (MsgReadTmp.Splite.Data0 != static_cast<unsigned char>(0x0A)))	aStartCount++;

				if(MsgReadTmp.Splite.dType == dgType_VAALRAM){
					//if( (MsgReadTmp.Splite.P0 >0 ) && (MsgReadTmp.Splite.P0 <= dPORT_MAX_COUNT) ) {
						aVaCount++;
					//}
				}else
				if(MsgReadTmp.Splite.dType == dgTYPE_FIRE_HydrantKp){
					aHP++;
				}
				if(MsgReadTmp.Splite.Data0 == static_cast<unsigned char>(0x0A)){
					aDelayCount++;
				}else
				if(MsgReadTmp.Splite.Data0 == static_cast<unsigned char>(0x09)){
					aStopCount++;
				}
			}else
			if(MsgReadTmp.Splite.mType == etLinkageFeedBack){
				aFbCount ++;
				if(MsgReadTmp.Splite.Data0 == 0x04){
					aMsCount++;
				}
			}
		}
	}
	
	ieMsgLinkage.Count = Cnt;
	ieMsgLinkage.StartCount = aStartCount;
	ieMsgLinkage.FbCount = aFbCount;
	ieMsgLinkage.MissingCount = aMsCount;
	ieMsgLinkage.VaCount = aVaCount;
	ieMsgLinkage.HydrantPadCount = aHP;
	ieMsgLinkage.DelayCount = aDelayCount;
	ieMsgLinkage.StopCount = aStopCount;
	ieMsgLinkage.ReflashIndex++;
	
	ieMsgLinkage.Lock = 0;
	
	if(IsMute == 0){
		if(ieMsgLinkage.Count == 0){
			if( (ieMsgSv.Count > 0) && (AlarmSoundWhat > 1) ){
				if(IsMuteSv == 0)AlarmSoundWhat = 2;
			}
			if( (ieMsgFault.Count > 0) && (AlarmSoundWhat > 2) ){
				if(IsMuteFault == 0)AlarmSoundWhat = 3;
			}
		}
	}
	
	ieGot = 1;
}

void TControlCenter::NwMachineMsgSvClear(unsigned int aCtlNum)
{
	int i,Cnt, FirstNullIndex, WritePst;
	int aVaCount;
	unsigned char aP0, aP1, EpSta;
	
	if( (aCtlNum == 0) || (aCtlNum == MainCtl.fData.Split.MyNum)  )return;
	
	ieMsgSv.RequestRead =0;
	ieMsgSv.Lock = 1;
	
	FirstNullIndex = ieMSG_SV_MAX_COUNT +1000;
	Cnt = 0;
	aVaCount = 0;
	for(i=0; i<ieMSG_SV_MAX_COUNT; i++){
		//To Find Dual, And Clear it
		SvGetViaPath(i);
		if(MsgReadTmp.D32[2] == 0){
			if(FirstNullIndex > ieMSG_SV_MAX_COUNT)FirstNullIndex = i;
		}else
		if( MsgReadTmp.Splite.PCtlNum == aCtlNum){
				SvMsgClearOne(i);
				if(FirstNullIndex > ieMSG_SV_MAX_COUNT){
					FirstNullIndex = i;
				}
		}else{
			if(FirstNullIndex < ieMSG_SV_MAX_COUNT){
				SvMsgCopyOne(FirstNullIndex);
				SvMsgClearOne(i);
				FirstNullIndex++;
			}
			Cnt++;
		}
	}
	
	ieMsgSv.Count = Cnt;
	ieMsgSv.ReflashIndex++;
	ieMsgSv.Lock = 0;
	
	if(IsMute == 0){
		if(ieMsgSv.Count == 0){
			if( (ieMsgFault.Count > 0) && (AlarmSoundWhat > 2) ){
				if(IsMuteFault == 0)AlarmSoundWhat = 3;
			}
		}
	}
	
	ieGot = 1;	
}



void TControlCenter::NwMachineMsgFaultClear(unsigned int aCtlNum)
{
	int i,Cnt, FirstNullIndex, WritePst;
	int aVaCount;
	unsigned char aP0, aP1, EpSta;
	
	if( (aCtlNum == 0) || (aCtlNum == MainCtl.fData.Split.MyNum)  )return;
	
	ieMsgFault.RequestRead =0;
	ieMsgFault.Lock = 1;
	
	FirstNullIndex = ieMSG_FAULT_MAX_COUNT +1000;
	Cnt = 0;
	aVaCount = 0;
	for(i=0; i<ieMSG_FAULT_MAX_COUNT; i++){
		//To Find Dual, And Clear it
		FaultGetViaPath(i);
		if(MsgReadTmp.D32[2] == 0){
			if(FirstNullIndex > ieMSG_FAULT_MAX_COUNT)FirstNullIndex = i;
		}else
		if( MsgReadTmp.Splite.PCtlNum == aCtlNum){
				FaultMsgClearOne(i);
				if(FirstNullIndex > ieMSG_FAULT_MAX_COUNT){
					FirstNullIndex = i;
				}
		}else{
			if(FirstNullIndex < ieMSG_FAULT_MAX_COUNT){
				FaultMsgCopyOne(FirstNullIndex);
				FaultMsgClearOne(i);
				FirstNullIndex++;
			}
			Cnt++;
			if(MsgReadTmp.Splite.dType == dgType_VAALRAM){
				if( (MsgReadTmp.Splite.P0 >0 ) && (MsgReadTmp.Splite.P0 <= dPORT_MAX_COUNT) ) {
					aVaCount++;
				}
			}
		}
	}
	
	ieMsgFault.Count = Cnt;
	ieMsgFault.VaCount = aVaCount;
	ieMsgFault.ReflashIndex++;
	ieMsgFault.Lock = 0;
	
	if( (ieMsgFault.Count == 0) && (AlarmSoundWhat > 2) ){
		AlarmSoundWhat = 0x07;
	}
	
	ieGot = 1;	
}

int TControlCenter::PassWordII_ReadWrite(int IsRead, TPassWord *pPsw)
{
	unsigned int aAddr, CheckOk;
	volatile unsigned int aWait;
	int i;
	//Read ExistTable 
	aAddr = dEepAddrPassWordII;
	CheckOk = 1;
	if(IsRead){
		DelayMs(10);
		eeprom_read_byte(0, aAddr, PassWordTemp.Content, 16);
		DelayMs(10);
		for(i=0;i<16;i++){
			if( (PassWordTemp.Content[i] < '0') || (PassWordTemp.Content[i] > '9') ){
				break;
			}
		}
		if( (i<6)|| (i > 12) )CheckOk = 0;
		else PassWordTemp.Len = i;
		if(CheckOk){
			PassWordII.Len = PassWordTemp.Len;
			PassWordII.Ava = 1;
			for(i=0;i<PassWordII.Len;i++){
				PassWordII.Content[i] = PassWordTemp.Content[i];
			}
			PassWordII.Content[i] = '\0';
		}else{
			PassWordII.Len = 6;
			PassWordII.Ava = 1;
			for(i=0;i<PassWordII.Len;i++){
				PassWordII.Content[i] = '1' + i;
			}
		}
		return 1;
	}else{
		if(pPsw != nullptr){
			DelayMs(10);
			aAddr = dEepAddrPassWordII;
			eeprom_write_byte_via_page(0, aAddr, pPsw->Content, 16);
			DelayMs(10);
			aAddr = dEepAddrPassWordII;
			DelayMs(10);
			eeprom_read_byte(0, aAddr, PassWordTemp.Content, 16);
			DelayMs(10);
			CheckOk = 1;
			for(i=0;i<16;i++){
				if( PassWordTemp.Content[i] != pPsw->Content[i]){
					CheckOk = 0;
				}
			}
			if(CheckOk){
				for(i=0;i<16;i++){
					if( (PassWordTemp.Content[i] < '0') || (PassWordTemp.Content[i] > '9') ){
						CheckOk = 0;
					}
				}
				PassWordTemp.Len = i;
				PassWordII.Len = PassWordTemp.Len;
				PassWordII.Ava = 1;
				for(i=0;i<PassWordII.Len;i++){
					PassWordII.Content[i] = PassWordTemp.Content[i];
				}
				PassWordII.Content[i] = '\0';
				return 1;
			}else{
				PassWordII.Len = 6;
				PassWordII.Ava = 1;
				for(i=0;i<PassWordII.Len;i++){
					PassWordII.Content[i] = '1' + i;
				}
				return -1;
			}
		}else{
			return 0;
		}
		return 0;
	}
}

int TControlCenter::PassWordIII_ReadWrite(int IsRead, TPassWord *pPsw)
{
	unsigned int aAddr, CheckOk;
	volatile unsigned int aWait;
	int i;
	//Read ExistTable 
	aAddr = dEepAddrPassWordIII;
	CheckOk = 1;
	if(IsRead){
		DelayMs(10);
		eeprom_read_byte(0, aAddr, PassWordTemp.Content, 16);
		DelayMs(10);
		for(i=0;i<16;i++){
			if( (PassWordTemp.Content[i] < '0') || (PassWordTemp.Content[i] > '9') ){
				break;
			}
		}
		if( (i<6)|| (i > 12) )CheckOk = 0;
		else PassWordTemp.Len = i;
		if(CheckOk){
			PassWordIII.Len = PassWordTemp.Len;
			PassWordIII.Ava = 1;
			for(i=0;i<PassWordIII.Len;i++){
				PassWordIII.Content[i] = PassWordTemp.Content[i];
			}
			PassWordIII.Content[i] = '\0';
		}else{
			PassWordIII.Len = 6;
			PassWordIII.Ava = 1;
			for(i=0;i<PassWordIII.Len;i++){
				PassWordIII.Content[i] = '1' + i;
			}
		}
		return 1;
	}else{
		if(pPsw != nullptr){
			DelayMs(10);
			aAddr = dEepAddrPassWordIII;
			eeprom_write_byte_via_page(0, aAddr, pPsw->Content, 16);
			DelayMs(10);
			aAddr = dEepAddrPassWordIII;
			DelayMs(10);
			eeprom_read_byte(0, aAddr, PassWordTemp.Content, 16);
			DelayMs(10);
			CheckOk = 1;
			for(i=0;i<16;i++){
				if( PassWordTemp.Content[i] != pPsw->Content[i]){
					CheckOk = 0;
				}
			}
			if(CheckOk){
				for(i=0;i<16;i++){
					if( (PassWordTemp.Content[i] < '0') || (PassWordTemp.Content[i] > '9') ){
						CheckOk = 0;
					}
				}
				PassWordTemp.Len = i;
				PassWordIII.Len = PassWordTemp.Len;
				PassWordIII.Ava = 1;
				for(i=0;i<PassWordIII.Len;i++){
					PassWordIII.Content[i] = PassWordTemp.Content[i];
				}
				PassWordIII.Content[i] = '\0';
				return 1;
			}else{
				PassWordIII.Len = 6;
				PassWordIII.Ava = 1;
				for(i=0;i<PassWordIII.Len;i++){
					PassWordIII.Content[i] = '1' + i;
				}
				return -1;
			}
		}else{
			return 0;
		}
		return 0;
	}
}

void TControlCenter::ieAddBrdFault(unsigned char aP0, unsigned char aDevFaultType)
{
	TRecordCommon aIm;

	aIm.Splite.Year = DateTime.Date.Split.Year;
	aIm.Splite.Month = DateTime.Date.Split.Month;
	aIm.Splite.Day = DateTime.Date.Split.Day;
	aIm.Splite.Hour = DateTime.Time.Split.Hour;
	aIm.Splite.Minute = DateTime.Time.Split.Minute;
	aIm.Splite.Second = DateTime.Time.Split.Second;	
	aIm.Splite.mType = CmdActiveDefaultSet[cmOtherErr][7];
	aIm.Splite.PCtlNum = MainCtl.fData.Split.MyNum ;	//(0,aRxMsg.Data.D8[1]);
	
	aIm.Splite.P0 = 0xE0;			//Board Idf
	aIm.Splite.P1 = aP0;
	aIm.Splite.P2 = aDevFaultType;	//Fault Type
	aIm.Splite.dType = 0;			
	aIm.Splite.Data0 = aDevFaultType;	//Fault Type
	aIm.Splite.Data1 = 0xF1;//Is Set=1  Recover=0
	aIm.Splite.iByte0	= CmdActiveDefaultSet[cmOtherErr][8];
		
	iePushOneMsgFault(aIm);
	Record.FaultAdd(aIm);
	
	if(MainCtl.fData.Split.NetworkMode == 2)ProtoC2C.oBufMsgInsertFault(aIm, cmOtherErr);
}

void TControlCenter::ieAddBrdFaultCancel(unsigned char aP0, unsigned char aDevFaultType)
{
	TRecordCommon aIm;
	
	aIm.Splite.Year = DateTime.Date.Split.Year;
	aIm.Splite.Month = DateTime.Date.Split.Month;
	aIm.Splite.Day = DateTime.Date.Split.Day;
	aIm.Splite.Hour = DateTime.Time.Split.Hour;
	aIm.Splite.Minute = DateTime.Time.Split.Minute;
	aIm.Splite.Second = DateTime.Time.Split.Second;	
	aIm.Splite.mType = CmdActiveDefaultSet[cmOtherErr][7];
	aIm.Splite.PCtlNum = MainCtl.fData.Split.MyNum ;	//(0,aRxMsg.Data.D8[1]);
	
	aIm.Splite.P0 = 0xE0;			//Board Index
	aIm.Splite.P1 = aP0;
	aIm.Splite.P2 = aDevFaultType;	//Fault Type
	aIm.Splite.dType = 0;			
	aIm.Splite.Data0 = aDevFaultType;	//Fault Type
	aIm.Splite.Data1 = 0xF0;//Is Set=1  Recover=0
	aIm.Splite.iByte0	= CmdActiveDefaultSet[cmOtherErr][8];
	
	iePushOneMsgFaultCancel(aIm);
	Record.FaultAdd(aIm);
	
	if(MainCtl.fData.Split.NetworkMode == 2)ProtoC2C.oBufMsgInsertFault(aIm, cmOtherErr);
}

void TControlCenter::ieAddBrdFaultLinkageStorage(unsigned short aInx)
{
	unsigned char aP0;
	unsigned char aDevFaultType;
	TRecordCommon aIm;
	
	if(aInx ==0)return;
	if(aInx>dLinkageOutputBufMaxCOunt)return;
	aP0 = aInx % 256;
	aDevFaultType = (aInx / 256) + static_cast<unsigned char>(TBoardFaultType::StorageLinkExpressionPart1);

	aIm.Splite.Year = DateTime.Date.Split.Year;
	aIm.Splite.Month = DateTime.Date.Split.Month;
	aIm.Splite.Day = DateTime.Date.Split.Day;
	aIm.Splite.Hour = DateTime.Time.Split.Hour;
	aIm.Splite.Minute = DateTime.Time.Split.Minute;
	aIm.Splite.Second = DateTime.Time.Split.Second;	
	aIm.Splite.mType = CmdActiveDefaultSet[cmOtherErr][7];
	aIm.Splite.PCtlNum = MainCtl.fData.Split.MyNum ;	//(0,aRxMsg.Data.D8[1]);
	
	aIm.Splite.P0 = 0xE0;			//Board Idf
	aIm.Splite.P1 = aP0;
	aIm.Splite.P2 = aDevFaultType;	//Fault Type
	aIm.Splite.dType = 0;			
	aIm.Splite.Data0 = aDevFaultType;	//Fault Type
	aIm.Splite.Data1 = 0xF1;//Is Set=1  Recover=0
	aIm.Splite.iByte0	= CmdActiveDefaultSet[cmOtherErr][8];
		
	iePushOneMsgFault(aIm);
	Record.FaultAdd(aIm);
	
	if(MainCtl.fData.Split.NetworkMode == 2)ProtoC2C.oBufMsgInsertFault(aIm, cmOtherErr);
}

void TControlCenter::ieAddBrdFaultCancelLinkageStorage(unsigned short aInx)
{
	unsigned char aP0;
	unsigned char aDevFaultType;
	TRecordCommon aIm;
	
	if(aInx ==0)return;
	if(aInx>dLinkageOutputBufMaxCOunt)return;
	aP0 = aInx % 256;
	aDevFaultType = (aInx / 256) + static_cast<unsigned char>(TBoardFaultType::StorageLinkExpressionPart1);
	
	aIm.Splite.Year = DateTime.Date.Split.Year;
	aIm.Splite.Month = DateTime.Date.Split.Month;
	aIm.Splite.Day = DateTime.Date.Split.Day;
	aIm.Splite.Hour = DateTime.Time.Split.Hour;
	aIm.Splite.Minute = DateTime.Time.Split.Minute;
	aIm.Splite.Second = DateTime.Time.Split.Second;	
	aIm.Splite.mType = CmdActiveDefaultSet[cmOtherErr][7];
	aIm.Splite.PCtlNum = MainCtl.fData.Split.MyNum ;	//(0,aRxMsg.Data.D8[1]);
	
	aIm.Splite.P0 = 0xE0;			//Board Index
	aIm.Splite.P1 = aP0;
	aIm.Splite.P2 = aDevFaultType;	//Fault Type
	aIm.Splite.dType = 0;			
	aIm.Splite.Data0 = aDevFaultType;	//Fault Type
	aIm.Splite.Data1 = 0xF0;//Is Set=1  Recover=0
	aIm.Splite.iByte0	= CmdActiveDefaultSet[cmOtherErr][8];
	
	iePushOneMsgFaultCancel(aIm);
	Record.FaultAdd(aIm);
	
	if(MainCtl.fData.Split.NetworkMode == 2)ProtoC2C.oBufMsgInsertFault(aIm, cmOtherErr);
}

void TControlCenter::CheckSoundFire()
{
	if(IsMute == 0){
		if( (ieMsgFire.Count == 0) && (AlarmSoundWhat ==0) ){
			if( (ieMsgLinkage.Count > 0) && (IsMuteLnk == 0)){
				AlarmSoundWhat = 1;
			}else
			if( (ieMsgSv.Count > 0) && (IsMuteSv == 0) ){
				AlarmSoundWhat = 2;
			}else
			if( (ieMsgFault.Count > 0) && (IsMuteFault == 0) ){
				AlarmSoundWhat = 3;
			}else{
				AlarmSoundWhat = 7;
			}
		}
	}
}

void TControlCenter::CheckSoundLinkage()
{
	if(IsMute == 0){
		if( (ieMsgLinkage.Count == 0) && (AlarmSoundWhat >0) ){
			if( (ieMsgSv.Count > 0) && (IsMuteSv == 0) ){
				AlarmSoundWhat = 2;
			}else
			if( (ieMsgFault.Count > 0) && (IsMuteFault == 0) ){
				AlarmSoundWhat = 3;
			}else{
				AlarmSoundWhat = 7;
			}
		}
	}
}

void TControlCenter::CheckSoundSv()
{
	if(IsMute == 0){
		if( (ieMsgSv.Count == 0) && (AlarmSoundWhat >1) ){
			if( (ieMsgFault.Count > 0) && (IsMuteFault == 0) ){
				AlarmSoundWhat = 3;
			}else{
				AlarmSoundWhat = 7;
			}
		}
	}
}

void TControlCenter::CheckSoundFault()
{
	if(IsMute == 0){
		if( (ieMsgFault.Count == 0) && (AlarmSoundWhat >2) ){
			AlarmSoundWhat = 7;
		}
	}
}

void TControlCenter::sFireGetViaIndex(unsigned int aIndex, TEventMsg *aMsg)
{
	unsigned int AddrSd = dAddrSdRam_MsgFire + (aIndex * 24);
	aMsg->D32[0] = *(volatile unsigned int*)(AddrSd);
	aMsg->D32[1] = *(volatile unsigned int*)(AddrSd + 4);
	aMsg->D32[2] = *(volatile unsigned int*)(AddrSd + 8);
	aMsg->D32[3] = *(volatile unsigned int*)(AddrSd + 12);
	aMsg->D32[4] = *(volatile unsigned int*)(AddrSd + 16);
	aMsg->D32[5] = *(volatile unsigned int*)(AddrSd + 20);
} 

void TControlCenter::sLinkageGetViaIndex(unsigned int aIndex, TEventMsg *aMsg)
{
	unsigned int AddrSd = dAddrSdRam_MsgLinkage + (aIndex * 24);
	aMsg->D32[0] = *(volatile unsigned int*)(AddrSd);
	aMsg->D32[1] = *(volatile unsigned int*)(AddrSd + 4);
	aMsg->D32[2] = *(volatile unsigned int*)(AddrSd + 8);
	aMsg->D32[3] = *(volatile unsigned int*)(AddrSd + 12);
	aMsg->D32[4] = *(volatile unsigned int*)(AddrSd + 16);
	aMsg->D32[5] = *(volatile unsigned int*)(AddrSd + 20);
} 

void TControlCenter::sSvGetViaIndex(unsigned int aIndex, TEventMsg *aMsg)
{
	unsigned int AddrSd = dAddrSdRam_MsgSv + (aIndex * 24);
	aMsg->D32[0] = *(volatile unsigned int*)(AddrSd);
	aMsg->D32[1] = *(volatile unsigned int*)(AddrSd + 4);
	aMsg->D32[2] = *(volatile unsigned int*)(AddrSd + 8);
	aMsg->D32[3] = *(volatile unsigned int*)(AddrSd + 12);
	aMsg->D32[4] = *(volatile unsigned int*)(AddrSd + 16);
	aMsg->D32[5] = *(volatile unsigned int*)(AddrSd + 20);	
}

void TControlCenter::sFaultGetViaIndex(unsigned int aIndex, TEventMsg *aMsg)
{
	unsigned int AddrSd = dAddrSdRam_MsgFault + (aIndex * 24);
	aMsg->D32[0] = *(volatile unsigned int*)(AddrSd);
	aMsg->D32[1] = *(volatile unsigned int*)(AddrSd + 4);
	aMsg->D32[2] = *(volatile unsigned int*)(AddrSd + 8);
	aMsg->D32[3] = *(volatile unsigned int*)(AddrSd + 12);
	aMsg->D32[4] = *(volatile unsigned int*)(AddrSd + 16);
	aMsg->D32[5] = *(volatile unsigned int*)(AddrSd + 20);	
}

void TControlCenter::sMaskGetViaIndex(unsigned int aIndex, TEventMsg *aMsg)
{
	unsigned int sAddr = dAddrSdRam_MsgMask + aIndex *24;
	aMsg->D32[0] = *(volatile unsigned int *)(sAddr + 0);
	aMsg->D32[1] = *(volatile unsigned int *)(sAddr + 4);
	aMsg->D32[2] = *(volatile unsigned int *)(sAddr + 8);
	aMsg->D32[3] = *(volatile unsigned int *)(sAddr + 12);
	aMsg->D32[4] = *(volatile unsigned int *)(sAddr + 16);
	aMsg->D32[5] = *(volatile unsigned int *)(sAddr + 20);
}

void TControlCenter::BroadcastAlternatingUpdate()
{
	if(Broadcast.SyncMode){
		if( (ieMsgLinkage.BroadcastCount) && (ieMsgLinkage.VaCount) ){
			if(ieMsgLinkage.IsBroadcastAndVa == 0){
				if(ieMsgLinkage.BroadcastCountOld == 0){
					BroadcastAlramAlternating.InBroadcast =1;
					BroadcastAlramAlternating.Tick =0;
					InnerCan.SendCmdBroadcastFast(1);
				}else{		
					BroadcastAlramAlternating.InBroadcast =0;
					BroadcastAlramAlternating.Tick =0;
					InnerCan.SendCmdBroadcastFast(0);
				}					
				ieMsgLinkage.IsBroadcastAndVa = 1;
			}
		}else{
			if(ieMsgLinkage.IsBroadcastAndVa){
				InnerCan.SendCmdBroadcastFast(2);	
				ieMsgLinkage.IsBroadcastAndVa = 0;
			}
		}
		ieMsgLinkage.VaCountOld = ieMsgLinkage.VaCount;
		ieMsgLinkage.BroadcastCountOld = ieMsgLinkage.BroadcastCount;
	}
}

void TControlCenter::PrcBroadcastAlternating()
{
	//SyncMode == 0 ²¢ÐÐ
	if(Broadcast.SyncMode){
		if(ieMsgLinkage.IsBroadcastAndVa){
			if(BroadcastAlramAlternating.InBroadcast){
				BroadcastAlramAlternating.Tick++;
				if(BroadcastAlramAlternating.Tick > Broadcast.BroadcastTime){
					BroadcastAlramAlternating.InBroadcast =0;
					BroadcastAlramAlternating.Tick =0;
					InnerCan.SendCmdBroadcastFast(0);
				}
			}else{
				BroadcastAlramAlternating.Tick++;
				if(BroadcastAlramAlternating.Tick > Broadcast.VaTime){
					BroadcastAlramAlternating.InBroadcast =1;
					BroadcastAlramAlternating.Tick =0;
					InnerCan.SendCmdBroadcastFast(1);
				}
			}
		}
	}
}

void TControlCenter::sSimFireGot(TRecordCommon *aRc)
{
	if(Register.IsDevRegisting != 0x00A5A5A5){
		aRc->Splite.P2 = 0;
		if(CtlCenter.ieMsgFire.Count <1)aRc->Splite.Data1 = 1;
		else aRc->Splite.Data1 = 0;
		
		aRc->Splite.Data0 = TControlCenter::LinkInputTypeFireSimulate;
		
		if(CtlCenter.IsMsgReseting == 0){
			if(CtlCenter.iePushOneMsgFireSimulate(*aRc,0)){
				Record.FireAdd(*aRc);
			}
		}
	}
}

void TControlCenter::sSimStartGot(TRecordCommon *aRc)
{
	if(Register.IsDevRegisting != 0x00A5A5A5){
		aRc->Splite.mType = etLinkageStart;
		aRc->Splite.iByte0 = 0x01;
		aRc->Splite.P2 = 0;
		if(CtlCenter.iePushOneMsgLinkageStartSimulate(*aRc)){
			aRc->Splite.Data1 = StartTypeSimulate;
			Record.LinkageAdd(*aRc);
			//this will been call in InterLink Running, so send to Master
			ProtoC2C.SlaveDoSend2MasterMachine(*aRc, cmVIrtualStart, TProtoC2C::mtLink);
		}
	}
}

void TControlCenter::sSimStartCancelGot(TRecordCommon *aRc)
{
	if(Register.IsDevRegisting != 0x00A5A5A5){
		aRc->Splite.P2 = 0;
		aRc->Splite.mType = etLinkageStartCancel;
		if(CtlCenter.iePushOneMsgLinkageStartCancelSimulate(*aRc)){
			//Record.LinkageAdd(*aRc);
			//this will been call in InterLink Running, so send to Master
			ProtoC2C.SlaveDoSend2MasterMachine(*aRc, smVIrtualStop, TProtoC2C::mtLink);
		}
	}
}

void TControlCenter::sSimSvGot(TRecordCommon *aRc)
{
	if(Register.IsDevRegisting != 0x00A5A5A5){
		aRc->Splite.P2 = 0;
		if(CtlCenter.iePushOneMsgSvSimulate(*aRc)){
			Record.SvAdd(*aRc);
		}
	}
}

void TControlCenter::sSimFeedBackGot(TRecordCommon *aRc)
{
	if(Register.IsDevRegisting != 0x00A5A5A5){
		aRc->Splite.P2 = 0;
		aRc->Splite.mType = etLinkageFeedBack;
		aRc->Splite.iByte0 = 0x03;
		if(CtlCenter.iePushOneMsgLinkageFbSimulate(*aRc)){
			aRc->Splite.Data1 = StartTypeSimulate;
			Record.LinkageAdd(*aRc);
		}
	}
}

void TControlCenter::RelaySetFireOnOff(unsigned char IsOnOff)
{
	set_Relay_alarm_state(IsOnOff);
}

void TControlCenter::RelaySetFaultOnOff(unsigned char  IsOnOff)
{
	set_Relay_fault_state(IsOnOff);
}

void TControlCenter::RelayTask()
{
	if(ieMsgFire.ActualCount >0){
		RelaySetFireOnOff(1);
	}else{
		RelaySetFireOnOff(0);
	}
	if(ieMsgFault.Count >0){
		RelaySetFaultOnOff(1);
	}else{
		RelaySetFaultOnOff(0);
	}
}

void TControlCenter::ProcPortSyncCmd(unsigned char aCmd, unsigned char aP0, unsigned char AnAck)
{
	//cmBusDataSyncRegisterReq,
	//cmBusDataSyncRegister,
	//cmBusDataSyncRegisterRes,
	//cmBusDataSyncIsolationReq,
	//cmBusDataSyncIsolation,
	//cmBusDataSyncIsolationRes,
	unsigned char tP0;
	TPort *pPort;
	
	if(StartUpCompleted == 0)return;
	if(aP0 == 0)return;
	if(aP0 >dPORT_MAX_COUNT)return;
	
	tP0 = aP0-1;
	pPort = &Port[tP0];
	pPort->Num = aP0;
	if(aCmd == cmBusDataSyncRegisterReq){
		if(AnAck == 1){
			//Req Start
			TPort::sRpyRegDataSyncReqStart(pPort);
		}else
		if(AnAck == 3){
			//Start to Send Data
			TPort::sSendRegDataSync(pPort);
		}
	}else
	if(aCmd == cmBusDataSyncRegisterRes){
		//Got Result
		unsigned char tP0 = aP0-1;
		if(AnAck == 0){
			Port[tP0].SyncRegisterResult = 2;
		}else
		if(AnAck ==1){
			Port[tP0].SyncRegisterResult = 1;
		}
	}else
	if(aCmd == cmBusDataSyncIsolationReq){
		if(AnAck == 1){
			//Req Start
			TPort::sRpyMaskDataSyncReqStart(pPort);
		}else
		if(AnAck == 3){
			//Start to Send Data
			TPort::sSnedMaskDataSync(pPort);
		}
	}else
	if(aCmd == cmBusDataSyncIsolationRes){
		//Got Result
		unsigned char tP0 = aP0-1;
		if(AnAck == 0){
			Port[tP0].SyncMaskResult = 2;
		}else
		if(AnAck ==1){
			Port[tP0].SyncMaskResult = 1;
		}
	}
}

void TControlCenter::PortSyncTask()
{
	int i;
	for(i=0; i<dPORT_MAX_COUNT; i++){
		Port[i].Num = i+1;
		Port[i].SyncTaskIntv1000Ms();
	}
}







