using DataEntity;
using DataEntity.Share;
using DataRWDAL.Rack;
using HxEnum;
using HxSocket;
using MySqlX.XDevAPI.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Xml;
using XCommon;
using XCommon.Log;
using XCommon.Xml;
using XCore;
using XHandler.Controls.Run.Com;
using XHandler.View;
using XHandler.View.Com;
using XImagingXhandler.XDAL;
using static HxEnum.OtherEnum;
namespace XHandler.Controls
{
///
/// 子方法逻辑控制类
///
public class SubMethodControl
{
#region 变量
// 系统语言
private string m_currentCulture = string.Empty;
private MethodSubMethod m_subMethodData = null;
public RunWnd launchView = null;
#region BLL
private SubMethodBll m_subMethodBll = new SubMethodBll();
#endregion
#endregion
///
/// 构造函数
///
public SubMethodControl()
{
m_currentCulture = Shared.SoftwareInformation.currentculture;
}
#region 执行子方法
///
/// 执行子方法
///
///
///
///
///
public bool Execute(XmlNode xmlEnv, XmlNode methodNode, bool isSimulator)
{
bool bResult = false;
var platformNodeList = xmlEnv.SelectNodes("platform");
string methodName = methodNode.SelectSingleNode("name").InnerText;
string methodContent= methodNode.SelectSingleNode("comment").InnerText;
#region 任务被取消
if (launchView._cancelSource.IsCancellationRequested)
{
return bResult;
}
#endregion
#region RunLog start
if (m_currentCulture.Equals("zh-CN"))
{
launchView.AddLogs("【" + DateTime.Now.ToString("HH:mm:ss:fff") + "】> Xhandler: 【" + methodName + "】" + Properties.MachineRunResource.strStart.ToString());
}
else
{
launchView.AddLogs("【" + DateTime.Now.ToString("HH:mm:ss:fff") + "】> Xhandler: 【" + methodName + "】" + "start:");
}
#endregion
try
{
#region 数据准备
m_subMethodData = m_subMethodBll.GenerateMethodSubMethodDataByXmlNode(methodNode);
#endregion
//MethodEx mExParent = ComUtility.FindMethodExByKeyId(Shared.AllNodeMethod, m_subMethodData.keyId);
//if (mExParent == null || mExParent.SubMethod == null)
//{
// #region 子方法节点找不到
// if (m_currentCulture.Equals("zh-CN"))
// {
// launchView.AddLogs("【" + DateTime.Now.ToString("HH:mm:ss:fff") + "】> Xhandler: 【" + methodName + "】" + Properties.MachineRunResource.strProgress.ToString() + "子方法节点找不到!");
// }
// else
// {
// launchView.AddLogs("【" + DateTime.Now.ToString("HH:mm:ss:fff") + "】> Xhandler: 【" + methodName + "】" + "progress: sub method not found!");
// }
// #endregion
// return bResult;
//}
//MethodEx mExSub = mExParent.SubMethod;
// 构建xml信息
XmlHelper xmlHelper = new XmlHelper();
string fullName = m_subMethodData.filePath;
if (File.Exists(fullName))
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(fullName);
//MethodListBll methodListBll = new MethodListBll();
//XmlNodeList xmlNodeMethodList = xmlDocument.SelectNodes("root/method[@id>=3]");
//for (int n = 0; n < xmlNodeMethodList.Count; n++)
//{
// string MethodName = xmlNodeMethodList[n].SelectSingleNode("name").InnerText;
// int iNum = methodListBll.getNumByMethodName(MethodName);
// switch (iNum)
// {
// case (int)MethodNameEnum.transferfile:
// if (xmlNodeMethodList[n].SelectSingleNode("status").InnerText == "enable")
// {
// xmlNodeMethodList[n].SelectSingleNode("filePath").InnerText = strParam2;
// }
// break;
// case (int)MethodNameEnum.coatingfile:
// if (xmlNodeMethodList[n].SelectSingleNode("status").InnerText == "enable")
// {
// xmlNodeMethodList[n].SelectSingleNode("filePath").InnerText = strParam2;
// }
// break;
// default:
// break;
// }
//}
//xmlHelper.xmlDoc = xmlDocument;//ViewCom.GenerateLaunchXmlDocument(mExSub, null, false);
// 获取Main的台面信息,往ChildRoot里最前插入
XmlNode envRoot = xmlDocument.SelectSingleNode("root/env");
XmlNode mainNode = xmlDocument.SelectSingleNode("root");
mainNode.RemoveChild(envRoot);
// 导入节点到目标文档
XmlNode importedNode = xmlDocument.ImportNode(xmlEnv, true);
mainNode.AppendChild(importedNode);
//envRoot = xmlEnv;
//mainRoot.InsertBefore(xmlEnv, refNode);
//ViewCom.CreateEnv(xmlHelper.xmlDoc, Shared.GdTableRun);
#region 运行开始节点
int iresult = -1;
XmlNode startNode = xmlDocument.SelectSingleNode("root/method[@id='3']");
if (startNode != null)
{
int mid = Convert.ToInt32(startNode.Attributes["id"].Value);
if (!m_subMethodData.isConcurrence)
{
Task tRun = Task.Run(() =>
{
iresult = launchView.ExecMethodList(xmlDocument, mid);
#region RunLog end
if (m_currentCulture.Equals("zh-CN"))
{
launchView.AddLogs("【" + DateTime.Now.ToString("HH:mm:ss:fff") + "】> Xhandler: 【" + methodName + "】" + methodContent + Properties.MachineRunResource.strEndSubmethod.ToString());
}
else
{
launchView.AddLogs("【" + DateTime.Now.ToString("HH:mm:ss:fff") + "】> Xhandler: 【" + methodName + "】" + methodContent + " method is executed completely;");
}
#endregion
}, launchView._cancelSource.Token);
Task.WaitAll(new Task[] { tRun });
bResult = (iresult == 0 || iresult == -2) ? false : true;
}
else
{
Task tRun = Task.Run(() =>
{
iresult = launchView.ExecMethodListForCombine(xmlDocument, mid);
#region RunLog end
if (m_currentCulture.Equals("zh-CN"))
{
launchView.AddLogs("【" + DateTime.Now.ToString("HH:mm:ss:fff") + "】> Xhandler: 【" + methodName + "】" + methodContent + Properties.MachineRunResource.strEndSubmethod.ToString());
}
else
{
launchView.AddLogs("【" + DateTime.Now.ToString("HH:mm:ss:fff") + "】> Xhandler: 【" + methodName + "】" + methodContent + " method is executed completely;");
}
#endregion
}, launchView._cancelSource.Token);
bResult = true;
launchView.taskForSubmethod.Add(tRun);
if (m_currentCulture.Equals("zh-CN"))
{
launchView.AddLogs("【" + DateTime.Now.ToString("HH:mm:ss:fff") + "】> Xhandler: 【" + methodName + "】" + Properties.MachineRunResource.strStartAsynchronization.ToString() + methodContent);
}
else
{
launchView.AddLogs("【" + DateTime.Now.ToString("HH:mm:ss:fff") + "】> Xhandler: 【" + methodName + "】" + "is executing method with the asynchronization");
}
}
}
#endregion
}
}
catch (Exception ex)
{
LoggerHelper.ErrorLog("ERROR:", ex);
#region Exception
launchView.AddLogs("【" + DateTime.Now.ToString("HH:mm:ss:fff") + "】> Xhandler: 【" + methodName + "】" + "error:source:" + ex.Source + ";error:" + ex.Message + ";");
bResult = false;
#endregion
}
return bResult;
}
#endregion
}
}