#include "gType.h"
#include "gvalue.h"
#include "RuntimeData.h"
#include "Runtime.h"
#include "Gui.h"
#include "CnCpp.h"
#include "ControlCenter.h"
#include "extern_C_include.h"
#include "server.h"

extern "C"{
	#include "uart_key_drv.h"
	#include "uart_linkage_drv.h"
	#include "uart_printer_drv.h"
	#include "fdcan_task.h"
	#include "task.h"
}

//const char ConstTestStr[64] = L"ABCDBDE";

void ProcessInnerCanRxMsg();
void fNetworkService(void);
void ProcessUserAlramKey(void);
void CheckDelayStart();
void SelfCheckReturn();
void DebugAdd250Fault();
void uart_key_task();
void StartupPasswdGuiCheck();
void RecallCrcFaultMsg();

char TestStr[64];

int ShrOrg;

unsigned char IsMenuOperate = 0;
static TRecordCommon sIm;

static TCanHeaderID tIde;
static unsigned char tData[8];

const static unsigned char cMonthDaysCount[12] = {31, 28, 31, 30, 31, 30,
								31, 31, 30, 31, 30, 31}; 
static int DtStartupCrt = 0;
static unsigned char DtCrt[6];

struct {
	unsigned char dType;
	unsigned char ID;
	unsigned char PortNum;
	unsigned char dump;
}DeviceItem;

enum{
	IsMenuLevel0 = 0,
	ISMenuLevel1,
	IsDeviceCheck,
	
}MenuActionList;

enum{
	rtIdle = 0,
	rtUserOprate,
	rt
}rtState;

typedef union{
	unsigned int D32;
	struct{
		unsigned char PCtl;
		unsigned char P0;
		unsigned char P1;
		unsigned char P2;
	}Body;
}TDMix_Path;

void 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 Service_Init(void)
{
	volatile unsigned int Times;
	PinyinT9.Init();
	
	DevStartStop.IsShow = 0;
	
	ProtoC2C.Init();
	ProtoC2C.NwDevDescpInit();
	ProtoC2C.BufInit();
	ProtoC2C.ReInit();
	
	InnerCan.SendCmdPowerOnFast();//SendCommonCmd(cmCtrlPowerOn);
	
	Times = 0;
	while(Times < 50){
		DelayMs(20);
		if(InnerCan.CheckOutBufFastFinish()){
			break;
		}
		can_task();
		Times++;
	}
	
	Record.ReadMainSet();
	
	SysLoopTick = 0;
	Record.AllMsgReadInit();
	Record.ReadRegisterData();
	Record.ReadProjAndCtlDescp();
	Record.ReadDescp();
	Record.FloorDescp();
	//Record.ReadDesignPrm();
	Record.ReadMaintain();
	Record.ReadLastMaintainTime();
	
	Record.ReadDevUserSet();
	Record.ReadDirectPadUserSet();
	
	Record.Read4Area();
	Record.ReadDevAreaNum();
	Record.ReadDirectPadAreaNum();
	
	TProtoM2M::sInit();
	
	Record.ReadNwCfg();
	Record.ReadNwM2MCfg();
	
	Record.OperateAdd_PowerOff(MainCtl.fData.Split.MyNum);
	Record.OperateAdd_PowerUp(MainCtl.fData.Split.MyNum);
	Record.SaveNewTime2Eep();
	
	Record.LoadBusPadLnk();
	Record.LoadDirectPadCfg();
	
	CtlCenter.MaskCountSet(DevMask.ReadMaskFromFlash());
	InterLink.Init();
	InterLink.CheckStorageCrc();
	InterLink.LoadExpression2Ram();
	
	InitAllNetworkEpData();
	
	Record.ReadDescpNw();
	
	ProtoC2C.SlaveDoUploadMaskList();
	
	ProtoC2C.ClearNwAllCtlFire();
	
	MainCtl.ClearState();
	
	DoPrint.Init();
	
	CtlCenter.PassWordII_ReadWrite(1,nullptr);
	CtlCenter.PassWordIII_ReadWrite(1,nullptr);
	
	InnerCan.ForceSecondUpdate();
	
	ExRecord.Init();
	ExRecord.SetStateCodeAll();
	FecBusCRT.Init();
	FecBusCRT.SetStateCodeAll();
	FecBusCRT.Upload.Active = 0;
	GraphDevCfg.ReadCrtCfg();
	
	FecStd.Init();
	Config.Init();
	MqttData.Init();
	
	printer_reset_callback();
	
	GeneralDev.LcdBlackLightSetOnStartUp();
	
	KeyPad.IsSendPowerSuplyCount = 1;
	
	FireVaTrenOned = 0;
	
	RunState = TRunModeCfg::RunModeRun;
	
	DesignPrmSet.BackEnd_LoadData2RuntimeData();
	
	DescpWrite.Init();
	DescpWrite.PreLoadReset();
	
	TBroadcastCfg::ReadSetting();
	
	CanNetworkRegister.sReadNetMode();
	ProtoC2C.ReadMaskCtlNum();
	
	DevAuthorize.Init();
	//SlaveRegByRemote.Init();
	
	RunModeCfg.Init();
	RunModeCfg.StartupSetRunMode();
	
	Maintain.Init();
	Maintain.StartUpInit();
	
	Record.ReadLanguage();
	
	Check4MaintainToShow();
	
	StartUpCompleted = 0x66666666;
	
	SysLoopTick = 0;
	
	DisplayPadDescpDownload.Init();
}


void ResetCallBack()
{
	unsigned int i;
	SysLoopTick = 0;
	MainMenu.Hide();
	PageIdle.Hide();
	EventShow.Hide();
	TipPopup.Hide();
	TipPopup.ClearTickOnEventOcc();
	GuiState = guiIdle;
	
	Register.IsOnShow =0;
	Register.IsOnGo = 0;
	ProtoC2C.RemoteReq.Reg.IsRequsting = 0;
	
	AlarmSoundWhat = 0x07;
	IsMute = 0;
	IsMuteFire = 0;
	IsMuteLnk = 0;
	IsMuteSv = 0;
	IsMuteFault = 0;
	IsShowingMsgReset = 1;
	CheckNormal.IsShowing = 0;
	CtlCenter.IsMsgReseting = 1;
	InnerCan.CmdSendReset();
	CtlCenter.MsgReset();
	EventShow.MsgReset();
	CtlCenter.IsMsgReseting = 0;
	CtlCenter.IsSendResetToPad =1;
	ResetAllNetworkEpState();
	
	EventShow.StartToResetPrc();
	
	sys_set_key_reset_state();
	
	DoPrint.ReInit();
	printer_init();
	
	MainCtl.ClearState();
	InterLink.ResetByClearKey();
	
	InitAllNetworkEpData();	
	
	KeyPadData.BatState = 0;
	PowerSuplyState = 0;
	PowerSuplyComm = 0;
	PowerBkState =0;
	BatState =0;
	PowerSuplyStateOld = 0;
	PowerSuplyCommOld = 0;
	PowerBkStateOld =0;
	BatStateOld =0;
	
	Register.IsDevRegisting = 0;
	
	AlramVaOnOff = 0;
	
	PowerSuplyStateOld =0;
	PowerSuplyState =0;
	PowerSuplyCommOld =0;
	PowerSuplyComm =0;
	PowerBkStateOld =0; 
	PowerBkState =0;
	BatStateOld =0;
	BatState =0;
	PowerSuplyOutputOld = 0;
	PowerSuplyOutput = 0;
	Bat1UnderVoltageOld = 0;
	Bat2UnderVoltageOld = 0;
	
	NwIsFbMiss = 0;
	IsStartDelay = 0;
	IsSelfChecking = 0;
	NwIsSelfCheck = 0;
	NwIsNormalCheck =0;

	for(i=0; i<17; i++){
		NwSelfCheckTick[i] = 0;
	}
	
	MainMenu.Hide();
	IsSelfChecking = 0;
	
	if(MainCtl.fData.Split.NetworkMode == 1){
		ProtoC2C.ClearNwAllCtlFire();
	}
	TProtoC2C::DevDelayStartTick = 0;
	
	DevPrinter.sOffline = 0;
	DevPrinter.sNoPaper = 0;
	ExRecord.ErrState.D32 = 0;
	ExRecord.ErrReport.D32 = 0;
	ExRecord.OnlineTick = 0;
	
	ExRecord.SetStateCodeAll();
	FecBusCRT.SetStateCodeAll();
	FecBusCRT.Upload.Active = 0;
	printer_reset_callback();
	GeneralDev.LcdBlackLightSetOn();
	
	PowerPrmValue.Sub1.D32[0] =0;
	PowerPrmValue.Sub1.D32[1] =0;
	PowerPrmValue.Sub1.D32[2] =0;
	PowerPrmValue.Sub1.D32[3] =0;
	PowerPrmValue.Sub1.D32[4] =0;
	
	PowerPrmValue.Sub2.D32[0] =0;
	PowerPrmValue.Sub2.D32[1] =0;
	PowerPrmValue.Sub2.D32[2] =0;
	PowerPrmValue.Sub2.D32[3] =0;
	PowerPrmValue.Sub2.D32[4] =0;
	
	DevStartStop.IsShow = 0;
	
	FireVaTrenOned = 0;
	
	SystemPermission = 1;
	PmsIntervene.IsShow = 0;
	
	GolbalDelayTick = 0;
	EventShow.GolbalDelayValueOnShowing = 0;
	SysLoopTick =0;
	
	DisplayPadDescpDownload.ExtReset();
}

void ResetAfterCallBack()
{
	unsigned int i;
	SysLoopTick = 0;
	CtlCenter.IsMsgReseting = 0;
	ResetAllNetworkEpState();
	
	if(MainCtl.fData.Split.NetworkMode == 1)CtlCenter.MaskCountSet(DevMask.ReadMaskFromFlash());
	EventShow.TryGetMaskData(2,2024);
	
	if(MainCtl.fData.Split.NetworkMode == 0){
		CtlCenter.SingleCtlModeSendMaskInf2Crt();
	}
	
	InitAllNetworkEpData();	
	
	for(i=0; i<17; i++){
		NwSelfCheckTick[i] = 0;
	}
	if(MainCtl.fData.Split.NetworkMode == 1)ProtoC2C.ClearNwAllCtlFire();
	
	CtlCenter.PassWordII_ReadWrite(1,nullptr);
	CtlCenter.PassWordIII_ReadWrite(1,nullptr);
	CtlCenter.PsBatUpDateTick = 0;
	
	DevPrinter.sOffline = 0;
	DevPrinter.sNoPaper = 0;
	
	ExRecord.ErrState.D32 = 0;
	ExRecord.ErrReport.D32 = 0;
	ExRecord.OnlineTick = 0;
	
	InnerCan.ForceSecondUpdate();
	
	ExRecord.SetStateCodeAll();
	FecBusCRT.SetStateCodeAll();
	GeneralDev.LcdBlackLightSetOn();
	
	GolbalDelayTick = 0;
	EventShow.GolbalDelayValueOnShowing = 0;
	
	TipPopup.Init();
	TipPopup.ClearTickOnKeyPress();
	
	//DebugAdd250Fault();
	SysLoopTick =0;
	UserRequestReset = 0;
	
	for(i=0; i<dInnerCanRxBufMaxCount; i++){
		InnerCanRxBuf.Body[i].Ide.D32 = 0;
		InnerCanRxBuf.Body[i].Len = 0;
		InnerCanRxBuf.Body[i].Da[0] = 0;
		InnerCanRxBuf.Body[i].Da[1] = 0;
		InnerCanRxBuf.Body[i].Da[2] = 0;
		InnerCanRxBuf.Body[i].Da[3] = 0;	
		InnerCanRxBuf.Body[i].Da[4] = 0;
		InnerCanRxBuf.Body[i].Da[5] = 0;
		InnerCanRxBuf.Body[i].Da[6] = 0;
		InnerCanRxBuf.Body[i].Da[7] = 0;
	}
	InnerCanRxBuf.WriteIndex =0;
	InnerCanRxBuf.ReadIndex =0;
	
	Record.CheckPrimaryKeyWarning();
	RecallCrcFaultMsg();
}

void HotKeyResetCallBack()
{
	ProtoC2C.ResetCoomm();
	FecBusCRT.ResetCallBack();
	FecStd.ResetCallBack();
	ResetCallBack();
	Record.OperateAdd_Reset(MainCtl.fData.Split.MyNum);
	FecStd.TriggerCmd01SysReset();
	FecBusCRT.setFecBusCRTMsg01(0);
}

void HotKeyAlramCallBack()
{
	ProcessUserAlramKey();
}

void HotKeySelfCheckCallBack()
{
	EventShow.Hide();
	MainMenu.Hide();
	CheckNormal.Hide();
	GuiState = guiIdle;
	Record.OperateAdd_SelfCheck(MainCtl.fData.Split.MyNum);	
	
	LiteSelfCheck.FinishCallBack = &SelfCheckReturn;
	LiteSelfCheck.Tick = 0;
	LiteSelfCheck.Seq =0;
	LiteSelfCheck.IsGo =1;
	LiteSelfCheck.CheckCmdHasSend = 0;
	LiteSelfCheck.aPanel = &CommonPanel;
	LiteSelfCheck.ShowMsg(0);
	LiteSelfCheck.IsShowing = 1;
	
	IsSelfChecking = 1;
	
	EventShow.FirstFireHadDraw = 0;
	EventShow.FirstFireShowDescpInteval = 0;
	CtlCenter.IsSendSelfCheckToPad =1;
	
	KeyPad.ForceClearSysErrLed();
	
	FecStd.TrigerCmd03SysSlefCheck();
	
	FecBusCRT.setFecBusCRTMsg03(0);
}


void GuiCheckHotKey(unsigned char aKey)
{
	//#define HOTKEY_FIRE_ALRAM	0x72
	//#define HOTKEY_LINKAGE_START	0x73	
	//#define HOTKEY_INF_CONF		0x74
	//#define HOTKEY_SELF_CHECK	0x75
	//#define HOTKEY_MUTE		0x76
	//#define HOTKEY_RESET		0xE9
	//#define HOTKEY_CHECK		0x77
	
	if(EventShow.IsGoReset)return;
	
	if(aKey == HOTKEY_FIRE_ALRAM){
		if(SystemPermission >1){
			HotKeyAlramCallBack();
		}else{
			PmsIntervene.Show4Alram();
		}
	}else
	if(aKey == HOTKEY_LINKAGE_START){
		if(Register.IsOnGo)return;
		if(AllowManual){
			LinkageManualGo = 1;
			CtlCenter.CheckHydrantKpActionForce();
			if(MainCtl.fData.Split.NetworkMode == 1){
				Record.OperateAdd_MasterLinkageGo(MainCtl.fData.Split.MyNum);
			}else{
				Record.OperateAdd_SlaveLinkageGo(MainCtl.fData.Split.MyNum);
			}
		}
	}else
	if(aKey == HOTKEY_INF_CONF){
		//This Run In EventShow
	}else
	if(aKey == HOTKEY_SELF_CHECK){
		if(SystemPermission >1){
			HotKeySelfCheckCallBack();
		}else{
			PmsIntervene.Show4SelfCheck();
		}
	}else
	if(aKey == HOTKEY_MUTE){
		if(AlarmSoundWhat < 4){
			AlarmSoundWhat = 0x07;
			Record.OperateAdd_Mute(MainCtl.fData.Split.MyNum);
			IsMute = 0xA5;
			IsMuteFire = 0xA5;
			IsMuteLnk = 0xA5;
			IsMuteSv = 0xA5;
			IsMuteFault = 0xA5;
			
			FecStd.TriggerCmd02SysMute();
			FecBusCRT.setFecBusCRTMsg02(0);
		}
	}else
	if(aKey == HOTKEY_RESET){
		//ProtoC2C.PublishEventReset();
		if(Register.IsOnGo)return;
		if(SystemPermission >1){
			HotKeyResetCallBack();
		}else{
			PmsIntervene.Show4Reset();
		}
	}else
	if(aKey == HOTKEY_CHECK){
		if(Register.IsOnGo)return;		
		if(EventShow.IsGoReset == 0){
			MainMenu.Hide();
			CheckNormal.Init(0, 36, 800, 406, 0, 0, clMedGray);
			//EventShow.MakeFastCheck();
			EventShow.KeyJab();
			EventShow.Hide();
			PageIdle.Hide();
			Record.ReadDesignPrm();
			CheckNormal.Show();
			CheckNormal.pDesignPrmSet = &DesignPrmSet;
			CheckNormal.LoadAndDrawData();
			GuiState = guiCheckNormal;
		}
	}else
	if(aKey == VK_OEM_JUMP){
		if( AllowAuto == 0 ){
			AllowAuto = 1;
			AllowManual = 0;
		}else{
			AllowAuto = 0;
			AllowManual = 2;
		}
		Allow.ExtChange(AllowAuto);
		ProcessUserSetAutoState();
		ExRecord.SetStateCodeAutoManual();
		FecBusCRT.SetStateCodeAutoManual();
		Record.OperateAdd_AllowSet(MainCtl.fData.Split.MyNum, AllowAuto + AllowManual);
	}else
	if( (aKey == HOTKEY_AUTO_ALLOW) || (aKey == HOTKEY_MANUAL_ALLOW) ){
		if(aKey == HOTKEY_AUTO_ALLOW){
			if(AllowAuto){
				if(AllowManual){
					AllowAuto = 0;
				}else{
					return;
				}
			}else{
				AllowAuto = 1;
			}
			ProcessUserSetAutoState();
			ExRecord.SetStateCodeAutoManual();
			FecBusCRT.SetStateCodeAutoManual();
			Record.OperateAdd_AutoAllowSet(MainCtl.fData.Split.MyNum, AllowAuto);
		}
		if(aKey == HOTKEY_MANUAL_ALLOW){
			if(AllowManual){
				if(AllowAuto){
					AllowManual = 0;
				}else{
					return;
				}
			}else{
				AllowManual = 1;
			}
			ProcessUserSetManualState();
			ExRecord.SetStateCodeAutoManual();
			FecBusCRT.SetStateCodeAutoManual();
			Record.OperateAdd_ManualAllowSet(MainCtl.fData.Split.MyNum, AllowManual);
		}
	}
}

void CheckNormalReturn()
{
	CheckNormal.Hide();
	EventShow.MakeFastCheck();
	if( 	(EventShow.MsgList[0].Count != 0) ||
		(EventShow.MsgList[1].Count != 0) ||
		(EventShow.MsgList[2].Count != 0) ||
		(EventShow.MsgList[3].Count != 0) ||
		(EventShow.MsgList[4].Count != 0) ){
			GuiState = guiShowEvent;
			EventShow.MakeShow();
	}else{
		GuiState = guiIdle;
		PageIdle.Show();
	}
}

void SelfCheckReturn()
{
	EventShow.FirstFireHadDraw = 0;
	EventShow.MakeFastCheck();
	if( 	(EventShow.MsgList[0].Count != 0) ||
		(EventShow.MsgList[1].Count != 0) ||
		(EventShow.MsgList[2].Count != 0) ||
		(EventShow.MsgList[3].Count != 0) ||
		(EventShow.MsgList[4].Count != 0) ){
			GuiState = guiShowEvent;
			EventShow.MakeShow();
	}else{
		GuiState = guiIdle;
		PageIdle.Show();
	}
}

void Service_CheckDevFault()
{
	int i;
	if(IsShowingMsgReset)return;
	if(KeyPadData.HasUpdate){
		KeyPadData.HasUpdate = 0;
		i = 0;
		if(PowerSuplyStateOld != PowerSuplyState){
			if(PowerSuplyState){
				CtlCenter.ieAddDevFault(dDevFaultPowerSuply, 0);
			}else{
				CtlCenter.ieAddDevFaultCancel(dDevFaultPowerSuply, 0);
			}
			PowerSuplyStateOld = PowerSuplyState;
			ExRecord.SetStateCodePsOnActive();
			FecBusCRT.SetStateCodePsOnActive();
		}
		
		if(PowerSuplyCommOld != PowerSuplyComm){
			if(PowerSuplyComm){
				CtlCenter.ieAddDevFault(dDevFaultPowerSuplyComm,0);
			}else{
				CtlCenter.ieAddDevFaultCancel(dDevFaultPowerSuplyComm,0);
			}
			PowerSuplyCommOld = PowerSuplyComm;
			i = 1;
		}
		
		if(PowerBkStateOld != PowerBkState){
			if(PowerBkState){
				CtlCenter.ieAddDevFault(dDevFaultPowerBk,0);
			}else{
				CtlCenter.ieAddDevFaultCancel(dDevFaultPowerBk,0);
			}
			PowerBkStateOld = PowerBkState;
			i = 1;
		}
		
		if(BatStateOld != BatState){
			if(BatState){
				CtlCenter.ieAddDevFault(dDevFaultBat,0);
			}else{
				CtlCenter.ieAddDevFaultCancel(dDevFaultBat,0);
			}
			BatStateOld = BatState;	
			i = 1;
		}
		
		if(PowerSuplyOutputOld != PowerSuplyOutput){
			if(PowerSuplyOutput){
				CtlCenter.ieAddDevFault(22,0);
			}else{
				CtlCenter.ieAddDevFaultCancel(22,0);
			}
			PowerSuplyOutputOld = PowerSuplyOutput;
			i = 1;
		}
		
		if(Bat1UnderVoltageOld != Bat1UnderVoltage){
			if(Bat1UnderVoltage){
				CtlCenter.ieAddDevFault(24,0);
			}else{
				CtlCenter.ieAddDevFaultCancel(24,0);
			}
			Bat1UnderVoltageOld = Bat1UnderVoltage;
			i = 1;
		}
		
		if(Bat2UnderVoltageOld != Bat2UnderVoltage){
			if(Bat2UnderVoltage){
				CtlCenter.ieAddDevFault(25,0);
			}else{
				CtlCenter.ieAddDevFaultCancel(25,0);
			}
			Bat2UnderVoltageOld = Bat2UnderVoltage;
			i = 1;
		}
		
		if(i){
			ExRecord.SetStateCodePsError();
			FecBusCRT.SetStateCodePsError();
		}
	}
}

void StartupPasswdGuiCheck()
{
	if(DialogStartup.CheckToShow()){
		DialogStartup.Show();
		DialogStartup.ShowSeries(MainCtl.Sn);
		while(DialogStartup.CheckSkip() == 0){
			SysLoopTick = 0;
			uart_key_task();
			if(KeyRdy == 0)continue;
			KeyRdy =0;
			(void)DialogStartup.KeyIn( KeyValue);
		}
	}
}

void Service_Gui(void)
{
	TGuiMsgReturn aMsg;
	unsigned int RequstReturn;
	static TGuiState aGuiState = guiIdle;
	static TGuiState GuiStateOld = guiIdle;
	static TGuiState GuiStateBak = guiIdle;
	unsigned int aAva =0;
	
	ShrOrg = -25;
	ShrOrg >>= 1;
	
	if(KeyRdy == 0)return;
	TipPopup.ClearTickOnKeyPress();
	
	GeneralDev.LcdBlackLightSetOn();
	
	if(GuiState == guiStateRemote){
		KeyRdy = 0;
		return;
	}
	
	KeyRdy =0;
	SystemPermissionTick = 0;
	if(EventShow.IsGoReset){
		KeyRdy =0;
		return;
	}
	if( (LiteSelfCheck.IsShowing) ){
		KeyRdy =0;
		return;
	}
	if(PmsIntervene.IsShow){
		aMsg = PmsIntervene.KeyIn(KeyValue);
		if(aMsg == guiMsgReturn){
			//reDraw
		}
		
		KeyValue = VK_NONE;

		if(aAva ==1){
			EventShow.KeyJab();
		}
		SystemPermissionTick = 0;
		return;
	}
	
	//if(KeyValue == '1')Test_AddMask1();
	//if(KeyValue == '2')Test_AddUnMask1();
	//if(KeyValue == '4')Test_AddMask2();
	//if(KeyValue == '5')Test_AddUnMask2();
	
	if(GuiState != GuiStateOld){
		GuiStateOld = GuiState;
	}
	
	GuiCheckHotKey(KeyValue);
	
	if(TipPopup.IsShow){
		GuiState = guiTipPopup;
	}else	
	if(EventShow.IsShowing){
		GuiState = guiShowEvent;
	}
	
	if( (GuiState == guiIdle) || (GuiState == guiTipPopup) ){
		if(KeyValue == VK_MENU){
			GuiStateBak = GuiState;
			GuiState = guiMenu;
			PageIdle.Hide();
			EventShow.Hide();
			TipPopup.Hide();
			MainMenu.aPanel = &CtlPanelLite;
			MainMenu.Show();
			aAva = 1;
		}else
		if(KeyValue == VK_F2){
			EventShow.MakeShow();
			if( EventShow.IsShowing ){
				PageIdle.Hide();
				MainMenu.Hide();
				TipPopup.Hide();
			}
		}else
		if(KeyValue == VK_RETURN){
			if(GuiState == guiTipPopup){
				EventShow.MakeShow();
				if( EventShow.IsShowing ){
					PageIdle.Hide();
					MainMenu.Hide();
					TipPopup.Hide();
				}else{
					MainMenu.Hide();
					TipPopup.Hide();
					PageIdle.Show();
					GuiState = guiIdle;
				}
			}
		}
	}else
	if(GuiState == guiShowEvent){
		if(KeyValue == VK_MENU){
			GuiStateBak = GuiState;
			GuiState = guiMenu;
			PageIdle.Hide();
			EventShow.Hide();
			MainMenu.aPanel = &CtlPanelLite;
			MainMenu.Show();
			aAva = 1;
		}else{
			aMsg = EventShow.KeyIn(KeyValue);
			/* Not return to Idle
			if(aMsg == guiMsgReturn){
				EventShow.MakeFastCheck();
				EventShow.Hide();
				GuiState = guiIdle;
				PageIdle.Show();
			}*/
		}
	}else
	if(GuiState == guiMenu){
		aMsg = MainMenu.KeyIn(KeyValue);
		if(aMsg == guiMsgReturn){
			MainMenu.Hide();
			EventShow.MakeFastCheck();
			EventShow.MakeShow();
			if( EventShow.IsShowing ){
				GuiState = guiShowEvent;
				PageIdle.Hide();
				EventShow.MakeShow();
			}else{
				GuiState = guiIdle;
				EventShow.Hide();
				PageIdle.Show();
			}
		}
		
		aAva = 1;
	}else
	if(GuiState == guiCheckNormal){
		aMsg = CheckNormal.KeyIn(KeyValue);
		if(aMsg == guiMsgReturn){
			CheckNormal.Hide();
			EventShow.MakeFastCheck();
			EventShow.MakeShow();
			if( EventShow.IsShowing ){
				GuiState = guiShowEvent;
				EventShow.MakeShow();
			}else{
				GuiState = guiIdle;
				EventShow.Hide();
				PageIdle.Show();
			}
		}
	}
		
	KeyValue = VK_NONE;
	
	if(aAva ==1){
		EventShow.KeyJab();
	}
}

void PreventPermissionDown()
{
	SystemPermissionTick = 0;
}

void Service_Time_1mS(void)
{
	//On tick
	CtlCenter.Task1Ms();
	C2cCompletedTick +=  get_excan_linkage_tx_complete();
}
/*
void ManageStartCountDown()
{
	//Not On tick
	int nSInx, Cnt; 
	int i,x;
	unsigned char aP0, aP1;
	
	if(EventShow.StartCountDownList.Count > 0){
		for(i=0; i<EventShow.StartCountDownList.Count; i++){
			if(EventShow.StartCountDownList.TimeSec[i] < 10){
				EventShow.StartCountDownList.TimeSec[i]++;
			}else{
				sIm.D32[2] = EventShow.StartCountDownList.Path[i];
				aP0 = sIm.Splite.P0 -1;
				aP1 = sIm.Splite.P1 -1;
				if(aP0 < 40){
					if( (Port[aP0].StateTable[aP1] & dEpStateBitStart) == 0 ){
						if((Port[aP0].StateTable[aP1] & dEpStateBitFbMiss) == 0 ){
							if( (Port[aP0].StateTable[aP1] & dEpStateBitDelayStart) == 0){
								Cnt = 1;
								sIm.Splite.Year 		= DateTime.Date.Split.Year;
								sIm.Splite.Month 		= DateTime.Date.Split.Month;
								sIm.Splite.Day 		= DateTime.Date.Split.Day;
								sIm.Splite.Hour 		= DateTime.Time.Split.Hour;
								sIm.Splite.Minute 	= DateTime.Time.Split.Minute;
								sIm.Splite.Second 	= DateTime.Time.Split.Second;
								
								sIm.Splite.mType = etLinkageFeedBack;
								//sIm.Splite.dType = ManualEventMsgAdd.dType;
								//0x04 = FeedBack Miss
								sIm.Splite.iByte0 =  0x04;
								
								CtlCenter.ieManualPushOneMsgLinkageFb(sIm);		
							}
						}
					}
				}
				EventShow.StartCountDownList.Path[i] = 0;
			}
		}
		
		Cnt = 0;
		nSInx = -1;
		for(i=0; i<EventShow.StartCountDownList.Count; i++){
			if(EventShow.StartCountDownList.Path[i] == 0){
				if(nSInx == -1)nSInx = i;
			}else{
				if(nSInx > -1){
					EventShow.StartCountDownList.Path[nSInx] = EventShow.StartCountDownList.Path[i];
					EventShow.StartCountDownList.TimeSec[nSInx] = EventShow.StartCountDownList.TimeSec[i];
					EventShow.StartCountDownList.Path[i] = 0;
					nSInx++;
				}
				Cnt++;
			}
		}
		EventShow.StartCountDownList.Count = Cnt;
	}
}
*/
void Service_Time_10mS(void)
{
	//Not On Tick
	Record.Task10Ms();
	InterLink.DoLnk();
	InterLink.Task10Ms();
	CtlCenter.Task10Ms();
	InnerCan.OBufCheckSend();
	ProtoC2C.Task10Ms();
	//fNetworkReg.Task10Ms();
	DoPrint.Task10Ms();
	ExRecord.Task10Ms();
	FecBusCRT.Task10Ms();
	FecStd.Task10ms();
	Config.Task10Ms();
	MqttData.Task10Ms();
}


void DateTimeCrt()
{

}

void Service_Time_100mS(void)
{
	//Not On Tick
	static unsigned int tick100ms;
	static unsigned int tick500ms;
	static unsigned int tick1000ms;
	static unsigned int tick4Hour =0;
	
	int d;

	SysLoopTick = 0;
	TaskTimeUpData();
	
	tick100ms++;
	if(tick100ms){
		tick100ms = 0;
		
		if(CtlCenter.ieGot){
			CtlCenter.ieGot = 0;
			EventShow.NoNewEventTick500mS = 0;
		}
		
		FecStd.Task100ms();
	}
	
	tick500ms++;
	if(tick500ms > 4){
		tick500ms = 0;
		if(CheckNormal.Task500Ms() == -1)CheckNormalReturn();
	}
	
	tick1000ms++;
	if(tick1000ms == 2){
		if(IsShowingMsgReset == 0){
			MotherBoard.CheckOnlineTask();
			ExRecord.Task1000Ms();
			StateBar_Pwr_SelfCheck();
			
			KeyPad.CheckKeyPadOffline();
			DisplayPadDescpDownload.Task1000Ms();
		}
	}else		
	if(tick1000ms == 4){
		ProtoC2C.CheckInBufCount();
		PmsIntervene.Task1000Ms();
		CtlCenter.CheckBatUpdate();
		if(GolbalDelayTick)GolbalDelayTick--;
		CheckDelayStart();
	}else
	if(tick1000ms == 6){
		for(d=0; d<dHANDCTL_MAX_COUNT; d++){
			HandCtlBoard[d].CheckKeyState(MainCtl.fData.Split.MyNum, d);
		}
		Service_CheckDevFault();
		LiteSelfCheck.Task1000Ms();
		GeneralDev.Task1000Ms();
		if( (GuiState != guiStateRemote) && (Register.IsOnShow == 0) ){
			TipPopup.pMenu = &MainMenu;
			int tSta = TipPopup.Task1000Ms();
			if(tSta == 2){
				GeneralDev.LcdBlackLightSetOn();
				HotKeySelfCheckCallBack();
			}else
			if(tSta ==1){
				MainMenu.Hide();
				EventShow.Hide();
				PageIdle.Hide();
				CheckNormal.Hide();
				GuiState = guiTipPopup;
				GeneralDev.LcdBlackLightSetOn();
			}else
			if(tSta == -1){
				PageIdle.Show();
				GuiState = guiIdle;
			}
		}
	}else
	if(tick1000ms >9){
		tick1000ms = 0;
		tick4Hour++;
		if(tick4Hour > 3599){
			tick4Hour = 0;
			TimeSecHasRun++;
		}
		
		//Not On Tick Run
		ProtoC2C.Task1000Ms();

		d = EventShow.Task1000Ms();
		if(EventShow.IsGoReset == 0){
			if(EventShow.IsShowing){
				GuiState = guiShowEvent;
				CheckNormal.Hide();
				PageIdle.Hide();
				MainMenu.Hide();
			}
			if(1 == d){
				if(EventShow.IsShowing){
					CheckNormal.Hide();
					PageIdle.Hide();
					MainMenu.Hide();
				}
			}else
			if(0 == d){
				if(EventShow.IsShowing){
					MainMenu.Hide();
					EventShow.Hide();
					PageIdle.Show();
					GuiState = guiIdle;
				}
			}else
			if(-1 == d){
				MainMenu.Hide();
				EventShow.Hide();
				PageIdle.Show();
				GuiState = guiIdle;
			}
		}

		CtlCenter.Task1000Ms();
		//ManageStartCountDown();
		
		if( (Register.IsOnGo) && (Register.IsOnNetwork == 2) ){
			GeneralDev.LcdBlackLightSetOn();
			Register.SlaveModeTask1000Ms();
		}else
		if(MainMenu.OnAction){
			(void)Register.Task1000Ms();
			OnlineCtrl.Task1000Ms();
			//SimRun.Task1000Ms();
			FwUpgrade.Task1000Ms();
			CanNetworkRegister.Task1000Ms();
			DevMask.Task1000Ms();
			RegisterManual.Task1000Ms();
			//CircuitCfg.Task1000Ms();
			DevProperty.Task1000Ms();
			DevStartStop.Task1000Ms();
			DirectPadCfg.Task1000Ms();
			DuplicateCheck.Task1000Ms();
			HwInf.Task1000Ms();
			
			ClearRegistedInf.Task1000Ms();
			ClearLinkageCfg.Task1000Ms();
			ClearDescp.Task1000Ms();
			DevOutputCfg.Task1000Ms();
			DevSensitiveCfg.Task1000Ms();
			DevVaCfg.Task1000Ms();
		}
		RunModeCfg.Task1000Ms();
		
		PageIdle.TaskBySecCall();
		
		SystemPermissionTick++;
		if(SystemPermissionTick > 89){
			SystemPermissionTick = 90;
			SystemPermission = 1;
			if(MainMenu.Active){
				MainMenu.Hide();
				EventShow.MakeFastCheck();
				EventShow.MakeShow();
				if( EventShow.IsShowing ){
						PageIdle.Hide();
						GuiState = guiShowEvent;
				}else{
					GuiState = guiIdle;
					PageIdle.Show();
				}
			}else
			if(PageIdle.IsShowing){
				EventShow.MakeShow();
				if( EventShow.IsShowing ){
					PageIdle.Hide();
					GuiState = guiShowEvent;
				}
			}
		}
		FecBusCRT.Upload2CrtTask();

	}
	
	if(Sys_1s_Rdy){
		Sys_1s_Rdy = 0;
		MaintainHourTick++;
		FecBusCRT.Task1000Ms();
		
		DevAuthorize.Task1000Ms();
		
		uInportExport.Task1000Ms();
		
		MqttData.Task1000Ms();
		
		if(MaintainHourTick > 3599){
			MaintainHourRdy =1;
			Check4MaintainToShow();
			if(LastMaintainTip != IsMaintainTip){
				if(IsSelfChecking == 0){
					StateBarReflashMaintain();
				}
				LastMaintainTip = IsMaintainTip;
			}
			MaintainHourRdy =0;
		}
	}
	
	if(ProtoC2C.RemoteReq.Reg.IsRequsting){
		ProtoC2C.RemoteReq.Reg.IsRequsting = 0;
		if(ProtoC2C.RemoteReq.Reg.RegPath <= dPORT_MAX_COUNT){
			PmsIntervene.IsShow = 0;
			MainMenu.Hide();
			EventShow.Hide();
			PageIdle.Hide();
			GuiState = guiStateRemote;
			SlaveDoRemoteReg();
		}
	}
}

void SlaveRemoteRegDoneResetCallBack()
{
	MainMenu.Hide();
	EventShow.MakeFastCheck();
	EventShow.MakeShow();
	if( EventShow.IsShowing ){
		GuiState = guiShowEvent;
		PageIdle.Hide();
		EventShow.MakeShow();
	}else{
		GuiState = guiIdle;
		EventShow.Hide();
		PageIdle.Show();
	}
}

void Service_InterLink(void)
{
	
}

void Service(void)
{
	//CtlCenter.TaskFreeRun();
	ProcessInnerCanRxMsg();
	ProtoC2C.iBufProcess();
	
	Service_InterLink();
	EventShow.ShowTask();
	
	FecStd.OperationTask();
	Config.OperationTask();
	
	MqttData.OperationTask();
}

void Debug_data_init(void)
{
	unsigned int i,m;
	MainCtl.fData.Split.PortCount = 3;
	MainCtl.fData.Split.ExistTablePort[0] = 0xA5;
	MainCtl.fData.Split.ExistTablePort[1] = 0xA5;
	MainCtl.fData.Split.ExistTablePort[2] = 0xA5;
	
	Port[0].fData.Port.ResistedCount = 32;
	Port[0].fData.Port.OnlineCount = 32;
	Port[0].fData.Port.dType = dgTYPE_PORT;
	Port[0].fData.Port.Id = 1;
	
	Port[1].fData.Port.ResistedCount = 32;
	Port[1].fData.Port.OnlineCount = 32;
	Port[1].fData.Port.dType = dgTYPE_PORT;
	Port[1].fData.Port.Id = 2;
	
	Port[2].fData.Port.ResistedCount = 32;
	Port[2].fData.Port.OnlineCount = 32;
	Port[2].fData.Port.dType = dgTYPE_PORT;
	Port[2].fData.Port.Id = 3;
	
	for(i=0; i<32; i++){
		Port[0].ExistTable[i] = 0xA5;
		m = i%3;
		if(m == 0){
			//EpDev[0][i].fData.Split.dType = dgTYPE_SMOKE_DETECTOR;
			Port[0].dTypeTable[i] = dgTYPE_SMOKE_DETECTOR;
		}else
		if(m == 1){
			//EpDev[0][i].fData.Split.dType = dgTYPE_TEMPE_DETECTOR;
			Port[0].dTypeTable[i] = dgTYPE_TEMPE_DETECTOR;
		}else
		if(m == 2){
			//EpDev[0][i].fData.Split.dType = dgTYPE_DISPLAY_PAD;
			Port[0].dTypeTable[i] = dgTYPE_DISPLAY_PAD;
		}
		//EpDev[0][i].fData.Split.Id = i+1;
		//EpDev[0][i].fData.Split.Flag = dEpStateBitMask;
		
		Port[0].StateTable[i] |= dEpStateBitMask;
	}

	for(i=0; i<32; i++){
		Port[1].ExistTable[i] = 0xA5;
		m = i%3;
		if(m == 0){
			//EpDev[1][i].fData.Split.dType = dgTYPE_SMOKE_DETECTOR;
			Port[1].dTypeTable[i] = dgTYPE_SMOKE_DETECTOR;
		}else
		if(m == 1){
			//EpDev[1][i].fData.Split.dType = dgTYPE_TEMPE_DETECTOR;
			Port[1].dTypeTable[i] = dgTYPE_TEMPE_DETECTOR;
		}else
		if(m == 2){
			//EpDev[1][i].fData.Split.dType = dgTYPE_DISPLAY_PAD;
			Port[1].dTypeTable[i] = dgTYPE_DISPLAY_PAD;
		}
		//EpDev[1][i].fData.Split.Id = i+1;
		//EpDev[1][i].fData.Split.Flag = dEpStateBitMask;
		
		Port[1].StateTable[i] |= dEpStateBitMask;
	}
	
	
	for(i=0; i<9; i++){
		Port[2].ExistTable[i] = 0xA5;
		Port[2].dTypeTable[i] = dgTYPE_SMOKE_DETECTOR;
		//EpDev[2][i].fData.Split.dType = dgTYPE_SMOKE_DETECTOR;
		//EpDev[2][i].fData.Split.Id = i+1;
	}
	for(i=11; i<12; i++){
		Port[2].ExistTable[i] = 0xA5;
		Port[2].dTypeTable[i] = dgTYPE_SMOKE_DETECTOR;
		//EpDev[2][i].fData.Split.dType = dgTYPE_SMOKE_DETECTOR;
		//EpDev[2][i].fData.Split.Id = i+1;
	}
	for(i=16; i<19; i++){
		Port[2].ExistTable[i] = 0xA5;
		Port[2].dTypeTable[i] = dgTYPE_TEMPE_DETECTOR;
		//EpDev[2][i].fData.Split.dType = dgTYPE_TEMPE_DETECTOR;
		//EpDev[2][i].fData.Split.Id = i+1;
	}
	for(i=24; i<32; i++){
		Port[2].ExistTable[i] = 0xA5;
		Port[2].dTypeTable[i] = dgTYPE_DISPLAY_PAD;
		//EpDev[2][i].fData.Split.dType = dgTYPE_DISPLAY_PAD;
		//EpDev[2][i].fData.Split.Id = i+1;
	}
	
	MainCtl.fData.Split.NetworkMode = 1;
	MainCtl.fData.Split.MyNum = 2;

	MainCtl.fData.Split.HandCtlCount = 4;
	MainCtl.fData.Split.ExistTableHandCtl[0] = 0xA5;
	MainCtl.fData.Split.ExistTableHandCtl[1] = 0xA5;
	MainCtl.fData.Split.ExistTableHandCtl[2] = 0xA5;
	MainCtl.fData.Split.ExistTableHandCtl[3] = 0xA5;

	HandCtlBoard[0].fData.HandCtl.ResistedCount = 128;
	HandCtlBoard[1].fData.HandCtl.ResistedCount = 128;
	HandCtlBoard[2].fData.HandCtl.ResistedCount = 128;
	HandCtlBoard[3].fData.HandCtl.ResistedCount = 128;
	
	HandCtlBoard[0].fData.HandCtl.dType = dgTYPE_HAND_CTL;
	HandCtlBoard[1].fData.HandCtl.dType = dgTYPE_HAND_CTL;
	HandCtlBoard[2].fData.HandCtl.dType = dgTYPE_HAND_CTL;
	HandCtlBoard[3].fData.HandCtl.dType = dgTYPE_HAND_CTL;
	
	HandCtlBoard[0].fData.HandCtl.Id = 41;
	HandCtlBoard[1].fData.HandCtl.Id = 42;
	HandCtlBoard[2].fData.HandCtl.Id = 43;
	HandCtlBoard[3].fData.HandCtl.Id = 44;
	
	MainCtl.fData.Split.DirectCtlCount = 6;
	MainCtl.fData.Split.ExistTableDirectCtl[0] = 0xA5;
	MainCtl.fData.Split.ExistTableDirectCtl[1] = 0xA5;
	MainCtl.fData.Split.ExistTableDirectCtl[2] = 0xA5;
	MainCtl.fData.Split.ExistTableDirectCtl[3] = 0xA5;
	MainCtl.fData.Split.ExistTableDirectCtl[4] = 0xA5;
	MainCtl.fData.Split.ExistTableDirectCtl[5] = 0xA5;
	
	DirectCtlBoard[0].fData.DirectCtl.ResistedCount = 64;
	DirectCtlBoard[1].fData.DirectCtl.ResistedCount = 64;
	DirectCtlBoard[2].fData.DirectCtl.ResistedCount = 64;
	DirectCtlBoard[3].fData.DirectCtl.ResistedCount = 64;
	DirectCtlBoard[4].fData.DirectCtl.ResistedCount = 64;
	DirectCtlBoard[5].fData.DirectCtl.ResistedCount = 64;
	DirectCtlBoard[6].fData.DirectCtl.ResistedCount = 64;
	DirectCtlBoard[7].fData.DirectCtl.ResistedCount = 64;
	
	DirectCtlBoard[0].fData.DirectCtl.dType = dgTYPE_DIRECT_CTL;
	DirectCtlBoard[1].fData.DirectCtl.dType = dgTYPE_DIRECT_CTL;
	DirectCtlBoard[2].fData.DirectCtl.dType = dgTYPE_DIRECT_CTL;
	DirectCtlBoard[3].fData.DirectCtl.dType = dgTYPE_DIRECT_CTL;
	DirectCtlBoard[4].fData.DirectCtl.dType = dgTYPE_DIRECT_CTL;
	DirectCtlBoard[5].fData.DirectCtl.dType = dgTYPE_DIRECT_CTL;
	DirectCtlBoard[6].fData.DirectCtl.dType = dgTYPE_DIRECT_CTL;
	DirectCtlBoard[7].fData.DirectCtl.dType = dgTYPE_DIRECT_CTL;
	
	DirectCtlBoard[0].fData.DirectCtl.Id = 49;
	DirectCtlBoard[1].fData.DirectCtl.Id = 50;
	DirectCtlBoard[2].fData.DirectCtl.Id = 51;
	DirectCtlBoard[3].fData.DirectCtl.Id = 52;
	DirectCtlBoard[4].fData.DirectCtl.Id = 53;
	DirectCtlBoard[5].fData.DirectCtl.Id = 54;
	DirectCtlBoard[6].fData.DirectCtl.Id = 55;
	DirectCtlBoard[7].fData.DirectCtl.Id = 56;
}

void CleanUpEvent(void)
{
	
}

void ProcessInnerCanRxMsg()
{
	int i;
	if(InnerCanRxBuf.WriteIndex > (dInnerCanRxBufMaxCount -1))InnerCanRxBuf.WriteIndex = 0;
	if(InnerCanRxBuf.ReadIndex != InnerCanRxBuf.WriteIndex){
		InnerCanRxBuf.ReadIndex++;
		if(InnerCanRxBuf.ReadIndex >= dInnerCanRxBufMaxCount)InnerCanRxBuf.ReadIndex = 0;
		InnerCan.aRxMsg.Ide.BitF.Cmd = InnerCanRxBuf.Body[InnerCanRxBuf.ReadIndex].Ide.BitF.Cmd;
		InnerCan.aRxMsg.Ide.BitF.sAddr = InnerCanRxBuf.Body[InnerCanRxBuf.ReadIndex].Ide.BitF.sAddr;
		InnerCan.aRxMsg.Ide.BitF.dAddr = InnerCanRxBuf.Body[InnerCanRxBuf.ReadIndex].Ide.BitF.dAddr;
		InnerCanRxBuf.Body[InnerCanRxBuf.ReadIndex].Ide.D32 = 0;
		InnerCan.aRxMsg.Dlc = InnerCanRxBuf.Body[InnerCanRxBuf.ReadIndex].Len;
		if(InnerCanRxBuf.Body[InnerCanRxBuf.ReadIndex].Len <9){
			for(i=0; i<InnerCanRxBuf.Body[InnerCanRxBuf.ReadIndex].Len; i++){
				InnerCan.aRxMsg.Data.D8[i] = InnerCanRxBuf.Body[InnerCanRxBuf.ReadIndex].Da[i];
			}
			InnerCan.aRxMsg.Ava = 1;
			InnerCan.ProcRxMsg();
		}
	}
}

void ProcessUserAlramKey(void)
{
	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 = etLinkageStart;
	aIm.Splite.PCtlNum = MainCtl.fData.Split.MyNum ;	//(0,aRxMsg.Data.D8[1]);
	
	aIm.Splite.P0 = 0xF0;	//Board Index
	aIm.Splite.P1 = 0xF0;
	aIm.Splite.P2 = 0xF0;	
	aIm.Splite.dType = 0;			
	aIm.Splite.Data0 = 0x06;
	aIm.Splite.Data1 = 0;//Is Set=1  Recover=0
	aIm.Splite.iByte0	= CmdActiveDefaultSet[cmVAAlramStart][8];
	
	if(AlramVaOnOff){
		FireVaTrenOned = 0;
		
		AlramVaOnOff = 0;
		aIm.Splite.Data0 = CmdActiveDefaultSet[cmVAAlramStop][8];
		aIm.Splite.iByte0	= CmdActiveDefaultSet[cmVAAlramStop][8];
		Record.OperateAdd_AlramSoundOff(MainCtl.fData.Split.MyNum);
		tData[0] = 0;
		tData[1] = 0;
		tData[2] = 0;
		InnerCan.OBufInsert(1, cmVAAlramStop, tData, 3);
	}else{
		AlramVaOnOff = 1;
		aIm.Splite.Data0 = CmdActiveDefaultSet[cmVAAlramStart][8];
		aIm.Splite.iByte0	= CmdActiveDefaultSet[cmVAAlramStart][8];
		Record.OperateAdd_AlramSoundOn(MainCtl.fData.Split.MyNum);
		tData[0] = 0;
		tData[1] = 0;
		tData[2] = 0;
		InnerCan.OBufInsert(1, cmVAAlramStart, tData, 3);
	}
	
	ProtoC2C.PublishEventAlramOnOff(AlramVaOnOff);
}

void ProcessUserSetAutoState()
{
	Record.WriteAllow(AllowAuto, AllowManual);
	Record.ReadAllowSet();
	
	if(AllowAuto){
		if(LanguageEnCn==0)STextAllowAuto.SetText("自动",24);
		else STextAllowAuto.SetText("AUTO",24); 
		STextAllowAuto.TextColor = clNearBlack;
		IconAllowAuto.SetActive();
	}else{
		if(LanguageEnCn==0)STextAllowAuto.SetText("自动",24);
		else STextAllowAuto.SetText("AUTO",24);
		STextAllowAuto.TextColor = clWhiteGray;
		IconAllowAuto.SetDeActive();	
	}
	if(AllowManual){
		//STextAllowManual.SetText("手动",24);
		//STextAllowManual.TextColor = clNearBlack;
		//IconAllowManual.SetActive();
	}else{
		//STextAllowManual.SetText("手动",24);
		//STextAllowManual.TextColor = clWhiteGray;
		//IconAllowManual.SetDeActive();
	}
	STextAllowAuto.Show();
	IconAllowAuto.Show();
	//STextAllowManual.Show();
	//IconAllowManual.Show();
	
	tData[1] =0; tData[1] =0;
	tData[2] =0; tData[3] =0; tData[4] =0; tData[5] =0; tData[6] =0; tData[7] =0;
	if(AllowAuto)tData[1] |= 0x01;
	if(AllowManual)tData[1] |= 0x02;
	InnerCan.OBufInsert(1, 88, tData, 2);
}

void ProcessUserSetManualState()
{
	Record.WriteAllow(AllowAuto, AllowManual);
	Record.ReadAllowSet();
	if(AllowAuto){
		if(LanguageEnCn==0)STextAllowAuto.SetText("自动",24);
		else STextAllowAuto.SetText("AUTO",24);
		STextAllowAuto.TextColor = clNearBlack;
		IconAllowAuto.SetActive();
	}else{
		if(LanguageEnCn==0)STextAllowAuto.SetText("自动",24);
		else STextAllowAuto.SetText("AUTO",24);
		STextAllowAuto.TextColor = clWhiteGray;
		IconAllowAuto.SetDeActive();	
	}
	if(AllowManual){
		//STextAllowManual.SetText("手动",24);
		//STextAllowManual.TextColor = clNearBlack;
		//IconAllowManual.SetActive();
	}else{
		//STextAllowManual.SetText("手动",24);
		//STextAllowManual.TextColor = clWhiteGray;
		//IconAllowManual.SetDeActive();
	}
	STextAllowAuto.Show();
	IconAllowAuto.Show();
	//STextAllowManual.Show();
	//IconAllowManual.Show();
	
	tData[1] =0; tData[1] =0;
	tData[2] =0; tData[3] =0; tData[4] =0; tData[5] =0; tData[6] =0; tData[7] =0;
	if(AllowAuto)tData[1] |= 0x01;
	if(AllowManual)tData[1] |= 0x02;
	InnerCan.OBufInsert(1, 88, tData, 2);
}

void CheckDelayStart()
{
	int i,x;
	unsigned int DiffVal;
	TDMix_Path aFullPath;
	for(i=0; i<dPORT_MAX_COUNT; i++){
		for(x=0; x<252; x++){
			if(Port[i].DelayTick[x] > 601){
				Port[i].DelayTick[x] = 601;
			}else
			if(Port[i].DelayTick[x] >1){
				if(Port[i].DelayTick[x] == 2){
					if(Port[i].DelayToDoEventType[x] == 0){
						Port[i].InterLinkGoStartDev(MainCtl.fData.Split.MyNum, i+1, x+1, Port[i].dTypeTable[x]);
						Port[i].DelayTick[x] =0;
					}else{
						//Add Sim
						Port[i].AddRemoveMsgDelayStartSimulate(1, i+1, x+1, Port[i].dTypeTable[x]);
						Port[i].DelayTick[x] =0;
					}
				}else{
					Port[i].DelayTick[x]--;
				}
			}
		}
	}
	for(i=0; i<dDIRECTCTL_MAX_COUNT; i++){
		for(x=0; x<8; x++){
			if(DirectCtlBoard[i].DelayTick[x] >601){
				DirectCtlBoard[i].DelayTick[x] = 601;
			}else
			if(DirectCtlBoard[i].DelayTick[x] > 1){
				if(DirectCtlBoard[i].DelayTick[x] == 2){
					if(DirectCtlBoard[i].DelayToDoEventType[x] == 0){
						DirectCtlBoard[i].InterLinkGoStartNode(MainCtl.fData.Split.MyNum, i +dADDR_DIRECTPAD_START, x+1);
						DirectCtlBoard[i].DelayTick[x] =0;
					}else{
						//Add Sim
						DirectCtlBoard[i].AddRemoveMsgDelayStartSimulate(1, i +dADDR_DIRECTPAD_START, x +1);
						DirectCtlBoard[i].DelayTick[x] =0;
					}
				}else{
					DirectCtlBoard[i].DelayTick[x]--;
				}
			}
		}
	}
}

void KeepInUiJustEnter()
{
	TipPopup.ClearTickOnEventOcc();
}

void KeepInUiOperating()
{
	SystemPermissionTick =0;
	EventShow.KeyJab();	
}

void SysDoRecover()
{
	if(LanguageEnCn==0){
					TextRender_string24(40, 160, clNearWhite, ContentColor, "1.清除注册配置               ");
				TClearRegistedInf::sIList = &ItemList;
				TClearRegistedInf::	sDoClear();
					LoopCheck1S();
					TextRender_string24(40, 160, clNearWhite, ContentColor, "2.清除联动配置               ");
				TClearLinkageCfg::sIList = &ItemList;
				TClearLinkageCfg::	sDoClear();
					LoopCheck1S();
					TextRender_string24(40, 160, clNearWhite, ContentColor, "3.清除注释及描述             ");
				TClearDescp::		sDoClear();
					LoopCheck1S();
					TextRender_string24(40, 160, clNearWhite, ContentColor, "4.清除打印机设置             ");
				TPrinterSet::		sRecoverSetDefault();
					LoopCheck1S();
					TextRender_string24(40, 160, clNearWhite, ContentColor, "5.清除密码                   ");
				TPasswdChange::		sRecoverSetDefault();
					LoopCheck1S();
					TextRender_string24(40, 160, clNearWhite, ContentColor, "6.锁屏设定                   ");
				TScreenSaveCfg::		sRecoverSetDefault();
					LoopCheck1S();
					TextRender_string24(40, 160, clNearWhite, ContentColor, "7.清除语言设定               ");
				TLanguageCfg::		sRecoverSetDefault();
					LoopCheck1S();
					TextRender_string24(40, 160, clNearWhite, ContentColor, "8.清除工程设定               ");
				TProjectSet::		sRecoverSetDefault();
					LoopCheck1S();
					TextRender_string24(40, 160, clNearWhite, ContentColor, "9.清除维保设定               ");
				TMaintain::			sRecoverSetDefault();
					LoopCheck1S();
					TextRender_string24(40, 160, clNearWhite, ContentColor, "10.清除运行模式              ");
				TRunModeCfg::		sRecoverSetDefault();
					LoopCheck1S();
					TextRender_string24(40, 160, clNearWhite, ContentColor, "11.清除设计数量              ");
				DesignPrmSet.Init();	DesignPrmSet.RecoverSetDefault();
					LoopCheck1S();
					TextRender_string24(40, 160, clNearWhite, ContentColor, "12.清除终端设备设定          ");
				TDevProperty::sIList = &ItemList;
				TDevProperty::		sRecoverSetDefault();
					LoopCheck1S();
					TextRender_string24(40, 160, clNearWhite, ContentColor, "13.清除终端设备屏蔽          ");
				TDevMask::sIList = &ItemList;
				TDevMask::			sRecoverSetDefault();
				(void)DevMask.ReadMaskFromFlash();
					LoopCheck1S();
					TextRender_string24(40, 160, clNearWhite, ContentColor, "14.清除总线盘设定            ");
				TBusPadLnk::sIList = &ItemList;
				TBusPadLnk::		sRecoverSetDefault();
					LoopCheck1S();
					TextRender_string24(40, 160, clNearWhite, ContentColor, "15.清除多线盘设定            ");
				TDirectPadCfg::sIList = &ItemList;	
				TDirectPadCfg::		sRecoverSetDefault();
					LoopCheck1S();
					TextRender_string24(40, 160, clNearWhite, ContentColor, "16.清除广播设定              ");
				TBroadcastCfg::		sRecoverSetDefault();
					LoopCheck1S();
					TextRender_string24(40, 160, clNearWhite, ContentColor, "17.清除FecBus设定            ");
				TLnkFecBusCfg::		sRecoverSetDefault();
					LoopCheck1S();
					TextRender_string24(40, 160, clNearWhite, ContentColor, "18.清除区域设置              ");
				TAreaCfg::			sRecoverSetDefault();
					LoopCheck1S();
					TextRender_string24(40, 160, clNearWhite, ContentColor, "19.清除网络设定              ");
				TCanNetworkRegister::	sRecoverSetDefault();
					LoopCheck1S();
					TextRender_string24(40, 160, clNearWhite, ContentColor, "20.清除网络屏蔽              ");
				TCanNetworkMask::		sRecoverSetDefault();
					LoopCheck1S();
					TextRender_string24(40, 160, clNearWhite, ContentColor, "21.清除CRT设定               ");
				TGraphDevCfg::		sRecoverSetDefault();
					LoopCheck1S();
					
					TextRender_string24(40, 160, clNearWhite, ContentColor, "22.清除已上传的区域机注释信息");
				TfNetworkReg::sRecoverSetDefaultNetworkDescp();
					LoopCheck1S();
					
					TextRender_string24(40, 160, clNearWhite, ContentColor, "23.清除传输设定,蓝牙,Wifi等  ");
					
					
	}else{
					TextRender_string24(40, 160, clNearWhite, ContentColor, "1.Clearing Registed Data           ");
				TClearRegistedInf::sIList = &ItemList;
				TClearRegistedInf::	sDoClear();
					LoopCheck1S();
					TextRender_string24(40, 160, clNearWhite, ContentColor, "2.Clearing Linkage Expression      ");
				TClearLinkageCfg::sIList = &ItemList;
				TClearLinkageCfg::	sDoClear();
					LoopCheck1S();
					TextRender_string24(40, 160, clNearWhite, ContentColor, "3.Clearing Description             ");
				TClearDescp::		sDoClear();
					LoopCheck1S();
					TextRender_string24(40, 160, clNearWhite, ContentColor, "4.Clearing Printer Config          ");
				TPrinterSet::		sRecoverSetDefault();
					LoopCheck1S();
					TextRender_string24(40, 160, clNearWhite, ContentColor, "5.Clearing Password                ");
				TPasswdChange::		sRecoverSetDefault();
					LoopCheck1S();
					TextRender_string24(40, 160, clNearWhite, ContentColor, "6.Clearing Screen Config           ");
				TScreenSaveCfg::		sRecoverSetDefault();
					LoopCheck1S();
					TextRender_string24(40, 160, clNearWhite, ContentColor, "7.Clearing Language Config         ");
				TLanguageCfg::		sRecoverSetDefault();
					LoopCheck1S();
					TextRender_string24(40, 160, clNearWhite, ContentColor, "8.Clearing Project Info            ");
				TProjectSet::		sRecoverSetDefault();
					LoopCheck1S();
					TextRender_string24(40, 160, clNearWhite, ContentColor, "9.Clearing Maintenance Config      ");
				TMaintain::			sRecoverSetDefault();
					LoopCheck1S();
					TextRender_string24(40, 160, clNearWhite, ContentColor, "10.Clearing Run Mode               ");
				TRunModeCfg::		sRecoverSetDefault();
					LoopCheck1S();
					TextRender_string24(40, 160, clNearWhite, ContentColor, "11.Clearing Designed Numbers       ");
				DesignPrmSet.Init();	DesignPrmSet.RecoverSetDefault();
					LoopCheck1S();
					TextRender_string24(40, 160, clNearWhite, ContentColor, "12.Clearing EP-Device Setting      ");
				TDevProperty::sIList = &ItemList;
				TDevProperty::		sRecoverSetDefault();
					LoopCheck1S();
					TextRender_string24(40, 160, clNearWhite, ContentColor, "13.Clearing Device Blocking        ");
				TDevMask::sIList = &ItemList;
				TDevMask::			sRecoverSetDefault();
				(void)DevMask.ReadMaskFromFlash();
					LoopCheck1S();
					TextRender_string24(40, 160, clNearWhite, ContentColor, "14.Clearing Bus Pad Board Config   ");
				TBusPadLnk::sIList = &ItemList;
				TBusPadLnk::		sRecoverSetDefault();
					LoopCheck1S();
					TextRender_string24(40, 160, clNearWhite, ContentColor, "15.Clearing Direct Pad Board Config");
				TDirectPadCfg::sIList = &ItemList;
				TDirectPadCfg::		sRecoverSetDefault();
					LoopCheck1S();
					TextRender_string24(40, 160, clNearWhite, ContentColor, "16.Clearing Broadcast Setting      ");
				TBroadcastCfg::		sRecoverSetDefault();
					LoopCheck1S();
					TextRender_string24(40, 160, clNearWhite, ContentColor, "17.Clearing FecBus Setting         ");
				TLnkFecBusCfg::		sRecoverSetDefault();
					LoopCheck1S();
					TextRender_string24(40, 160, clNearWhite, ContentColor, "18.Clearing Zone Config            ");
				TAreaCfg::			sRecoverSetDefault();
					LoopCheck1S();
					TextRender_string24(40, 160, clNearWhite, ContentColor, "19.Clearing Network Config         ");
				TCanNetworkRegister::	sRecoverSetDefault();
					LoopCheck1S();
					TextRender_string24(40, 160, clNearWhite, ContentColor, "20.Clearing Network Blocking       ");
				TCanNetworkMask::		sRecoverSetDefault();
					LoopCheck1S();
					TextRender_string24(40, 160, clNearWhite, ContentColor, "21.Clearing CRT Setting            ");
				TGraphDevCfg::		sRecoverSetDefault();
					LoopCheck1S();
					
					TextRender_string24(40, 160, clNearWhite, ContentColor, "22.Clearing Regional Description   ");
				TfNetworkReg::sRecoverSetDefaultNetworkDescp();
					LoopCheck1S();
					
					TextRender_string24(40, 160, clNearWhite, ContentColor, "23.Clearing Transmiting Setting    ");
					
	}
	
	//Has Not imp 2025 0331 
	TBlueTooth::		sRecoverSetDefault();
		LoopCheck1S();
	TWifiConn::			sRecoverSetDefault();
		LoopCheck1S();
	TTransmiteDev::		sRecoverSetDefault();
		LoopCheck1S();
		
	InnerCan.SendCmdMaskRemoveAll();
		
	if(LanguageEnCn==0){	
		TextRender_string24(40, 160, clNearWhite, ContentColor, 	"即将重新启动                    ");
	}else{
		TextRender_string24(40, 160, clNearWhite, ContentColor, 	"Restart Now                     ");
	}
	while(1){
		LL_IWDG_ReloadCounter(IWDG1);
		DelayMs(20);
		if(InnerCan.CheckOutBufFastFinish())break;
	}
	UserRequestReset = 0x12345678;
	volatile unsigned int Stt;
	while(1){
		Stt++;
	}
}

void DebugAdd250Fault()
{
	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]);
	
	for(int i=0; i<250; i++){
		aIm.Splite.P0 = 1;			//Board Index
		aIm.Splite.P1 = i+1;
		aIm.Splite.P2 = 0;	//Fault Type			
		aIm.Splite.Data0 = 1;	//Fault Type
		aIm.Splite.Data1 = 1;//Is Set=1  Recover=0

		
		aIm.Splite.dType = 68;
		aIm.Splite.mType	= 0;
		
		CtlCenter.iePushOneMsgFault(aIm);
	}
}

void RecallCrcFaultMsg()
{
	int i;
	SysLoopTick = 0;
	for(i=0;i<dPORT_MAX_COUNT;i++){
		Port[i].sRecallCrcFaultMsg(i);
	}
	for(i=0;i<dHANDCTL_MAX_COUNT;i++){	
		HandCtlBoard[i].sRecallCrcFaultMsg(i);
	}
	for(i=0;i<dDIRECTCTL_MAX_COUNT;i++){	
		DirectCtlBoard[i].sRecallCrcFaultMsg(i);
	}
	
	InterLink.RecallCrcFaultMsg();
	MainCtl.RecallCrcFaultMsg();
	DevMask.RecallCrcFaultMsg();
}






