#include "RuntimeData.h"
#include "DevProperty.h"
#include "DevSensitiveCfg.h"
#include "DevMask.h"
#include "server.h"

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

static const short svGrid1X[20] = {
	13, 52, 91, 130, 169, 208, 247, 286, 325, 364,      403, 442, 481, 520, 559, 598,  637, 676, 715, 754
};
static const short svGrid1X2[20] = {
	48, 87, 126, 165, 204, 243, 282, 321, 360, 399,      438, 477, 516, 555, 594, 633,  672, 711, 750, 789
};

void TDevSensitiveCfg::DrawFixText()
{
	if(LanguageEnCn==0){
		TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite, CaptionColor, "工程->批量查询烟感灵敏度");	//"工程->批量设置烟感灵敏度");
		TextRender_string24(Lv.Grid.Left[0] +16, Lv.Grid.Top[0] +  4, clNearWhite, ItemColor, "本机带烟感设备数量:");
		TextRender_string24(Lv.Grid.Left[1] +16, Lv.Grid.Top[1] + 4, clNearWhite, ItemColor, "设定回路地址段以查询");
		TextRender_string24(Lv.Grid.Left[1] +16, Lv.Grid.Top[1] + 38, clNearWhite, ItemColor, "回路号");
		TextRender_string24(Lv.Grid.Left[1] +16, Lv.Grid.Top[1] + 72, clNearWhite, ItemColor, "起始地址");
		TextRender_string24(Lv.Grid.Left[1] +16, Lv.Grid.Top[1] + 106, clNearWhite, ItemColor, "结束地址");
		TextRender_string24(Lv.Grid.Left[1] +16, Lv.Grid.Top[1] + 140, clNearWhite, ItemColor, "灵敏度");
		TextRender_string24(Lv.Grid.Left[2] +16, Lv.Grid.Top[2] + 8, clNearWhite, ItemColor, "查询");			//"查询或执行设定");
		TRoundRect::sDrawBySize(Lv.Grid.Left[2] +40, Lv.Grid.Top[2] + 42, 120, 30, 7, clNearWhite, "开始查询", 14, clNearBlack);
		//TRoundRect::sDrawBySize(Lv.Grid.Left[2] +180, Lv.Grid.Top[2] + 42, 120, 30, 7, clNearWhite, "保存配置", 14, clNearBlack);
	}else{
		TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite, CaptionColor, "Project->Smoke Detetor Sensitivity");
		TextRender_string24(Lv.Grid.Left[0] +16, Lv.Grid.Top[0] +  4, clNearWhite, ItemColor, "Smoke Detetors:");
		TextRender_string24(Lv.Grid.Left[1] +16, Lv.Grid.Top[1] + 4, clNearWhite, ItemColor, "Set The Scope Of One Loop");
		TextRender_string24(Lv.Grid.Left[1] +16, Lv.Grid.Top[1] + 38, clNearWhite, ItemColor, "Loop");
		TextRender_string24(Lv.Grid.Left[1] +16, Lv.Grid.Top[1] + 72, clNearWhite, ItemColor, "Begin AD");
		TextRender_string24(Lv.Grid.Left[1] +16, Lv.Grid.Top[1] + 106, clNearWhite, ItemColor, "End Addr");
		TextRender_string24(Lv.Grid.Left[1] +16, Lv.Grid.Top[1] + 140, clNearWhite, ItemColor, "Sensit-y");
		TextRender_string24(Lv.Grid.Left[2] +16, Lv.Grid.Top[2] + 8, clNearWhite, ItemColor, "Query");				//"Query Or Set");
		TRoundRect::sDrawBySize(Lv.Grid.Left[2] +40, Lv.Grid.Top[2] + 42, 120, 30, 7, clNearWhite, " Query  ", 14, clNearBlack);
		//TRoundRect::sDrawBySize(Lv.Grid.Left[2] +180, Lv.Grid.Top[2] + 42, 120, 30, 7, clNearWhite, " Config ", 14, clNearBlack);
	}		
}

void TDevSensitiveCfg::Init()
{
	Path0 = 1;
	StartAddr = 1;
	EndAddr = 250;
	State = Idle;
	Tag.Inx = 0;
	WriteResult =0;
	WritingCfByte = TDevProperty::dSmokeSensGrade_MarkValue[1];
	IList = nullptr;
	KeepOnMeJustEnter = nullptr;
	KeepUiOnMe = nullptr;
}

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

void TDevSensitiveCfg::DrawSelf(void)
{
	RenderBackGround();

	RectFillRender(Lv.Caption.Left, Lv.Caption.Top, Lv.Caption.Right, Lv.Caption.Bottom, CaptionColor);

	for(int i=0; i<Lv.Grid.UsingCount; 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);

	CheckHostCount();
	TextDigitRender5_24(Lv.Grid.Left[0] +244, Lv.Grid.Top[0] + 4, clNearWhite, ItemColor, Count.Total);
	
	IList->Edit[0].Init(Lv.Grid.Left[1] +114, Lv.Grid.Top[1] + 36, 100,30,0,0,1,clNearBlack);
	IList->Edit[0].SetMaxLen(2);
	IList->Edit[0].Str.Clear();
	TMyString::sFrom2Dg(Path0,Text);
	IList->Edit[0].SetText(Text, 24);
	IList->Edit[0].SetPstTail();
	IList->Edit[0].Show(); 
	
	IList->Edit[1].Init(Lv.Grid.Left[1] +114, Lv.Grid.Top[1] + 70, 100,30,0,0,1,clNearBlack);
	IList->Edit[1].SetMaxLen(3);
	IList->Edit[1].Str.Clear();
	TMyString::sFrom3Dg(StartAddr,Text);
	IList->Edit[1].SetText(Text, 24);
	IList->Edit[1].Show();
	
	IList->Edit[2].Init(Lv.Grid.Left[1] +114, Lv.Grid.Top[1] + 104, 100,30,0,0,1,clNearBlack);
	IList->Edit[2].SetMaxLen(3);
	IList->Edit[2].Str.Clear();
	TMyString::sFrom3Dg(EndAddr,Text);
	IList->Edit[2].SetText(Text, 24);
	IList->Edit[2].Show();
	
	IList->Edit[3].Init(Lv.Grid.Left[1] +114, Lv.Grid.Top[1] + 138, 100,30,0,0,1,clNearBlack);
	RedrawCfgByte();
	DrawFixText();
	
	InitReadData();
	Check4Selected();
	
}

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

void TDevSensitiveCfg::Init4DrawRead()
{
	unsigned int x, y, x2, y2, w, h;

	Bound.SetBySize(0, 36, 800, 406);
	Content.Set(Bound.Left +3, Bound.Top+3 + 30, Bound.Right-4, Bound.Bottom -4);
	
	VertLineRender(Bound.Left,		Bound.Top, 			Bound.Height -1,			0xFFFFFFFF);
	VertLineRender(Bound.Right -1,	Bound.Top +1,		Bound.Height -2,			0xFF808080);
	VertLineRender(Bound.Right,		Bound.Top,			Bound.Height,			0xFF404040);
	
	HorizLineRender(Bound.Left, 		Bound.Top, 			Bound.Width - 1, 			0xFFFFFFFF);
	HorizLineRender(Bound.Left +1, 	Bound.Bottom -1,  	Bound.Width - 2, 			0xFF808080);
	HorizLineRender(Bound.Left,		Bound.Bottom,		Bound.Width,			0xFF404040);
	
	x = Bound.Left +2 ;	
	x2 = Bound.Right -2;
	y = Bound.Top +2;
	y2 = Bound.Bottom - 2;
	w = Bound.Width -4;
	h = Bound.Height - 4;
	VertLineRender	(x,		y, 		h-1,	0xFF808080);
	HorizLineRender	(x,		y, 		w-1,	0xFF808080);
		
	VertLineRender	(x+1,		y+1, 		h-2,	0xFF404040);
	HorizLineRender	(x+1,		y+1, 		w-2,	0xFF404040);
	
	VertLineRender	(x2-1,	y+1, 		h-2,	0xFFD4D0C8);
	HorizLineRender	(x+1,		y2-1, 	w-2,	0xFFD4D0C8);

	VertLineRender	(x2,		y, 	  	h,	0xFFFFFFFF);
	HorizLineRender	(x,		y2, 		w,	0xFFFFFFFF);

	RectFillRender(Content.Left, Content.Top, Content.Right, Content.Bottom, clNearWhite);
	
	#define GS	24
	#define TS	96	
	
	unsigned int aLeft, aTop, aTopR;
	
	aLeft = Content.Left + 10;
	aTop = Content.Top +4;
	if(LanguageEnCn==0)TextRender_string24(aLeft,aTop,clNearBlack, "回路");
	else TextRender_string24(aLeft,aTop,clNearBlack, "Loop");
	TextDigitRender2_24(aLeft +50, aTop, clNearBlack, clNearWhite, Path0);
	
	aLeft = Content.Left + 100;
	aTop = Content.Top +4;
	aTopR = Content.Top +6;	
	
	RectFillRender( aLeft, aTopR, aLeft + 20, aTopR + 18, clSilver);
	aLeft += GS;
	if(LanguageEnCn==0)TextRender_string24(aLeft,aTop,clNearBlack, "非烟感");
	else TextRender_string24(aLeft,aTop,clNearBlack, "Others");	
	aLeft += TS;
	
	RectFillRender( aLeft, aTopR, aLeft + 20, aTopR + 18, clRed);
	aLeft += GS;
	if(LanguageEnCn==0)TextRender_string24(aLeft,aTop,clNearBlack, "未读");
	else TextRender_string24(aLeft,aTop,clNearBlack, "UnRead");	
	aLeft += TS;
	
	RectFillRender( aLeft, aTopR, aLeft + 20, aTopR + 18, clDeepGreen);
	aLeft += GS;
	if(LanguageEnCn==0)TextRender_string24(aLeft,aTop,clNearBlack, "灵敏");
	else TextRender_string24(aLeft,aTop,clNearBlack, "Std ");	
	aLeft += TS -20;
	
	RectFillRender( aLeft, aTopR, aLeft + 20, aTopR + 18, clTeal);
	aLeft += GS;
	if(LanguageEnCn==0)TextRender_string24(aLeft,aTop,clNearBlack, "中灵敏");
	else TextRender_string24(aLeft,aTop,clNearBlack, "Middle");	
	aLeft += TS;
	
	RectFillRender( aLeft, aTopR, aLeft + 20, aTopR + 18, clNavy);
	aLeft += GS;
	if(LanguageEnCn==0)TextRender_string24(aLeft,aTop,clNearBlack, "高灵敏");
	else TextRender_string24(aLeft,aTop,clNearBlack, "ExHigh");	
	aLeft += TS;
	
	RectFillRender( aLeft, aTopR, aLeft + 20, aTopR + 18, clPurple);
	aLeft += GS;
	if(LanguageEnCn==0)TextRender_string24(aLeft,aTop,clNearBlack, "超灵敏");
	else TextRender_string24(aLeft,aTop,clNearBlack, "Ultra ");
	aLeft += TS;
}

void TDevSensitiveCfg::Draw4Read()
{
	int a, x,y,t,b;
	unsigned char p = Path0-1;
	unsigned int tClr;
	RectFillRender(Content.Left, Content.Top + 30, Content.Right, Content.Bottom, clNearWhite);
	if(p>dPORT_MAX_COUNT)return;
	if(IList == nullptr)return;
	for(a=0; a<dEP_MAX_COUNT_PER_PORT; a++){
		x = a % 20;
		y = a /20;
		t = (Content.Top + 34) + (25 * y);
		b = t + 21;
		if( Port[p].ExistTable[a] == 0xA5 ){
			if( Port[p].dTypeTable[a] == dgTYPE_SMOKE_DETECTOR ){
				if(IList->ReadWriteData.D8[a+1]){
					if(IList->RealtimeData.Data[0][a+1] == TDevProperty::dSmokeSensGrade_MarkValue[1]){
						//Is Setting Check
						tClr = clDeepGreen;
					}else
					if(IList->RealtimeData.Data[0][a+1] == TDevProperty::dSmokeSensGrade_MarkValue[2]){
						//Is Not Setting Check
						tClr = clTeal;
					}else
					if(IList->RealtimeData.Data[0][a+1] == TDevProperty::dSmokeSensGrade_MarkValue[3]){
						//Is Not Setting Check
						tClr = clNavy;
					}else
					if(IList->RealtimeData.Data[0][a+1] == TDevProperty::dSmokeSensGrade_MarkValue[4]){
						//Is Not Setting Check
						tClr = clPurple;
					}else{
						tClr = clBlack;
					}
				}else{
					//Still wait for Done This Addr
					tClr = clRed;
				}
				RectFillRender( svGrid1X[x], t, svGrid1X2[x], b, tClr);
			}else{
				RectFillRender( svGrid1X[x], t, svGrid1X2[x], b, clSilver);	
			}
			TMyString::sFrom3Dg(a+1, Text);
			lcd_draw_str_8x16( svGrid1X[x]+6, t+5, clNearWhite, Text);
		}
	}
}

void TDevSensitiveCfg::FullRedraw(int Prm)
{
	RenderBackGround();

	RectFillRender(Lv.Caption.Left, Lv.Caption.Top, Lv.Caption.Right, Lv.Caption.Bottom, CaptionColor);
	
	for(int i=0; i<Lv.Grid.UsingCount; 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);
	
	CheckHostCount();
	TextDigitRender5_24(Lv.Grid.Left[0] +244, Lv.Grid.Top[0] + 4, clNearWhite, ItemColor, Count.Total);
	
	IList->Edit[0].Show(); 
	IList->Edit[1].Show();
	IList->Edit[2].Show();
	RedrawCfgByte();
	DrawFixText();
	Check4Selected();
	if(State != RangeReading){
		DrawWriteResult(0);
	}
	
	if(IsPermission)PmsIntervene4Gui.ReDraw4GuiRecall();
}

void TDevSensitiveCfg::PermissionCmdReturnDraw()
{
	FullRedraw(1);
}

void TDevSensitiveCfg::RedrawCfgByte()
{
	if(IList == nullptr)return;
	if(LanguageEnCn==0){
		switch(WritingCfByte){
			case TDevProperty::dSmokeSensGrade_MarkValue[1]:
				IList->Edit[3].SetText("灵敏  ", 24);
				break;
			case TDevProperty::dSmokeSensGrade_MarkValue[2]:
				IList->Edit[3].SetText("中灵敏", 24);
				break;
			case TDevProperty::dSmokeSensGrade_MarkValue[3]:
				IList->Edit[3].SetText("高灵敏", 24);
				break;
			case TDevProperty::dSmokeSensGrade_MarkValue[4]:
				IList->Edit[3].SetText("超灵敏", 24);
				break;	
		}
	}else{
		switch(WritingCfByte){
			case TDevProperty::dSmokeSensGrade_MarkValue[1]:
				IList->Edit[3].SetText("Normal", 24);
				break;
			case TDevProperty::dSmokeSensGrade_MarkValue[2]:
				IList->Edit[3].SetText("Middle", 24);
				break;
			case TDevProperty::dSmokeSensGrade_MarkValue[3]:
				IList->Edit[3].SetText("ExHigh", 24);
				break;
			case TDevProperty::dSmokeSensGrade_MarkValue[4]:
				IList->Edit[3].SetText("Ultra ", 24);
				break;	
		}
	}		
	IList->Edit[3].Show();
}

void TDevSensitiveCfg::DrawWriteTime()
{
	unsigned int tClr;
	if(IsSelfChecking != 0)return;
	if(RunData.WaitTick < 300)tClr = clNearWhite; else tClr = clRed;
	if(LanguageEnCn==0)TextRender_string24(TipPoint.x+220, TipPoint.y, clNearWhite, ContentColor, 	"用时:     ");
	else TextRender_string24(TipPoint.x+220, TipPoint.y, clNearWhite, ContentColor, 			"Time:     ");
	TextDigitRender3_24(TipPoint.x+220+60, TipPoint.y, tClr, ContentColor, RunData.WaitTick);
}

void TDevSensitiveCfg::DrawWriteResult()
{
	if(LanguageEnCn==0){
		if(WriteResult == 0){
			TextRender_string24(TipPoint.x, TipPoint.y, clNearWhite, ContentColor, "          ");
		}else
		if(WriteResult == 1){
			TextRender_string24(TipPoint.x, TipPoint.y, clNearWhite, ContentColor, "写入成功  ");
		}else
		if(WriteResult == 2){
			TextRender_string24(TipPoint.x, TipPoint.y, clNearWhite, ContentColor, "写入失败  ");
		}else
		if(WriteResult == 3){
			TextRender_string24(TipPoint.x, TipPoint.y, clNearWhite, ContentColor, "正在写入  ");
		}
	}else{
		if(WriteResult == 0){
			TextRender_string24(TipPoint.x, TipPoint.y, clNearWhite, ContentColor, "          ");
		}else
		if(WriteResult == 1){
			TextRender_string24(TipPoint.x, TipPoint.y, clNearWhite, ContentColor, "Wrote OK       ");
		}else
		if(WriteResult == 2){
			TextRender_string24(TipPoint.x, TipPoint.y, clNearWhite, ContentColor, "Failed To Write");
		}else
		if(WriteResult == 3){
			TextRender_string24(TipPoint.x, TipPoint.y, clNearWhite, ContentColor, "Writing        ");
		}
	}
}

void TDevSensitiveCfg::DrawWriteResult(unsigned char aResult)
{
	WriteResult = aResult;
	DrawWriteResult();
}

void TDevSensitiveCfg::InitReadData()
{
	int i;
	RunData.WaitTick =0;
	RunData.RecReadUpData = 0;
	if(IList == nullptr)return;
	IList->ReadWriteData.Reset();
	IList->RealtimeData.Reset();
}

void TDevSensitiveCfg::InitWriteData()
{
	RunData.WaitTick =0;
	RunData.RecWriteOk =0;
}

void TDevSensitiveCfg::CheckHostCount()
{
	int x,y;
	Count.Total =0;
	for(x=0; x<dPORT_MAX_COUNT; x++){
		if(MainCtl.fData.Split.ExistTablePort[x]){
			for(y=0; y<dEP_MAX_COUNT_PER_PORT; y++){
				if(Port[x].ExistTable[y]){
					if( (Port[x].dTypeTable[y] == dgTYPE_OUT_MODULE) || (Port[x].dTypeTable[y] == dgTYPE_INOUT_MODULE_NonSource) || (Port[x].dTypeTable[y] == dgTYPE_INOUT_MODULE_Source) ){
						Count.Total++;
					}
				}
			}
		}
	}
}

void TDevSensitiveCfg::ClearSelected(void)
{
	char *tText;
	switch(Tag.Inx){
		case 0:
			IList->Edit[0].Selected = 0;
			IList->Edit[0].Show();
			break;
		case 1:
			IList->Edit[1].Selected = 0;
			IList->Edit[1].Show();
			break;
		case 2:
			IList->Edit[2].Selected = 0;
			IList->Edit[2].Show();
			break;
		case 3:
			IList->Edit[3].Selected = 0;
			IList->Edit[3].Show();
			break;
		case 4:
			if(LanguageEnCn==0)tText="开始查询"; else tText=" Query  ";
			TRoundRect::sDrawBySize(Lv.Grid.Left[2] +40, Lv.Grid.Top[2] + 42, 120, 30, 7, clNearWhite, tText, 14, clNearBlack);
			break;
		case 5:
			if(LanguageEnCn==0)tText="保存配置"; else tText=" Config ";
			TRoundRect::sDrawBySize(Lv.Grid.Left[2] +180, Lv.Grid.Top[2] + 42, 120, 30, 7, clNearWhite, tText, 14, clNearBlack);
			break;
	}		
}

void TDevSensitiveCfg::Check4Selected(void)
{
	char *tText;
	switch(Tag.Inx){
		case 0:
			IList->Edit[0].Selected = 1;
			IList->Edit[0].SetPstTail();
			IList->Edit[0].Show();
			IList->Edit[0].ShowCursor();
			break;
		case 1:
			IList->Edit[1].Selected = 1;
			IList->Edit[1].SetPstTail();
			IList->Edit[1].Show();
			IList->Edit[1].ShowCursor();
			break;
		case 2:
			IList->Edit[2].Selected = 1;
			IList->Edit[2].SetPstTail();
			IList->Edit[2].Show();
			IList->Edit[2].ShowCursor();
			break;
		case 3:
			IList->Edit[3].Selected = 1;
			IList->Edit[3].Show();
			break;
		case 4:
			if(LanguageEnCn==0)tText="开始查询"; else tText=" Query  ";
			TRoundRect::sDrawBySize(Lv.Grid.Left[2] +40, Lv.Grid.Top[2] + 42, 120, 30, 7, clBlue, tText, 14, clNearWhite);
			break;
		case 5:
			if(LanguageEnCn==0)tText="保存配置"; else tText=" Config ";
			TRoundRect::sDrawBySize(Lv.Grid.Left[2] +180, Lv.Grid.Top[2] + 42, 120, 30, 7, clBlue, tText, 14, clNearWhite);
			break;
	}		
}

void TDevSensitiveCfg::PushInnerCanData(unsigned char aCmd, unsigned char *pBuf)
{
	if(aCmd == cmQueryStaticDataAddrSegment){
		//Read
		if(IList == nullptr)return;
		if(State == RangeReading){
			if( (pBuf[1] == Path0) && (pBuf[2] >= StartAddr) && (pBuf[2] <= EndAddr) && (pBuf[3] ==0x01) ){
				IList->ReadWriteData.D8[pBuf[2]] =1;
				IList->RealtimeData.Data[0][pBuf[2]] = pBuf[4];
				RunData.RecReadUpData = 1;
			}
		}
	}else
	if(aCmd == cmSetConfigAddrSegment){
		//Write
		if(State == RangeWriting){
			if( (pBuf[1] == Path0) && (pBuf[2] == StartAddr) && (pBuf[3] == EndAddr) && (pBuf[4] ==0x01) && (pBuf[5] == WritingCfByte) ){
				RunData.RecWriteOk = 1;
			}
		}
	}
}

void TDevSensitiveCfg::SendCmdReadOutputIsCheck(unsigned char aPort, unsigned char aStartAddr, unsigned char aEndAddr)
{
	TCanHeaderID aIde;
	unsigned char cVal;
	unsigned int i;
	unsigned int Len;
	int iVal;
	
	Len =5;
	
	aIde.BitF.Cmd = cmQueryStaticDataAddrSegment;
	for(i=0; i<8; i++){
		Data[i] = 0;
	}
	
	Data[0] = 0;
	Data[1] = aPort;
	Data[2] = aStartAddr;
	Data[3] = aEndAddr;
	Data[4] = 0x01;
	
	aIde.BitF.Pri = 1;
	aIde.BitF.sLvl = 1;
	aIde.BitF.dLvl = 7;
	aIde.BitF.sAddr = 0;
	aIde.BitF.dAddr = 127;
	
	InnerCan.Buf4GuiSend.Insert(aIde, Data, Len);
}

void TDevSensitiveCfg::SendCmdReadOutputIsCheckAbort()
{
	TCanHeaderID aIde;
	unsigned char cVal;
	unsigned int i;
	unsigned int Len;
	int iVal;
	
	Len =5;
	
	aIde.BitF.Cmd = cmQueryStaticDataAddrSegment;
	for(i=0; i<8; i++){
		Data[i] = 0;
	}
	
	Data[0] = 0;
	Data[1] = 255;
	Data[2] = 0;
	Data[3] = 0;
	Data[4] = 0;
	
	aIde.BitF.Pri = 1;
	aIde.BitF.sLvl = 1;
	aIde.BitF.dLvl = 7;
	aIde.BitF.sAddr = 0;
	aIde.BitF.dAddr = 127;
	
	InnerCan.Buf4GuiSend.Insert(aIde, Data, Len);
}

void TDevSensitiveCfg::SendCmdWriteOutputIsCheck(unsigned char aPort, unsigned char aStartAddr, unsigned char aEndAddr, unsigned char aSet)
{
	TCanHeaderID aIde;
	unsigned char cVal;
	unsigned int i;
	unsigned int Len;
	int iVal;
	
	Len =6;
	
	aIde.BitF.Cmd = cmSetConfigAddrSegment;
	for(i=0; i<8; i++){
		Data[i] = 0;
	}
	
	Data[0] = 0;
	Data[1] = aPort;
	Data[2] = aStartAddr;
	Data[3] = aEndAddr;
	Data[4] = 0x01;
	Data[5] = aSet;
	
	aIde.BitF.Pri = 1;
	aIde.BitF.sLvl = 1;
	aIde.BitF.dLvl = 7;
	aIde.BitF.sAddr = 0;
	aIde.BitF.dAddr = 127;
	
	InnerCan.Buf4GuiSend.Insert(aIde, Data, Len);	
}


TGuiMsgReturn TDevSensitiveCfg::KeyIn(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	TGuiMsgReturn PmsRtnMsg;
	unsigned char tCtlNum, aCc, Ow, Err;
	
	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(State == RangeReading){
		switch(aKey){
			case VK_RETURN:
				State = Idle;
				SendCmdReadOutputIsCheckAbort();
				FullRedraw(1);
				break;
		}		
	}else
	if(State == RangeWriting){
		switch(aKey){
			case VK_RETURN:
				State =Idle;
				IsShow = 0;
				aMsg = guiMsgReturn;
				break;
		}
	}else{
		switch(aKey){
			case '0':
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':
			case '8':
			case '9':
			case VK_LEFT:
			case VK_RIGHT:
			case VK_DELETE:
				if(Tag.Inx ==0){
					IList->Edit[0].KeyIn(aKey);
					aCc = IList->Edit[0].Str.ToInteger();
					if( (aCc == 0) || (aCc > dPORT_MAX_COUNT) ){
						IList->Edit[0].SelectedColor = clRed;
					}else{
						IList->Edit[0].SelectedColor = clBlue;
					}
					Path0 = aCc;
					IList->Edit[0].Show();
					IList->Edit[0].ShowCursor();
				}else
				if(Tag.Inx ==1){
					IList->Edit[1].KeyIn(aKey);
					aCc = IList->Edit[1].Str.ToInteger();
					if( (aCc == 0) || (aCc > dEP_MAX_COUNT_PER_PORT) ){
						IList->Edit[1].SelectedColor = clRed;
					}else{
						IList->Edit[1].SelectedColor = clBlue;
					}
					StartAddr = aCc;
					IList->Edit[1].Show();
					IList->Edit[1].ShowCursor();
				}else
				if(Tag.Inx ==2){
					IList->Edit[2].KeyIn(aKey);
					aCc = IList->Edit[2].Str.ToInteger();
					if( (aCc == 0) || (aCc > dEP_MAX_COUNT_PER_PORT) ){
						IList->Edit[2].SelectedColor = clRed;
					}else{
						IList->Edit[2].SelectedColor = clBlue;
					}
					EndAddr = aCc;
					IList->Edit[2].Show();
					IList->Edit[2].ShowCursor();
				}else
				if(Tag.Inx ==3){
					switch(WritingCfByte){
						case TDevProperty::dSmokeSensGrade_MarkValue[1]:WritingCfByte = TDevProperty::dSmokeSensGrade_MarkValue[2];break;
						case TDevProperty::dSmokeSensGrade_MarkValue[2]:WritingCfByte = TDevProperty::dSmokeSensGrade_MarkValue[3];break;
						case TDevProperty::dSmokeSensGrade_MarkValue[3]:WritingCfByte = TDevProperty::dSmokeSensGrade_MarkValue[4];break;
						case TDevProperty::dSmokeSensGrade_MarkValue[4]:WritingCfByte = TDevProperty::dSmokeSensGrade_MarkValue[1];break;
						default:WritingCfByte = TDevProperty::dSmokeSensGrade_MarkValue[1];break;
					}
					RedrawCfgByte();
				}/*else
				if(Tag.Inx ==4){
					if(aKey == VK_RIGHT){
						ClearSelected();
						Tag.Inx++;
						Check4Selected();
					}
				}else
				if(Tag.Inx ==5){
					if(aKey == VK_LEFT){
						ClearSelected();
						Tag.Inx--;
						Check4Selected();
					}
				}*/
				break;
			case VK_UP:
				if(Tag.Inx >0){
					ClearSelected();
					Tag.Inx--;
					Check4Selected();
				}
				break;
			case VK_DOWN:
				if(Tag.Inx <4){
				//if(Tag.Inx <5){
					ClearSelected();
					Tag.Inx++;
					Check4Selected();
				}
				break;
			case VK_TAB:

				break;
			case dfKEY_SETUP:
				if(Tag.Inx ==3){
					WritingCfByte = WritingCfByte ? 0: 1;
					RedrawCfgByte();
				}
				break;
			case dfKEY_QUERY:

				break;
			case VK_EXECUTE:
				if(Tag.Inx == 4){
					if(Path0 && StartAddr && EndAddr){
						if( (Path0 < dPORT_MAX_COUNT) && (StartAddr >0) && (EndAddr <= dEP_MAX_COUNT_PER_PORT) && (StartAddr <= EndAddr)){
							PermissionCmd = 0;
							PermissionCmdGO();
						}
					}
				}else
				if(Tag.Inx == 5){
					if(Path0 && StartAddr && EndAddr){
						if( (Path0 < dPORT_MAX_COUNT) && (StartAddr >0) && (EndAddr <= dEP_MAX_COUNT_PER_PORT) && (StartAddr <= EndAddr)){
							PermissionCmd = 1;
							if(SystemPermission > 2){
								PermissionCmdGO();	
							}else{
								IsPermission = 1;
								PmsIntervene4Gui.Show4Gui(Lv.Grid.Left[1] +300, Lv.Grid.Top[1] + 16, 400, 140, 3);
							}
						}
					}
				}
				break;
			case VK_RETURN:
				State =Idle;
				IsShow = 0;
				if(State == RangeReading)SendCmdReadOutputIsCheckAbort();
				aMsg = guiMsgReturn;
				break;
		}
	}
	
	return aMsg;	
}

void TDevSensitiveCfg::PermissionCmdGO()
{
	if(PermissionCmd == 0){
		//Go Action
		if(Path0 && StartAddr && EndAddr){
			if( (Path0 < dPORT_MAX_COUNT) && (StartAddr >0) && (EndAddr <= dEP_MAX_COUNT_PER_PORT) && (StartAddr <= EndAddr)){
				InitReadData();
				State = RangeReading;
				SendCmdReadOutputIsCheck(Path0, StartAddr, EndAddr);
				Init4DrawRead();
				Draw4Read();
			}
		}
	}else
	if(PermissionCmd == 1){
		if(Path0 && StartAddr && EndAddr){
			if( (Path0 < dPORT_MAX_COUNT) && (StartAddr >0) && (EndAddr <= dEP_MAX_COUNT_PER_PORT) && (StartAddr <= EndAddr)){
				DrawWriteResult(3);
				State = RangeWriting;
				RunData.WaitTick =0;
				SendCmdWriteOutputIsCheck(Path0, StartAddr, EndAddr, WritingCfByte);
			}
		}
	}
}

void TDevSensitiveCfg::Task1000Ms()
{
	if(IsShow == 0)return;
	if(State == RangeReading){
		if(RunData.WaitTick<60){
			RunData.WaitTick++;
			if(KeepUiOnMe != nullptr)KeepUiOnMe();
		}
		if(RunData.RecReadUpData){
			if( (IsSelfChecking == 0) && (PmsIntervene.IsShow == 0) ){
				RunData.RecReadUpData = 0;
				Draw4Read();
			}
		}
	}else
	if(State == RangeWriting){
		RunData.WaitTick++;
		DrawWriteTime();
		if(RunData.WaitTick <360){
			if(KeepUiOnMe != nullptr)KeepUiOnMe();
			if(RunData.RecWriteOk){
				//Write Ok
				if( (IsSelfChecking == 0) && (PmsIntervene.IsShow == 0) ){
					DrawWriteResult(1);
				}else{
					WriteResult = 1;
				}
				State = Idle;
			}
		}else{
			//Write Fail
			if( (IsSelfChecking == 0) && (PmsIntervene.IsShow == 0) ){
				DrawWriteResult(2);
			}else{
				WriteResult = 2;
			}
			State = Idle;
		}
	}
}

int TDevSensitiveCfg::ExtRequst(unsigned char Prm)
{
	IsShow = 0;
	if(State == RangeReading)SendCmdReadOutputIsCheckAbort();
	State =Idle;
	return 0;
}

