| | |
| | | } |
| | | } |
| | | |
| | | |
| | | var matchedItems = GlobalVar.dicProcessSetting |
| | | .Where(item => |
| | | { |
| | |
| | | |
| | | if (matchedItems.Count <= 0) |
| | | { |
| | | matchedItems = GlobalVar.dicProcessSetting |
| | | .Where(item => |
| | | { |
| | | var value = item.Value; |
| | | var triggerComm = value["触发通讯"]; |
| | | var triggerChar = value["触发字符"]; |
| | | return triggerComm != null && !triggerComm.Equals("无"); |
| | | //return triggerComm != null && triggerComm.Equals(name) && |
| | | // (string.IsNullOrEmpty(triggerChar?.ToString()) || |
| | | // msg.StartsWith(triggerChar.ToString())); |
| | | }) |
| | | .ToList(); // 避免重复字典访问和装箱操作 |
| | | } |
| | | |
| | | |
| | | if (matchedItems.Count <= 0) |
| | | { |
| | | return; |
| | | } |
| | | |
| | |
| | | |
| | | GlobalVar.dicProcesses.Values.AsParallel().ForAll(v => v.bCompleted = false); |
| | | LogInfo(string.Format($"检查到可被触发的流程,清空所有流程运行完成标记位!"), LogInfoType.INFO); |
| | | |
| | | Task.Factory.StartNew(() => |
| | | { |
| | | Parallel.ForEach(matchedItems, item => |
| | |
| | | } |
| | | }); |
| | | }); |
| | | |
| | | //Task.Factory.StartNew(() => |
| | | //{ |
| | | // 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 |
| | | // } |
| | | // }); |
| | | //}); |
| | | } |
| | | |
| | | private void VisionForm_FormClosing(object sender, FormClosingEventArgs e) |