#include "RuntimeData.h"
#include "DuplicateCheck.h"


#define TextTopOffSet	6
#define ListLineCnt	10

void TDuplicateCheck::DrawFixText()
{
	if(LanguageEnCn==0){
		TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite, CaptionColor, "回路->重码检测");
		TextRender_string24(Lv.Grid.Left[0] + 10, Lv.Grid.Top[0] +6, clNearWhite, "机器号");
		if(MainCtl.fData.Split.NetworkMode == 1)
			IList->STextCtlType.SetText("本集中机",24);
		else if(MainCtl.fData.Split.NetworkMode == 2)
			IList->STextCtlType.SetText("本区域机",24);
		else
			IList->STextCtlType.SetText("本单机  ",24);
		IList->STextCtlType.Show();
		TRoundRect::sDrawBySize(Lv.Grid.Left[0] +10, Lv.Grid.Top[0] + 36, 120, 30, 7, clNearWhite, "开始检测", 14, clNearBlack);
		TextRender_string24(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] +6, clNearWhite, "分回路检测");
		TextRender_string24(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] +38, clNearWhite, "回路号");
		TRoundRect::sDrawBySize(Lv.Grid.Left[1] +10, Lv.Grid.Top[1] + 72, 120, 30, 7, clNearWhite, "回路检测", 14, clNearBlack);
		TextRender_string24(Lv.Grid.Left[2] + 10, Lv.Grid.Top[2] +10, clNearWhite, "操作");
		TRoundRect::sDrawBySize(Lv.Grid.Left[2] +10, Lv.Grid.Top[2] + 38, 76, 30, 7, clNearWhite, "点灯", 14, clNearBlack);
	}else{
		TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite, CaptionColor, "Loop->Duplicate Addr Detect");
		TextRender_string24(Lv.Grid.Left[0] + 10, Lv.Grid.Top[0] +6, clNearWhite, "CtlID");
		if(MainCtl.fData.Split.NetworkMode == 1)
			IList->STextCtlType.SetText("Local Master     ",24);
		else if(MainCtl.fData.Split.NetworkMode == 2)
			IList->STextCtlType.SetText("Local Regional   ",24);
		else
			IList->STextCtlType.SetText("Local Stand Alone",24);
		IList->STextCtlType.Show();
		TRoundRect::sDrawBySize(Lv.Grid.Left[0] +10, Lv.Grid.Top[0] + 36, 120, 30, 7, clNearWhite, " Detect ", 14, clNearBlack);
		TextRender_string24(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] +6, clNearWhite, "Single Loop Check");
		TextRender_string24(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] +38, clNearWhite, "LoopID");
		TRoundRect::sDrawBySize(Lv.Grid.Left[1] +10, Lv.Grid.Top[1] + 72, 120, 30, 7, clNearWhite, " Detect ", 14, clNearBlack);
		TextRender_string24(Lv.Grid.Left[2] + 10, Lv.Grid.Top[2] +10, clNearWhite, "Operation");
		TRoundRect::sDrawBySize(Lv.Grid.Left[2] +10, Lv.Grid.Top[2] + 38, 76, 30, 7, clNearWhite, "LED ", 14, clNearBlack);		
	}
}


void TDuplicateCheck::Init()
{
	IsShow =0;
	IsPermission =0;
	IsFastCmd = 0;
	Tag.Reset();
	if(MainCtl.fData.Split.NetworkMode == 1){
		Tag.InxMin = 0;
	}else{
		Tag.InxMin = 1;
	}
		Tag.InxMin = 1;
	Tag.Inx = Tag.InxMin;
	
	IsChecking =0;
	IsOnNetwork =0;
	CheckWhat =0;
	CheckTick =0;
	IsShowDetail =0;
	CtlNum = MainCtl.fData.Split.MyNum;
	KeepOnMeJustEnter = nullptr;
	KeepUiOnMe = nullptr;
	
	IsDulAddrChecking = 0;
}

void TDuplicateCheck::ResetBackEndData()
{
	//dAddrSdRam_DupCheck
	int i,x;
	volatile unsigned int tAddrCnt;
	PortCnt =0;
	for(i=1; i<=dPORT_MAX_COUNT; i++){
		tAddrCnt = dAddrSdRam_DupCheck + (i*256);
		for(x=0; x<256; x++){
			*(volatile unsigned char *)(tAddrCnt + x) = 0;
		}
	}
}

void TDuplicateCheck::SetData(unsigned char aPort, unsigned char aAddr, unsigned char aCnt)
{
	volatile unsigned int tAddrCnt;
	tAddrCnt = dAddrSdRam_DupCheck + (static_cast<unsigned int>(aPort) * 256) + static_cast<unsigned int>(aAddr);
	*(volatile unsigned char *)(tAddrCnt) = aCnt;
}

unsigned char TDuplicateCheck::GetData(unsigned char aPort, unsigned char aAddr)
{
	volatile unsigned int tAddrCnt;
	unsigned char tCnt;
	tAddrCnt = dAddrSdRam_DupCheck + (static_cast<unsigned int>(aPort) * 256) + static_cast<unsigned int>(aAddr);
	tCnt = *(volatile unsigned char *)(tAddrCnt);
	return tCnt;
}

void TDuplicateCheck::RenderBackGround(void)
{
	RectFillRender(ContentGeometry.Left, ContentGeometry.Top, ContentGeometry.Right, ContentGeometry.Bottom, ContentColor);
}

void TDuplicateCheck::RenderBackGround(unsigned int aClr)
{
	RectFillRender(ContentGeometry.Left, ContentGeometry.Top, ContentGeometry.Right, ContentGeometry.Bottom, aClr);
}

void TDuplicateCheck::DrawSelf(void)
{
	RenderBackGround();
	RectFillRender(Lv.Caption.Left, Lv.Caption.Top, Lv.Caption.Right, Lv.Caption.Bottom, CaptionColor);

	
	for(int i=0; i<5; i++)RectFillRender(Lv.Grid.Left[i], Lv.Grid.Top[i], Lv.Grid.Right[i], Lv.Grid.Bottom[i], ItemColor);
	if(Lv.Panel.Active)RectFillRender(Lv.Panel.Left, Lv.Panel.Top, Lv.Panel.Right, Lv.Panel.Bottom, PanelColor);
	if(Lv.Panel.Active)RectFillRender(Lv.PanelSmall.Left, Lv.PanelSmall.Top, Lv.PanelSmall.Right, Lv.PanelSmall.Bottom, SmallPanelColor);
	IList->EditCtlNum.Init(Lv.Grid.Left[0] + 100, Lv.Grid.Top[0] + 2, 36, 30, 0, 0, 1, clNearWhite);
	if(MainCtl.fData.Split.NetworkMode == 1)
		IList->EditCtlNum.Enable = 1;
	else
		IList->EditCtlNum.Enable = 0;
	IList->EditCtlNum.Visible =1;;
	IList->EditCtlNum.SetMaxLen(2);
	IList->EditCtlNum.Str.FromUInt2Dg( MainCtl.fData.Split.MyNum);
	IList->EditCtlNum.SelectedColor = clBlue;
	IList->EditCtlNum.Enable = 0;
	IList->EditCtlNum.Show();
	CtlNum =  MainCtl.fData.Split.MyNum;
	IList->STextCtlType.Init(Lv.Grid.Left[0] + 146, Lv.Grid.Top[0] + 2, 106, 30, 0, 0, clNearWhite, ItemColor);
	IList->EditPortNum.Init(Lv.Grid.Left[1] + 100, Lv.Grid.Top[1] + 34, 36, 30, 0, 0, 1, clNearWhite);
	IList->EditPortNum.Enable = 1;
	IList->EditPortNum.Visible =1;;
	IList->EditPortNum.SetMaxLen(2);
	IList->EditPortNum.Str.FromUInt2Dg( 1);
	IList->EditPortNum.SelectedColor = clBlue;
	IList->EditPortNum.Show();
	IList->STextLastState.Init(Lv.Grid.Left[2] + 96, Lv.Grid.Top[2] + 6, 180, 30, 0, 0, clNearWhite, ItemColor);
	DrawFixText();
	IList->TextOutClear();
	Check4Selected();
	CtlNumAvalble =1;
	PortNumAvalble =1;
}

void TDuplicateCheck::Show(void)
{	
	IsShow =1;
	RenderBackGround();
	DrawSelf();
}

void TDuplicateCheck::FullRedraw(int Prm)
{	
	if(Prm == 0){
		RenderBackGround();
		DrawSelf();
	}else
	if(Prm == 1){
		RectFillRender(GuiRedrawDataList[1][0], GuiRedrawDataList[1][1], GuiRedrawDataList[1][2], GuiRedrawDataList[1][3], Color);
		for(int i=0; i<3; i++)RectFillRender(Lv.Grid.Left[i], Lv.Grid.Top[i], Lv.Grid.Right[i], Lv.Grid.Bottom[i], ItemColor);
		if(Lv.Panel.Active)RectFillRender(Lv.Panel.Left, Lv.Panel.Top, Lv.Panel.Right, Lv.Panel.Bottom, PanelColor);
		DrawCommonByExReturn();

		if(IsPermission)PmsIntervene4Gui.ReDraw4GuiRecall();
	}
}

void TDuplicateCheck::DrawCommonByExReturn()
{
	//0
	DrawFixText();
	IList->EditCtlNum.Show();
	IList->STextCtlType.Show();
	IList->EditPortNum.Show();
	
	Check4Selected();
}

void TDuplicateCheck::DrawProgress()
{
	//Percent;
	if(LanguageEnCn==0){
		RectFillRender(Lv.PanelSmall.Left, Lv.PanelSmall.Top, Lv.PanelSmall.Right, Lv.PanelSmall.Bottom, SmallPanelColor);
		TextRender_string24(Lv.PanelSmall.Left +4, 		Lv.PanelSmall.Top + 4, 	clNearWhite, SmallPanelColor, "检测进度:");
		TextDigitRender3_24(Lv.PanelSmall.Left +4+108, 		Lv.PanelSmall.Top + 4, 	clNearWhite, SmallPanelColor, Percent);
		TextRender_string24(Lv.PanelSmall.Left +4+108+36, 		Lv.PanelSmall.Top + 4, 	clNearWhite, SmallPanelColor, "%");
		
		if(IsTransmitingFinish){
			TextRender_string24(Lv.PanelSmall.Left +4+200, 		Lv.PanelSmall.Top + 4, 	clNearWhite, SmallPanelColor, "传输完成");
		}else
		if(IsTransmiting){
			TextRender_string24(Lv.PanelSmall.Left +4+200, 		Lv.PanelSmall.Top + 4, 	clNearWhite, SmallPanelColor, "正在传输");
		}
	}else{
		RectFillRender(Lv.PanelSmall.Left, Lv.PanelSmall.Top, Lv.PanelSmall.Right, Lv.PanelSmall.Bottom, SmallPanelColor);
		TextRender_string24(Lv.PanelSmall.Left +4, 		Lv.PanelSmall.Top + 4, 	clNearWhite, SmallPanelColor, "Progress:");
		TextDigitRender3_24(Lv.PanelSmall.Left +4+108, 		Lv.PanelSmall.Top + 4, 	clNearWhite, SmallPanelColor, Percent);
		TextRender_string24(Lv.PanelSmall.Left +4+108+36, 		Lv.PanelSmall.Top + 4, 	clNearWhite, SmallPanelColor, "%");
		
		if(IsTransmitingFinish){
			TextRender_string24(Lv.PanelSmall.Left +4+200, 		Lv.PanelSmall.Top + 4, 	clNearWhite, SmallPanelColor, "Completed");
		}else
		if(IsTransmiting){
			TextRender_string24(Lv.PanelSmall.Left +4+200, 		Lv.PanelSmall.Top + 4, 	clNearWhite, SmallPanelColor, "Transmite");
		}
	}
}

void TDuplicateCheck::DrawDetailOutline()
{
	unsigned int aClr;
	if(IsShowDetail){
		aClr = clBlue;
	}else{
		aClr = clMedGray;
	}
	
	RectFillRender(Lv.Panel.Left,Lv.Panel.Top,Lv.Panel.Right,Lv.Panel.Bottom,PanelColor);
	
	RectFillRender(Lv.Panel.Left,Lv.Panel.Top,Lv.Panel.Left +4,Lv.Panel.Bottom,aClr);
	RectFillRender(Lv.Panel.Left,Lv.Panel.Top,Lv.Panel.Right,Lv.Panel.Top +4,aClr);
	RectFillRender(Lv.Panel.Right-4,Lv.Panel.Top,Lv.Panel.Right,Lv.Panel.Bottom,aClr);
	RectFillRender(Lv.Panel.Left,Lv.Panel.Bottom -4,Lv.Panel.Right,Lv.Panel.Bottom,aClr);
}

void TDuplicateCheck::CheckResult()
{
	const int tOffsetX = 84+4;
	int tP0,tP1;
	int Pages, Lines, Pst;
	unsigned char Val;
	Pages =0;
	Lines = 0;
	Pst = 0;
	for(tP0=1; tP0<=dPORT_MAX_COUNT; tP0++){
		Lines++;
		if(Lines>LinesCount){
			Pages++;
			Lines =1;
			Pst = 0;
		}
		for(tP1=1;tP1<=dEP_MAX_COUNT_PER_PORT;tP1++){
			Val = GetData(tP0,tP1);
			if(Val){
				Pst++;
				if(Pst>=5){
					Pst=0;
					Lines++;
					if(Lines>LinesCount){
						Pages++;
						Lines =1;
						Pst = 0;
					}
				}
			}
		}
	}
	Details.PagesCount = Pages+1;
	Details.PageX = 0;
}

int TDuplicateCheck::TryDrawDetail()
{
	const int tOffsetX = 84+4;
	int tP0,tP1;
	int Pages, Lines, Pst, PageHasDraw;
	unsigned char Val;
	Pages =0;
	Lines = 0;
	Pst = 0;
	PageHasDraw = 0;
	if(Lv.Panel.Active)RectFillRender(Lv.Panel.Left, Lv.Panel.Top, Lv.Panel.Right, Lv.Panel.Bottom, PanelColor);
	DrawDetailOutline();
	for(tP0=1; tP0<=dPORT_MAX_COUNT; tP0++){
		Lines++;
		if(Lines>LinesCount){
			Pages++;
			Lines =1;
			Pst = 1;
		}
		if(Pages == Details.PageX){
			PageHasDraw =1;
			if(LanguageEnCn==0)TextRender_string24(Lv.Panel.Left +4, 		Lv.Panel.Top + 4 + (Lines * 30 -30), 	clNearWhite, PanelColor, 	"回路:");
			else TextRender_string24(Lv.Panel.Left +4, 		Lv.Panel.Top + 4 + (Lines * 30 -30), 	clNearWhite, PanelColor, 			"Loop:");
			TextDigitRender2_24(Lv.Panel.Left +4+48, 		Lv.Panel.Top + 4 + (Lines * 30 -30), 	clNearWhite, PanelColor, tP0);
			TextRender_string24(Lv.Panel.Left +4+48+24, 	Lv.Panel.Top + 4 + (Lines * 30 -30), 	clNearWhite, PanelColor, ":");
		}
		for(tP1=1;tP1<=dEP_MAX_COUNT_PER_PORT;tP1++){
			Val = GetData(tP0,tP1);
			if(Val){
				Pst++;
				if(Pst>5){
					Pst=1;
					Lines++;
					if(Lines>LinesCount){
						Pages++;
						Lines =1;
					}
				}
				if(Pages == Details.PageX){
					PageHasDraw = 1;
					TextDigitRender3_24(Lv.Panel.Left +tOffsetX + (Pst*72-72), 		Lv.Panel.Top + 4 + (Lines * 30 -30), 	clNearWhite, PanelColor, tP1);
					Text[0]='x';Text[2]='\0'; Text[3]='\0';
					if(Val<10){
						Text[1]= Val + '0';
					}else{
						Text[1]= 'B';
					}
					TextRender_string24(Lv.Panel.Left +tOffsetX + (Pst*72-72) + 36, 	Lv.Panel.Top + 4 + (Lines * 30 -30), 	clNearWhite, PanelColor, Text);
				}
			}
		}
	}	
	return PageHasDraw;
}

void TDuplicateCheck::DrawDetail()
{
	if(TryDrawDetail() == 0){
		Details.PageX = 0;
		TryDrawDetail();
	}
}

//______________________________________________________________________________________________________________________________
void TDuplicateCheck::ClearSelected(void)
{
	char *tText;
	switch(Tag.Inx){
		case 0:
			IList->EditCtlNum.Selected = 0;
			IList->EditCtlNum.Show();
			break;
		case 1:
			if(LanguageEnCn==0)tText = "开始检测"; else tText = " Detect ";
			TRoundRect::sDrawBySize(Lv.Grid.Left[0] +10, Lv.Grid.Top[0] + 36, 120, 30, 7, clNearWhite, tText, 14, clNearBlack);
			break;
		case 2:
			IList->EditPortNum.Selected = 0;
			IList->EditPortNum.Show();
			break;
		case 3:
			if(LanguageEnCn==0)tText = "回路检测"; else tText = " Detect ";
			TRoundRect::sDrawBySize(Lv.Grid.Left[1] +10, Lv.Grid.Top[1] + 72, 120, 30, 7, clNearWhite, tText, 14, clNearBlack);
			break;
		case 4:
			if(LanguageEnCn==0)tText = "点灯"; else tText = "LED";
			TRoundRect::sDrawBySize(Lv.Grid.Left[2] +10, Lv.Grid.Top[2] + 38, 76, 30, 7, clNearWhite, tText, 14, clNearBlack);
			break;
		default:
			break;
	}	
}

void TDuplicateCheck::Check4Selected(void)
{
	char *tText;
	switch(Tag.Inx){
		case 0:
			IList->EditCtlNum.Selected = 1;
			IList->EditCtlNum.SetPstTail();
			IList->EditCtlNum.Show();
			IList->EditCtlNum.ShowCursor();
			break;
		case 1:
			if(LanguageEnCn==0)tText = "开始检测"; else tText = " Detect ";
			TRoundRect::sDrawBySize(Lv.Grid.Left[0] +10, Lv.Grid.Top[0] + 36, 120, 30, 7, clBlue, tText, 14, clNearWhite);
			break;
		case 2:
			IList->EditPortNum.Selected = 1;
			IList->EditPortNum.SetPstTail();
			IList->EditPortNum.Show();
			IList->EditPortNum.ShowCursor();
			break;
		case 3:
			if(LanguageEnCn==0)tText = "回路检测"; else tText = " Detect ";
			TRoundRect::sDrawBySize(Lv.Grid.Left[1] +10, Lv.Grid.Top[1] + 72, 120, 30, 7, clBlue, tText, 14, clNearWhite);
			break;
		case 4:
			if(LanguageEnCn==0)tText = "点灯"; else tText = "LED";
			TRoundRect::sDrawBySize(Lv.Grid.Left[2] +10, Lv.Grid.Top[2] + 38, 76, 30, 7, clBlue, tText, 14, clNearWhite);
			break;
		
		default:

			break;
	}	
}

void TDuplicateCheck::PermissionCmdReturnDraw()
{
	int x,y;
	Check4Selected();
	if(Lv.Panel.Active)RectFillRender(Lv.Panel.Left, Lv.Panel.Top, Lv.Panel.Right, Lv.Panel.Bottom, PanelColor);
	DrawDetailOutline();
	if(IsChecking && (Percent == 100)){
		
	}
}
//_____________________________________________________________________________________________________________________________

void TDuplicateCheck::PermissionCmdGO()
{
	if(PermissionCmd == 0){
		ResetBackEndData();
		IList->RealtimeData.Reset();
		IList->TextOutClear();
		Data[0] = 0;
		Data[1] = CtlNum;
		Data[2] = 0xFF;
		Data[3] = 0;
		Data[4] = 0;
		Data[5] = 0;
		Data[6] = 0;
		Data[7] = 1;
		InnerCan.OBufInsert(1, cmDuplicateAddrCheckStart, Data, 3);
		//IList->STextLastState.SetText("全部回路探测",24);
		//IList->STextLastState.Show();
		
		IsShowDetail = 0;
		IsProgressUpdata =0;
		IsDetailUpdata =0;
		WastingTime = 0;		
		
		IsChecking =1;
		IsTransmiting =0;
		IsTransmitingFinish = 0;
		if(CtlNum == MainCtl.fData.Split.MyNum){
			IsOnNetwork =0;
			CheckWhat =0;
			Percent =0;
			DrawProgress();
		}else{
			IsOnNetwork =1;
		}
		IsDulAddrChecking =1;
		if(KeepUiOnMe != nullptr)KeepUiOnMe();
	}else
	if(PermissionCmd == 1){
		ResetBackEndData();
		IList->RealtimeData.Reset();
		IList->TextOutClear();
		int iVal = IList->EditPortNum.Str.ToInteger();
		if( (iVal) && (iVal<=dPORT_MAX_COUNT)){
			Data[0] = 0;
			Data[1] = CtlNum;
			Data[2] = iVal;
			Data[3] = 0;
			Data[4] = 0;
			Data[5] = 0;
			Data[6] = 0;
			Data[7] = 1;
			InnerCan.OBufInsert(1, cmDuplicateAddrCheckStart, Data, 3);
			//IList->STextLastState.SetText("单回路探测",24);
			//IList->STextLastState.Show();

			IsShowDetail = 0;			
			IsProgressUpdata =0;
			IsDetailUpdata =0;
			WastingTime =0;
			Percent =0;
			
			IsChecking =1;
			IsTransmiting =0;
			IsTransmitingFinish = 0;
			if(CtlNum == MainCtl.fData.Split.MyNum){
				IsOnNetwork =0;
				CheckWhat =iVal;
				iVal--;
				DrawProgress();
			}else{
				IsOnNetwork =1;
			}
			IsDulAddrChecking =1;
			if(KeepUiOnMe != nullptr)KeepUiOnMe();
		}
	}else
	if(PermissionCmd == 2){
		//Light On
		TCanHeaderID aIde;
		if(CtlNum == MainCtl.fData.Split.MyNum){
			aIde.BitF.Cmd = cmDevLightOn;
			aIde.BitF.Pri = 1;
			aIde.BitF.sLvl = 1;
			aIde.BitF.dLvl = 7;
			aIde.BitF.sAddr = 0;
			aIde.BitF.dAddr = 127;
			for(int i=4; i<8; i++){
				Data[i] = 0;
			}
			Data[0] = 0;
			Data[1] = 0xFF;
			Data[2] = 1;
			InnerCan.OBufInsert(1, cmDuplicateAddrEpDeviceLed, Data, 3);
		}
	}
}

TGuiMsgReturn TDuplicateCheck::KeyIn(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	TGuiMsgReturn PmsRtnMsg;
	unsigned char aP0,aP1,Updata;
	unsigned char x,y,aCh;
	Updata = 0;
	
	if(IsPermission){
		PmsRtnMsg = PmsIntervene4Gui.KeyIn(aKey);
		if(PmsRtnMsg == guiMsgOkAndReturn){
			//Enter And Run Go
			IsPermission = 0;
			PermissionCmdReturnDraw();
			PermissionCmdGO();
		}else
		if(PmsRtnMsg == guiMsgReturn){
			//Not Enter Redraw
			IsPermission = 0;
			PermissionCmdReturnDraw();
		}
		return aMsg;
	}
	
	if(IsShowDetail){
		switch(aKey){
			case VK_LEFT:
			case VK_UP:
				if(Details.PageX>0){
					Details.PageX--;
				}
				
				DrawDetail();
				break;
			case VK_RIGHT:
			case VK_DOWN :
				Details.PageX++;
				if(Details.PageX>=(Details.PagesCount)){
					Details.PageX =0;
				}
				DrawDetail();
				break;
			case VK_TAB:
				if(IsTransmitingFinish){
					IsTransmitingFinish =0;
					IsShowDetail = 0;
					DrawDetailOutline();
				}
				break;
			case VK_RETURN:
				IsShow = 0;
				SendEndCmd();
				aMsg = guiMsgReturn;
				break;
		}
		return aMsg;
	}
	
	switch(aKey){
		case VK_0:
		case VK_1:
		case VK_2:
		case VK_3:
		case VK_4:
		case VK_5:
		case VK_6:
		case VK_7:
		case VK_8:
		case VK_9:
		case VK_DELETE:
		case VK_LEFT:
		case VK_RIGHT:
			if(Tag.Inx == 0){
				IList->EditCtlNum.KeyIn(aKey);
				int iVal = IList->EditCtlNum.Str.ToInteger();
				if( (iVal > 0) && (iVal <= 16) ){
					IList->EditCtlNum.SelectedColor = clBlue;
					CtlNumAvalble = 1;
				}else{
					IList->EditCtlNum.SelectedColor = clRed;
					CtlNumAvalble = 0;
				}
				IList->EditCtlNum.Show();
			}else
			if(Tag.Inx == 2){
				IList->EditPortNum.KeyIn(aKey);
				int iVal = IList->EditPortNum.Str.ToInteger();
				if( (iVal > 0) && (iVal <= dPORT_MAX_COUNT) ){
					IList->EditPortNum.SelectedColor = clBlue;
					PortNumAvalble = 1;
				}else{
					IList->EditPortNum.SelectedColor = clRed;
					PortNumAvalble = 0;
				}
				IList->EditPortNum.Show();
			}
			break;

		case VK_UP:
			if(Tag.Inx>Tag.InxMin){
				ClearSelected();
				Tag.Inx--;
				Check4Selected();
			}else{
				ClearSelected();
				Tag.Inx=4;
				Check4Selected();
			}
			break;
		case VK_DOWN :
			if(Tag.Inx<4){
				ClearSelected();
				Tag.Inx++;
				Check4Selected();
			}else{
				ClearSelected();
				Tag.Inx=Tag.InxMin;
				Check4Selected();
			}
			break;
		case VK_EXECUTE:
			if(Tag.Inx == 1){
				if(CtlNumAvalble){
					if(SystemPermission >1){
						PermissionCmd = 0;
						PermissionCmdGO();
					}else{
						PermissionCmd = 0;
						IsPermission = 1;
						PmsIntervene4Gui.Show4Gui(Lv.Panel.Left + 16, Lv.Panel.Top + 16, Lv.Panel.Width -34, 140, 2);
					}
				}
			}else
			if(Tag.Inx == 3){
				if( (CtlNumAvalble) && (PortNumAvalble) ){
					if(SystemPermission >1){
						PermissionCmd = 1;
						PermissionCmdGO();
					}else{
						PermissionCmd = 1;
						IsPermission = 1;
						PmsIntervene4Gui.Show4Gui(Lv.Panel.Left + 16, Lv.Panel.Top + 16, Lv.Panel.Width -34, 140, 2);
					}
				}
			}else
			if(Tag.Inx == 4){
				if( 1 ){
					if(SystemPermission >1){
						PermissionCmd = 2;
						PermissionCmdGO();
					}else{
						PermissionCmd = 2;
						IsPermission = 1;
						PmsIntervene4Gui.Show4Gui(Lv.Panel.Left + 16, Lv.Panel.Top + 16, Lv.Panel.Width -34, 140, 2);
					}
				}
			}
			break;
		case VK_TAB:
			//IsShowDetail = 1;
			//DrawDetailOutline();
			break;
		case VK_RETURN:
			IsShow = 0;
			SendEndCmd();
			aMsg = guiMsgReturn;
			break;
	}
	return aMsg;	
}

void TDuplicateCheck::InnerCanPushData(unsigned char aCmd, unsigned char *pData)
{
	unsigned int i,x,v, ov, tPortCnt, RecVal, FullVal;
	if( aCmd == cmDuplicateAddrCheckPrg ){
		if( (pData[1]) && (pData[1]<=dPORT_MAX_COUNT) ){
			x = pData[1];
			v = pData[2];
			if(v>100)v=100;
			IList->RealtimeData.Data[0][x]=v;
			if(CheckWhat == 0){
				tPortCnt=0;
				for(i=0; i<dPORT_MAX_COUNT;i++)if(MainCtl.fData.Split.ExistTablePort[i])tPortCnt++;
				FullVal = tPortCnt * 100;
			}else{
				FullVal = 100;
			}
			RecVal = 0;
			for(i=1;i<=dPORT_MAX_COUNT;i++){
				RecVal += static_cast<unsigned int>(IList->RealtimeData.Data[0][i]);
			}
			tPortCnt = RecVal * 100 / FullVal;
			Percent = static_cast<unsigned char>(tPortCnt % 101);
			IsProgressUpdata =1;
		}
	}else
	if(aCmd == cmDuplicateAddrData){
		if( (pData[1]) && (pData[1]<=dPORT_MAX_COUNT) && (pData[2] == 0)){
			x = pData[1];
			if(pData[4] && pData[5]){
				SetData(x,pData[4],pData[5]);
				IsDetailUpdata =1;
				IsTransmiting =1;
			}
			if(pData[6] && pData[7]){
				SetData(x,pData[6],pData[7]);
				IsDetailUpdata =1;
				IsTransmiting =1;
			}
		}			
	}else
	if(aCmd == cmDuplicateAddrCheckEnd){
		if( (pData[1]) && (pData[1]<=dPORT_MAX_COUNT) ){
			x = pData[1];
			v = pData[2];
			if(1){
				IList->RealtimeData.Data[1][x]=1;
				if(CheckWhat == 0){
					tPortCnt=0;
					for(i=0; i<dPORT_MAX_COUNT;i++)if(MainCtl.fData.Split.ExistTablePort[i])tPortCnt++;
				}else{
					tPortCnt = 1;
				}
				RecVal = 0;
				for(i=1;i<=dPORT_MAX_COUNT;i++){
					RecVal += IList->RealtimeData.Data[1][i];
				}
				if(RecVal == tPortCnt){
					IsDetailUpdata = 1;
					IsTransmitingFinish = 1;
					IsProgressUpdata =1;
				}					
			}
		}
	}
}

void TDuplicateCheck::SendEndCmd()
{
	TCanHeaderID aIde;
	aIde.BitF.Cmd = cmDuplicateAddrCheckEnd;
	aIde.BitF.Pri = 1;
	aIde.BitF.sLvl = 1;
	aIde.BitF.dLvl = 7;
	aIde.BitF.sAddr = 0;
	aIde.BitF.dAddr = 127;
	for(int i=0; i<8; i++){
		Data[i] = 0;
	}
	Data[0] = 0;
	Data[1] = CtlNum;
	Data[2] = 0xFE;

	InnerCan.Buf4GuiSend.Insert(aIde, Data,6);
}

void TDuplicateCheck::Task1000Ms()
{
	unsigned int ttProgress, tt100, Val;
	if(IsShow == 0){
		IsDulAddrChecking = 0;
		return;
	}
	if(IsChecking == 0)return;
	if(WastingTime<360){
		WastingTime++;
		if(KeepUiOnMe != nullptr)KeepUiOnMe();
	}
	if(IsOnNetwork == 0){
		if(IsProgressUpdata){
			IsProgressUpdata =0;
			DrawProgress();
		}
		if( IsDetailUpdata ){
			IsShowDetail =1;
			IsDetailUpdata = 0;
			CheckResult();
			DrawDetailOutline();
			DrawDetail();
		}
	}else{
		
	}
}

int TDuplicateCheck::ExtRequst(unsigned char Prm)
{
	IsDulAddrChecking = 0;
	IsShow =0; 
	SendEndCmd(); 
	return 0;
}
	


