var epe_oper = {
	net_sw:0,
	comm_port: "",
	comm_baud: "",
	comm_parity: -1,
	comm_stopbit: -1,
	server_ip:"192.1678.60.11",
	server_port:"20570",
	client_port:"20570",
	master_firmware:"",
	jcxb_firmware:"",
	checker_plan:"",
	ew_code:"",
	readack_addr:0,
	master_runapp_key:[],
	jcxb_runapp_key:[],
}
var epe_qthandle = null;

function Epe_Start_Comm() {
	console.log("Epe_Start_Comm ");
	if (typeof(epe_qthandle) != "undefined" && epe_qthandle != null) {
		epe_qthandle.SearchCommport();
		console.log("SearchCommport ");
	}
}
function Epe_Open_CommPort(main_serial) {
	epe_oper.comm_port = main_serial.comm_port;
	epe_oper.comm_baud = main_serial.comm_baud;
	epe_oper.comm_parity = main_serial.comm_parity;
	epe_oper.comm_stopbit = main_serial.comm_stopbit;

	Main_Close_CommPort_Select();
	var json_str = JSON.stringify(epe_oper);
	console.log(json_str);
	if (typeof(epe_qthandle) != "undefined" && epe_qthandle != null) {
		epe_qthandle.OpenCommPort(json_str);
	}
}

function EPe_ConectNet_SW()
{
	var obj_btn = $("#epe_net_swbtn")[0];
	epe_oper.server_ip = $('#epe_server_ip').val();
	epe_oper.server_port =parseInt($('#epe_server_port').val());
	epe_oper.client_port =parseInt($('#epe_client_port').val());
	if(epe_oper.net_sw == 1)
	{
		obj_btn.innerText = "启动网络";
		epe_oper.net_sw = 0;
	}else{
		epe_oper.net_sw = 1;
		obj_btn.innerText = "关闭网络" ;
	}
	var json_str = JSON.stringify(epe_oper);
	
	if (typeof(epe_qthandle) != "undefined" && epe_qthandle != null) {
		console.log(json_str);
		epe_qthandle.Epe_NetConnect(json_str);
	}
}

function Epe_OpenMasterFirmware()
{
	if (typeof(epe_qthandle) != "undefined" && epe_qthandle != null) {
		epe_qthandle.Epe_OpenMasterFirmware();
	}
	
}
function Epe_OpenJCXBFirmware()
{
	if (typeof(epe_qthandle) != "undefined" && epe_qthandle != null) {
		epe_qthandle.Epe_OpenJCXBFirmware();
	}
}

function Epe_OpenCheckerPlan()
{
	if (typeof(epe_qthandle) != "undefined" && epe_qthandle != null) {
		epe_qthandle.Epe_OpenCheckerPlan();
	}
}
function Epe_OpenEwCode()
{
	if (typeof(epe_qthandle) != "undefined" && epe_qthandle != null) {
		epe_qthandle.Epe_OpenEwCode();
	}
}

function Epe_Init() {
	console.log("Epe_Init   ---");
	if (typeof(qchannel) != "undefined" && qchannel != null) {
		if(epe_qthandle != null && typeof(epe_qthandle) != "undefined")
		{
			console.log("Epe_Inited  OK ---");
		}else{
			
			epe_qthandle = qchannel.objects.epe_qthandle;
			if ((typeof(epe_qthandle) != "undefined") && (epe_qthandle != null)) {
				console.log("Epe_Init  OK ---");
				epe_qthandle.ShowCommMes.connect(function(mesg, type) {
					ShowCommMes(mesg, type);
			
				});
				epe_qthandle.Epe_ShowMastFireware.connect(function(filepath) {
					epe_oper.master_firmware = filepath;
					$('#epe_master_firmware').val(epe_oper.master_firmware);
				});
				epe_qthandle.Epe_ShowJCXBFireware.connect(function(filepath) {
					epe_oper.jcxb_firmware = filepath;
					$('#epe_jcxb_firmware').val(epe_oper.jcxb_firmware);
				});
				epe_qthandle.Epe_ShowCheckerPlan.connect(function(filepath) {
					epe_oper.checker_plan = filepath;
					$('#epe_checker_plan').val(epe_oper.checker_plan);
				});
				epe_qthandle.Epe_ShowEwCode.connect(function(filepath) {
					epe_oper.ew_code = filepath;
					$('#epe_ewcode_file').val(epe_oper.ew_code);
				});
				epe_qthandle.ShowCommport.connect(function(jsoninfo) {
					Main_ShowCommport(jsoninfo);
				});
				epe_qthandle.Epe_RecCrcFilelen.connect(function(type,jsoninfo) {
					var frame = JSON.parse(jsoninfo);
					var mes = "App跳转信息： "+ Bytes2Str(frame);
					ShowCommMes(mes,MesgType.MESG_TYPE_INFO);
					if(type == 1)
					{
						epe_oper.master_runapp_key = frame;
					}else if(type == 2)
					{
						epe_oper.jcxb_runapp_key = frame;
					}
				});
				epe_qthandle.EPeComm_Recive.connect(function(jsoninfo) {
					EPeComm_Recive(jsoninfo);
				});
			}
			
		}
	}
	EPe_ShowDef();
	for(var i = 0; i < 16; i++)
	{
		epe_oper.master_runapp_key[i] = 0x0FF;
	}
}

function  EPe_ShowDef()
{
	$('#epe_m_firmware').val(epe_oper.master_firmware);
	$('#epe_jcxb_firmware').val(epe_oper.jcxb_firmware);
	$('#epe_readack_addr').val(Byte2Hex(epe_oper.readack_addr));
	$('#epe_ewcode_file').val(epe_oper.ew_code);
}

function EPeComm_Recive(jsoninfo)
{
	var frame = JSON.parse(jsoninfo);
	var frame_str = "Rx: " + Bytes2Str(frame);
	ShowCommMes(frame_str, MesgType.MESG_TYPE_INFO);
	if(frame[5] == 0xFB && frame[6] == 0x01)
	{
		var addr_buf = [];
		addr_buf[0] = frame[4];
		var mes = "地址 0x"+ Bytes2Str(addr_buf)+" 在线";
		ShowCommMes(mes, MesgType.MESG_TYPE_INFO);
	}
}
var Epe_Cmd_Head = "Ym";
function Epe_SendCmd(cmd_buf) {

	var buf = Str2Bytes(Epe_Cmd_Head);
	buf[2] = 0; //数据长度
	buf[3] = 0;
	for (var index = 0; index < cmd_buf.length; index++) {
		buf[4 + index] = cmd_buf[index];
	}
	buf[buf.length] = 0;
	buf[2] = buf.length & 0x0FF;
	buf[3] = (buf.length >> 8) & 0x0FF;
	buf[buf.length-1] = Epj_CalcCrc8(buf,buf.length-1);
	var json_str = JSON.stringify(buf);
	if (typeof(epe_qthandle) != "undefined" && epe_qthandle != null) {
		epe_qthandle.Epe_SendData(json_str);
	}
	var mes = "Tx:" + Bytes2Str(buf);
	ShowCommMes(mes, MesgType.MESG_TYPE_INFO);
}

async function Epe_DownLoadMaster()
{
	var commaddr = parseInt($('#epe_readack_addr').val(),16);
	var cmdbuf = [];
	cmdbuf[0] = commaddr;
	cmdbuf[1] = 0xFE;//擦除代码空间
	Epe_SendCmd(cmdbuf);
	await TransSleep(5000);
	ShowCommMes("擦除代码备份区", MesgType.MESG_TYPE_INFO);
	if (typeof(epe_qthandle) != "undefined" && epe_qthandle != null) {
		epe_qthandle.Epe_DownLoadMaster(epe_oper.master_firmware,"0x08080010",commaddr);
	}
}

async function Epe_JCXBJumpBoot()
{
	var commaddr = parseInt($('#epe_readack_addr').val(),16);
	var cmdbuf = [];
	cmdbuf[0] = commaddr;
	cmdbuf[1] = 0xFA;//运行BootLoader;
	Epe_SendCmd(cmdbuf);
	ShowCommMes("跳转BootLoader", MesgType.MESG_TYPE_INFO);
	await TransSleep(1000);
}

async function Epe_UpdateJCXB()
{
	var commaddr = parseInt($('#epe_readack_addr').val(),16);
	var cmdbuf = [];
	cmdbuf[0] = commaddr;
	cmdbuf[1] = 0xFE;//擦除代码空间
	Epe_SendCmd(cmdbuf);
	await TransSleep(2000);
	ShowCommMes("擦除代码空间", MesgType.MESG_TYPE_INFO);
	if (typeof(epe_qthandle) != "undefined" && epe_qthandle != null) {
		epe_qthandle.Epe_UpdateJCXB(epe_oper.jcxb_firmware,"0x08003000",commaddr);
	}
}

async function Epe_ReadAck()
{
	var commaddr = parseInt($('#epe_readack_addr').val(),16);
	var cmdbuf = [];
	cmdbuf[0] = commaddr;
	cmdbuf[1] = 0xFB;//在线闻讯
	Epe_SendCmd(cmdbuf);
}

async function Epe_JCXBRunApp()
{
	var commaddr = parseInt($('#epe_readack_addr').val(),16);
	var cmdbuf = [];
	cmdbuf[0] = commaddr;
	cmdbuf[1] = 0xFC;//跳转App
	for(var i = 0; i < 12;i++ )
	{
		cmdbuf[i+2] = epe_oper.jcxb_runapp_key[i];
	}
	Epe_SendCmd(cmdbuf);
}


async function Epe_ReloadMaster()
{
	var commaddr = parseInt($('#epe_readack_addr').val(),16);
	var cmdbuf = [];
	cmdbuf[0] = commaddr;
	cmdbuf[1] = 0xFC;//跳转App
	for(var i = 0; i < 12;i++ )
	{
		cmdbuf[i+2] = epe_oper.master_runapp_key[i];
	}
	Epe_SendCmd(cmdbuf);
}

async function Epe_MasterYmExist()
{
	var commaddr = parseInt($('#epe_readack_addr').val(),16);
	var cmdbuf = [];
	cmdbuf[0] = commaddr;
	cmdbuf[1] = 0x03;//跳转App
	Epe_SendCmd(cmdbuf);
}

function Epe_DownPlan()
{
	if (typeof(epe_qthandle) != "undefined" && epe_qthandle != null) {
		//epe_qthandle.Epe_DownPlan( epe_oper.checker_plan,0x800F400);
		epe_qthandle.Epe_DownPlan( epe_oper.checker_plan,0x803F000);
	}
}



function Epe_DownEwCode()
{
	
		if (typeof(epe_qthandle) != "undefined" && epe_qthandle != null) {
			//epe_qthandle.Epe_DownPlan( epe_oper.checker_plan,0x800F400);
			epe_oper.ew_code = $('#epe_ewcode_file').val();
			epe_qthandle.Epe_DownEwCode( epe_oper.ew_code,0x803B000);
		}
}
