AutoTestViewModel.cs 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711
  1. using AutoMapper;
  2. using BizService;
  3. using Microsoft.Extensions.Logging;
  4. using Model.Dto;
  5. using Model.Entities;
  6. using Newtonsoft.Json;
  7. using PLCTool.Common;
  8. using PLCTool.Events;
  9. using PLCTool.Models;
  10. using Prism.Commands;
  11. using Prism.Events;
  12. using Prism.Mvvm;
  13. using Prism.Services.Dialogs;
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Collections.ObjectModel;
  17. using System.Configuration;
  18. using System.Linq;
  19. using System.Text;
  20. using System.Threading;
  21. using System.Threading.Tasks;
  22. using System.Windows;
  23. using System.Xaml;
  24. using static System.Windows.Forms.VisualStyles.VisualStyleElement.ListView;
  25. namespace PLCTool.ViewModels.BusinessManageViewModel
  26. {
  27. public class AutoTestViewModel : BindableBase, IDialogAware
  28. {
  29. private readonly IDialogService _dialog;
  30. private readonly IEventAggregator _aggregator;
  31. private readonly IOptionConfigService _optionConfigService;
  32. private readonly IBasicPlcTestSchemeService _basicPlcTestSchemeService;
  33. private readonly IBasicPlcTestSchemeDtlService _basicPlcTestSchemeDtlService;
  34. private readonly IBizTestRecordService _iBizTestRecordService;
  35. private readonly IBizTestRecordDtlService _iBizTestRecordDtlService;
  36. private readonly IMapper _mapper;
  37. private readonly ILogger _logger;
  38. private List<OptionConfigDto> _optionConfigs;
  39. private DateTime startTime = DateTime.Now;
  40. private DateTime endTime = DateTime.Now;
  41. private int testStatus = 0; //测试状态
  42. private int testResult = 0; //测试结果
  43. private long schId = 0; //方案ID
  44. private long globalSchDetailId = 0; //测试方案明细ID
  45. private long globalDeviceId = 0; //设备ID
  46. private long selctStartDetailId = 0; //当前选择的第一个方案明细id
  47. private BizTestRecordDtlDto bizTestRecordDtlDto = new BizTestRecordDtlDto(); //测试方案明细表
  48. private const string TestMode = "自动测试";
  49. private int defaultDelayTime = Convert.ToInt32(ConfigurationManager.AppSettings["DelayTime"]);
  50. private int delayTime;
  51. ManualResetEvent m = new ManualResetEvent(true); //实例化阻塞事件
  52. CancellationTokenSource tokensource = new CancellationTokenSource(); //声明令牌
  53. public AutoTestViewModel(IDialogService dialog, IEventAggregator aggregator, IOptionConfigService optionConfigService, IBasicPlcTestSchemeService basicPlcTestSchemeService, IBasicPlcTestSchemeDtlService basicPlcTestSchemeDtlService, IBizTestRecordService iBizTestRecordService, IBizTestRecordDtlService iBizTestRecordDtlService, IMapper mapper, ILogger logger)
  54. {
  55. _dialog = dialog;
  56. _aggregator = aggregator;
  57. _optionConfigService = optionConfigService;
  58. _basicPlcTestSchemeService = basicPlcTestSchemeService;
  59. _basicPlcTestSchemeDtlService = basicPlcTestSchemeDtlService;
  60. _iBizTestRecordDtlService = iBizTestRecordDtlService;
  61. _iBizTestRecordService = iBizTestRecordService;
  62. _mapper = mapper;
  63. _logger = logger;
  64. CloseCommand = new DelegateCommand(Close);
  65. StartCommand = new DelegateCommand(Start);
  66. StopOrContinueCommand = new DelegateCommand<object>(StopOrContinueMethod);
  67. CancelCommand = new DelegateCommand<object>(Cancel);
  68. UnLoadCommand = new DelegateCommand(Unload);
  69. ItemChangeCommand = new DelegateCommand(ItemChange);
  70. BeforeConList = new ObservableCollection<BasPlcItemConfigDto>();
  71. InConList = new ObservableCollection<BasPlcItemConfigDto>();
  72. OutConList = new ObservableCollection<BasPlcItemConfigDto>();
  73. GetConfigOption();
  74. ReadPreTask();//读取前置项线程
  75. m2.Set();
  76. }
  77. /// <summary>
  78. /// 关闭窗体是触发
  79. /// </summary>
  80. /// <exception cref="NotImplementedException"></exception>
  81. private void Unload()
  82. {
  83. m.Reset(); //阻塞线程
  84. tokensource.Cancel(); //取消自动检测线程
  85. tokensource2.Cancel(); //取消读取前置项的线程
  86. RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel));
  87. }
  88. private void Cancel(object obj)
  89. {
  90. tokensource.Cancel();
  91. }
  92. private void StopOrContinueMethod(object obj)
  93. {
  94. if(StopOrContinueCont=="暂停")
  95. {
  96. m.Reset(); //阻塞线程
  97. StopOrContinueCont = "继续";
  98. }
  99. else
  100. {
  101. if (StopOrContinueCont == "继续")
  102. {
  103. m.Set(); //继续线程
  104. StopOrContinueCont = "暂停";
  105. }
  106. else
  107. {
  108. if (StopOrContinueCont == "测试完成")
  109. {
  110. RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
  111. }
  112. }
  113. }
  114. }
  115. /// <summary>
  116. /// 发送log之界面显示
  117. /// </summary>
  118. /// <param name="msg"></param>
  119. private void SendLogToDis(string msg)
  120. {
  121. _aggregator.GetEvent<LogEvent>().Publish(new LogMessage
  122. {
  123. LogTime = DateTime.Now,
  124. LogMsg = msg
  125. });
  126. }
  127. /// <summary>
  128. /// 选中测试项
  129. /// </summary>
  130. private void ItemChange()
  131. {
  132. var findDtlMsg = _basicPlcTestSchemeDtlService.FindAllBySchId(schId)?.FirstOrDefault(X => X.item_name == SelectItem.ItemName);
  133. if (findDtlMsg != null)
  134. {
  135. int id = Convert.ToInt32(findDtlMsg.scheme_dtl_id);
  136. // 根据 测试方案明细主键ID 查找测试方案id 及方案名和设备名
  137. globalSchDetailId = id;
  138. selctStartDetailId = id;
  139. GetDetailInfo(id);
  140. m2.Set(); //继续线程(读取前置项)
  141. }
  142. }
  143. #region idialog接口实现
  144. public string Title { set; get; } = "自动测试";
  145. public event Action<IDialogResult> RequestClose;
  146. public bool CanCloseDialog()
  147. {
  148. return true;
  149. }
  150. public void OnDialogClosed()
  151. {
  152. }
  153. public void OnDialogOpened(IDialogParameters parameters)
  154. {
  155. //编辑
  156. var getMsg = parameters.GetValues<long>("Key");
  157. ///值不为空,表示修改
  158. if (getMsg != null)
  159. {
  160. foreach (var item in getMsg)
  161. {
  162. //测试方案号
  163. schId = item;
  164. var schDtls = _basicPlcTestSchemeDtlService.FindAllBySchId(schId).OrderBy(x => x.sort).ThenBy(x => x.scheme_dtl_id).ToList(); ;
  165. foreach (var sch in schDtls)
  166. {
  167. long dtlId = sch.scheme_dtl_id;
  168. SchItems.Add(new ItemAndBackColorModel()
  169. {
  170. ItemName = sch.item_name,
  171. BackResultValue = TestResult.NoTest,
  172. });
  173. }
  174. //默认显示第一个测试项的信息
  175. if (schDtls.Count != 0)
  176. {
  177. int id = Convert.ToInt32(schDtls[0].scheme_dtl_id);
  178. selctStartDetailId = id;
  179. // 根据 测试方案明细主键ID 查找测试方案id 及方案名和设备名
  180. GetDetailInfo(id);
  181. SelectItemIndex = 0;
  182. }
  183. //globalSchDetailId = item; //测试方案明细ID
  184. //int id = Convert.ToInt32(item);
  185. //// 根据 测试方案明细主键ID 查找测试方案id 及方案名和设备名
  186. //GetDetailInfo(id);
  187. }
  188. }
  189. //获取设备id
  190. var getDes = parameters.GetValues<long>("Key2");
  191. if (getDes != null)
  192. {
  193. foreach (var item in getDes)
  194. {
  195. globalDeviceId = item;
  196. }
  197. }
  198. }
  199. /// <summary>
  200. /// //根据 测试方案明细主键ID 查找测试方案id 及方案名和设备名
  201. /// </summary>
  202. /// <param name="id"></param>
  203. private void GetDetailInfo(int schDetailId)
  204. {
  205. //根据 测试方案明细主键ID 查找测试方案id 及方案名和设备名
  206. var findEntity = _basicPlcTestSchemeDtlService.Find(schDetailId);
  207. var findresult = _mapper.Map<bas_plc_test_scheme_dtl, BasicPlcTestSchemeDtlDto>(findEntity);
  208. if (findresult != null)
  209. {
  210. //根据方案id 在方案表中查找方案名 设备名
  211. schId = findresult.SchemeId.Value; //方案id,方便下一步查找
  212. int intSchId = Convert.ToInt32(findresult.SchemeId);
  213. var findSch = _basicPlcTestSchemeService.Find(intSchId);
  214. if (findSch != null)
  215. {
  216. ScheduleName = findSch.scheme_name;
  217. DeviceKindName = findSch.devicekind_name;
  218. }
  219. TestName = findresult.ItemName;
  220. SelectTest = findresult.ItemType;
  221. //测试时间
  222. if (findresult.ItemTime == 0)
  223. {
  224. delayTime = defaultDelayTime;
  225. }
  226. else
  227. {
  228. delayTime = findresult.ItemTime;
  229. }
  230. //前置项解析
  231. string preconStr = findresult.Precondition.ToString();
  232. if (!string.IsNullOrEmpty(preconStr))
  233. {
  234. JsonModel preconditionModel = JsonConvert.DeserializeObject<JsonModel>(preconStr);
  235. BeforeSelectJudge = preconditionModel.ItemType;
  236. SelectLogic = preconditionModel.ItemLogical;
  237. BeforeDetail = preconditionModel.Description;
  238. BeforeConList.Clear();
  239. foreach (var detail in preconditionModel.DetailInfo)
  240. {
  241. BasPlcItemConfigDto basPlcItemConfigDto = new BasPlcItemConfigDto();
  242. basPlcItemConfigDto.Id = detail.Id;
  243. basPlcItemConfigDto.PlcItem = detail.PlcItem;
  244. basPlcItemConfigDto.PlcAddress = detail.PlcAddress;
  245. basPlcItemConfigDto.PlcAddType = detail.PlcAddType;
  246. basPlcItemConfigDto.PlcValue = detail.PlcValue;
  247. basPlcItemConfigDto.Remark = detail.Remark;
  248. BeforeConList.Add(basPlcItemConfigDto);
  249. }
  250. }
  251. //输入项解析
  252. string inStr = findresult.Action.ToString();
  253. if (!string.IsNullOrEmpty(inStr))
  254. {
  255. JsonModel inModel = JsonConvert.DeserializeObject<JsonModel>(inStr);
  256. SelectInJudge = inModel.ItemType;
  257. InSelectLogic = inModel.ItemLogical;
  258. InDetail = inModel.Description;
  259. InConList.Clear();
  260. foreach (var detail in inModel.DetailInfo)
  261. {
  262. BasPlcItemConfigDto basPlcItemConfigDto = new BasPlcItemConfigDto();
  263. basPlcItemConfigDto.Id = detail.Id;
  264. basPlcItemConfigDto.PlcItem = detail.PlcItem;
  265. basPlcItemConfigDto.PlcAddress = detail.PlcAddress;
  266. basPlcItemConfigDto.PlcAddType = detail.PlcAddType;
  267. basPlcItemConfigDto.PlcValue = detail.PlcValue;
  268. basPlcItemConfigDto.Remark = detail.Remark;
  269. InConList.Add(basPlcItemConfigDto);
  270. }
  271. }
  272. //输出项解析
  273. string outStr = findresult.JudgementResult.ToString();
  274. if (!string.IsNullOrEmpty(outStr))
  275. {
  276. JsonModel outModel = JsonConvert.DeserializeObject<JsonModel>(outStr);
  277. SelectOutJudge = outModel.ItemType;
  278. OutSelectLogic = outModel.ItemLogical;
  279. OutDetail = outModel.Description;
  280. OutConList.Clear();
  281. foreach (var detail in outModel.DetailInfo)
  282. {
  283. BasPlcItemConfigDto basPlcItemConfigDto = new BasPlcItemConfigDto();
  284. basPlcItemConfigDto.Id = detail.Id;
  285. basPlcItemConfigDto.PlcItem = detail.PlcItem;
  286. basPlcItemConfigDto.PlcAddress = detail.PlcAddress;
  287. basPlcItemConfigDto.PlcAddType = detail.PlcAddType;
  288. basPlcItemConfigDto.PlcValue = detail.PlcValue;
  289. basPlcItemConfigDto.Remark = detail.Remark;
  290. OutConList.Add(basPlcItemConfigDto);
  291. }
  292. }
  293. }
  294. }
  295. #endregion
  296. #region 私有发方法
  297. CancellationTokenSource tokensource2 = new CancellationTokenSource(); //声明令牌
  298. ManualResetEvent m2 = new ManualResetEvent(true); //实例化阻塞事件
  299. private void ReadPreTask()
  300. {
  301. CancellationToken cancellationToken2 = tokensource2.Token;
  302. Task.Run(async () =>
  303. {
  304. while (true)
  305. {
  306. if (cancellationToken2.IsCancellationRequested)
  307. {
  308. return;
  309. }
  310. m2.WaitOne(); //当m等于true,才会往下执行,否则一直在此等待
  311. System.Windows.Application.Current.Dispatcher.Invoke((delegate
  312. {
  313. foreach (var item in BeforeConList.OrderBy(x => x.Id))
  314. {
  315. string plcAddress = item.PlcAddress;
  316. string plcAddType = item.PlcAddType;
  317. string plcValue = item.PlcValue;
  318. switch (plcAddType)
  319. {
  320. case "bool":
  321. string readResult = PLCConnectionFactory.CreatePLCCont().ReadPlcObject(plcAddress, VarType.Bit);
  322. ///实时值记录
  323. item.RealValue = readResult;
  324. item.TestTime = DateTime.Now;
  325. break;
  326. case "word":
  327. string readResult2 = PLCConnectionFactory.CreatePLCCont().ReadPlcObject(plcAddress, VarType.Word);
  328. ///实时值记录
  329. item.RealValue = readResult2;
  330. item.TestTime = DateTime.Now;
  331. break;
  332. }
  333. }
  334. }));
  335. await Task.Delay(2000);
  336. }
  337. }, cancellationToken2);//绑定令牌到多线程;
  338. }
  339. /// <summary>
  340. /// 增加或更新测试记录
  341. /// </summary>
  342. private void AddTestRecord()
  343. {
  344. //记录记录主表中是否有这个方案的记录,没有则添加
  345. var findRecord = _iBizTestRecordService.FindRecorddByDeviceIdAndSchname(globalDeviceId,ScheduleName);
  346. if (findRecord == null)
  347. {
  348. //状态status 没有赋值
  349. BizTestRecordDto bizTestRecordDto = new BizTestRecordDto();
  350. bizTestRecordDto.RecordName = ScheduleName + "_Record";
  351. bizTestRecordDto.SchemeName = ScheduleName;
  352. bizTestRecordDto.DeviceId = globalDeviceId;
  353. bizTestRecordDto.Tester = Appsession.UserName;
  354. bizTestRecordDto.StartTestTime = startTime;
  355. bizTestRecordDto.FinishTestTime = endTime;
  356. bizTestRecordDto.CreateBy = Appsession.UserName;
  357. bizTestRecordDto.CreateTime = DateTime.Now;
  358. bizTestRecordDto.UpdateBy = Appsession.UserName;
  359. bizTestRecordDto.UpdateTime = DateTime.Now;
  360. var testRecord = _mapper.Map<BizTestRecordDto, biz_test_record>(bizTestRecordDto);
  361. _iBizTestRecordService.Add(testRecord);
  362. }
  363. }
  364. /// <summary>
  365. /// 增加测试记录明细
  366. /// </summary>
  367. /// <summary>
  368. /// 获取记录明细
  369. /// </summary>
  370. private void AddDtltRecord(long schDetailId)
  371. {
  372. //查找record ID
  373. long recordId = 0;
  374. var findRecordID = _iBizTestRecordService.FindRecorddByDeviceIdAndSchname(globalDeviceId, ScheduleName);
  375. if (findRecordID != null)
  376. {
  377. recordId = findRecordID.record_id;
  378. }
  379. testStatus = 99;//测试状态赋值为99
  380. //状态status 没有赋值
  381. DateTime finishTime = DateTime.Now;
  382. bizTestRecordDtlDto.RecordId = recordId;
  383. bizTestRecordDtlDto.SchemeDtlId = schDetailId;
  384. bizTestRecordDtlDto.StartTestTime = startTime;
  385. bizTestRecordDtlDto.FinishTestTime = endTime;
  386. bizTestRecordDtlDto.TestMode = TestMode;
  387. bizTestRecordDtlDto.Status = testStatus;
  388. bizTestRecordDtlDto.TestResult = GetTestResult();
  389. bizTestRecordDtlDto.CreateBy = Appsession.UserName;
  390. bizTestRecordDtlDto.CreateTime = startTime;
  391. bizTestRecordDtlDto.UpdateBy = Appsession.UserName;
  392. bizTestRecordDtlDto.UpdateTime = finishTime;
  393. var testRecordDtl = _mapper.Map<BizTestRecordDtlDto, biz_test_record_dtl>(bizTestRecordDtlDto);
  394. _iBizTestRecordDtlService.Add(testRecordDtl);
  395. //跟新测试记录的_iBizTestRecord的完成时间
  396. var findRecord = _iBizTestRecordService.Find((int)recordId);
  397. if (findRecord != null)
  398. {
  399. findRecord.finish_test_time = finishTime;
  400. _iBizTestRecordService.Edit(findRecord);
  401. }
  402. }
  403. /// <summary>
  404. /// 前置项结果判定
  405. /// </summary>
  406. /// <returns></returns>
  407. private int GetPrecedtionResult()
  408. {
  409. //前置项
  410. int beforeResult = 0;
  411. if (BeforeConList.Count == 0)
  412. {
  413. beforeResult = 1;
  414. }
  415. else
  416. {
  417. if (SelectLogic == "OR")
  418. {
  419. var find = BeforeConList.FirstOrDefault(x => x.TestResult == "通过");
  420. if (find != null)
  421. {
  422. beforeResult = 1;
  423. }
  424. else
  425. {
  426. beforeResult = 0;
  427. }
  428. }
  429. else
  430. {
  431. if (SelectLogic == "AND" || SelectLogic == "ONLY")
  432. {
  433. var find = BeforeConList.FirstOrDefault(x => x.TestResult == "不通过");
  434. if (find != null)
  435. {
  436. beforeResult = 0;
  437. }
  438. else
  439. {
  440. beforeResult = 1;
  441. }
  442. }
  443. else
  444. {
  445. if (SelectLogic == "NULL")
  446. {
  447. beforeResult = 1;
  448. }
  449. }
  450. }
  451. }
  452. return beforeResult;
  453. }
  454. /// <summary>
  455. /// 获取测试结果
  456. /// </summary>
  457. /// <returns></returns>
  458. private int GetTestResult()
  459. {
  460. //前置项
  461. int beforeResult = 0;
  462. int inResult = 0;
  463. int outResult = 0;
  464. if (BeforeConList.Count == 0)
  465. {
  466. beforeResult = 1;
  467. }
  468. else
  469. {
  470. if (SelectLogic == "OR")
  471. {
  472. var find = BeforeConList.FirstOrDefault(x => x.TestResult == "通过");
  473. if (find != null)
  474. {
  475. beforeResult = 1;
  476. }
  477. else
  478. {
  479. beforeResult = 0;
  480. }
  481. }
  482. else
  483. {
  484. if (SelectLogic == "AND" || SelectLogic == "ONLY")
  485. {
  486. var find = BeforeConList.FirstOrDefault(x => x.TestResult == "不通过");
  487. if (find != null)
  488. {
  489. beforeResult = 0;
  490. }
  491. else
  492. {
  493. beforeResult = 1;
  494. }
  495. }
  496. else
  497. {
  498. if (SelectLogic == "NULL")
  499. {
  500. beforeResult = 1;
  501. }
  502. }
  503. }
  504. }
  505. //输入项
  506. if (InConList.Count == 0)
  507. {
  508. inResult = 1;
  509. }
  510. else
  511. {
  512. if (InSelectLogic == "OR")
  513. {
  514. var find = InConList.FirstOrDefault(x => x.TestResult == "通过");
  515. if (find != null)
  516. {
  517. inResult = 1;
  518. }
  519. else
  520. {
  521. inResult = 0;
  522. }
  523. }
  524. else
  525. {
  526. if (InSelectLogic == "AND" || InSelectLogic == "ONLY")
  527. {
  528. var find = InConList.FirstOrDefault(x => x.TestResult == "不通过");
  529. if (find != null)
  530. {
  531. inResult = 0;
  532. }
  533. else
  534. {
  535. inResult = 1;
  536. }
  537. }
  538. else
  539. {
  540. if (InSelectLogic == "NULL")
  541. {
  542. inResult = 1;
  543. }
  544. }
  545. }
  546. }
  547. //输出项
  548. if (OutConList.Count == 0)
  549. {
  550. outResult = 1;
  551. }
  552. else
  553. {
  554. if (OutSelectLogic == "OR")
  555. {
  556. var find = OutConList.FirstOrDefault(x => x.TestResult == "通过");
  557. if (find != null)
  558. {
  559. outResult = 1;
  560. }
  561. else
  562. {
  563. outResult = 0;
  564. }
  565. }
  566. else
  567. {
  568. if (OutSelectLogic == "AND" || OutSelectLogic == "ONLY")
  569. {
  570. var find = OutConList.FirstOrDefault(x => x.TestResult == "不通过");
  571. if (find != null)
  572. {
  573. outResult = 0;
  574. }
  575. else
  576. {
  577. outResult = 1;
  578. }
  579. }
  580. else
  581. {
  582. if (OutSelectLogic == "NULL")
  583. {
  584. outResult = 1;
  585. }
  586. }
  587. }
  588. }
  589. if (beforeResult == 1 && inResult == 1 && outResult == 1)
  590. {
  591. return 1;
  592. }
  593. else
  594. {
  595. return 0;
  596. }
  597. }
  598. /// <summary>
  599. /// 更新测试记录
  600. /// </summary>
  601. private void UpdateTesDtltRecord()
  602. {
  603. var testRecordDtl = _mapper.Map<BizTestRecordDtlDto, biz_test_record_dtl>(bizTestRecordDtlDto);
  604. _iBizTestRecordDtlService.Edit(testRecordDtl);
  605. }
  606. /// <summary>
  607. /// 开始检测
  608. /// </summary>
  609. /// <param name="obj"></param>
  610. private void Start()
  611. {
  612. MessageBoxResult boxResult = MessageBox.Show("确认开始测试?", "确认", MessageBoxButton.OKCancel, MessageBoxImage.Question);
  613. if (boxResult != MessageBoxResult.OK)
  614. {
  615. return;
  616. }
  617. m2.Reset(); //阻塞线程(不再读取前置项)
  618. CancellationToken cancellationToken = tokensource.Token;
  619. StartEnalbe = false;
  620. Task.Run(async() => //模拟耗时任务
  621. {
  622. var basicSchDtls = _basicPlcTestSchemeDtlService.FindAllBySchId(schId)?.OrderBy(x => x.sort).ThenBy(x => x.scheme_dtl_id);
  623. foreach (var item in basicSchDtls)
  624. {
  625. if (cancellationToken.IsCancellationRequested)
  626. {
  627. return;
  628. }
  629. m.WaitOne(); //当m等于true,才会往下执行,否则一直在此等待
  630. long schDetailId = item.scheme_dtl_id;
  631. ////编号小于当前选择的就不测试了,
  632. if (schDetailId < selctStartDetailId)
  633. {
  634. continue;
  635. }
  636. globalSchDetailId = schDetailId;
  637. //当前选择的项不在更新显示,更新后面的显示
  638. if (selctStartDetailId!=schDetailId)
  639. {
  640. //更新下一个界面显示
  641. int id = Convert.ToInt32(globalSchDetailId);
  642. //界面显示
  643. System.Windows.Application.Current.Dispatcher.Invoke((delegate
  644. {
  645. GetDetailInfo(id);
  646. }));
  647. }
  648. _logger.LogInformation($"开始自动测试。方案名{ScheduleName},测试项{TestName}");
  649. SendLogToDis($"开始自动测试。方案名{ScheduleName},测试项{TestName}");
  650. //测试
  651. start: // 标记位置
  652. bool isExec=await ExecTest();
  653. //条件不满足,直接退出
  654. if(!isExec)
  655. {
  656. MessageBox.Show("前置项条件不满足,请确认前置条件!", "确认", MessageBoxButton.OK, MessageBoxImage.Information);
  657. //System.Windows.Application.Current.Dispatcher.Invoke((delegate
  658. //{
  659. // StopOrContinueCont = "继续";
  660. //}));
  661. //m.Reset(); //阻塞线程
  662. //m.WaitOne(); //当m等于true,才会往下执行,否则一直在此等待
  663. //goto start;
  664. }
  665. //条件满足
  666. SendLogToDis($"自动测试完成。方案名{ScheduleName},测试项{TestName}");
  667. _logger.LogInformation($"自动测试完成。方案名{ScheduleName},测试项{TestName}");
  668. await Task.Delay(2000);
  669. System.Windows.Application.Current.Dispatcher.Invoke((delegate
  670. {
  671. //listbox 的index+1
  672. SelectItemIndex++;
  673. }));
  674. }
  675. System.Windows.Application.Current.Dispatcher.Invoke((delegate
  676. {
  677. StopOrContinueCont = "测试完成";
  678. }));
  679. }, cancellationToken);//绑定令牌到多线程
  680. ///查找方案下的所有方案明细,并排序,
  681. }
  682. private async Task<bool> ExecTest()
  683. {
  684. bool isSucc = true;
  685. //开始时间
  686. startTime = DateTime.Now;
  687. //增加测试记录主表
  688. AddTestRecord();
  689. //前置项
  690. bool preBool = TestPreceditionMethod();
  691. //前置项结果判断
  692. int preceditionResult = GetPrecedtionResult();
  693. if (preceditionResult == 0)
  694. {
  695. return false;
  696. }
  697. //输入项
  698. bool isAction = await TestActionMethod();
  699. //输出项
  700. bool isJudgeResult = await TestJudgementMethod();
  701. //增加测试记录
  702. AddDtltRecord(globalSchDetailId);
  703. //更新左侧菜单颜色
  704. if (GetTestResult() == 1)
  705. {
  706. System.Windows.Application.Current.Dispatcher.Invoke((delegate
  707. {
  708. var findItem = SchItems.FirstOrDefault(x => x.ItemName == SelectItem.ItemName);
  709. if (findItem != null)
  710. {
  711. findItem.BackResultValue = TestResult.Pass;
  712. }
  713. }));
  714. }
  715. else
  716. {
  717. System.Windows.Application.Current.Dispatcher.Invoke((delegate
  718. {
  719. var findItem = SchItems.FirstOrDefault(x => x.ItemName == SelectItem.ItemName);
  720. if (findItem != null)
  721. {
  722. findItem.BackResultValue = TestResult.NoPass;
  723. }
  724. }));
  725. }
  726. //下一项
  727. StepIndex = 0;
  728. return isSucc;
  729. }
  730. /// <summary>
  731. /// 更新测试结果
  732. /// </summary>
  733. private void UpdateTestRecord()
  734. {
  735. }
  736. /// <summary>
  737. /// 测试前置项目
  738. /// </summary>
  739. private bool TestPreceditionMethod()
  740. {
  741. switch (BeforeSelectJudge)
  742. {
  743. case "人工判定":
  744. //弹出确认的对话框
  745. //前置项测试结果json转化
  746. string prefixJsonStr = ModelToJsonToStr(BeforeSelectJudge, SelectLogic, BeforeDetail, BeforeConList);
  747. //json字符串
  748. bizTestRecordDtlDto.PreconditionFinal = prefixJsonStr;
  749. bizTestRecordDtlDto.PreconditionResult = "通过";
  750. StepIndex = 1;
  751. //MessageBoxResult boxResult = MessageBox.Show(BeforeDetail, "确认", MessageBoxButton.OKCancel, MessageBoxImage.Information);
  752. //if (boxResult == MessageBoxResult.OK)
  753. //{
  754. // //前置项测试结果json转化
  755. // string prefixJsonStr = ModelToJsonToStr(BeforeSelectJudge, SelectLogic, BeforeDetail, BeforeConList);
  756. // //json字符串
  757. // bizTestRecordDtlDto.PreconditionFinal = prefixJsonStr;
  758. // bizTestRecordDtlDto.PreconditionResult = "通过";
  759. // StepIndex = 1;
  760. //}
  761. //else
  762. //{
  763. // return false;
  764. //}
  765. break;
  766. case "自动判定":
  767. //读取plc的值
  768. switch (SelectLogic)
  769. {
  770. case "NULL":
  771. //弹出确认的对话框
  772. //前置项测试结果json转化
  773. string prefixJsonStrAuto = ModelToJsonToStr(BeforeSelectJudge, SelectLogic, BeforeDetail, BeforeConList);
  774. //json字符串
  775. bizTestRecordDtlDto.PreconditionFinal = prefixJsonStrAuto;
  776. bizTestRecordDtlDto.PreconditionResult = "通过";
  777. StepIndex = 1;
  778. //MessageBoxResult boxResultAuto = MessageBox.Show(BeforeDetail, "确认", MessageBoxButton.OKCancel, MessageBoxImage.Information);
  779. //if (boxResultAuto == MessageBoxResult.OK)
  780. //{
  781. // //前置项测试结果json转化
  782. // string prefixJsonStr = ModelToJsonToStr(BeforeSelectJudge, SelectLogic, BeforeDetail, BeforeConList);
  783. // //json字符串
  784. // bizTestRecordDtlDto.PreconditionFinal = prefixJsonStr;
  785. // bizTestRecordDtlDto.PreconditionResult = "通过";
  786. // StepIndex = 1;
  787. //}
  788. //else
  789. //{
  790. // return false;
  791. //}
  792. break;
  793. case "AND":
  794. case "ONLY":
  795. case "OR":
  796. ReadPredice(SelectLogic);
  797. break;
  798. case "ORDER":
  799. break;
  800. }
  801. break;
  802. }
  803. return true;
  804. }
  805. private void ReadPredice(string strLogic)
  806. {
  807. int countCond = 0;
  808. foreach (var item in BeforeConList.OrderBy(x => x.Id))
  809. {
  810. string plcAddress = item.PlcAddress;
  811. string plcAddType = item.PlcAddType;
  812. string plcValue = item.PlcValue;
  813. switch (plcAddType)
  814. {
  815. case "bool":
  816. string readResult = PLCConnectionFactory.CreatePLCCont().ReadPlcObject(plcAddress, VarType.Bit);
  817. ///实时值记录
  818. item.RealValue = readResult;
  819. item.TestTime = DateTime.Now;
  820. if (readResult.Trim() == plcValue?.Trim())
  821. {
  822. item.TestResult = "通过";
  823. countCond++;
  824. }
  825. else
  826. {
  827. item.TestResult = "不通过";
  828. }
  829. break;
  830. case "word":
  831. string readResult2 = PLCConnectionFactory.CreatePLCCont().ReadPlcObject(plcAddress, VarType.Word);
  832. ///实时值记录
  833. item.RealValue = readResult2;
  834. item.TestTime = DateTime.Now;
  835. if (readResult2.Trim() == plcValue?.Trim())
  836. {
  837. item.TestResult = "通过";
  838. countCond++;
  839. }
  840. else
  841. {
  842. item.TestResult = "不通过";
  843. }
  844. break;
  845. }
  846. }
  847. //测试记录存入数据库
  848. //前置项测试结果json转化
  849. string prefixJsonStr = ModelToJsonToStr(BeforeSelectJudge, SelectLogic, BeforeDetail, BeforeConList);
  850. //json字符串
  851. bizTestRecordDtlDto.PreconditionFinal = prefixJsonStr;
  852. //条件满足
  853. switch (strLogic)
  854. {
  855. case "AND":
  856. case "ONLY":
  857. if ((countCond == BeforeConList.Count) && (countCond != 0))
  858. {
  859. bizTestRecordDtlDto.PreconditionResult = "通过";
  860. }
  861. else
  862. {
  863. bizTestRecordDtlDto.PreconditionResult = "不通过";
  864. }
  865. break;
  866. case "OR":
  867. if ((countCond != 0))
  868. {
  869. bizTestRecordDtlDto.PreconditionResult = "通过";
  870. }
  871. else
  872. {
  873. bizTestRecordDtlDto.PreconditionResult = "不通过";
  874. }
  875. break;
  876. }
  877. StepIndex = 1;
  878. }
  879. /// <summary>
  880. /// 输入项写入
  881. /// </summary>
  882. private async Task<bool> TestActionMethod()
  883. {
  884. bool isActionResult = true;
  885. switch (SelectInJudge)
  886. {
  887. case "人工判定":
  888. //弹出确认的对话框
  889. //MessageBoxResult boxResult = MessageBox.Show(InDetail, "确认", MessageBoxButton.OKCancel, MessageBoxImage.Information);
  890. //if (boxResult == MessageBoxResult.OK)
  891. {
  892. //前置项测试结果json转化
  893. string actionJsonStr = ModelToJsonToStr(SelectInJudge, InSelectLogic, InDetail, InConList);
  894. //json字符串
  895. bizTestRecordDtlDto.ActionFinal = actionJsonStr;
  896. bizTestRecordDtlDto.ActionResult = "通过";
  897. StepIndex = 2;
  898. }
  899. break;
  900. case "自动判定":
  901. //读取plc的值
  902. switch (InSelectLogic)
  903. {
  904. case "NULL":
  905. //弹出确认的对话框
  906. //MessageBoxResult boxResultAction = MessageBox.Show(InDetail, "确认", MessageBoxButton.OKCancel, MessageBoxImage.Information);
  907. //if (boxResultAction == MessageBoxResult.OK)
  908. {
  909. //前置项测试结果json转化
  910. string actionJsonStr = ModelToJsonToStr(SelectInJudge, InSelectLogic, InDetail, InConList);
  911. //json字符串
  912. bizTestRecordDtlDto.ActionFinal = actionJsonStr;
  913. bizTestRecordDtlDto.ActionResult = "通过";
  914. StepIndex = 2;
  915. }
  916. break;
  917. case "AND":
  918. case "ONLY":
  919. case "OR":
  920. isActionResult =await SendActionToPLC(InSelectLogic);
  921. break;
  922. case "ORDER":
  923. break;
  924. }
  925. break;
  926. }
  927. return isActionResult;
  928. }
  929. /// <summary>
  930. /// 发送命令给PLC
  931. /// </summary>
  932. private async Task<bool> SendActionToPLC(string strActionLogic)
  933. {
  934. bool isAction = true;
  935. int countCond = 0;
  936. foreach (var item in InConList.OrderBy(x => x.Id))
  937. {
  938. string plcAddress = item.PlcAddress;
  939. string plcAddType = item.PlcAddType;
  940. string plcValue = item.PlcValue;
  941. switch (plcAddType)
  942. {
  943. case "bool":
  944. bool writeResult = PLCConnectionFactory.CreatePLCCont().WritePlcObject(plcAddress, VarType.Bit, plcValue);
  945. ///实时值记录
  946. System.Windows.Application.Current.Dispatcher.Invoke((delegate
  947. {
  948. ///实时值记录
  949. item.RealValue = plcValue;
  950. item.TestTime = DateTime.Now;
  951. }));
  952. if (writeResult)
  953. {
  954. ///单个测试项通过
  955. item.TestResult = "通过";
  956. countCond++;
  957. }
  958. else
  959. {
  960. item.TestResult = "不通过";
  961. }
  962. break;
  963. case "word":
  964. bool writeWrordResult = PLCConnectionFactory.CreatePLCCont().WritePlcObject(plcAddress, VarType.Word, plcValue);
  965. ///实时值记录
  966. ///
  967. System.Windows.Application.Current.Dispatcher.Invoke((delegate
  968. {
  969. ///实时值记录
  970. item.RealValue = plcValue;
  971. item.TestTime = DateTime.Now;
  972. }));
  973. //item.RealValue = plcValue;
  974. //item.TestTime = DateTime.Now;
  975. if (writeWrordResult)
  976. {
  977. ///单个测试项通过
  978. item.TestResult = "通过";
  979. countCond++;
  980. }
  981. else
  982. {
  983. item.TestResult = "不通过";
  984. }
  985. break;
  986. }
  987. await Task.Delay(500);
  988. }
  989. if (!isAction)
  990. {
  991. return isAction;
  992. }
  993. //测试记录存入数据库
  994. //前置项测试结果json转化
  995. string actionJsonStr = ModelToJsonToStr(SelectInJudge, InSelectLogic, InDetail, InConList);
  996. //json字符串
  997. bizTestRecordDtlDto.ActionFinal = actionJsonStr;
  998. //条件满足
  999. //条件满足
  1000. switch (strActionLogic)
  1001. {
  1002. case "AND":
  1003. case "ONLY":
  1004. if ((countCond == BeforeConList.Count) && (countCond != 0))
  1005. {
  1006. bizTestRecordDtlDto.ActionResult = "通过";
  1007. }
  1008. else
  1009. {
  1010. bizTestRecordDtlDto.ActionResult = "不通过";
  1011. }
  1012. break;
  1013. case "OR":
  1014. if ((countCond != 0))
  1015. {
  1016. bizTestRecordDtlDto.ActionResult = "通过";
  1017. }
  1018. else
  1019. {
  1020. bizTestRecordDtlDto.ActionResult = "不通过";
  1021. }
  1022. break;
  1023. }
  1024. StepIndex = 2;
  1025. return isAction;
  1026. }
  1027. /// <summary>
  1028. /// 结果项判定
  1029. /// </summary>
  1030. private async Task<bool> TestJudgementMethod()
  1031. {
  1032. bool isJudge = true;
  1033. switch (SelectOutJudge)
  1034. {
  1035. case "人工判定":
  1036. DateTime entryTime = DateTime.Now;
  1037. await Task.Run(async () =>
  1038. {
  1039. while (entryTime.AddMilliseconds(delayTime * 1000) > DateTime.Now)
  1040. {
  1041. //显示进度条
  1042. System.Windows.Application.Current.Dispatcher.Invoke((delegate
  1043. {
  1044. ProVisibility = Visibility.Visible;
  1045. }));
  1046. await Task.Delay(1000);
  1047. }
  1048. });
  1049. ProVisibility = Visibility.Hidden;
  1050. //弹出确认的对话框
  1051. string finalJsonStr = ModelToJsonToStr(SelectOutJudge, OutSelectLogic, OutDetail, OutConList);
  1052. //json字符串
  1053. bizTestRecordDtlDto.JudgementResultFinal = finalJsonStr;
  1054. MessageBoxResult boxResult = MessageBox.Show(OutDetail, "确认", MessageBoxButton.OKCancel, MessageBoxImage.Information);
  1055. if (boxResult == MessageBoxResult.OK)
  1056. {
  1057. bizTestRecordDtlDto.JudgementResult = "通过";
  1058. StepIndex = 3;
  1059. }
  1060. else
  1061. {
  1062. isJudge = false;
  1063. }
  1064. break;
  1065. case "自动判定":
  1066. //读取plc的值
  1067. switch (OutSelectLogic)
  1068. {
  1069. case "NULL":
  1070. DateTime entryTime2 = DateTime.Now;
  1071. await Task.Run(async () =>
  1072. {
  1073. while (entryTime2.AddMilliseconds(delayTime * 1000) > DateTime.Now)
  1074. {
  1075. //显示进度条
  1076. System.Windows.Application.Current.Dispatcher.Invoke((delegate
  1077. {
  1078. ProVisibility = Visibility.Visible;
  1079. }));
  1080. await Task.Delay(1000);
  1081. }
  1082. });
  1083. ProVisibility = Visibility.Hidden;
  1084. string finalJsonStr2 = ModelToJsonToStr(SelectOutJudge, OutSelectLogic, OutDetail, OutConList);
  1085. //json字符串
  1086. bizTestRecordDtlDto.JudgementResultFinal = finalJsonStr2;
  1087. bizTestRecordDtlDto.JudgementResult = "通过";
  1088. StepIndex = 3;
  1089. break;
  1090. case "AND":
  1091. case "ONLY":
  1092. case "OR":
  1093. await ReadJudge(OutSelectLogic);
  1094. break;
  1095. case "ORDER":
  1096. break;
  1097. }
  1098. break;
  1099. }
  1100. endTime = DateTime.Now;
  1101. return isJudge;
  1102. }
  1103. /// <summary>
  1104. /// 读取结果
  1105. /// </summary>
  1106. private async Task ReadJudge(String strLogic)
  1107. {
  1108. int countCond = 0;
  1109. DateTime entryTime = DateTime.Now;
  1110. foreach (var item in OutConList.OrderBy(x => x.Id))
  1111. {
  1112. string plcAddress = item.PlcAddress;
  1113. string plcAddType = item.PlcAddType;
  1114. string plcValue = item.PlcValue;
  1115. switch (plcAddType)
  1116. {
  1117. case "bool":
  1118. string readResult = string.Empty;
  1119. await Task.Run(async () =>
  1120. {
  1121. while (entryTime.AddMilliseconds(delayTime * 1000) > DateTime.Now)
  1122. {
  1123. //显示进度条
  1124. System.Windows.Application.Current.Dispatcher.Invoke((delegate
  1125. {
  1126. ProVisibility = Visibility.Visible;
  1127. }));
  1128. //先延时,在读取,避免读取传感器刚开始的数据
  1129. await Task.Delay(1000);
  1130. readResult = PLCConnectionFactory.CreatePLCCont().ReadPlcObject(plcAddress, VarType.Bit);
  1131. if (readResult == item.PlcValue)
  1132. {
  1133. break;
  1134. }
  1135. }
  1136. });
  1137. //可能有多个条件判定的,第二次时间到了直接跳出延时,直接读取,不用在延时20s
  1138. readResult = PLCConnectionFactory.CreatePLCCont().ReadPlcObject(plcAddress, VarType.Bit);
  1139. System.Windows.Application.Current.Dispatcher.Invoke((delegate
  1140. {
  1141. ProVisibility = Visibility.Hidden;
  1142. }));
  1143. System.Windows.Application.Current.Dispatcher.Invoke((delegate
  1144. {
  1145. ///实时值记录
  1146. item.RealValue = readResult;
  1147. item.TestTime = DateTime.Now;
  1148. }));
  1149. if (readResult.Trim() == plcValue?.Trim())
  1150. {
  1151. item.TestResult = "通过";
  1152. countCond++;
  1153. }
  1154. else
  1155. {
  1156. item.TestResult = "不通过";
  1157. }
  1158. break;
  1159. case "word":
  1160. string readResult2 = string.Empty;
  1161. //一直读
  1162. await Task.Run(async () =>
  1163. {
  1164. while (entryTime.AddMilliseconds(delayTime * 1000) > DateTime.Now)
  1165. {
  1166. //显示进度条
  1167. System.Windows.Application.Current.Dispatcher.Invoke((delegate
  1168. {
  1169. ProVisibility = Visibility.Visible;
  1170. }));
  1171. readResult2 = PLCConnectionFactory.CreatePLCCont().ReadPlcObject(plcAddress, VarType.Word);
  1172. if (readResult2 == item.PlcValue)
  1173. {
  1174. break;
  1175. }
  1176. await Task.Delay(1000);
  1177. }
  1178. });
  1179. //可能有多个条件判定的,第二次时间到了直接跳出延时,直接读取,不用在延时20s
  1180. readResult2 = PLCConnectionFactory.CreatePLCCont().ReadPlcObject(plcAddress, VarType.Word);
  1181. ProVisibility = Visibility.Hidden;
  1182. ///实时值记录
  1183. item.RealValue = readResult2;
  1184. item.TestTime = DateTime.Now;
  1185. if (readResult2.Trim() == plcValue?.Trim())
  1186. {
  1187. item.TestResult = "通过";
  1188. countCond++;
  1189. }
  1190. else
  1191. {
  1192. item.TestResult = "不通过";
  1193. }
  1194. break;
  1195. }
  1196. }
  1197. //测试记录存入数据库
  1198. //输出项测试结果json转化
  1199. string prefixJsonStr = ModelToJsonToStr(SelectOutJudge, OutSelectLogic, OutDetail, OutConList);
  1200. //json字符串
  1201. bizTestRecordDtlDto.JudgementResultFinal = prefixJsonStr;
  1202. //条件满足
  1203. switch (strLogic)
  1204. {
  1205. case "AND":
  1206. case "ONLY":
  1207. if ((countCond == OutConList.Count) && (countCond != 0))
  1208. {
  1209. bizTestRecordDtlDto.JudgementResult = "通过";
  1210. }
  1211. else
  1212. {
  1213. bizTestRecordDtlDto.JudgementResult = "不通过";
  1214. }
  1215. break;
  1216. case "OR":
  1217. if (countCond != 0)
  1218. {
  1219. bizTestRecordDtlDto.JudgementResult = "通过";
  1220. }
  1221. else
  1222. {
  1223. bizTestRecordDtlDto.JudgementResult = "不通过";
  1224. }
  1225. break;
  1226. }
  1227. StepIndex = 3;
  1228. }
  1229. /// <summary>
  1230. /// 获取配置
  1231. /// </summary>
  1232. private void GetConfigOption()
  1233. {
  1234. var configList = _optionConfigService.QueryList();
  1235. _optionConfigs = _mapper.Map<List<OptionConfig>, List<OptionConfigDto>>(configList);
  1236. var tests = _optionConfigs.FindAll(x => x.TypeID == 1);
  1237. TestKinds = new List<string>();
  1238. foreach (var test in tests)
  1239. {
  1240. TestKinds.Add(test.ContentOption);
  1241. }
  1242. //测试项类型
  1243. var judges = _optionConfigs.FindAll(x => x.TypeID == 2);
  1244. foreach (var judge in judges)
  1245. {
  1246. BeforeJudgeKinds.Add(judge.ContentOption);
  1247. InJudgeKinds.Add(judge.ContentOption);
  1248. OutJudgeKinds.Add(judge.ContentOption);
  1249. }
  1250. //判定逻辑
  1251. var logics = _optionConfigs.FindAll(x => x.TypeID == 3);
  1252. foreach (var logic in logics)
  1253. {
  1254. JudgeLogicKinds.Add(logic.ContentOption);
  1255. InJudgeLogicKinds.Add(logic.ContentOption);
  1256. OutJudgeLogicKinds.Add(logic.ContentOption);
  1257. }
  1258. }
  1259. /// <summary>
  1260. /// 关闭按钮
  1261. /// </summary>
  1262. private void Close()
  1263. {
  1264. }
  1265. /// <summary>
  1266. /// mode转json
  1267. /// </summary>
  1268. /// <param name="itemType"></param>
  1269. /// <param name="itemLogical"></param>
  1270. /// <param name="description"></param>
  1271. /// <param name="conList"></param>
  1272. /// <returns></returns>
  1273. private string ModelToJsonToStr(string itemType, string itemLogical, string description, ObservableCollection<BasPlcItemConfigDto> conList)
  1274. {
  1275. JsonModel prefixJsonModel = new JsonModel();
  1276. prefixJsonModel.ItemType = itemType;
  1277. prefixJsonModel.ItemLogical = itemLogical;
  1278. prefixJsonModel.Description = description;
  1279. prefixJsonModel.DetailInfo = new List<DetailPLC>();
  1280. foreach (var item in conList)
  1281. {
  1282. DetailPLC plcItem = new DetailPLC();
  1283. plcItem.Id = item.Id;
  1284. plcItem.PlcItem = item.PlcItem;
  1285. plcItem.PlcAddress = item.PlcAddress;
  1286. plcItem.PlcAddType = item.PlcAddType;
  1287. plcItem.PlcValue = item.PlcValue;
  1288. plcItem.RealValue = item.RealValue;
  1289. plcItem.TestTime = item.TestTime;
  1290. plcItem.TestResult = item.TestResult;
  1291. plcItem.Remark = item.Remark;
  1292. prefixJsonModel.DetailInfo.Add(plcItem);
  1293. }
  1294. string prefixJsonStr = JsonConvert.SerializeObject(prefixJsonModel);
  1295. return prefixJsonStr;
  1296. }
  1297. #endregion
  1298. #region 命令绑定
  1299. public DelegateCommand CloseCommand { set; get; }
  1300. public DelegateCommand StartCommand { set; get; }
  1301. public DelegateCommand<object> StopOrContinueCommand { set; get; }
  1302. public DelegateCommand<object> CancelCommand { set; get; }
  1303. public DelegateCommand UnLoadCommand { set; get; }
  1304. public DelegateCommand ItemChangeCommand { set; get; }
  1305. #endregion
  1306. #region 变量绑定
  1307. /// <summary>
  1308. /// 测试方案编码
  1309. /// </summary>
  1310. private string scheduleName;
  1311. public string ScheduleName
  1312. {
  1313. get { return scheduleName; }
  1314. set { scheduleName = value; RaisePropertyChanged(); }
  1315. }
  1316. /// <summary>
  1317. /// 设备名称
  1318. /// </summary>
  1319. private string deviceKindName;
  1320. public string DeviceKindName
  1321. {
  1322. get { return deviceKindName; }
  1323. set { deviceKindName = value; RaisePropertyChanged(); }
  1324. }
  1325. /// <summary>
  1326. /// 测试项名称
  1327. /// </summary>
  1328. private string testName;
  1329. public string TestName
  1330. {
  1331. get { return testName; }
  1332. set { testName = value; RaisePropertyChanged(); }
  1333. }
  1334. /// <summary>
  1335. /// 测试项类型
  1336. /// </summary>
  1337. private List<string> testKinds = new List<string>();
  1338. public List<string> TestKinds
  1339. {
  1340. get { return testKinds; }
  1341. set { testKinds = value; RaisePropertyChanged(); }
  1342. }
  1343. /// <summary>
  1344. ///
  1345. /// </summary>
  1346. private string selectTest;
  1347. public string SelectTest
  1348. {
  1349. get { return selectTest; }
  1350. set { selectTest = value; RaisePropertyChanged(); }
  1351. }
  1352. /// <summary>
  1353. /// 判定类型(前置项)
  1354. /// </summary>
  1355. private List<string> beforeJudgeKinds = new List<string>();
  1356. public List<string> BeforeJudgeKinds
  1357. {
  1358. get { return beforeJudgeKinds; }
  1359. set { beforeJudgeKinds = value; RaisePropertyChanged(); }
  1360. }
  1361. private string beforeSelectJudge;
  1362. public string BeforeSelectJudge
  1363. {
  1364. get { return beforeSelectJudge; }
  1365. set { beforeSelectJudge = value; RaisePropertyChanged(); }
  1366. }
  1367. /// <summary>
  1368. /// 逻辑判断
  1369. /// </summary>
  1370. private List<string> judgeLogicKinds = new List<string>();
  1371. public List<string> JudgeLogicKinds
  1372. {
  1373. get { return judgeLogicKinds; }
  1374. set { judgeLogicKinds = value; RaisePropertyChanged(); }
  1375. }
  1376. private string selectLogic;
  1377. public string SelectLogic
  1378. {
  1379. get { return selectLogic; }
  1380. set { selectLogic = value; RaisePropertyChanged(); }
  1381. }
  1382. private string beforeDetail;
  1383. public string BeforeDetail
  1384. {
  1385. get { return beforeDetail; }
  1386. set { beforeDetail = value; RaisePropertyChanged(); }
  1387. }
  1388. private ObservableCollection<BasPlcItemConfigDto> beforeConList;
  1389. public ObservableCollection<BasPlcItemConfigDto> BeforeConList
  1390. {
  1391. get { return beforeConList; }
  1392. set { beforeConList = value; RaisePropertyChanged(); }
  1393. }
  1394. /// <summary>
  1395. /// 判定类型(输入项)
  1396. /// </summary>
  1397. private List<string> inJudgeKinds = new List<string>();
  1398. public List<string> InJudgeKinds
  1399. {
  1400. get { return inJudgeKinds; }
  1401. set { inJudgeKinds = value; RaisePropertyChanged(); }
  1402. }
  1403. private string selectInJudge;
  1404. public string SelectInJudge
  1405. {
  1406. get { return selectInJudge; }
  1407. set { selectInJudge = value; RaisePropertyChanged(); }
  1408. }
  1409. /// <summary>
  1410. /// 逻辑判断
  1411. /// </summary>
  1412. private List<string> inJudgeLogicKinds = new List<string>();
  1413. public List<string> InJudgeLogicKinds
  1414. {
  1415. get { return inJudgeLogicKinds; }
  1416. set { inJudgeLogicKinds = value; RaisePropertyChanged(); }
  1417. }
  1418. private string inSelectLogic;
  1419. public string InSelectLogic
  1420. {
  1421. get { return inSelectLogic; }
  1422. set { inSelectLogic = value; RaisePropertyChanged(); }
  1423. }
  1424. private string inDetail;
  1425. public string InDetail
  1426. {
  1427. get { return inDetail; }
  1428. set { inDetail = value; RaisePropertyChanged(); }
  1429. }
  1430. private ObservableCollection<BasPlcItemConfigDto> inConList;
  1431. public ObservableCollection<BasPlcItemConfigDto> InConList
  1432. {
  1433. get { return inConList; }
  1434. set { inConList = value; RaisePropertyChanged(); }
  1435. }
  1436. /// <summary>
  1437. /// 判定类型(结果项)
  1438. /// </summary>
  1439. private List<string> outJudgeKinds = new List<string>();
  1440. public List<string> OutJudgeKinds
  1441. {
  1442. get { return outJudgeKinds; }
  1443. set { outJudgeKinds = value; RaisePropertyChanged(); }
  1444. }
  1445. private string selectOutJudge;
  1446. public string SelectOutJudge
  1447. {
  1448. get { return selectOutJudge; }
  1449. set { selectOutJudge = value; RaisePropertyChanged(); }
  1450. }
  1451. /// <summary>
  1452. /// 逻辑判断
  1453. /// </summary>
  1454. private List<string> outJudgeLogicKinds = new List<string>();
  1455. public List<string> OutJudgeLogicKinds
  1456. {
  1457. get { return outJudgeLogicKinds; }
  1458. set { outJudgeLogicKinds = value; RaisePropertyChanged(); }
  1459. }
  1460. private string outSelectLogic;
  1461. public string OutSelectLogic
  1462. {
  1463. get { return outSelectLogic; }
  1464. set { outSelectLogic = value; RaisePropertyChanged(); }
  1465. }
  1466. private string outDetail;
  1467. public string OutDetail
  1468. {
  1469. get { return outDetail; }
  1470. set { outDetail = value; RaisePropertyChanged(); }
  1471. }
  1472. private ObservableCollection<BasPlcItemConfigDto> outConList;
  1473. public ObservableCollection<BasPlcItemConfigDto> OutConList
  1474. {
  1475. get { return outConList; }
  1476. set { outConList = value; RaisePropertyChanged(); }
  1477. }
  1478. private int stepIndex = 0;
  1479. public int StepIndex
  1480. {
  1481. get { return stepIndex; }
  1482. set { stepIndex = value; RaisePropertyChanged(); }
  1483. }
  1484. private Visibility proVisibility = Visibility.Hidden;
  1485. public Visibility ProVisibility
  1486. {
  1487. get { return proVisibility; }
  1488. set { proVisibility = value; RaisePropertyChanged(); }
  1489. }
  1490. private string stopOrContinueCont ="暂停";
  1491. public string StopOrContinueCont
  1492. {
  1493. get { return stopOrContinueCont; }
  1494. set { stopOrContinueCont = value; RaisePropertyChanged(); }
  1495. }
  1496. private bool startEnalbe = true;
  1497. public bool StartEnalbe
  1498. {
  1499. get { return startEnalbe; }
  1500. set { startEnalbe = value; RaisePropertyChanged(); }
  1501. }
  1502. //所有测试项列表
  1503. //所有测试项列表
  1504. private List<ItemAndBackColorModel> schItems = new List<ItemAndBackColorModel>();
  1505. public List<ItemAndBackColorModel> SchItems
  1506. {
  1507. get { return schItems; }
  1508. set { schItems = value; RaisePropertyChanged(); }
  1509. }
  1510. //所选测试方案
  1511. private ItemAndBackColorModel selectItem;
  1512. public ItemAndBackColorModel SelectItem
  1513. {
  1514. get { return selectItem; }
  1515. set { selectItem = value; RaisePropertyChanged(); }
  1516. }
  1517. private int selectItemIndex;
  1518. public int SelectItemIndex
  1519. {
  1520. get { return selectItemIndex; }
  1521. set { selectItemIndex = value; RaisePropertyChanged(); }
  1522. }
  1523. #endregion
  1524. }
  1525. }