| | |
| | | } |
| | | |
| | | } |
| | | bool is3D = false; |
| | | bool is2D = false; |
| | | |
| | | private void TriggerRunMessageReceived(string name, string msg) |
| | | { |
| | |
| | | GlobalVar.dicProcesses.Values.AsParallel().ForAll(v => v.bCompleted = false); |
| | | LogInfo(string.Format($"检查到可被触发的流程,清空所有流程运行完成标记位!"), LogInfoType.INFO); |
| | | |
| | | Task.Factory.StartNew(() => |
| | | if (!is3D && name.Contains("L")) |
| | | { |
| | | Parallel.ForEach(matchedItems, item => |
| | | Task.Factory.StartNew(() => |
| | | { |
| | | string ProcessName = item.Value["流程名"]; |
| | | LogInfo($"流程[{ProcessName}]开始运行", LogInfoType.INFO); |
| | | |
| | | if (!GlobalVar.dicProcesses.ContainsKey(ProcessName)) |
| | | if (name.Contains("L")) |
| | | { |
| | | LogInfo(string.Format("流程[{0}]不存在,请检查流程设置", ProcessName), LogInfoType.ERROR); |
| | | return; |
| | | is3D = true; |
| | | } |
| | | |
| | | ProcessRunBll RunBll = GlobalVar.dicProcesses[ProcessName]; |
| | | if (RunBll == null || RunBll.bRuning) |
| | | Parallel.ForEach(matchedItems, item => |
| | | { |
| | | LogInfo(string.Format("流程[{0}]上次未运行完成,触发失败", ProcessName) |
| | | , LogInfoType.ERROR); |
| | | return; |
| | | } |
| | | string ProcessName = item.Value["流程名"]; |
| | | LogInfo($"流程[{ProcessName}]开始运行", LogInfoType.INFO); |
| | | |
| | | try |
| | | { |
| | | bool result = false; |
| | | string msg = string.Empty; |
| | | int times = Convert.ToInt32(item.Value["重测次数"].ToString()); |
| | | string ConnecResult = item.Value["关联结果"]; |
| | | |
| | | if (times < 0) |
| | | if (!GlobalVar.dicProcesses.ContainsKey(ProcessName)) |
| | | { |
| | | result = RunBll.Run(); |
| | | msg = RunBll.Msg; |
| | | |
| | | if (!(string.IsNullOrEmpty(ConnecResult) || ConnecResult.Trim() == "未关联")) |
| | | { |
| | | if (!result) |
| | | { |
| | | LogInfo(string.Format("流程[{0}]运行结果失败,原因是[{1}]", ProcessName, msg), LogInfoType.NOSHOW); |
| | | } |
| | | RunBll.GetBooleanOutput(ConnecResult, out result); |
| | | RunBll.Result = result; |
| | | } |
| | | |
| | | if (!result) |
| | | { |
| | | LogInfo($"流程[{ProcessName}]被强制运行成功", LogInfoType.WARN); |
| | | RunBll.Result = true; |
| | | result = true; |
| | | } |
| | | LogInfo(string.Format("流程[{0}]不存在,请检查流程设置", ProcessName), LogInfoType.ERROR); |
| | | return; |
| | | } |
| | | else |
| | | |
| | | ProcessRunBll RunBll = GlobalVar.dicProcesses[ProcessName]; |
| | | if (RunBll == null || RunBll.bRuning) |
| | | { |
| | | while (times >= 0) |
| | | LogInfo(string.Format("流程[{0}]上次未运行完成,触发失败", ProcessName) |
| | | , LogInfoType.ERROR); |
| | | return; |
| | | } |
| | | |
| | | try |
| | | { |
| | | bool result = false; |
| | | string msg = string.Empty; |
| | | int times = Convert.ToInt32(item.Value["重测次数"].ToString()); |
| | | string ConnecResult = item.Value["关联结果"]; |
| | | |
| | | if (times < 0) |
| | | { |
| | | result = RunBll.Run(); |
| | | msg = RunBll.Msg; |
| | | |
| | | if (!(string.IsNullOrEmpty(ConnecResult) || ConnecResult.Trim() == "未关联")) |
| | | { |
| | | if (!result) |
| | | { |
| | | LogInfo(string.Format("流程[{0}]运行结果失败,原因是[{1}]", ProcessName, msg), LogInfoType.NOSHOW); |
| | | } |
| | | |
| | | RunBll.GetBooleanOutput(ConnecResult, out result); |
| | | RunBll.Result = result; |
| | | } |
| | | |
| | | if (result) |
| | | if (!result) |
| | | { |
| | | break; |
| | | LogInfo($"流程[{ProcessName}]被强制运行成功", LogInfoType.WARN); |
| | | RunBll.Result = true; |
| | | result = true; |
| | | } |
| | | else if (!result && times > 0) |
| | | { |
| | | LogInfo(string.Format("流程[{0}]运行失败重新测试,剩余次数[{1}]", ProcessName, times), LogInfoType.WARN); |
| | | } |
| | | |
| | | times--; |
| | | } |
| | | } |
| | | |
| | | string ConnectProcess = item.Value["关联流程"]; |
| | | if (!(ConnectProcess == null || string.IsNullOrEmpty(ConnectProcess) || ConnectProcess.Trim() == "")) |
| | | { |
| | | //用逗号或者分号去间隔关联流程 |
| | | string[] arrConnectProcess; |
| | | if (ConnectProcess.Split(';').Length >= ConnectProcess.Split(',').Length) |
| | | { |
| | | arrConnectProcess = ConnectProcess.Split(';'); |
| | | } |
| | | else |
| | | { |
| | | arrConnectProcess = ConnectProcess.Split(','); |
| | | } |
| | | |
| | | foreach (string strConnectProcess in arrConnectProcess) |
| | | { |
| | | if (GlobalVar.dicProcesses.ContainsKey(strConnectProcess)) |
| | | while (times >= 0) |
| | | { |
| | | ProcessRunBll ConnectRunBll = GlobalVar.dicProcesses[strConnectProcess]; |
| | | |
| | | int waitTime = 10; |
| | | DateTime startTime = DateTime.Now; |
| | | while ((DateTime.Now - startTime).TotalSeconds < waitTime |
| | | && (ConnectRunBll.bRuning || !ConnectRunBll.bCompleted)) |
| | | result = RunBll.Run(); |
| | | msg = RunBll.Msg; |
| | | if (!(string.IsNullOrEmpty(ConnecResult) || ConnecResult.Trim() == "未关联")) |
| | | { |
| | | LogInfo(string.Format("关联流程[{0}]未运行完成,剩余等待[{1}]s", strConnectProcess, (waitTime - ((DateTime.Now - startTime).TotalSeconds))) |
| | | , LogInfoType.NOSHOW); |
| | | Thread.Sleep(1000); |
| | | continue; |
| | | if (!result) |
| | | { |
| | | LogInfo(string.Format("流程[{0}]运行结果失败,原因是[{1}]", ProcessName, msg), LogInfoType.NOSHOW); |
| | | } |
| | | |
| | | RunBll.GetBooleanOutput(ConnecResult, out result); |
| | | RunBll.Result = result; |
| | | } |
| | | |
| | | if (ConnectRunBll.bRuning || !ConnectRunBll.bCompleted) |
| | | if (result) |
| | | { |
| | | GlobalVar.dicProcesses[ProcessName].Msg = string.Format("流程[{0}]未运行完成", ProcessName); |
| | | LogInfo(string.Format("关联流程[{0}]未运行完成", strConnectProcess), LogInfoType.ERROR); |
| | | result = false; |
| | | break; |
| | | } |
| | | else if (!ConnectRunBll.bRuning && ConnectRunBll.bCompleted) |
| | | else if (!result && times > 0) |
| | | { |
| | | LogInfo(string.Format("关联流程[{0}]运行完成", strConnectProcess), LogInfoType.INFO); |
| | | LogInfo(string.Format("流程[{0}]运行失败重新测试,剩余次数[{1}]", ProcessName, times), LogInfoType.WARN); |
| | | } |
| | | |
| | | result &= ConnectRunBll.Result; |
| | | if (!ConnectRunBll.Result) |
| | | times--; |
| | | } |
| | | } |
| | | |
| | | string ConnectProcess = item.Value["关联流程"]; |
| | | if (!(ConnectProcess == null || string.IsNullOrEmpty(ConnectProcess) || ConnectProcess.Trim() == "")) |
| | | { |
| | | //用逗号或者分号去间隔关联流程 |
| | | string[] arrConnectProcess; |
| | | if (ConnectProcess.Split(';').Length >= ConnectProcess.Split(',').Length) |
| | | { |
| | | arrConnectProcess = ConnectProcess.Split(';'); |
| | | } |
| | | else |
| | | { |
| | | arrConnectProcess = ConnectProcess.Split(','); |
| | | } |
| | | |
| | | foreach (string strConnectProcess in arrConnectProcess) |
| | | { |
| | | if (GlobalVar.dicProcesses.ContainsKey(strConnectProcess)) |
| | | { |
| | | LogInfo($"流程[{ProcessName}]的关联流程[{strConnectProcess}]运行失败", LogInfoType.ERROR); |
| | | msg = $"关联流程[{strConnectProcess}]运行失败"; |
| | | ProcessRunBll ConnectRunBll = GlobalVar.dicProcesses[strConnectProcess]; |
| | | |
| | | int waitTime = 10; |
| | | DateTime startTime = DateTime.Now; |
| | | while ((DateTime.Now - startTime).TotalSeconds < waitTime |
| | | && (ConnectRunBll.bRuning || !ConnectRunBll.bCompleted)) |
| | | { |
| | | LogInfo(string.Format("关联流程[{0}]未运行完成,剩余等待[{1}]s", strConnectProcess, (waitTime - ((DateTime.Now - startTime).TotalSeconds))) |
| | | , LogInfoType.NOSHOW); |
| | | Thread.Sleep(1000); |
| | | continue; |
| | | } |
| | | |
| | | if (ConnectRunBll.bRuning || !ConnectRunBll.bCompleted) |
| | | { |
| | | GlobalVar.dicProcesses[ProcessName].Msg = string.Format("流程[{0}]未运行完成", ProcessName); |
| | | LogInfo(string.Format("关联流程[{0}]未运行完成", strConnectProcess), LogInfoType.ERROR); |
| | | result = false; |
| | | break; |
| | | } |
| | | else if (!ConnectRunBll.bRuning && ConnectRunBll.bCompleted) |
| | | { |
| | | LogInfo(string.Format("关联流程[{0}]运行完成", strConnectProcess), LogInfoType.INFO); |
| | | } |
| | | |
| | | result &= ConnectRunBll.Result; |
| | | if (!ConnectRunBll.Result) |
| | | { |
| | | LogInfo($"流程[{ProcessName}]的关联流程[{strConnectProcess}]运行失败", LogInfoType.ERROR); |
| | | msg = $"关联流程[{strConnectProcess}]运行失败"; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | LogInfo(result ? $"流程[{ProcessName}]运行成功" : $"流程[{ProcessName}]运行失败,原因是{msg}", result ? LogInfoType.PASS : LogInfoType.ERROR); |
| | | LogInfo(result ? $"流程[{ProcessName}]运行成功" : $"流程[{ProcessName}]运行失败,原因是{msg}", result ? LogInfoType.PASS : LogInfoType.ERROR); |
| | | |
| | | string SendComName = result ? item.Value["成功通讯"] : item.Value["失败通讯"]; |
| | | string SendMsg = result ? item.Value["成功字符"] : item.Value["失败字符"]; |
| | | if (GlobalVar.dicCommunicators.ContainsKey(SendComName) && (!string.IsNullOrEmpty(SendMsg) || SendMsg.Trim() != "")) |
| | | { |
| | | GlobalVar.dicCommunicators[SendComName].SendMessage(SendMsg); |
| | | LogInfo(string.Format("发送给[{0}]了消息\"{1}\"", SendComName, SendMsg), LogInfoType.INFO); |
| | | } |
| | | |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | LogInfo(string.Format("流程[{0}]运行发生了意外,原因是:{1}", ProcessName, ex.Message + $"【{ex.StackTrace}】"), LogInfoType.ERROR); |
| | | |
| | | RunBll.Result = false; |
| | | RunBll.Msg = $"[意外]{ex.Message}"; |
| | | } |
| | | finally |
| | | { |
| | | #region 从RunSettingPage和Layout中获取是否保存图片 |
| | | |
| | | string strImageType = "jpeg"; |
| | | bool bSaveRunImage = false; |
| | | bool bSaveResultImage = false; |
| | | long lImageQuality = 100L; |
| | | |
| | | //ckbSaveRunImage |
| | | if (GlobalVar.ControlStates.TryGetValue("ckbSaveRunImage_CheckBox", out object oSaveRunImage)) |
| | | { |
| | | if (oSaveRunImage != null && oSaveRunImage is bool) |
| | | string SendComName = result ? item.Value["成功通讯"] : item.Value["失败通讯"]; |
| | | string SendMsg = result ? item.Value["成功字符"] : item.Value["失败字符"]; |
| | | if (GlobalVar.dicCommunicators.ContainsKey(SendComName) && (!string.IsNullOrEmpty(SendMsg) || SendMsg.Trim() != "")) |
| | | { |
| | | bSaveRunImage = (bool)oSaveRunImage; |
| | | GlobalVar.dicCommunicators[SendComName].SendMessage(SendMsg); |
| | | LogInfo(string.Format("发送给[{0}]了消息\"{1}\"", SendComName, SendMsg), LogInfoType.INFO); |
| | | } |
| | | } |
| | | |
| | | //ckbSaveResultImage |
| | | if (GlobalVar.ControlStates.TryGetValue("ckbSaveResultImage_CheckBox", out object oSaveResultImage)) |
| | | { |
| | | if (oSaveResultImage != null && oSaveResultImage is bool) |
| | | { |
| | | bSaveResultImage = (bool)oSaveResultImage; |
| | | } |
| | | } |
| | | |
| | | //txtImageQuality |
| | | if (GlobalVar.ControlStates.TryGetValue("txtImageQuality_TextBox", out object oImageQuality)) |
| | | catch (Exception ex) |
| | | { |
| | | if (oImageQuality != null && oImageQuality is string) |
| | | { |
| | | lImageQuality = Convert.ToInt64((string)oImageQuality); |
| | | } |
| | | LogInfo(string.Format("流程[{0}]运行发生了意外,原因是:{1}", ProcessName, ex.Message + $"【{ex.StackTrace}】"), LogInfoType.ERROR); |
| | | |
| | | RunBll.Result = false; |
| | | RunBll.Msg = $"[意外]{ex.Message}"; |
| | | } |
| | | |
| | | //cmbImageType |
| | | if (GlobalVar.ControlStates.TryGetValue("cmbImageType_ComboBox", out object oImageType)) |
| | | finally |
| | | { |
| | | try |
| | | #region 从RunSettingPage和Layout中获取是否保存图片 |
| | | |
| | | string strImageType = "jpeg"; |
| | | bool bSaveRunImage = false; |
| | | bool bSaveResultImage = false; |
| | | long lImageQuality = 100L; |
| | | |
| | | //ckbSaveRunImage |
| | | if (GlobalVar.ControlStates.TryGetValue("ckbSaveRunImage_CheckBox", out object oSaveRunImage)) |
| | | { |
| | | // 动态解析ComboBox数据 |
| | | var json = JsonConvert.SerializeObject(oImageType); |
| | | var comboData = JsonConvert.DeserializeAnonymousType(json, new |
| | | if (oSaveRunImage != null && oSaveRunImage is bool) |
| | | { |
| | | Items = new List<object>(), |
| | | SelectedIndex = 0 |
| | | }); |
| | | |
| | | if (comboData != null && comboData.Items.Count > 0) |
| | | { |
| | | strImageType = comboData.Items[comboData.SelectedIndex].ToString(); |
| | | bSaveRunImage = (bool)oSaveRunImage; |
| | | } |
| | | } |
| | | catch { } |
| | | } |
| | | |
| | | // 生成图片并显示到控件中 |
| | | HObject InputImage = null; |
| | | HObject RecordImage = null; |
| | | |
| | | foreach (var layout in GlobalVar.dicLayout.Values |
| | | .Where(layout => layout.ProcessName == ProcessName) |
| | | .ToList()) |
| | | { |
| | | string title = layout.Title; |
| | | string strImagePath = layout.SaveImageDir; |
| | | if (!AllProcessesPage.dicProcessControls.ContainsKey(title)) |
| | | //ckbSaveResultImage |
| | | if (GlobalVar.ControlStates.TryGetValue("ckbSaveResultImage_CheckBox", out object oSaveResultImage)) |
| | | { |
| | | continue; |
| | | } |
| | | |
| | | RunBll.GetImage(layout, out InputImage, out RecordImage); |
| | | AllProcessesPage.dicProcessControls[title].ShowHoImage(RecordImage); |
| | | |
| | | if (!string.IsNullOrEmpty(layout.SaveImageDir)) |
| | | { |
| | | string fileNameHead = layout.SaveImageHead; |
| | | |
| | | string result = Regex.Replace(fileNameHead, @"\{[^}]+\}", match => |
| | | if (oSaveResultImage != null && oSaveResultImage is bool) |
| | | { |
| | | // 去除{}只保留括号内的内容 |
| | | string content = match.Value; |
| | | content = content.Trim('{', '}'); // 去除首尾的{} |
| | | |
| | | RunBll.GetStringOutput(content, out string str); |
| | | return str; |
| | | }); |
| | | |
| | | string fileName = $"{result}-[{DateTime.Now.ToString("HH.mm.ss.ffff")}]"; |
| | | |
| | | // 使用正则表达式替换所有非法字符 |
| | | string invalidChars = Regex.Escape(new string(Path.GetInvalidFileNameChars())); |
| | | string pattern = $"[{invalidChars}]"; |
| | | fileName = Regex.Replace(fileName, pattern, "-"); |
| | | |
| | | strImagePath = Regex.Replace(strImagePath, @"\{[^}]+\}", match => |
| | | { |
| | | // 去除{}只保留括号内的内容 |
| | | string content = match.Value; |
| | | content = content.Trim('{', '}'); // 去除首尾的{} |
| | | |
| | | RunBll.GetStringOutput(content, out string str); |
| | | return str; |
| | | }); |
| | | |
| | | if (bSaveRunImage) |
| | | { |
| | | // 最后一级目录必须为年月日,会根据时间来删除旧图片 |
| | | string directoryPath = Path.Combine(strImagePath, $"{ProcessName}\\原图\\{RunBll.Result}\\{DateTime.Now.ToString("yyyyMMdd")}\\"); |
| | | LB_SmartVision.Tool.Tool.AddRealImage(InputImage, directoryPath, fileName, strImageType, lImageQuality); |
| | | } |
| | | |
| | | if (bSaveResultImage) |
| | | { |
| | | // 最后一级目录必须为年月日,会根据时间来删除旧图片 |
| | | string directoryPath = Path.Combine(strImagePath, $"{ProcessName}\\截图\\{RunBll.Result}\\{DateTime.Now.ToString("yyyyMMdd")}\\"); |
| | | LB_SmartVision.Tool.Tool.AddRealImage(RecordImage, directoryPath, fileName, "jpg", 50L); |
| | | bSaveResultImage = (bool)oSaveResultImage; |
| | | } |
| | | } |
| | | } |
| | | |
| | | foreach (var csv in GlobalVar.dicCsvSetting.Values |
| | | .Where(csv => csv.ProcessName == ProcessName) |
| | | .ToList()) |
| | | { |
| | | if (RunBll.GetCsv(csv |
| | | , out List<string> DataTitle, out Dictionary<string, object> ResultData)) |
| | | //txtImageQuality |
| | | if (GlobalVar.ControlStates.TryGetValue("txtImageQuality_TextBox", out object oImageQuality)) |
| | | { |
| | | string filePath = Path.Combine(GlobalVar.strPathCsv, $"{ProcessName}.csv"); |
| | | LB_SmartVision.Tool.Tool.SaveData(filePath, DataTitle, ResultData); |
| | | if (oImageQuality != null && oImageQuality is string) |
| | | { |
| | | lImageQuality = Convert.ToInt64((string)oImageQuality); |
| | | } |
| | | } |
| | | |
| | | //cmbImageType |
| | | if (GlobalVar.ControlStates.TryGetValue("cmbImageType_ComboBox", out object oImageType)) |
| | | { |
| | | try |
| | | { |
| | | // 动态解析ComboBox数据 |
| | | var json = JsonConvert.SerializeObject(oImageType); |
| | | var comboData = JsonConvert.DeserializeAnonymousType(json, new |
| | | { |
| | | Items = new List<object>(), |
| | | SelectedIndex = 0 |
| | | }); |
| | | |
| | | if (comboData != null && comboData.Items.Count > 0) |
| | | { |
| | | strImageType = comboData.Items[comboData.SelectedIndex].ToString(); |
| | | } |
| | | } |
| | | catch { } |
| | | } |
| | | |
| | | // 生成图片并显示到控件中 |
| | | HObject InputImage = null; |
| | | HObject RecordImage = null; |
| | | |
| | | foreach (var layout in GlobalVar.dicLayout.Values |
| | | .Where(layout => layout.ProcessName == ProcessName) |
| | | .ToList()) |
| | | { |
| | | string title = layout.Title; |
| | | string strImagePath = layout.SaveImageDir; |
| | | if (!AllProcessesPage.dicProcessControls.ContainsKey(title)) |
| | | { |
| | | continue; |
| | | } |
| | | |
| | | RunBll.GetImage(layout, out InputImage, out RecordImage); |
| | | AllProcessesPage.dicProcessControls[title].ShowHoImage(RecordImage); |
| | | |
| | | if (!string.IsNullOrEmpty(layout.SaveImageDir)) |
| | | { |
| | | string fileNameHead = layout.SaveImageHead; |
| | | |
| | | string result = Regex.Replace(fileNameHead, @"\{[^}]+\}", match => |
| | | { |
| | | // 去除{}只保留括号内的内容 |
| | | string content = match.Value; |
| | | content = content.Trim('{', '}'); // 去除首尾的{} |
| | | |
| | | RunBll.GetStringOutput(content, out string str); |
| | | return str; |
| | | }); |
| | | |
| | | string fileName = $"{result}-[{DateTime.Now.ToString("HH.mm.ss.ffff")}]"; |
| | | |
| | | // 使用正则表达式替换所有非法字符 |
| | | string invalidChars = Regex.Escape(new string(Path.GetInvalidFileNameChars())); |
| | | string pattern = $"[{invalidChars}]"; |
| | | fileName = Regex.Replace(fileName, pattern, "-"); |
| | | |
| | | strImagePath = Regex.Replace(strImagePath, @"\{[^}]+\}", match => |
| | | { |
| | | // 去除{}只保留括号内的内容 |
| | | string content = match.Value; |
| | | content = content.Trim('{', '}'); // 去除首尾的{} |
| | | |
| | | RunBll.GetStringOutput(content, out string str); |
| | | return str; |
| | | }); |
| | | |
| | | if (bSaveRunImage) |
| | | { |
| | | // 最后一级目录必须为年月日,会根据时间来删除旧图片 |
| | | string directoryPath = Path.Combine(strImagePath, $"{ProcessName}\\原图\\{RunBll.Result}\\{DateTime.Now.ToString("yyyyMMdd")}\\"); |
| | | LB_SmartVision.Tool.Tool.AddRealImage(InputImage, directoryPath, fileName, strImageType, lImageQuality); |
| | | } |
| | | |
| | | if (bSaveResultImage) |
| | | { |
| | | // 最后一级目录必须为年月日,会根据时间来删除旧图片 |
| | | string directoryPath = Path.Combine(strImagePath, $"{ProcessName}\\截图\\{RunBll.Result}\\{DateTime.Now.ToString("yyyyMMdd")}\\"); |
| | | LB_SmartVision.Tool.Tool.AddRealImage(RecordImage, directoryPath, fileName, "jpg", 50L); |
| | | } |
| | | } |
| | | } |
| | | |
| | | foreach (var csv in GlobalVar.dicCsvSetting.Values |
| | | .Where(csv => csv.ProcessName == ProcessName) |
| | | .ToList()) |
| | | { |
| | | if (RunBll.GetCsv(csv |
| | | , out List<string> DataTitle, out Dictionary<string, object> ResultData)) |
| | | { |
| | | string filePath = Path.Combine(GlobalVar.strPathCsv, $"{ProcessName}.csv"); |
| | | LB_SmartVision.Tool.Tool.SaveData(filePath, DataTitle, ResultData); |
| | | } |
| | | } |
| | | #endregion |
| | | } |
| | | #endregion |
| | | } |
| | | is3D = false; |
| | | }); |
| | | }); |
| | | }); |
| | | } |
| | | else if (!is2D && !name.Contains("L")) |
| | | { |
| | | Task.Factory.StartNew(() => |
| | | { |
| | | if (!name.Contains("L")) |
| | | { |
| | | is2D = true; |
| | | } |
| | | Parallel.ForEach(matchedItems, item => |
| | | { |
| | | string ProcessName = item.Value["流程名"]; |
| | | LogInfo($"流程[{ProcessName}]开始运行", LogInfoType.INFO); |
| | | |
| | | if (!GlobalVar.dicProcesses.ContainsKey(ProcessName)) |
| | | { |
| | | LogInfo(string.Format("流程[{0}]不存在,请检查流程设置", ProcessName), LogInfoType.ERROR); |
| | | return; |
| | | } |
| | | |
| | | ProcessRunBll RunBll = GlobalVar.dicProcesses[ProcessName]; |
| | | if (RunBll == null || RunBll.bRuning) |
| | | { |
| | | LogInfo(string.Format("流程[{0}]上次未运行完成,触发失败", ProcessName) |
| | | , LogInfoType.ERROR); |
| | | return; |
| | | } |
| | | |
| | | try |
| | | { |
| | | bool result = false; |
| | | string msg = string.Empty; |
| | | int times = Convert.ToInt32(item.Value["重测次数"].ToString()); |
| | | string ConnecResult = item.Value["关联结果"]; |
| | | |
| | | if (times < 0) |
| | | { |
| | | result = RunBll.Run(); |
| | | msg = RunBll.Msg; |
| | | |
| | | if (!(string.IsNullOrEmpty(ConnecResult) || ConnecResult.Trim() == "未关联")) |
| | | { |
| | | if (!result) |
| | | { |
| | | LogInfo(string.Format("流程[{0}]运行结果失败,原因是[{1}]", ProcessName, msg), LogInfoType.NOSHOW); |
| | | } |
| | | RunBll.GetBooleanOutput(ConnecResult, out result); |
| | | RunBll.Result = result; |
| | | } |
| | | |
| | | if (!result) |
| | | { |
| | | LogInfo($"流程[{ProcessName}]被强制运行成功", LogInfoType.WARN); |
| | | RunBll.Result = true; |
| | | result = true; |
| | | } |
| | | } |
| | | else |
| | | { |
| | | while (times >= 0) |
| | | { |
| | | result = RunBll.Run(); |
| | | msg = RunBll.Msg; |
| | | if (!(string.IsNullOrEmpty(ConnecResult) || ConnecResult.Trim() == "未关联")) |
| | | { |
| | | if (!result) |
| | | { |
| | | LogInfo(string.Format("流程[{0}]运行结果失败,原因是[{1}]", ProcessName, msg), LogInfoType.NOSHOW); |
| | | } |
| | | |
| | | RunBll.GetBooleanOutput(ConnecResult, out result); |
| | | RunBll.Result = result; |
| | | } |
| | | |
| | | if (result) |
| | | { |
| | | break; |
| | | } |
| | | else if (!result && times > 0) |
| | | { |
| | | LogInfo(string.Format("流程[{0}]运行失败重新测试,剩余次数[{1}]", ProcessName, times), LogInfoType.WARN); |
| | | } |
| | | |
| | | times--; |
| | | } |
| | | } |
| | | |
| | | string ConnectProcess = item.Value["关联流程"]; |
| | | if (!(ConnectProcess == null || string.IsNullOrEmpty(ConnectProcess) || ConnectProcess.Trim() == "")) |
| | | { |
| | | //用逗号或者分号去间隔关联流程 |
| | | string[] arrConnectProcess; |
| | | if (ConnectProcess.Split(';').Length >= ConnectProcess.Split(',').Length) |
| | | { |
| | | arrConnectProcess = ConnectProcess.Split(';'); |
| | | } |
| | | else |
| | | { |
| | | arrConnectProcess = ConnectProcess.Split(','); |
| | | } |
| | | |
| | | foreach (string strConnectProcess in arrConnectProcess) |
| | | { |
| | | if (GlobalVar.dicProcesses.ContainsKey(strConnectProcess)) |
| | | { |
| | | ProcessRunBll ConnectRunBll = GlobalVar.dicProcesses[strConnectProcess]; |
| | | |
| | | int waitTime = 10; |
| | | DateTime startTime = DateTime.Now; |
| | | while ((DateTime.Now - startTime).TotalSeconds < waitTime |
| | | && (ConnectRunBll.bRuning || !ConnectRunBll.bCompleted)) |
| | | { |
| | | LogInfo(string.Format("关联流程[{0}]未运行完成,剩余等待[{1}]s", strConnectProcess, (waitTime - ((DateTime.Now - startTime).TotalSeconds))) |
| | | , LogInfoType.NOSHOW); |
| | | Thread.Sleep(1000); |
| | | continue; |
| | | } |
| | | |
| | | if (ConnectRunBll.bRuning || !ConnectRunBll.bCompleted) |
| | | { |
| | | GlobalVar.dicProcesses[ProcessName].Msg = string.Format("流程[{0}]未运行完成", ProcessName); |
| | | LogInfo(string.Format("关联流程[{0}]未运行完成", strConnectProcess), LogInfoType.ERROR); |
| | | result = false; |
| | | break; |
| | | } |
| | | else if (!ConnectRunBll.bRuning && ConnectRunBll.bCompleted) |
| | | { |
| | | LogInfo(string.Format("关联流程[{0}]运行完成", strConnectProcess), LogInfoType.INFO); |
| | | } |
| | | |
| | | result &= ConnectRunBll.Result; |
| | | if (!ConnectRunBll.Result) |
| | | { |
| | | LogInfo($"流程[{ProcessName}]的关联流程[{strConnectProcess}]运行失败", LogInfoType.ERROR); |
| | | msg = $"关联流程[{strConnectProcess}]运行失败"; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | LogInfo(result ? $"流程[{ProcessName}]运行成功" : $"流程[{ProcessName}]运行失败,原因是{msg}", result ? LogInfoType.PASS : LogInfoType.ERROR); |
| | | |
| | | string SendComName = result ? item.Value["成功通讯"] : item.Value["失败通讯"]; |
| | | string SendMsg = result ? item.Value["成功字符"] : item.Value["失败字符"]; |
| | | if (GlobalVar.dicCommunicators.ContainsKey(SendComName) && (!string.IsNullOrEmpty(SendMsg) || SendMsg.Trim() != "")) |
| | | { |
| | | GlobalVar.dicCommunicators[SendComName].SendMessage(SendMsg); |
| | | LogInfo(string.Format("发送给[{0}]了消息\"{1}\"", SendComName, SendMsg), LogInfoType.INFO); |
| | | } |
| | | |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | LogInfo(string.Format("流程[{0}]运行发生了意外,原因是:{1}", ProcessName, ex.Message + $"【{ex.StackTrace}】"), LogInfoType.ERROR); |
| | | |
| | | RunBll.Result = false; |
| | | RunBll.Msg = $"[意外]{ex.Message}"; |
| | | } |
| | | finally |
| | | { |
| | | #region 从RunSettingPage和Layout中获取是否保存图片 |
| | | |
| | | string strImageType = "jpeg"; |
| | | bool bSaveRunImage = false; |
| | | bool bSaveResultImage = false; |
| | | long lImageQuality = 100L; |
| | | |
| | | //ckbSaveRunImage |
| | | if (GlobalVar.ControlStates.TryGetValue("ckbSaveRunImage_CheckBox", out object oSaveRunImage)) |
| | | { |
| | | if (oSaveRunImage != null && oSaveRunImage is bool) |
| | | { |
| | | bSaveRunImage = (bool)oSaveRunImage; |
| | | } |
| | | } |
| | | |
| | | //ckbSaveResultImage |
| | | if (GlobalVar.ControlStates.TryGetValue("ckbSaveResultImage_CheckBox", out object oSaveResultImage)) |
| | | { |
| | | if (oSaveResultImage != null && oSaveResultImage is bool) |
| | | { |
| | | bSaveResultImage = (bool)oSaveResultImage; |
| | | } |
| | | } |
| | | |
| | | //txtImageQuality |
| | | if (GlobalVar.ControlStates.TryGetValue("txtImageQuality_TextBox", out object oImageQuality)) |
| | | { |
| | | if (oImageQuality != null && oImageQuality is string) |
| | | { |
| | | lImageQuality = Convert.ToInt64((string)oImageQuality); |
| | | } |
| | | } |
| | | |
| | | //cmbImageType |
| | | if (GlobalVar.ControlStates.TryGetValue("cmbImageType_ComboBox", out object oImageType)) |
| | | { |
| | | try |
| | | { |
| | | // 动态解析ComboBox数据 |
| | | var json = JsonConvert.SerializeObject(oImageType); |
| | | var comboData = JsonConvert.DeserializeAnonymousType(json, new |
| | | { |
| | | Items = new List<object>(), |
| | | SelectedIndex = 0 |
| | | }); |
| | | |
| | | if (comboData != null && comboData.Items.Count > 0) |
| | | { |
| | | strImageType = comboData.Items[comboData.SelectedIndex].ToString(); |
| | | } |
| | | } |
| | | catch { } |
| | | } |
| | | |
| | | // 生成图片并显示到控件中 |
| | | HObject InputImage = null; |
| | | HObject RecordImage = null; |
| | | |
| | | foreach (var layout in GlobalVar.dicLayout.Values |
| | | .Where(layout => layout.ProcessName == ProcessName) |
| | | .ToList()) |
| | | { |
| | | string title = layout.Title; |
| | | string strImagePath = layout.SaveImageDir; |
| | | if (!AllProcessesPage.dicProcessControls.ContainsKey(title)) |
| | | { |
| | | continue; |
| | | } |
| | | |
| | | RunBll.GetImage(layout, out InputImage, out RecordImage); |
| | | AllProcessesPage.dicProcessControls[title].ShowHoImage(RecordImage); |
| | | |
| | | if (!string.IsNullOrEmpty(layout.SaveImageDir)) |
| | | { |
| | | string fileNameHead = layout.SaveImageHead; |
| | | |
| | | string result = Regex.Replace(fileNameHead, @"\{[^}]+\}", match => |
| | | { |
| | | // 去除{}只保留括号内的内容 |
| | | string content = match.Value; |
| | | content = content.Trim('{', '}'); // 去除首尾的{} |
| | | |
| | | RunBll.GetStringOutput(content, out string str); |
| | | return str; |
| | | }); |
| | | |
| | | string fileName = $"{result}-[{DateTime.Now.ToString("HH.mm.ss.ffff")}]"; |
| | | |
| | | // 使用正则表达式替换所有非法字符 |
| | | string invalidChars = Regex.Escape(new string(Path.GetInvalidFileNameChars())); |
| | | string pattern = $"[{invalidChars}]"; |
| | | fileName = Regex.Replace(fileName, pattern, "-"); |
| | | |
| | | strImagePath = Regex.Replace(strImagePath, @"\{[^}]+\}", match => |
| | | { |
| | | // 去除{}只保留括号内的内容 |
| | | string content = match.Value; |
| | | content = content.Trim('{', '}'); // 去除首尾的{} |
| | | |
| | | RunBll.GetStringOutput(content, out string str); |
| | | return str; |
| | | }); |
| | | |
| | | if (bSaveRunImage) |
| | | { |
| | | // 最后一级目录必须为年月日,会根据时间来删除旧图片 |
| | | string directoryPath = Path.Combine(strImagePath, $"{ProcessName}\\原图\\{RunBll.Result}\\{DateTime.Now.ToString("yyyyMMdd")}\\"); |
| | | LB_SmartVision.Tool.Tool.AddRealImage(InputImage, directoryPath, fileName, strImageType, lImageQuality); |
| | | } |
| | | |
| | | if (bSaveResultImage) |
| | | { |
| | | // 最后一级目录必须为年月日,会根据时间来删除旧图片 |
| | | string directoryPath = Path.Combine(strImagePath, $"{ProcessName}\\截图\\{RunBll.Result}\\{DateTime.Now.ToString("yyyyMMdd")}\\"); |
| | | LB_SmartVision.Tool.Tool.AddRealImage(RecordImage, directoryPath, fileName, "jpg", 50L); |
| | | } |
| | | } |
| | | } |
| | | |
| | | foreach (var csv in GlobalVar.dicCsvSetting.Values |
| | | .Where(csv => csv.ProcessName == ProcessName) |
| | | .ToList()) |
| | | { |
| | | if (RunBll.GetCsv(csv |
| | | , out List<string> DataTitle, out Dictionary<string, object> ResultData)) |
| | | { |
| | | string filePath = Path.Combine(GlobalVar.strPathCsv, $"{ProcessName}.csv"); |
| | | LB_SmartVision.Tool.Tool.SaveData(filePath, DataTitle, ResultData); |
| | | } |
| | | } |
| | | #endregion |
| | | } |
| | | is2D = false; |
| | | }); |
| | | }); |
| | | } |
| | | |
| | | //Task.Factory.StartNew(() => |
| | | //{ |