schangxiang@126.com
2025-11-04 f5ed29dc26c7cd952d56ec5721a2efc43cd25992
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
using DataEntity.Rack;
using DataEntity.Share;
using DataRWDAL;
using DriverLib.Engine;
using HxEnum;
using HxSocket;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Xml;
using XCommon;
using XCommon.Log;
using XCore;
using XHandler.Controls.Run.Com;
using XHandler.View;
using XImagingXhandler.XDAL;
using static HxEnum.OtherEnum;
 
namespace XHandler.Controls
{
    /// <summary>
    /// 挑选转板控制逻辑类
    /// </summary>
    public class ChoiceTransferControl
    {
        #region 变量
        // 系统语言
        private string m_currentCulture = string.Empty;
        private WellCalc wellCalc = new WellCalc();
 
        #region 挑菌用变量
        public static MethodChoiceAndTransfer ChoiceData = null;
        // 当前孔在所有有效孔中的排序
        private static int CurValidWellIndex = -1;
        // 全部有效孔位list:A1、B1、C1
        private static List<string> LstAllValidWells = new List<string>();
        // 挑菌台面位置
        private static string ChoiceLatticeName = string.Empty;
        // 菌落信息
        public static Bacteria BacteriaInfo = null;
        private static XmlNode XmlEnv = null;
        private bool IsSimulator = false;
        #endregion
 
        #region BLL
        private CoatingFileBll coatingFileBll = new CoatingFileBll();
        private BacteriaBll bacteriaBll = new BacteriaBll();
        public static ChoiceBll choiceBll = new ChoiceBll();
        #endregion
 
        // 运行界面
        public static RunWnd LaunchView = null;
        public HxSocketClient SocketTcpClientToRemote = null;
        #endregion
 
        /// <summary>
        /// 构造函数
        /// </summary>
        public ChoiceTransferControl()
        {
            m_currentCulture = Shared.SoftwareInformation.currentculture;
        }
 
        #region 执行挑选转板
        /// <summary>
        /// 执行挑选转板
        /// </summary>
        /// <param name="xmlEnv"></param>
        /// <param name="methodNode"></param>
        /// <param name="isSimulator"></param>
        /// <returns>0:异常结束, 1:正常继续(跳出for),2:正常结束</returns>
        public int ExecuteChoiceTransfer(XmlDocument xmlDocument, XmlNode methodNode, bool isSimulator)
        {
            int nResult = 2;
            bool bResult = false;
            this.IsSimulator = isSimulator;
            string strMethodName = methodNode.SelectSingleNode("name").InnerText;
 
            #region 任务被取消
            if (LaunchView._cancelSource.IsCancellationRequested)
            {
                nResult = 0;
                return nResult;
            }
            #endregion
 
            XmlEnv = xmlDocument.SelectSingleNode("root/env");
 
            #region Log
            if (m_currentCulture.Equals("zh-CN"))
            {
                LaunchView.AddLogs("【" + DateTime.Now.ToString("HH:mm:ss:fff") + "】> Xhandler: 【" + strMethodName + "】" + Properties.MachineRunResource.strStart.ToString());
            }
            else
            {
                LaunchView.AddLogs("【" + DateTime.Now.ToString("HH:mm:ss:fff") + "】> Xhandler: 【" + strMethodName + "】start:");
            }
            #endregion
 
            try
            {
                #region 数据准备1
                ChoiceData = choiceBll.GenerateMethodChoiceAndTransferDataByXmlNode(methodNode);
 
                XmlNode curChoiceLabNode = null;                            // 当前挑菌板位的台面信息
                string curValidWell = string.Empty;                         // 当前挑菌的孔位:A1
                List<string> lstResidueValidWells = new List<string>();     // 剩余有效孔位list:B1、C1
                ChoiceLatticeName = string.Empty;                           // 挑菌台面位置
                string choiceLabwareBarcode = string.Empty;                 // 挑菌耗材条码
                int nLatticeid = -1;                                        // 挑菌板位lattice_id
                // Z轴安全位置
                float zAxisSafeVal = (float)Convert.ToDouble(ConfigurationManager.AppSettings["zAxisSafeVal"]);
                BacteriaInfo = bacteriaBll.GetABacteria(ChoiceData.bacteriaValue);
 
                LoggerHelper.InfoLog($"[ChoiceTransferControl]:Args:  {JsonConvert.SerializeObject(ChoiceData)}");
                #endregion
 
                #region 获取挑菌类型所有效的孔
                ResidueValidWells residueValidWells = GetResidueValidWells();
                if (residueValidWells == null)
                {
                    LaunchView.AddLogs("【" + DateTime.Now.ToString("HH:mm:ss:fff") + "】> Xhandler: 【" + strMethodName + "】" + Properties.ChoiceResource.strNoValidWells.ToString());
                    nResult = 1;
                    return nResult;
                }
 
                curChoiceLabNode = residueValidWells.CurChoiceLabNode;
                lstResidueValidWells = residueValidWells.LstResidueValidWells;
                #endregion
 
                #region 数据准备2
                // 全部有效孔list
                string allValidWells = curChoiceLabNode.SelectSingleNode("allValidWells").InnerText;
                LstAllValidWells = ComUtility.GetValidWells(allValidWells);
                // 当前挑菌的孔位
                curValidWell = lstResidueValidWells[0];
                // 当前孔在所有有效孔中的排序
                CurValidWellIndex = LstAllValidWells.IndexOf(curValidWell);
                // 挑菌板位:Px
                ChoiceLatticeName = curChoiceLabNode.SelectSingleNode("labware_sname").InnerText;
                // 挑菌耗材条码
                choiceLabwareBarcode = curChoiceLabNode.SelectSingleNode("labware_barcode").InnerText;
                // 挑菌板位lattice_id
                nLatticeid = ControlCom.GetLatticeId(Convert.ToInt32(curChoiceLabNode.SelectSingleNode("lattice_id").InnerText));
                #endregion
 
                #region 自动开盖
                // 自动开挑选板盖
                bResult = OpenClover(XmlEnv, isSimulator);
                if (!bResult)
                {
                    nResult = 0;
                    return nResult;
                }
 
                // 查找涂布转板方法
                XmlNodeList xmlNodeMethodList = xmlDocument.SelectNodes("root/method[label='涂布转板']");
                if (xmlNodeMethodList.Count > 0)
                {
                    // 自动开涂布板盖
                    CoatingTransferControl.LaunchView = LaunchView;
                    bResult = CoatingTransferControl.OpenClover(XmlEnv, xmlNodeMethodList[0], isSimulator);
                    if (!bResult)
                    {
                        nResult = 0;
                        return nResult;
                    }
                }
                #endregion
 
                #region 超声探测
                // 超声已开启
                if (CurValidWellIndex == 0 && ChoiceData.isEnableultrasonic)
                {
                    // 超声探测
                    int nRtn = ControlCom.DoUltrasonic(LaunchView, XmlEnv, methodNode, isSimulator, ChoiceLatticeName);
                    if (nRtn == 2)      // 2:停止
                    {
                        nResult = 0;
                        return nResult;
                    }
                }
                #endregion
 
                #region 安装涂布头
                //先去安装枪头
                float tipoffsetLength = float.Parse(methodNode.SelectSingleNode("choiceOffset").InnerText);
                bool bLoadTipResult = ControlCom.LoadTipAutomation(XmlEnv.SelectNodes("platform"), methodNode,
                    isSimulator, LaunchView, SocketTcpClientToRemote, tipoffsetLength);
                #endregion
 
                #region 挑选参数
                // 获取板位坐标
                Lattice latticeChoice = LatticeDB.GetLatticeDataByIdFromdb(nLatticeid.ToString());
                // 获取耗材信息
                Labware labwares = LabwareDB.GetLabware(ChoiceData.choiceLabwareValue);
                // 获取板位孔的坐标数据
                List<TipsTable> lstTipsTable = ControlCom.GenerateWellCoordinate(labwares, latticeChoice);
 
                // 获取耗材上指定孔位的坐标
                TipsTable targetWellInfo = lstTipsTable.SingleOrDefault(t => t.lattice_id.Equals(nLatticeid.ToString())
                                            && t.labware_id.Equals(ChoiceData.choiceLabwareValue)
                                            && t.wellname.Equals(curValidWell));
                #endregion
 
                #region 挑菌前吸液
                if (ChoiceData.choiceAgoAspirateData.bEnableBeforeAspirate)
                {
                    string wellName = string.Empty;
                    string labwareId = string.Empty;
                    ChoiceData.choiceAgoAspirateData.armValue = ChoiceData.armValue;
 
                    // 吸液用来源板的参数
                    if (ChoiceData.choiceMode == EnumManagement.GetEnumValue(ChoiceModeEnum.AspirateMode))      // 吸液
                    {
                        wellName = curValidWell;
                        labwareId = ChoiceData.choiceLabwareValue;
                    }
                    else
                    {
                        wellName = ChoiceData.choiceAgoAspirateData.wellarray;
                        labwareId = ChoiceData.choiceAgoAspirateData.labwareValue;
                    }
 
                    // 挑菌前吸液
                    Lattice latticeBeforeAspirate = null;
                    if (ChoiceData.choiceMode == EnumManagement.GetEnumValue(ChoiceModeEnum.AspirateMode))      // 吸液
                    {
                        ChoiceData.choiceAgoAspirateData.labwareValue = ChoiceData.choiceLabwareValue;      // 吸液耗材Id
                        ChoiceData.choiceAgoAspirateData.positionValue = nLatticeid.ToString();             // 吸液板位Id
                    }
 
                    // 获取台面板位信息
                    latticeBeforeAspirate = LatticeDB.GetLatticeDataByIdFromdb(ChoiceData.choiceAgoAspirateData.positionValue);
                    // 吸液
                    bool bBeforeAspirate = ControlCom.DoBeforeAspirate(LaunchView, XmlEnv, strMethodName,
                        ChoiceData.choiceAgoAspirateData, latticeBeforeAspirate,
                        wellName, labwareId, choiceLabwareBarcode, isSimulator);
                    if (!bBeforeAspirate)
                    {
                        nResult = 0;
                        return nResult;
                    }
 
                    // 吸液用来源板的参数
                    if (ChoiceData.choiceMode == EnumManagement.GetEnumValue(ChoiceModeEnum.AspirateMode))      // 吸液
                    {
                        // 更新挑菌孔位
                        UpdateChoiceWellData(lstResidueValidWells, curChoiceLabNode);
                    }
                }
                #endregion
 
                #region 挑选
                // 吸液以外
                if (ChoiceData.choiceMode != EnumManagement.GetEnumValue(ChoiceModeEnum.AspirateMode))
                {
                    #region 执行挑选动作
                    ChoiceMParam choiceMParam = new ChoiceMParam();
                    choiceMParam.xAxisVal = (float)Convert.ToDouble(targetWellInfo.axis_b_X.ToString()) +
                        float.Parse(BacteriaInfo.choice_xaxis_distance.ToString());
                    choiceMParam.yAxisVal = (float)Convert.ToDouble(targetWellInfo.axis_b_Y.ToString()) +
                        float.Parse(BacteriaInfo.choice_yaxis_distance.ToString());
                    choiceMParam.xAxisOffsetVal = 0;
                    choiceMParam.yAxisOffsetVal = 0;
                    choiceMParam.armId = Convert.ToInt32(ChoiceData.armValue);
                    choiceMParam.channelId = 1;
                    choiceMParam.choiceMode = ChoiceData.choiceMode;                                        // 挑菌模式:1:单选; 2:穿刺; 3:吸放液; 4:放液
                    choiceMParam.basezAxisVal = zAxisSafeVal;
                    choiceMParam.enableSupersonic = ChoiceData.isEnableultrasonic;                          // 挑菌时是否开启超声波探测值作为Z轴值:true:用;false:不用
                    choiceMParam.currentLengthOfTip = ControlCom.CurrentLengthOfTip;                        // 当前挑菌安装枪头后,枪增加的长度
                    choiceMParam.choiceTimeOnSameplace = 1;                                                 // 同一个菌落点第几次挑选;1:第一次;2:第二次...
                    choiceMParam.latticeName = ChoiceLatticeName;                                           // 挑菌板位名称
 
                    if (ChoiceData.choiceMode == EnumManagement.GetEnumValue(ChoiceModeEnum.SigleSelectMode))    // 单选
                    {
                        choiceMParam.xAxisOffsetVal = 0;
                        choiceMParam.yAxisOffsetVal = 0;
                        choiceMParam.zAxisVal = (float)targetWellInfo.axis_b_Z - float.Parse(BacteriaInfo.choice_position_distance.ToString());
                    }
                    else if (ChoiceData.choiceMode == EnumManagement.GetEnumValue(ChoiceModeEnum.PricksMode))    // 穿刺
                    {
                        choiceMParam.xAxisOffsetVal = float.Parse(BacteriaInfo.choice_xaxis_distance.ToString());
                        choiceMParam.yAxisOffsetVal = float.Parse(BacteriaInfo.choice_yaxis_distance.ToString());
 
                        // 丝状真菌-固定高度挑菌:板位Z高度 - 底孔高度 - 枪头突出高度 - 挑菌位置距离
                        choiceMParam.zAxisVal = (float)latticeChoice.lattice_Z - (float)Convert.ToDouble(labwares.well_bottom_height) -
                            ControlCom.CurrentLengthOfTip - (float)BacteriaInfo.choice_position_distance;
                    }
 
                    // 执行挑菌动作
                    bool bIsOk = ControlCom.RunChoice(LaunchView, strMethodName, choiceMParam, targetWellInfo.wellname,
                        ChoiceLatticeName, choiceLabwareBarcode, isSimulator, targetWellInfo);
                    if (!bIsOk)
                    {
                        return 0;
                    }
                    #endregion
 
                    // 更新挑菌孔位
                    UpdateChoiceWellData(lstResidueValidWells, curChoiceLabNode);
                }
                #endregion
            }
            catch (Exception ex)
            {
                LoggerHelper.ErrorLog("ERROR:", ex);
 
                #region Exception
                LaunchView.AddLogs("【" + DateTime.Now.ToString("HH:mm:ss:fff") + "】>Xhandler: 【" + strMethodName + "】error:source:" + ex.Source + ";error:" + ex.Message + ";");
                nResult = 0;
                #endregion
            }
 
            return nResult;
        }
 
        /// <summary>
        /// 获取挑菌类型所有效的孔
        /// </summary>
        /// <returns></returns>
        public static ResidueValidWells GetResidueValidWells()
        {
            ResidueValidWells residueValidWells = null;
            List<string> lstResidueValidWells = new List<string>();     // 剩余有效孔位list:B1、C1
 
            // 生成和挑菌耗材类型一致的条件
            string strChoiceLabwareWhere = string.Format("platform[labware_id='{0}']", ChoiceData.choiceLabwareValue);
            // 获取满足挑菌耗材类型的所有台面点位
            XmlNodeList choiceLabwareNodeList = XmlEnv.SelectNodes(strChoiceLabwareWhere);
 
            // 循环台面耗材点位,查找
            foreach (XmlNode choiceLabwareNodeItem in choiceLabwareNodeList)
            {
                string strResidueValidWells = choiceLabwareNodeItem.SelectSingleNode("residueValidWells").InnerText;
 
                // 有效孔字符串转换成List
                lstResidueValidWells = ComUtility.GetValidWells(strResidueValidWells);
                if (lstResidueValidWells.Count == 0)
                {
                    continue;
                }
 
                residueValidWells = new ResidueValidWells();
                residueValidWells.CurChoiceLabNode = choiceLabwareNodeItem;
                residueValidWells.LstResidueValidWells = lstResidueValidWells;
                break;
            }
 
            return residueValidWells;
        }
        #endregion
 
        #region 自动开挑选板盖
        /// <summary>
        /// 自动开挑选板盖
        /// </summary>
        /// <param name="xmlEnv"></param>
        /// <param name="isSimulator"></param>
        /// <returns></returns>
        public static bool OpenClover(XmlNode xmlEnv, bool isSimulator)
        {
            bool bResult = true;
 
            // 自动开盖: 当前孔是第一个有效孔
            if (ChoiceData != null &&
                ChoiceData.autoOpenCloseCover == 1 &&
                CurValidWellIndex == 0)
            {
                // 挑菌的板放盖位号:P2,P3
                List<string> listCoatingCoverLatticeId = ComUtility.GetPlateCoverLatticeName();
 
                // 转移板盖到放盖位方法
                string strTransferPlateCoverMethodFileName = AppDomain.CurrentDomain.BaseDirectory +
                    "\\" + ConfigurationManager.AppSettings["transferDesPlateCoverMethod"].ToString();
 
                // 转移目标板的盖到放盖位:PX->P2
                bResult = ControlCom.DoReadMethodFileAndActionTransfer(strTransferPlateCoverMethodFileName, xmlEnv, ChoiceLatticeName, PositonTypeEnum.Desktop, GripperModelEnum.Right,
                    listCoatingCoverLatticeId[0], PositonTypeEnum.Desktop, GripperModelEnum.Right, isSimulator, LaunchView, TransferTypeEnum.Open);
                return bResult;
            }
 
            return bResult;
        }
 
        /// <summary>
        /// 自动关挑选板盖
        /// </summary>
        /// <param name="xmlEnv"></param>
        /// <param name="isSimulator"></param>
        /// <param name="isForceCloseClover">是否强制关盖</param>
        /// <returns></returns>
        public static bool CloseClover(XmlNode xmlEnv, bool isSimulator, bool isForceCloseClover = false)
        {
            bool bResult = true;
 
            // 自动关盖: 当前孔是第最后一个有效孔
            if (ChoiceData != null &&
                ChoiceData.autoOpenCloseCover == 1 &&
                ((CurValidWellIndex == LstAllValidWells.Count() - 1) || isForceCloseClover))
            {
                // 挑菌的板放盖位号:P2,P3
                List<string> listCoatingCoverLatticeId = ComUtility.GetPlateCoverLatticeName();
                // 转移板盖到放盖位方法
                string strTransferPlateCoverMethodFileName = AppDomain.CurrentDomain.BaseDirectory +
                    "\\" + ConfigurationManager.AppSettings["transferDesPlateCoverMethod"].ToString();
 
                // 转移目标板的盖到板上:P2->Px
                bResult = ControlCom.DoReadMethodFileAndActionTransfer(strTransferPlateCoverMethodFileName, xmlEnv, listCoatingCoverLatticeId[0], PositonTypeEnum.Desktop, GripperModelEnum.Right,
                    ChoiceLatticeName, PositonTypeEnum.Desktop, GripperModelEnum.Right, isSimulator, LaunchView, TransferTypeEnum.Close);
            }
 
            return bResult;
        }
        #endregion
 
        /// <summary>
        /// 更新挑菌孔位
        /// </summary>
        /// <param name="lstResidueValidWells"></param>
        /// <param name="curChoiceLabNode"></param>
        private void UpdateChoiceWellData(List<string> lstResidueValidWells, XmlNode curChoiceLabNode)
        {
            // 删除当前挑菌孔位
            lstResidueValidWells.RemoveAt(0);
            // 更新剩余有效孔位
            curChoiceLabNode.SelectSingleNode("residueValidWells").InnerText = string.Join(",", lstResidueValidWells);
        }
    }
}