
var fac_bind_run = 0;
async function EWRunFire()
{
	
	ShowCommMes("起爆脚本本加载",MesgType.MESG_TYPE_INFO);
	fac_bind_init_flag =1;
	var content_str = '<div class="layui-row" style="line-height: 1.8rem; padding-top: 2px;">';
	
	content_str +=  '<div class="layui-col-xs2">'
	content_str += '<button type="button" class="layui-btn layui-btn-normal plugin_bind_test" style="height: 1.8rem; line-height: 1.8rem;width: 90%;" onclick="Ew_Autobind()">注码</button>';
	content_str += '</div>'
	
	content_str +=  '<div class="layui-col-xs2">'
	content_str += '<button type="button" class="layui-btn layui-btn-normal plugin_fire_test" style="height: 1.8rem; line-height: 1.8rem;width: 90%;" onclick="Ew_AutoFire()">单发起爆</button>';
	content_str += '</div>'
	
	content_str +=  '<div class="layui-col-xs2">'
	content_str += '<button type="button" class="layui-btn layui-btn-normal plugin_fire_test" style="height: 1.8rem; line-height: 1.8rem;width: 90%;" onclick="Ew_AutoFirePrapare()">组网</button>';
	content_str += '</div>'
	
	content_str +=  '<div class="layui-col-xs2">'
	content_str += '<button type="button" class="layui-btn layui-btn-normal plugin_fire_test" style="height: 1.8rem; line-height: 1.8rem;width: 90%;" onclick="Ew_MutilFire()">起爆</button>';
	content_str += '</div>'
	
	content_str +=  '<div class="layui-col-xs2">'
	content_str += '<button type="button" class="layui-btn layui-btn-normal plugin_fire_test" style="height: 1.8rem; line-height: 1.8rem;width: 90%;" onclick="Ew_PowerOnTest()">上电测试</button>';
	content_str += '</div>'
	

	content_str += '</div>'
	 $("#Oper_View").append(content_str);
}
	
async function Ew_Autobind()
{
	var rtv = 0;
	
	ep2_getInput();
	epro2_oper.cmd = 13;
	await Ep2_HeartBeat_SW();
	if(fac_bind_run == 0)
	{
		fac_bind_run = 1;
		var obj = $(".plugin_bind_test")[0];
		obj.innerText = "停止注码";
		
	}else{
		
		ShowCommMes("注码脚本运行结束",MesgType.MESG_TYPE_INFO);
		fac_bind_run = 0;
		var obj = $(".plugin_bind_test")[0];
		obj.innerText = "启动注码";
		return;
	}
	await Ep2_BusSwitch(1);
	while(fac_bind_run > 0)
	{
		ShowCommMes("等待模块接入。。 ",MesgType.MESG_TYPE_INFO);
		while(ep2_bus_cur < 0.01 && fac_bind_run > 0 && ep2_bus_cur < 0.025)
		{
			await  TransSleep(300);
		}
		await  TransSleep(2000);
		epro2_oper.commaddr = 1;
		//读状态
		rtv = await Ep2_ReadSysStatesCmd(epro2_oper.commaddr);
		if(rtv > 0)
		{
			continue; 
		}
		epro2_oper.cmd = 29;
		await Ep2_EnMTPWrite(1);
		await  TransSleep(300);
		epro2_oper.cmd = 20;
		await Ep2_WriteUID();
		epro2_oper.cmd = 22;
		await Ep2_WritePWD();
		epro2_oper.cmd = 19;
		rtv = await Ep2_ReadUID();
		if(rtv != 0)
		{
			epro2_oper.uid = last_uid;
			ShowCommMes("赋码失败 rtv " + rtv,MesgType.MESG_TYPE_ERROR);
		}else{
			ShowCommMes("赋码成功",MesgType.MESG_TYPE_INFO);
			flow_num++;
		}
		
		epro2_oper.cmd = 1;
		await Ep2_ChipReset();
		
		break;
	}
}

async function Ew_AutoFire()
{
	ep2_getInput();
	//开总线
	epro2_oper.cmd = 25;
	await Ep2_FastCfgCmd(epro2_oper.uid,epro2_oper.commaddr,epro2_oper.delayfire); 
	epro2_oper.cmd =  17;
	await Ep2_Charge(); 
	await  TransSleep(8000);
	epro2_oper.cmd = 29;
	await Ep2_EnMTPWrite(1);
	epro2_oper.cmd =  7 ;
    await Ep2_ClkAmend();
	await  TransSleep(500);
	epro2_oper.cmd = 3 ;
	await Ep2_SaveFirInfo();
	await  TransSleep(1000);
	epro2_oper.cmd = 32;
	await Ep2_ReadSysStatesCmd(epro2_oper.commaddr);
	await  TransSleep(200);
	epro2_oper.cmd =  18;
	await Ep2_Blast();
ShowCommMes("起爆成功",MesgType.MESG_TYPE_INFO);
ShowCommMes("起爆成功",MesgType.MESG_TYPE_INFO);
ShowCommMes("起爆成功",MesgType.MESG_TYPE_INFO);
ShowCommMes("起爆成功",MesgType.MESG_TYPE_INFO);
ShowCommMes("起爆成功",MesgType.MESG_TYPE_INFO);
ShowCommMes("起爆成功",MesgType.MESG_TYPE_INFO);
ShowCommMes("起爆成功",MesgType.MESG_TYPE_INFO);
ShowCommMes("起爆成功",MesgType.MESG_TYPE_INFO);
ShowCommMes("起爆成功",MesgType.MESG_TYPE_INFO);
ShowCommMes("起爆成功",MesgType.MESG_TYPE_INFO);
ShowCommMes("起爆成功",MesgType.MESG_TYPE_INFO);
ShowCommMes("起爆成功",MesgType.MESG_TYPE_INFO);
ShowCommMes("起爆成功",MesgType.MESG_TYPE_INFO);

	await Ep2_BusSwitch(0);
}

var Ew_AutoFire_Flag = 0;
async function Ew_AutoFirePrapare()
{
	// var run_counter = 0;
	// if(Ew_AutoFire_Flag == 0)
	// {
	// 	Ew_AutoFire_Flag = 1;
	// }else{
	// 	Ew_AutoFire_Flag = 0;
	// 	return;
	// }
	// while(Ew_AutoFire_Flag > 0)
//	{
		ep2_getInput();
		epro2_oper.ep2_high_voltage = 85;
		epro2_oper.ep2_middle_voltage = 50;
		await Ep2_BusSwitch(1);
		await  TransSleep(2000);
		ShowCommMes("快速配置",MesgType.MESG_TYPE_INFO);
		epro2_oper.cmd = 25;
		epro2_oper.cycle_test = 1;
		await Ep2_ExcueOperCmd();
		
		epro2_oper.ep2_high_voltage = 200;
		await Ep2_BusSwitch(1);
		await  TransSleep(2000);
		
		ShowCommMes("充电",MesgType.MESG_TYPE_INFO);
		epro2_oper.cmd = 17;
		epro2_oper.cycle_test = 0;
		await Ep2_ExcueOperCmd();
		await  TransSleep(8000);
		
		ShowCommMes("使能MTP",MesgType.MESG_TYPE_INFO);
		epro2_oper.cmd = 29;
		await Ep2_ExcueOperCmd();
		await  TransSleep(200);
		
		
		ShowCommMes("校准",MesgType.MESG_TYPE_INFO);
		epro2_oper.cmd = 7;
		await Ep2_ExcueOperCmd();
		await  TransSleep(1500);
		
		ShowCommMes("保持配置",MesgType.MESG_TYPE_INFO);
		epro2_oper.cmd = 3;
		await Ep2_ExcueOperCmd();
		await  TransSleep(1000);
		
		epro2_oper.cmd = 4;
		await Ep2_ExcueOperCmd();
		await  TransSleep(1000);
		
		if( typeof(ep2_qthandle) != "undefined"  && ep2_qthandle != null )
		{
			ep2_qthandle.Ep2_Beep(2);
		}
		epro2_oper.cmd =  18;
		await Ep2_ExcueOperCmd();
		await Ep2_BusSwitch(0);
	// 	await  TransSleep(2000);
	// 	run_counter++;
	// 	ShowCommMes("运行次数 "+run_counter,MesgType.MESG_TYPE_INFO);
	// }
}


async function Ew_MutilFire()
{
	epro2_oper.cmd =  18;
	await Ep2_ExcueOperCmd();
	await Ep2_BusSwitch(0);
}
var EW_PorwerOnTest_Flag = 0;
async function Ew_PowerOnTest()
{
	if(EW_PorwerOnTest_Flag == 0)
	{
		EW_PorwerOnTest_Flag = 1;
	}else{
		EW_PorwerOnTest_Flag = 0;
		return;
	}
	ep2_getInput();
	//开心跳 
	
	epro2_oper.ep2_heartbeat_sw = 0;
	epro2_oper.cmd = 13;
	await Ep2_HeartBeat_SW();
	var count = 0;
	var delay_count = 0;
	while(count < 1000 && EW_PorwerOnTest_Flag > 0)
	{
		count++;
		//关总线
		delay_count = 0;
		epro2_oper.cmd = 16;
		await Ep2_BusSwitch(0);
		while(delay_count < 120 && EW_PorwerOnTest_Flag > 0)
		{
			await  TransSleep(1000);
			delay_count++;
		}
		if(EW_PorwerOnTest_Flag == 0)
		{
			break;
		}
		epro2_oper.cmd = 15;
		await Ep2_BusSwitch(1);//上电
		delay_count = 0;
		while(delay_count < 5 && EW_PorwerOnTest_Flag > 0)
		{
			await  TransSleep(1000);
			delay_count++;
		}
		if(EW_PorwerOnTest_Flag == 0)
		{
			break;
		}
		if(ep2_bus_cur > 12)
		{
			ShowCommMes("上电测试失败 "+ count,MesgType.MESG_TYPE_ERROR,1);
			BTErrorCount(1);
			break;
		}else{
			
			ShowCommMes("上电测试通过 "+ count,MesgType.MESG_TYPE_INFO,1);
		}
	}
	ShowCommMes("上电测试结束 ",MesgType.MESG_TYPE_INFO,1);
}