schangxiang@126.com
2025-11-04 ca398287db3f5ea01f03aac4a85edb13b28100a6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
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
{
    /// <summary>
    /// 子方法逻辑控制类
    /// </summary>
    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
 
        /// <summary>
        /// 构造函数
        /// </summary>
        public SubMethodControl()
        {
            m_currentCulture = Shared.SoftwareInformation.currentculture;
        }
 
        #region 执行子方法
        /// <summary>
        /// 执行子方法
        /// </summary>
        /// <param name="xmlEnv"></param>
        /// <param name="methodNode"></param>
        /// <param name="isSimulator"></param>
        /// <returns></returns>
        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
 
    }
}