#include "RuntimeData.h"
#include "DevStartStop.h"

#define cmDeviceSetTempeSensType	211
#define cmDeviceReadTempeSensType	212

#define cmDeviceSetSignalInOutType	199
#define cmDeviceQuerySignalInOutType 	200

#define cmDeviceDisplayPadMute	203
#define cmDeviceDisplayPadDescpSet	204

#define cmDeviceRuntimeDataStart	207
#define cmDeviceRuntimeDataStop	208

#define cmDeviceLinkagePowerSuplyMute	209

static const unsigned char stHexNum[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

void TDevStartStop::SendStartStopCmd(unsigned int IsStart)
{
	unsigned char tCmd;
	unsigned char cVal;
	unsigned int i;
	unsigned int Len;
	int iVal;
	unsigned char aP0, aP1;
	
	if(IsSelfCtl){
		if(IsStart){
			tCmd = cmAutoStart;
		}else{
			tCmd = cmAutoStop;
		}
		for(i=0; i<8; i++){
			CommData[i] = 0;
		}
		CommData[0] = 0;
		CommData[1] = CtlNum;
		CommData[2] = Path0;
		CommData[3] = Path1;
		CommData[4] = 0;
		CommData[5] = dType;
		aP0 = Path0 -1; aP1 = Path1 -1;
		if( (Port[aP0].StateTable[aP1] & dEpStateBitMask) == 0){
			InnerCan.OBufFastInsert(1, tCmd, CommData, 6);
		}else{
		}
	}else{
		if(MainCtl.fData.Split.NetworkMode == 1){
			if(pNumFound){
				ProtoC2C.MasterDoDevRemoteStart(CtlNum, Path0, Path1, IsStart);
			}else{
			}
		}else{
			
		}
	}
}

void TDevStartStop::SendSmokeCmdRead()
{
	TCanHeaderID aIde;
	unsigned char cVal;
	unsigned int i;
	unsigned int Len;
	int iVal;
	unsigned char aPx;
	
	ProcessingRt.DevType = 0;	//点型烟感
	ProcessingRt.DataType = 1;	//烟感灵敏度
	
	Len =4;
	
	for(i=0; i<8; i++){
		CommData[i] = 0;
	}
	
	CommData[0] = 3;
	
	CommData[1] = Path0;
	ProcessingRt.P0 = Path0;
	
	CommData[2] = Path1;
	ProcessingRt.P1 = Path1;
	
	CommData[3] = ProcessingRt.DataType;
	
	aIde.BitF.Cmd = cmQuerySens;
	aIde.BitF.Pri = 1;
	aIde.BitF.sLvl = 1;
	aIde.BitF.dLvl = 7;
	aIde.BitF.sAddr = 0;
	aIde.BitF.dAddr = 127;
	
	InnerCan.Buf4GuiSend.Insert(aIde, CommData, Len);
}

void TDevStartStop::SendTempeCmdRead()
{
	TCanHeaderID aIde;
	unsigned char cVal;
	unsigned int i;
	unsigned int Len;
	int iVal;
	unsigned char aPx;
	
	Len =8;
	
	for(i=0; i<8; i++){
		CommData[i] = 0;
	}
	
	CommData[0] = 0;
	CommData[1] = MainCtl.fData.Split.MyNum;
	CommData[2] = Path0;
	ProcessingRt.P0 = Path0;
	
	CommData[3] = Path1;
	ProcessingRt.P1 = Path1;
	
	ProcessingRt.DataType = 0x06;
	
	CommData[4] = ProcessingRt.DataType;
	
	aIde.BitF.Cmd = cmQueryStaticData;
	aIde.BitF.Pri = 1;
	aIde.BitF.sLvl = 1;
	aIde.BitF.dLvl = 7;
	aIde.BitF.sAddr = 0;
	aIde.BitF.dAddr = 127;
	
	InnerCan.Buf4GuiSend.Insert(aIde, CommData, Len);
}

void TDevStartStop::SendModuleSignalInOutTypeRead()
{
	TCanHeaderID aIde;
	unsigned char cVal;
	unsigned int i;
	unsigned int Len;
	int iVal;
	unsigned char aPx;
	
	ProcessingRt.DevType = 0;	//点型烟感 
	ProcessingRt.DataType = 1;	//烟感灵敏度
	
	Len =4;
	
	for(i=0; i<8; i++){
		CommData[i] = 0;
	}
	
	CommData[0] = 3;
	
	CommData[1] = Path0;
	ProcessingRt.P0 = Path0;
	
	CommData[2] = Path1;
	ProcessingRt.P1 = Path1;
	
	CommData[3] = ProcessingRt.DataType;

	aIde.BitF.Cmd = cmDeviceQuerySignalInOutType;
	aIde.BitF.Pri = 1;
	aIde.BitF.sLvl = 1;
	aIde.BitF.dLvl = 7;
	aIde.BitF.sAddr = 0;
	aIde.BitF.dAddr = 127;
	
	InnerCan.Buf4GuiSend.Insert(aIde, CommData, Len);
}

void TDevStartStop::SendVaSettingRead()
{
	TCanHeaderID aIde;
	unsigned char cVal;
	unsigned int i;
	unsigned int Len;
	int iVal;
	unsigned char aPx;
	
	ProcessingRt.DevType = 0;	//点型烟感
	ProcessingRt.DataType = 1;	//烟感灵敏度
	
	Len =4;
	
	for(i=0; i<8; i++){
		CommData[i] = 0;
	}
	
	CommData[0] = 3;
	CommData[1] = MainCtl.fData.Split.MyNum;
	CommData[2] = Path0;
	ProcessingRt.P0 = Path0;
	
	CommData[3] = Path1;
	ProcessingRt.P1 = Path1;
	
	CommData[4] = 0;

	aIde.BitF.Cmd = cmQueryStaticData;
	aIde.BitF.Pri = 1;
	aIde.BitF.sLvl = 1;
	aIde.BitF.dLvl = 7;
	aIde.BitF.sAddr = 0;
	aIde.BitF.dAddr = 127;
	
	InnerCan.Buf4GuiSend.Insert(aIde, CommData, Len);
}

void TDevStartStop::RealTimeSendCmd(const int StartStop)
{
	TCanHeaderID aIde;
	unsigned int i;
	unsigned int Len;
	
	for(i=0; i<8; i++){
		CommData[i] = 0;
	}
	
	CommData[2] = Path0;
	CommData[3] = Path1;
	
	if(StartStop != 0){
		aIde.BitF.Cmd = cmQueryRuntimeData;
	}else{
		aIde.BitF.Cmd = cmQueryAbout;
	}
	Len = 4;

	aIde.BitF.Pri = 1;
	aIde.BitF.sLvl = 1;
	aIde.BitF.dLvl = 7;
	aIde.BitF.sAddr = 0;
	aIde.BitF.dAddr = 127;
	
	InnerCan.Buf4GuiSend.Insert(aIde, CommData, Len);
}

void TDevStartStop::RealTimeSendCmdAbort()
{
	TCanHeaderID aIde;
	unsigned char cVal;
	unsigned int i;
	unsigned int Len;
	int iVal;
	unsigned char aP0,aP1;
	
	for(i=0; i<8; i++){
		CommData[i] = 0;
	}
	
	aIde.BitF.Cmd = cmQueryAbout;
	Len = 4;
	//Prm 
	CommData[0] = 0;
	CommData[1] = 0;
	CommData[2] = Path0;
	CommData[3] = Path1;
	
	aIde.BitF.Pri = 1;
	aIde.BitF.sLvl = 1;
	aIde.BitF.dLvl = 7;
	aIde.BitF.sAddr = 0;
	aIde.BitF.dAddr = 127;

	InnerCan.Buf4GuiSend.Insert(aIde, CommData, Len);
}

void TDevStartStop::RunTimeSendCmd(const int StartStop)
{
	TCanHeaderID aIde;
	unsigned int i;
	unsigned int Len;
	
	for(i=0; i<8; i++){
		CommData[i] = 0;
	}
	
	CommData[2] = Path0;
	CommData[3] = Path1;
	
	if(StartStop != 0){
		aIde.BitF.Cmd = cmDeviceRuntimeDataStart;
	}else{
		aIde.BitF.Cmd = cmDeviceRuntimeDataStop;
	}
	Len = 4;

	aIde.BitF.Pri = 1;
	aIde.BitF.sLvl = 1;
	aIde.BitF.dLvl = 7;
	aIde.BitF.sAddr = 0;
	aIde.BitF.dAddr = 127;
	
	InnerCan.Buf4GuiSend.Insert(aIde, CommData, Len);
}

void TDevStartStop::PermissionCmdGO()
{
	int aVal;
	switch(PermissionCmd){
		case TDevProperty::pcStart:
			SendStartStopCmd(1);
			break;
		case TDevProperty::pcStop:
			SendStartStopCmd(0);
			break;
	}
}

void TDevStartStop::ClearReqDataState()
{
	SmokerRt.IsWait4ReadSens = 0;
	SmokerRt.IsWait4WriteSens = 0;
	TempeRt.IsWait4ReadSensType =0;
	ModuleInRt.IsWait4ReadSignalType =0;
	ModuleInRt.IsWait4WriteSignalType =0;
	ModuleInOutRt.IsWait4ReadSignalType =0;
	ModuleInOutRt.IsWait4WriteSignalType =0;
	ModuleOutRt.IsWait4ReadSignalType =0;
	ModuleOutRt.IsWait4WriteSignalType =0;
	VaRt.IsWait4ReadVaSetting =0;
	VaRt.IsWait4WriteVaSetting =0;
	DisplayPadRt.IsMute =0;
	DisplayPadRt.IsDownloadDescp =0;
}

void TDevStartStop::UpdataOnlineData()
{
	if(dType == dgTYPE_SMOKE_DETECTOR){
		if(IsSelfCtl){
			SmokerRt.IsWait4ReadSens = 1;
			SmokerRt.IsWait4WriteSens = 0;
			SmokerRt.RecRdy = 0;
			SmokerRt.RecCmd = 0;
			SmokerRt.WaitTick = 0;
			SendSmokeCmdRead();
			if(EdittingDescp == 0)TextRender_string24(IList->Edit[0].Bound.Right + 6, IList->Edit[0].Bound.Top + 4, clNearWhite, ItemColor, "获取中  ");
		}else{
			if(EdittingDescp == 0)TextRender_string24(IList->Edit[0].Bound.Right + 6, IList->Edit[0].Bound.Top + 4, clNearWhite, ItemColor, "--------");
		}
	}else
	if(dType == dgTYPE_TEMPE_DETECTOR){
		if(IsSelfCtl){
			TempeRt.IsWait4ReadSensType = 1;
			TempeRt.RecRdy = 0;
			TempeRt.RecCmd = 0;
			TempeRt.WaitTick = 0;
			SendTempeCmdRead();
			if(EdittingDescp == 0)TextRender_string24(IList->Edit[0].Bound.Right + 6, IList->Edit[0].Bound.Top + 4, clNearWhite, ItemColor, "获取中  ");
		}else{
			if(EdittingDescp == 0)TextRender_string24(IList->Edit[0].Bound.Right + 6, IList->Edit[0].Bound.Top + 4, clNearWhite, ItemColor, "--------");
		}
	}else
	if(dType == dgTYPE_HAND_REPORT){

	}else
	if(dType == dgTYPE_FIRE_HydrantKp){

	}else
	if(dType == dgTYPE_IN_MODULE){

	}else
	if(dType == dgTYPE_OUT_MODULE){

	}else
	if(dType == dgTYPE_INOUT_MODULE_NonSource){

	}else
	if(dType == dgType_VAALRAM){

	}else
	if(dType == dgTYPE_DISPLAY_PAD){

	}else
	if(dType == dgTYPE_LINKAGE_POWERSUPLY){

	}	
}

void TDevStartStop::PushOneRealDataReport(u8 aP0, u8 aP1, u8 aInx, u8 D0, u8 D1, u8 D2, u8 D3)
{
	unsigned int aAddr, i, Num, Num10, Pst;
	unsigned char Da[4];
	int x,y;
	
	const int MaxLen=41;
	
	if(IsShow == 0)return;
	
	if(aP0 != Path0)return;
	if(aP1 != Path1)return;
	
	Da[0] = D0;
	Da[1] = D1;
	Da[2] = D2;
	Da[3] = D3;
	if(PanelDrawData.TextDrawLine >7){
		PanelDrawData.Reset();
	}	
	if(aInx == 0){
		RuntimeData.voltage = Da[1];
		PanelDrawData.TextDrawLine++;
		if(PanelDrawData.TextDrawLine >7){
			PanelDrawData.Reset();
		}
		if(PanelDrawData.TextDrawLine == 0){
			for(int z=0; z<8; z++){
				for(int g=0; g<80; g++){
					IList->TextOut[z][g] = '\0';
				}
			}
			if( (EdittingDescp == 0) && (IsPermission == 0) ){
				if( PanelIsUsingText && (IsSelfChecking == 0) && (PmsIntervene.IsShow == 0)  ){
					RectFillRender(Lv.Panel.Left, Lv.Panel.Top, Lv.Panel.Right, Lv.Panel.Bottom, PanelColor);
				}
			}
		}
		y = Lv.Panel.Top + 1;
		y = y + PanelDrawData.TextDrawLine  * 27;
		x = Lv.Panel.Left + 2;
		TMyString::sClear(TextTmp);
		PanelDrawData.UsingTextHex = PanelDrawData.DesertTextHex;
		if(PanelDrawData.IsHexList[PanelDrawData.TextDrawLine] == dtNone){
			if(PanelDrawData.UsingTextHex)PanelDrawData.IsHexList[PanelDrawData.TextDrawLine] =dtHex;
			else PanelDrawData.IsHexList[PanelDrawData.TextDrawLine] =dtDec;
		}
		if(PanelDrawData.IsHexList[PanelDrawData.TextDrawLine] == dtHex){
			for(i=0; i<4; i++){
				Pst= 0 + i*3;
				Num = Da[i] % 16;
				Num10 = Da[i] / 16 % 16;
				IList->TextOut[PanelDrawData.TextDrawLine][Pst+0] = stHexNum[Num10];
				IList->TextOut[PanelDrawData.TextDrawLine][Pst+1] = stHexNum[Num];
				if(i<3)IList->TextOut[PanelDrawData.TextDrawLine][Pst+2] = ' ';
				else IList->TextOut[PanelDrawData.TextDrawLine][Pst+2] = ',';
			}
			if( (EdittingDescp == 0) && (IsPermission == 0) ){
				if( PanelIsUsingText && (IsSelfChecking == 0) && (PmsIntervene.IsShow == 0)  )
					TextRender_string24(x,y,clNearBlack, PanelColor, IList->TextOut[PanelDrawData.TextDrawLine]);
			}
		}else
		if(PanelDrawData.IsHexList[PanelDrawData.TextDrawLine] == dtDec){
			for(i=0; i<4; i++){
				Pst= 0 + i*4;
				Num = Da[i] / 100;
				IList->TextOut[PanelDrawData.TextDrawLine][Pst+0] = Num + '0';
				Num = Da[i] % 100 / 10;
				IList->TextOut[PanelDrawData.TextDrawLine][Pst+1] = Num + '0';
				Num = Da[i] % 10;
				IList->TextOut[PanelDrawData.TextDrawLine][Pst+2] = Num + '0';
				if(i<3)IList->TextOut[PanelDrawData.TextDrawLine][Pst+3] = ' ';
				else IList->TextOut[PanelDrawData.TextDrawLine][Pst+3] = ',';
			}
			if( (EdittingDescp == 0) && (IsPermission == 0) ){
				if( PanelIsUsingText && (IsSelfChecking == 0) && (PmsIntervene.IsShow == 0)  )
					TextRender_string24(x,y,clNearBlack, PanelColor, IList->TextOut[PanelDrawData.TextDrawLine]);
			}
		}
	}else
	if(aInx < 4){
		if(aInx == 2){
			/*if(dType == dgTYPE_TEMPE_DETECTOR){
				Data.GotTempeSensType[0] = 0xF1; 
				Data.GotTempeSensType[1] = Path0;
				Data.GotTempeSensType[2] = Path1;
				Data.GotTempeSensType[3] = D1;
			}*/
		}
		
		y = Lv.Panel.Top + 1;
		y = y + PanelDrawData.TextDrawLine  * 27;
		x = Lv.Panel.Left + 2;
		TMyString::sClear(TextTmp);
		if(PanelDrawData.IsHexList[PanelDrawData.TextDrawLine] == dtHex){
			for(i=0; i<4; i++){
				Pst= aInx*12 + i*3;
				if(Pst > (MaxLen-1-2))break;
				Num = Da[i] % 16;
				Num10 = Da[i] / 16 % 16;
				IList->TextOut[PanelDrawData.TextDrawLine][Pst+0] = stHexNum[Num10];
				IList->TextOut[PanelDrawData.TextDrawLine][Pst+1] = stHexNum[Num];
				if((Pst+2)<=(MaxLen-1)){
					if(i<3)IList->TextOut[PanelDrawData.TextDrawLine][Pst+2] = ' ';
					else IList->TextOut[PanelDrawData.TextDrawLine][Pst+2] = ',';
				}
			}
			if( (EdittingDescp == 0) && (IsPermission == 0) ){
				if( PanelIsUsingText && (IsSelfChecking == 0) && (PmsIntervene.IsShow == 0) /*&& (aInx == 3)*/ )
					TextRender_string24(x,y,clNearBlack, PanelColor, IList->TextOut[PanelDrawData.TextDrawLine]);
			}
		}else
		if(PanelDrawData.IsHexList[PanelDrawData.TextDrawLine] == dtDec){
			for(i=0; i<4; i++){
				Pst= aInx*16 + i*4;
				if(Pst > (MaxLen-1-3))break;
				Num = Da[i] / 100;
				IList->TextOut[PanelDrawData.TextDrawLine][Pst+0] = Num + '0';
				Num = Da[i] % 100 / 10;
				IList->TextOut[PanelDrawData.TextDrawLine][Pst+1] = Num + '0';
				Num = Da[i] % 10;
				IList->TextOut[PanelDrawData.TextDrawLine][Pst+2] = Num + '0';
				if((Pst+3)<=(MaxLen-1)){
					if(i<3)IList->TextOut[PanelDrawData.TextDrawLine][Pst+3] = ' ';
					else IList->TextOut[PanelDrawData.TextDrawLine][Pst+3] = ',';
				}
			}
			if( (EdittingDescp == 0) && (IsPermission == 0) ){
				if( PanelIsUsingText && (IsSelfChecking == 0) && (PmsIntervene.IsShow == 0) /*&& (aInx == 3)*/ )
					TextRender_string24(x,y,clNearBlack, PanelColor, IList->TextOut[PanelDrawData.TextDrawLine]);
			}
		}
	}
}

void TDevStartStop::PushInnerCanData(unsigned char aCmd, unsigned char *pData)
{
	int i;
	switch(HwDevType){
		case TDevProperty::htNoType:
			
			break;
		case TDevProperty::htSmoker:
			if(aCmd == cmQuerySens){
				for(i=0; i<8; i++)RecData[i] = pData[i];
				if(   (ProcessingRt.P0 == RecData[1]) &&
					(ProcessingRt.P1 == RecData[2]) &&
					(ProcessingRt.DataType == RecData[3]) ){
						ProcessingRt.dValGot[0] = RecData[4];
						ProcessingRt.dValGot[1] = RecData[5];
						ProcessingRt.dValGot[2] = RecData[6];
						ProcessingRt.dValGot[3] = RecData[7];
						SmokerRt.RecRdy =1;
						SmokerRt.RecCmd =aCmd;
				}
			}
			break;
		case TDevProperty::htTempe:
			if(aCmd == cmQueryStaticData){
				for(i=0; i<8; i++)RecData[i] = pData[i];
				if(   (ProcessingRt.P0 == RecData[1]) &&
					(ProcessingRt.P1 == RecData[2]) ){
						if( RecData[3] == 0 ){
							Data.TempeSenType = RecData[5];
							TempeRt.RecRdy =1;
							TempeRt.RecCmd =aCmd;
						}
				}
			}
			break;
		case TDevProperty::htHandPad:
			
			break;
		case TDevProperty::htHydrantPad:
			
			break;
		case TDevProperty::htModuleIn:
			if(aCmd == cmDeviceQuerySignalInOutType){
				for(i=0; i<8; i++)RecData[i] = pData[i];
				if(   (ProcessingRt.P0 == RecData[1]) &&
					(ProcessingRt.P1 == RecData[2]) &&
					(ProcessingRt.DataType == RecData[3]) ){
						ProcessingRt.dValGot[0] = RecData[4];
						ModuleInRt.RecRdy =1;
						ModuleInRt.RecCmd =aCmd;
				}
			}
			break;
		case TDevProperty::htModuleInOut:
			if(aCmd == cmDeviceQuerySignalInOutType){
				for(i=0; i<8; i++)RecData[i] = pData[i];
				if(   (ProcessingRt.P0 == RecData[1]) &&
					(ProcessingRt.P1 == RecData[2]) &&
					(ProcessingRt.DataType == RecData[3]) ){
						ProcessingRt.dValGot[0] = RecData[4];
						ModuleInOutRt.RecRdy =1;
						ModuleInOutRt.RecCmd =aCmd;
				}
			}
			break;
		case TDevProperty::htModuleOut:
			if(aCmd == cmDeviceQuerySignalInOutType){
				for(i=0; i<8; i++)RecData[i] = pData[i];
				if(   (ProcessingRt.P0 == RecData[1]) &&
					(ProcessingRt.P1 == RecData[2]) &&
					(ProcessingRt.DataType == RecData[3]) ){
						ProcessingRt.dValGot[0] = RecData[4];
						ModuleOutRt.RecRdy =1;
						ModuleOutRt.RecCmd =aCmd;
				}
			}			
			break;
		case TDevProperty::htVa:
			if(aCmd == cmQueryStaticData){	
				for(i=0; i<8; i++)RecData[i] = pData[i];
				if(   (ProcessingRt.P0 == RecData[1]) &&
					(ProcessingRt.P1 == RecData[2]) &&
					(              0 == RecData[3]) ){
						Data.VaSoundType = RecData[4];
						Data.VaVisionType = RecData[5];
						Data.VaIs4Linkage = RecData[6];
						VaRt.RecRdy =1;
						VaRt.RecCmd =aCmd;
				}
			}			
			break;
		case TDevProperty::htDisplayPad:

			break;
		case TDevProperty::htLinkagePowerSuply:
	
			break;
		default :
			
			break;
	}
}


