AutoTestViewModel.cs 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237
  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 Prism.Commands;
  9. using Prism.Events;
  10. using Prism.Mvvm;
  11. using Prism.Services.Dialogs;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Collections.ObjectModel;
  15. using System.Linq;
  16. using System.Text;
  17. using System.Threading;
  18. using System.Threading.Tasks;
  19. using System.Windows;
  20. using System.Xaml;
  21. namespace PLCTool.ViewModels.BusinessManageViewModel
  22. {
  23. public class AutoTestViewModel : BindableBase, IDialogAware
  24. {
  25. private readonly IDialogService _dialog;
  26. private readonly IEventAggregator _aggregator;
  27. private readonly IOptionConfigService _optionConfigService;
  28. private readonly IBasicPlcTestSchemeService _basicPlcTestSchemeService;
  29. private readonly IBasicPlcTestSchemeDtlService _basicPlcTestSchemeDtlService;
  30. private readonly IBizTestRecordService _iBizTestRecordService;
  31. private readonly IBizTestRecordDtlService _iBizTestRecordDtlService;
  32. private readonly IMapper _mapper;
  33. private readonly ILogger _logger;
  34. private List<OptionConfigDto> _optionConfigs;
  35. private DateTime startTime = DateTime.Now;
  36. private DateTime endTime = DateTime.Now;
  37. private int testStatus = 0; //测试状态
  38. private int testResult = 0; //测试结果
  39. private long schId = 0; //方案ID
  40. private long globalSchDetailId = 0; //测试方案明细ID
  41. private long selctStartDetailId = 0; //当前选择的第一个方案明细id
  42. private BizTestRecordDtlDto bizTestRecordDtlDto = new BizTestRecordDtlDto(); //测试方案明细表
  43. private const string TestMode = "自动测试";
  44. ManualResetEvent m = new ManualResetEvent(true); //实例化阻塞事件
  45. CancellationTokenSource tokensource = new CancellationTokenSource(); //声明令牌
  46. public AutoTestViewModel(IDialogService dialog, IEventAggregator aggregator, IOptionConfigService optionConfigService, IBasicPlcTestSchemeService basicPlcTestSchemeService, IBasicPlcTestSchemeDtlService basicPlcTestSchemeDtlService, IBizTestRecordService iBizTestRecordService, IBizTestRecordDtlService iBizTestRecordDtlService, IMapper mapper, ILogger logger)
  47. {
  48. _dialog = dialog;
  49. _aggregator = aggregator;
  50. _optionConfigService = optionConfigService;
  51. _basicPlcTestSchemeService = basicPlcTestSchemeService;
  52. _basicPlcTestSchemeDtlService = basicPlcTestSchemeDtlService;
  53. _iBizTestRecordDtlService = iBizTestRecordDtlService;
  54. _iBizTestRecordService = iBizTestRecordService;
  55. _mapper = mapper;
  56. _logger = logger;
  57. CloseCommand = new DelegateCommand(Close);
  58. StartCommand = new DelegateCommand(Start);
  59. StopOrContinueCommand = new DelegateCommand<object>(StopOrContinueMethod);
  60. BeforeConList = new ObservableCollection<BasPlcItemConfigDto>();
  61. InConList = new ObservableCollection<BasPlcItemConfigDto>();
  62. OutConList = new ObservableCollection<BasPlcItemConfigDto>();
  63. GetConfigOption();
  64. }
  65. private void StopOrContinueMethod(object obj)
  66. {
  67. if(StopOrContinueCont=="暂停")
  68. {
  69. m.Reset(); //阻塞线程
  70. StopOrContinueCont = "继续";
  71. }
  72. else
  73. {
  74. if (StopOrContinueCont == "继续")
  75. {
  76. m.Set(); //继续线程
  77. StopOrContinueCont = "暂停";
  78. }
  79. }
  80. }
  81. #region idialog接口实现
  82. public string Title { set; get; } = "自动测试";
  83. public event Action<IDialogResult> RequestClose;
  84. public bool CanCloseDialog()
  85. {
  86. return true;
  87. }
  88. public void OnDialogClosed()
  89. {
  90. }
  91. public void OnDialogOpened(IDialogParameters parameters)
  92. {
  93. //编辑
  94. var getMsg = parameters.GetValues<long>("Key");
  95. ///值不为空,表示修改
  96. if (getMsg != null)
  97. {
  98. foreach (var item in getMsg)
  99. {
  100. selctStartDetailId= item;//测试方案明细ID
  101. int id = Convert.ToInt32(item);
  102. // 根据 测试方案明细主键ID 查找测试方案id 及方案名和设备名
  103. GetDetailInfo(id);
  104. }
  105. }
  106. }
  107. /// <summary>
  108. /// //根据 测试方案明细主键ID 查找测试方案id 及方案名和设备名
  109. /// </summary>
  110. /// <param name="id"></param>
  111. private void GetDetailInfo(int schDetailId)
  112. {
  113. //根据 测试方案明细主键ID 查找测试方案id 及方案名和设备名
  114. var findEntity = _basicPlcTestSchemeDtlService.Find(schDetailId);
  115. var findresult = _mapper.Map<bas_plc_test_scheme_dtl, BasicPlcTestSchemeDtlDto>(findEntity);
  116. if (findresult != null)
  117. {
  118. //根据方案id 在方案表中查找方案名 设备名
  119. schId = findresult.SchemeId.Value; //方案id,方便下一步查找
  120. int intSchId = Convert.ToInt32(findresult.SchemeId);
  121. var findSch = _basicPlcTestSchemeService.Find(intSchId);
  122. if (findSch != null)
  123. {
  124. ScheduleName = findSch.scheme_name;
  125. DeviceName = findSch.device_name;
  126. }
  127. TestName = findresult.ItemName;
  128. SelectTest = findresult.ItemType;
  129. //前置项解析
  130. string preconStr = findresult.Precondition.ToString();
  131. if (!string.IsNullOrEmpty(preconStr))
  132. {
  133. JsonModel preconditionModel = JsonConvert.DeserializeObject<JsonModel>(preconStr);
  134. BeforeSelectJudge = preconditionModel.ItemType;
  135. SelectLogic = preconditionModel.ItemLogical;
  136. BeforeDetail = preconditionModel.Description;
  137. BeforeConList.Clear();
  138. foreach (var detail in preconditionModel.DetailInfo)
  139. {
  140. BasPlcItemConfigDto basPlcItemConfigDto = new BasPlcItemConfigDto();
  141. basPlcItemConfigDto.PlcItem = detail.PlcItem;
  142. basPlcItemConfigDto.PlcAddress = detail.PlcAddress;
  143. basPlcItemConfigDto.PlcAddType = detail.PlcAddType;
  144. basPlcItemConfigDto.PlcValue = detail.PlcValue;
  145. basPlcItemConfigDto.Remark = detail.Remark;
  146. BeforeConList.Add(basPlcItemConfigDto);
  147. }
  148. }
  149. //输入项解析
  150. string inStr = findresult.Action.ToString();
  151. if (!string.IsNullOrEmpty(inStr))
  152. {
  153. JsonModel inModel = JsonConvert.DeserializeObject<JsonModel>(inStr);
  154. SelectInJudge = inModel.ItemType;
  155. InSelectLogic = inModel.ItemLogical;
  156. InDetail = inModel.Description;
  157. InConList.Clear();
  158. foreach (var detail in inModel.DetailInfo)
  159. {
  160. BasPlcItemConfigDto basPlcItemConfigDto = new BasPlcItemConfigDto();
  161. basPlcItemConfigDto.PlcItem = detail.PlcItem;
  162. basPlcItemConfigDto.PlcAddress = detail.PlcAddress;
  163. basPlcItemConfigDto.PlcAddType = detail.PlcAddType;
  164. basPlcItemConfigDto.PlcValue = detail.PlcValue;
  165. basPlcItemConfigDto.Remark = detail.Remark;
  166. InConList.Add(basPlcItemConfigDto);
  167. }
  168. }
  169. //输出项解析
  170. string outStr = findresult.JudgementResult.ToString();
  171. if (!string.IsNullOrEmpty(outStr))
  172. {
  173. JsonModel outModel = JsonConvert.DeserializeObject<JsonModel>(outStr);
  174. SelectOutJudge = outModel.ItemType;
  175. OutSelectLogic = outModel.ItemLogical;
  176. OutDetail = outModel.Description;
  177. OutConList.Clear();
  178. foreach (var detail in outModel.DetailInfo)
  179. {
  180. BasPlcItemConfigDto basPlcItemConfigDto = new BasPlcItemConfigDto();
  181. basPlcItemConfigDto.PlcItem = detail.PlcItem;
  182. basPlcItemConfigDto.PlcAddress = detail.PlcAddress;
  183. basPlcItemConfigDto.PlcAddType = detail.PlcAddType;
  184. basPlcItemConfigDto.PlcValue = detail.PlcValue;
  185. basPlcItemConfigDto.Remark = detail.Remark;
  186. OutConList.Add(basPlcItemConfigDto);
  187. }
  188. }
  189. }
  190. }
  191. #endregion
  192. #region 私有发方法
  193. /// <summary>
  194. /// 增加或更新测试记录
  195. /// </summary>
  196. private void AddTestRecord()
  197. {
  198. //记录记录主表中是否有这个方案的记录,没有则添加
  199. var findRecord = _iBizTestRecordService.FindRecorddBySchname(ScheduleName);
  200. if (findRecord == null)
  201. {
  202. //状态status 没有赋值
  203. BizTestRecordDto bizTestRecordDto = new BizTestRecordDto();
  204. bizTestRecordDto.RecordName = ScheduleName + "_Record";
  205. bizTestRecordDto.SchemeName = ScheduleName;
  206. bizTestRecordDto.Tester = Appsession.UserName;
  207. bizTestRecordDto.StartTestTime = startTime;
  208. bizTestRecordDto.FinishTestTime = endTime;
  209. bizTestRecordDto.CreateBy = Appsession.UserName;
  210. bizTestRecordDto.CreateTime = DateTime.Now;
  211. bizTestRecordDto.UpdateBy = Appsession.UserName;
  212. bizTestRecordDto.UpdateTime = DateTime.Now;
  213. var testRecord = _mapper.Map<BizTestRecordDto, biz_test_record>(bizTestRecordDto);
  214. _iBizTestRecordService.Add(testRecord);
  215. }
  216. }
  217. /// <summary>
  218. /// 增加测试记录明细
  219. /// </summary>
  220. private void AddOrEditTesDtltRecord(long schDetailId)
  221. {
  222. //查找record ID
  223. long recordId = 0;
  224. var findRecordID = _iBizTestRecordService.FindRecorddBySchname(ScheduleName);
  225. if (findRecordID != null)
  226. {
  227. recordId = findRecordID.record_id;
  228. }
  229. testStatus = 99;//测试状态赋值为99
  230. //状态status 没有赋值
  231. //记录记录明细表中是否有这个方案明细ID的记录,没有则添加,有则更新记录状态
  232. var findRecordDetail = _iBizTestRecordDtlService.FindRecordDetailBySchDtlID(schDetailId);
  233. if (findRecordDetail == null)
  234. {
  235. BizTestRecordDtlDto newBizTestEwDetail = new BizTestRecordDtlDto();
  236. newBizTestEwDetail.RecordId = recordId;
  237. newBizTestEwDetail.SchemeDtlId = schDetailId;
  238. newBizTestEwDetail.StartTestTime = startTime;
  239. newBizTestEwDetail.FinishTestTime = endTime;
  240. newBizTestEwDetail.TestMode = TestMode;
  241. newBizTestEwDetail.Status = testStatus;
  242. newBizTestEwDetail.TestResult = GetTestResult();
  243. newBizTestEwDetail.CreateBy = Appsession.UserName;
  244. newBizTestEwDetail.CreateTime = startTime;
  245. newBizTestEwDetail.UpdateBy = Appsession.UserName;
  246. newBizTestEwDetail.UpdateTime = DateTime.Now;
  247. var testRecordDtl = _mapper.Map<BizTestRecordDtlDto, biz_test_record_dtl>(newBizTestEwDetail);
  248. _iBizTestRecordDtlService.Add(testRecordDtl);
  249. }
  250. else
  251. {
  252. bizTestRecordDtlDto.RecordDtlId = findRecordDetail.record_dtl_id;
  253. bizTestRecordDtlDto.RecordId = recordId;
  254. bizTestRecordDtlDto.SchemeDtlId = schDetailId;
  255. bizTestRecordDtlDto.StartTestTime = startTime;
  256. bizTestRecordDtlDto.FinishTestTime = endTime;
  257. bizTestRecordDtlDto.TestMode = TestMode;
  258. bizTestRecordDtlDto.Status = testStatus;
  259. bizTestRecordDtlDto.TestResult = GetTestResult();
  260. bizTestRecordDtlDto.CreateBy = Appsession.UserName;
  261. bizTestRecordDtlDto.CreateTime = startTime;
  262. bizTestRecordDtlDto.UpdateBy = Appsession.UserName;
  263. bizTestRecordDtlDto.UpdateTime = DateTime.Now;
  264. //更新时间
  265. bizTestRecordDtlDto.UpdateBy = Appsession.UserName;
  266. bizTestRecordDtlDto.UpdateTime = DateTime.Now;
  267. UpdateTesDtltRecord();
  268. }
  269. }
  270. /// <summary>
  271. /// 获取测试结果
  272. /// </summary>
  273. /// <returns></returns>
  274. private int GetTestResult()
  275. {
  276. //前置项
  277. int beforeResult = 0;
  278. int inResult = 0;
  279. int outResult = 0;
  280. if (SelectLogic == "OR")
  281. {
  282. var find = BeforeConList.FirstOrDefault(x => x.TestResult == "合格");
  283. if (find != null)
  284. {
  285. beforeResult = 1;
  286. }
  287. }
  288. else
  289. {
  290. if (SelectLogic == "AND" || SelectLogic == "ONLY")
  291. {
  292. var find = BeforeConList.FirstOrDefault(x => x.TestResult == "不合格");
  293. if (find != null)
  294. {
  295. beforeResult = 0;
  296. }
  297. }
  298. else
  299. {
  300. if (SelectLogic == "NULL")
  301. {
  302. beforeResult = 1;
  303. }
  304. }
  305. }
  306. //输入项
  307. if (InSelectLogic == "OR")
  308. {
  309. var find = InConList.FirstOrDefault(x => x.TestResult == "合格");
  310. if (find != null)
  311. {
  312. inResult = 1;
  313. }
  314. }
  315. else
  316. {
  317. if (InSelectLogic == "AND" || InSelectLogic == "ONLY")
  318. {
  319. var find = InConList.FirstOrDefault(x => x.TestResult == "不合格");
  320. if (find != null)
  321. {
  322. inResult = 0;
  323. }
  324. }
  325. else
  326. {
  327. if (InSelectLogic == "NULL")
  328. {
  329. inResult = 1;
  330. }
  331. }
  332. }
  333. //输出项
  334. if (OutSelectLogic == "OR")
  335. {
  336. var find = OutConList.FirstOrDefault(x => x.TestResult == "合格");
  337. if (find != null)
  338. {
  339. outResult = 1;
  340. }
  341. }
  342. else
  343. {
  344. if (OutSelectLogic == "AND" || OutSelectLogic == "ONLY")
  345. {
  346. var find = OutConList.FirstOrDefault(x => x.TestResult == "不合格");
  347. if (find != null)
  348. {
  349. outResult = 0;
  350. }
  351. }
  352. else
  353. {
  354. if (OutSelectLogic == "NULL")
  355. {
  356. outResult = 1;
  357. }
  358. }
  359. }
  360. if (beforeResult == 1 && inResult == 1 && outResult == 1)
  361. {
  362. return 1;
  363. }
  364. else
  365. {
  366. return 0;
  367. }
  368. }
  369. /// <summary>
  370. /// 更新测试记录
  371. /// </summary>
  372. private void UpdateTesDtltRecord()
  373. {
  374. var testRecordDtl = _mapper.Map<BizTestRecordDtlDto, biz_test_record_dtl>(bizTestRecordDtlDto);
  375. _iBizTestRecordDtlService.Edit(testRecordDtl);
  376. }
  377. /// <summary>
  378. /// 开始检测
  379. /// </summary>
  380. /// <param name="obj"></param>
  381. private async void Start()
  382. {
  383. ///查找方案下的所有方案明细,并排序,
  384. var basicSchDtls = _basicPlcTestSchemeDtlService.FindAllBySchId(schId)?.OrderBy(x => x.scheme_dtl_id);
  385. foreach (var item in basicSchDtls)
  386. {
  387. long schDetailId = item.scheme_dtl_id;
  388. //编号小于当前选择的就不测试了,
  389. if(schDetailId < selctStartDetailId)
  390. {
  391. continue;
  392. }
  393. globalSchDetailId = schDetailId;
  394. await ExecTest();
  395. //更新下一个界面显示
  396. int id = Convert.ToInt32(globalSchDetailId);
  397. //界面显示
  398. System.Windows.Application.Current.Dispatcher.Invoke((delegate
  399. {
  400. GetDetailInfo(id);
  401. }));
  402. //最后一项必须要测试
  403. if (globalSchDetailId == basicSchDtls.Max(x => x.scheme_dtl_id))
  404. {
  405. await ExecTest();
  406. }
  407. }
  408. }
  409. private async Task ExecTest()
  410. { //开始时间
  411. startTime = DateTime.Now;
  412. //增加测试记录主表
  413. AddTestRecord();
  414. //前置项
  415. bool preBool = TestPreceditionMethod();
  416. //输入项
  417. bool isAction = TestActionMethod();
  418. //输出项
  419. bool isJudgeResult = await TestJudgementMethod();
  420. //下一项
  421. StepIndex = 0;
  422. }
  423. /// <summary>
  424. /// 更新测试结果
  425. /// </summary>
  426. private void UpdateTestRecord()
  427. {
  428. }
  429. /// <summary>
  430. /// 测试前置项目
  431. /// </summary>
  432. private bool TestPreceditionMethod()
  433. {
  434. switch (BeforeSelectJudge)
  435. {
  436. case "人工判定":
  437. //弹出确认的对话框
  438. //前置项测试结果json转化
  439. string prefixJsonStr = ModelToJsonToStr(BeforeSelectJudge, SelectLogic, BeforeDetail, BeforeConList);
  440. //json字符串
  441. bizTestRecordDtlDto.PreconditionFinal = prefixJsonStr;
  442. bizTestRecordDtlDto.PreconditionResult = "合格";
  443. StepIndex = 1;
  444. //MessageBoxResult boxResult = MessageBox.Show(BeforeDetail, "确认", MessageBoxButton.OKCancel, MessageBoxImage.Information);
  445. //if (boxResult == MessageBoxResult.OK)
  446. //{
  447. // //前置项测试结果json转化
  448. // string prefixJsonStr = ModelToJsonToStr(BeforeSelectJudge, SelectLogic, BeforeDetail, BeforeConList);
  449. // //json字符串
  450. // bizTestRecordDtlDto.PreconditionFinal = prefixJsonStr;
  451. // bizTestRecordDtlDto.PreconditionResult = "合格";
  452. // StepIndex = 1;
  453. //}
  454. //else
  455. //{
  456. // return false;
  457. //}
  458. break;
  459. case "自动判定":
  460. //读取plc的值
  461. switch (SelectLogic)
  462. {
  463. case "NULL":
  464. //弹出确认的对话框
  465. //前置项测试结果json转化
  466. string prefixJsonStrAuto = ModelToJsonToStr(BeforeSelectJudge, SelectLogic, BeforeDetail, BeforeConList);
  467. //json字符串
  468. bizTestRecordDtlDto.PreconditionFinal = prefixJsonStrAuto;
  469. bizTestRecordDtlDto.PreconditionResult = "合格";
  470. StepIndex = 1;
  471. //MessageBoxResult boxResultAuto = MessageBox.Show(BeforeDetail, "确认", MessageBoxButton.OKCancel, MessageBoxImage.Information);
  472. //if (boxResultAuto == MessageBoxResult.OK)
  473. //{
  474. // //前置项测试结果json转化
  475. // string prefixJsonStr = ModelToJsonToStr(BeforeSelectJudge, SelectLogic, BeforeDetail, BeforeConList);
  476. // //json字符串
  477. // bizTestRecordDtlDto.PreconditionFinal = prefixJsonStr;
  478. // bizTestRecordDtlDto.PreconditionResult = "合格";
  479. // StepIndex = 1;
  480. //}
  481. //else
  482. //{
  483. // return false;
  484. //}
  485. break;
  486. case "AND":
  487. case "ONLY":
  488. case "OR":
  489. ReadPredice(SelectLogic);
  490. break;
  491. case "ORDER":
  492. break;
  493. }
  494. break;
  495. }
  496. //更新测试记录
  497. AddOrEditTesDtltRecord(globalSchDetailId);
  498. return true;
  499. }
  500. private void ReadPredice(string strLogic)
  501. {
  502. int countCond = 0;
  503. foreach (var item in BeforeConList)
  504. {
  505. string plcAddress = item.PlcAddress;
  506. string plcAddType = item.PlcAddType;
  507. string plcValue = item.PlcValue;
  508. switch (plcAddType)
  509. {
  510. case "bool":
  511. string readResult = PLCCom.GetInstance().ReadPlcObject(plcAddress, VarType.Bit);
  512. ///实时值记录
  513. item.RealValue = readResult;
  514. item.TestTime = DateTime.Now;
  515. if (readResult.Trim() == plcValue.Trim())
  516. {
  517. item.TestResult = "合格";
  518. countCond++;
  519. }
  520. else
  521. {
  522. item.TestResult = "不合格";
  523. }
  524. break;
  525. case "word":
  526. break;
  527. }
  528. }
  529. //测试记录存入数据库
  530. //前置项测试结果json转化
  531. string prefixJsonStr = ModelToJsonToStr(BeforeSelectJudge, SelectLogic, BeforeDetail, BeforeConList);
  532. //json字符串
  533. bizTestRecordDtlDto.PreconditionFinal = prefixJsonStr;
  534. //条件满足
  535. switch (strLogic)
  536. {
  537. case "AND":
  538. case "ONLY":
  539. if ((countCond == BeforeConList.Count) && (countCond != 0))
  540. {
  541. bizTestRecordDtlDto.PreconditionResult = "合格";
  542. }
  543. else
  544. {
  545. bizTestRecordDtlDto.PreconditionResult = "不合格";
  546. }
  547. break;
  548. case "OR":
  549. if ((countCond != 0))
  550. {
  551. bizTestRecordDtlDto.PreconditionResult = "合格";
  552. }
  553. else
  554. {
  555. bizTestRecordDtlDto.PreconditionResult = "不合格";
  556. }
  557. break;
  558. }
  559. StepIndex = 1;
  560. }
  561. /// <summary>
  562. /// 输入项写入
  563. /// </summary>
  564. private bool TestActionMethod()
  565. {
  566. bool isActionResult = true;
  567. switch (SelectInJudge)
  568. {
  569. case "人工判定":
  570. //弹出确认的对话框
  571. MessageBoxResult boxResult = MessageBox.Show(InDetail, "确认", MessageBoxButton.OKCancel, MessageBoxImage.Information);
  572. if (boxResult == MessageBoxResult.OK)
  573. {
  574. //前置项测试结果json转化
  575. string actionJsonStr = ModelToJsonToStr(SelectInJudge, InSelectLogic, InDetail, InConList);
  576. //json字符串
  577. bizTestRecordDtlDto.ActionFinal = actionJsonStr;
  578. bizTestRecordDtlDto.ActionResult = "合格";
  579. StepIndex = 2;
  580. }
  581. else
  582. {
  583. isActionResult = false;
  584. }
  585. break;
  586. case "自动判定":
  587. //读取plc的值
  588. switch (InSelectLogic)
  589. {
  590. case "NULL":
  591. //弹出确认的对话框
  592. MessageBoxResult boxResultAction = MessageBox.Show(InDetail, "确认", MessageBoxButton.OKCancel, MessageBoxImage.Information);
  593. if (boxResultAction == MessageBoxResult.OK)
  594. {
  595. //前置项测试结果json转化
  596. string actionJsonStr = ModelToJsonToStr(SelectInJudge, InSelectLogic, InDetail, InConList);
  597. //json字符串
  598. bizTestRecordDtlDto.ActionFinal = actionJsonStr;
  599. bizTestRecordDtlDto.ActionResult = "合格";
  600. StepIndex = 2;
  601. }
  602. else
  603. {
  604. isActionResult = false;
  605. }
  606. break;
  607. case "AND":
  608. case "ONLY":
  609. case "OR":
  610. isActionResult = SendActionToPLC(InSelectLogic);
  611. break;
  612. case "ORDER":
  613. break;
  614. }
  615. break;
  616. }
  617. //更新测试记录
  618. //更新测试记录
  619. AddOrEditTesDtltRecord(globalSchDetailId);
  620. return isActionResult;
  621. }
  622. /// <summary>
  623. /// 发送命令给PLC
  624. /// </summary>
  625. private bool SendActionToPLC(string strActionLogic)
  626. {
  627. bool isAction = true;
  628. int countCond = 0;
  629. foreach (var item in InConList)
  630. {
  631. string plcAddress = item.PlcAddress;
  632. string plcAddType = item.PlcAddType;
  633. string plcValue = item.PlcValue;
  634. switch (plcAddType)
  635. {
  636. case "bool":
  637. bool writeResult = PLCCom.GetInstance().WritePlcObject(plcAddress, VarType.Bit, plcValue);
  638. ///实时值记录
  639. item.RealValue = plcValue;
  640. item.TestTime = DateTime.Now;
  641. if (writeResult)
  642. {
  643. ///单个测试项合格
  644. item.TestResult = "合格";
  645. countCond++;
  646. }
  647. else
  648. {
  649. item.TestResult = "不合格";
  650. }
  651. break;
  652. case "word":
  653. bool writeWrordResult = PLCCom.GetInstance().WritePlcObject(plcAddress, VarType.Word, plcValue);
  654. ///实时值记录
  655. item.RealValue = plcValue;
  656. item.TestTime = DateTime.Now;
  657. if (writeWrordResult)
  658. {
  659. ///单个测试项合格
  660. item.TestResult = "合格";
  661. countCond++;
  662. }
  663. else
  664. {
  665. item.TestResult = "不合格";
  666. }
  667. break;
  668. }
  669. }
  670. if (!isAction)
  671. {
  672. return isAction;
  673. }
  674. //测试记录存入数据库
  675. //前置项测试结果json转化
  676. string actionJsonStr = ModelToJsonToStr(SelectInJudge, InSelectLogic, InDetail, InConList);
  677. //json字符串
  678. bizTestRecordDtlDto.ActionFinal = actionJsonStr;
  679. //条件满足
  680. //条件满足
  681. switch (strActionLogic)
  682. {
  683. case "AND":
  684. case "ONLY":
  685. if ((countCond == BeforeConList.Count) && (countCond != 0))
  686. {
  687. bizTestRecordDtlDto.ActionResult = "合格";
  688. }
  689. else
  690. {
  691. bizTestRecordDtlDto.ActionResult = "不合格";
  692. }
  693. break;
  694. case "OR":
  695. if ((countCond != 0))
  696. {
  697. bizTestRecordDtlDto.ActionResult = "合格";
  698. }
  699. else
  700. {
  701. bizTestRecordDtlDto.ActionResult = "不合格";
  702. }
  703. break;
  704. }
  705. StepIndex = 2;
  706. return isAction;
  707. }
  708. /// <summary>
  709. /// 结果项判定
  710. /// </summary>
  711. private async Task<bool> TestJudgementMethod()
  712. {
  713. bool isJudge = true;
  714. switch (SelectOutJudge)
  715. {
  716. case "人工判定":
  717. //弹出确认的对话框
  718. MessageBoxResult boxResult = MessageBox.Show(OutDetail, "确认", MessageBoxButton.OKCancel, MessageBoxImage.Information);
  719. if (boxResult == MessageBoxResult.OK)
  720. {
  721. bizTestRecordDtlDto.JudgementResult = "合格";
  722. StepIndex = 3;
  723. }
  724. else
  725. {
  726. isJudge = false;
  727. }
  728. break;
  729. case "自动判定":
  730. //读取plc的值
  731. switch (OutSelectLogic)
  732. {
  733. case "NULL":
  734. bizTestRecordDtlDto.JudgementResult = "合格";
  735. StepIndex = 3;
  736. break;
  737. case "AND":
  738. case "ONLY":
  739. case "OR":
  740. await ReadJudge(OutSelectLogic);
  741. break;
  742. case "ORDER":
  743. break;
  744. }
  745. break;
  746. }
  747. //更新测试记录
  748. //更新测试记录
  749. AddOrEditTesDtltRecord(globalSchDetailId);
  750. //判定是否为最后一项
  751. long maxDtlId = _basicPlcTestSchemeDtlService.FindAllBySchId(schId).Select(x => x.scheme_dtl_id).Max();
  752. if (globalSchDetailId == maxDtlId)
  753. {
  754. MessageBox.Show("已是此测试方案最后一个测试项!", "确认", MessageBoxButton.OK, MessageBoxImage.Information);
  755. }
  756. return isJudge;
  757. }
  758. /// <summary>
  759. /// 读取结果
  760. /// </summary>
  761. private async Task ReadJudge(String strLogic)
  762. {
  763. int countCond = 0;
  764. foreach (var item in OutConList)
  765. {
  766. string plcAddress = item.PlcAddress;
  767. string plcAddType = item.PlcAddType;
  768. string plcValue = item.PlcValue;
  769. switch (plcAddType)
  770. {
  771. case "bool":
  772. DateTime entryTime = DateTime.Now;
  773. string readResult = string.Empty;
  774. //一直读
  775. await Task.Run(async () =>
  776. {
  777. while (entryTime.AddMilliseconds(10 * 1000) > DateTime.Now)
  778. {
  779. //显示进度条
  780. System.Windows.Application.Current.Dispatcher.Invoke((delegate
  781. {
  782. ProVisibility = Visibility.Visible;
  783. }));
  784. readResult = PLCCom.GetInstance().ReadPlcObject(plcAddress, VarType.Bit);
  785. if (readResult == item.PlcValue)
  786. {
  787. break;
  788. }
  789. await Task.Delay(1000);
  790. }
  791. });
  792. ProVisibility = Visibility.Hidden;
  793. ///实时值记录
  794. item.RealValue = readResult;
  795. item.TestTime = DateTime.Now;
  796. if (readResult.Trim() == plcValue?.Trim())
  797. {
  798. item.TestResult = "合格";
  799. countCond++;
  800. }
  801. else
  802. {
  803. item.TestResult = "不合格";
  804. }
  805. break;
  806. case "word":
  807. break;
  808. }
  809. }
  810. //测试记录存入数据库
  811. //输出项测试结果json转化
  812. string prefixJsonStr = ModelToJsonToStr(SelectOutJudge, OutSelectLogic, OutDetail, OutConList);
  813. //json字符串
  814. bizTestRecordDtlDto.JudgementResultFinal = prefixJsonStr;
  815. //条件满足
  816. switch (strLogic)
  817. {
  818. case "AND":
  819. case "ONLY":
  820. if ((countCond == OutConList.Count) && (countCond != 0))
  821. {
  822. bizTestRecordDtlDto.JudgementResult = "合格";
  823. }
  824. else
  825. {
  826. bizTestRecordDtlDto.JudgementResult = "不合格";
  827. }
  828. break;
  829. case "OR":
  830. if (countCond != 0)
  831. {
  832. bizTestRecordDtlDto.JudgementResult = "合格";
  833. }
  834. else
  835. {
  836. bizTestRecordDtlDto.JudgementResult = "不合格";
  837. }
  838. break;
  839. }
  840. StepIndex = 3;
  841. }
  842. /// <summary>
  843. /// 获取配置
  844. /// </summary>
  845. private void GetConfigOption()
  846. {
  847. var configList = _optionConfigService.QueryList();
  848. _optionConfigs = _mapper.Map<List<OptionConfig>, List<OptionConfigDto>>(configList);
  849. var tests = _optionConfigs.FindAll(x => x.TypeID == 1);
  850. TestKinds = new List<string>();
  851. foreach (var test in tests)
  852. {
  853. TestKinds.Add(test.ContentOption);
  854. }
  855. //测试项类型
  856. var judges = _optionConfigs.FindAll(x => x.TypeID == 2);
  857. foreach (var judge in judges)
  858. {
  859. BeforeJudgeKinds.Add(judge.ContentOption);
  860. InJudgeKinds.Add(judge.ContentOption);
  861. OutJudgeKinds.Add(judge.ContentOption);
  862. }
  863. //判定逻辑
  864. var logics = _optionConfigs.FindAll(x => x.TypeID == 3);
  865. foreach (var logic in logics)
  866. {
  867. JudgeLogicKinds.Add(logic.ContentOption);
  868. InJudgeLogicKinds.Add(logic.ContentOption);
  869. OutJudgeLogicKinds.Add(logic.ContentOption);
  870. }
  871. }
  872. /// <summary>
  873. /// 关闭按钮
  874. /// </summary>
  875. private void Close()
  876. {
  877. RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel));
  878. }
  879. /// <summary>
  880. /// mode转json
  881. /// </summary>
  882. /// <param name="itemType"></param>
  883. /// <param name="itemLogical"></param>
  884. /// <param name="description"></param>
  885. /// <param name="conList"></param>
  886. /// <returns></returns>
  887. private string ModelToJsonToStr(string itemType, string itemLogical, string description, ObservableCollection<BasPlcItemConfigDto> conList)
  888. {
  889. JsonModel prefixJsonModel = new JsonModel();
  890. prefixJsonModel.ItemType = itemType;
  891. prefixJsonModel.ItemLogical = itemLogical;
  892. prefixJsonModel.Description = description;
  893. prefixJsonModel.DetailInfo = new List<DetailPLC>();
  894. foreach (var item in conList)
  895. {
  896. DetailPLC plcItem = new DetailPLC();
  897. plcItem.PlcItem = item.PlcItem;
  898. plcItem.PlcAddress = item.PlcAddress;
  899. plcItem.PlcAddType = item.PlcAddType;
  900. plcItem.PlcValue = item.PlcValue;
  901. plcItem.RealValue = item.RealValue;
  902. plcItem.TestTime = item.TestTime;
  903. plcItem.TestResult = item.TestResult;
  904. plcItem.Remark = item.Remark;
  905. prefixJsonModel.DetailInfo.Add(plcItem);
  906. }
  907. string prefixJsonStr = JsonConvert.SerializeObject(prefixJsonModel);
  908. return prefixJsonStr;
  909. }
  910. #endregion
  911. #region 命令绑定
  912. public DelegateCommand CloseCommand { set; get; }
  913. public DelegateCommand StartCommand { set; get; }
  914. public DelegateCommand<object> StopOrContinueCommand { set; get; }
  915. #endregion
  916. #region 变量绑定
  917. /// <summary>
  918. /// 测试方案编码
  919. /// </summary>
  920. private string scheduleName;
  921. public string ScheduleName
  922. {
  923. get { return scheduleName; }
  924. set { scheduleName = value; RaisePropertyChanged(); }
  925. }
  926. /// <summary>
  927. /// 设备名称
  928. /// </summary>
  929. private string deviceName;
  930. public string DeviceName
  931. {
  932. get { return deviceName; }
  933. set { deviceName = value; RaisePropertyChanged(); }
  934. }
  935. /// <summary>
  936. /// 测试项名称
  937. /// </summary>
  938. private string testName;
  939. public string TestName
  940. {
  941. get { return testName; }
  942. set { testName = value; RaisePropertyChanged(); }
  943. }
  944. /// <summary>
  945. /// 测试项类型
  946. /// </summary>
  947. private List<string> testKinds = new List<string>();
  948. public List<string> TestKinds
  949. {
  950. get { return testKinds; }
  951. set { testKinds = value; RaisePropertyChanged(); }
  952. }
  953. /// <summary>
  954. ///
  955. /// </summary>
  956. private string selectTest;
  957. public string SelectTest
  958. {
  959. get { return selectTest; }
  960. set { selectTest = value; RaisePropertyChanged(); }
  961. }
  962. /// <summary>
  963. /// 判定类型(前置项)
  964. /// </summary>
  965. private List<string> beforeJudgeKinds = new List<string>();
  966. public List<string> BeforeJudgeKinds
  967. {
  968. get { return beforeJudgeKinds; }
  969. set { beforeJudgeKinds = value; RaisePropertyChanged(); }
  970. }
  971. private string beforeSelectJudge;
  972. public string BeforeSelectJudge
  973. {
  974. get { return beforeSelectJudge; }
  975. set { beforeSelectJudge = value; RaisePropertyChanged(); }
  976. }
  977. /// <summary>
  978. /// 逻辑判断
  979. /// </summary>
  980. private List<string> judgeLogicKinds = new List<string>();
  981. public List<string> JudgeLogicKinds
  982. {
  983. get { return judgeLogicKinds; }
  984. set { judgeLogicKinds = value; RaisePropertyChanged(); }
  985. }
  986. private string selectLogic;
  987. public string SelectLogic
  988. {
  989. get { return selectLogic; }
  990. set { selectLogic = value; RaisePropertyChanged(); }
  991. }
  992. private string beforeDetail;
  993. public string BeforeDetail
  994. {
  995. get { return beforeDetail; }
  996. set { beforeDetail = value; RaisePropertyChanged(); }
  997. }
  998. private ObservableCollection<BasPlcItemConfigDto> beforeConList;
  999. public ObservableCollection<BasPlcItemConfigDto> BeforeConList
  1000. {
  1001. get { return beforeConList; }
  1002. set { beforeConList = value; RaisePropertyChanged(); }
  1003. }
  1004. /// <summary>
  1005. /// 判定类型(输入项)
  1006. /// </summary>
  1007. private List<string> inJudgeKinds = new List<string>();
  1008. public List<string> InJudgeKinds
  1009. {
  1010. get { return inJudgeKinds; }
  1011. set { inJudgeKinds = value; RaisePropertyChanged(); }
  1012. }
  1013. private string selectInJudge;
  1014. public string SelectInJudge
  1015. {
  1016. get { return selectInJudge; }
  1017. set { selectInJudge = value; RaisePropertyChanged(); }
  1018. }
  1019. /// <summary>
  1020. /// 逻辑判断
  1021. /// </summary>
  1022. private List<string> inJudgeLogicKinds = new List<string>();
  1023. public List<string> InJudgeLogicKinds
  1024. {
  1025. get { return inJudgeLogicKinds; }
  1026. set { inJudgeLogicKinds = value; RaisePropertyChanged(); }
  1027. }
  1028. private string inSelectLogic;
  1029. public string InSelectLogic
  1030. {
  1031. get { return inSelectLogic; }
  1032. set { inSelectLogic = value; RaisePropertyChanged(); }
  1033. }
  1034. private string inDetail;
  1035. public string InDetail
  1036. {
  1037. get { return inDetail; }
  1038. set { inDetail = value; RaisePropertyChanged(); }
  1039. }
  1040. private ObservableCollection<BasPlcItemConfigDto> inConList;
  1041. public ObservableCollection<BasPlcItemConfigDto> InConList
  1042. {
  1043. get { return inConList; }
  1044. set { inConList = value; RaisePropertyChanged(); }
  1045. }
  1046. /// <summary>
  1047. /// 判定类型(结果项)
  1048. /// </summary>
  1049. private List<string> outJudgeKinds = new List<string>();
  1050. public List<string> OutJudgeKinds
  1051. {
  1052. get { return outJudgeKinds; }
  1053. set { outJudgeKinds = value; RaisePropertyChanged(); }
  1054. }
  1055. private string selectOutJudge;
  1056. public string SelectOutJudge
  1057. {
  1058. get { return selectOutJudge; }
  1059. set { selectOutJudge = value; RaisePropertyChanged(); }
  1060. }
  1061. /// <summary>
  1062. /// 逻辑判断
  1063. /// </summary>
  1064. private List<string> outJudgeLogicKinds = new List<string>();
  1065. public List<string> OutJudgeLogicKinds
  1066. {
  1067. get { return outJudgeLogicKinds; }
  1068. set { outJudgeLogicKinds = value; RaisePropertyChanged(); }
  1069. }
  1070. private string outSelectLogic;
  1071. public string OutSelectLogic
  1072. {
  1073. get { return outSelectLogic; }
  1074. set { outSelectLogic = value; RaisePropertyChanged(); }
  1075. }
  1076. private string outDetail;
  1077. public string OutDetail
  1078. {
  1079. get { return outDetail; }
  1080. set { outDetail = value; RaisePropertyChanged(); }
  1081. }
  1082. private ObservableCollection<BasPlcItemConfigDto> outConList;
  1083. public ObservableCollection<BasPlcItemConfigDto> OutConList
  1084. {
  1085. get { return outConList; }
  1086. set { outConList = value; RaisePropertyChanged(); }
  1087. }
  1088. private int stepIndex = 0;
  1089. public int StepIndex
  1090. {
  1091. get { return stepIndex; }
  1092. set { stepIndex = value; RaisePropertyChanged(); }
  1093. }
  1094. private Visibility proVisibility = Visibility.Hidden;
  1095. public Visibility ProVisibility
  1096. {
  1097. get { return proVisibility; }
  1098. set { proVisibility = value; RaisePropertyChanged(); }
  1099. }
  1100. private string stopOrContinueCont ="暂停";
  1101. public string StopOrContinueCont
  1102. {
  1103. get { return stopOrContinueCont; }
  1104. set { stopOrContinueCont = value; RaisePropertyChanged(); }
  1105. }
  1106. #endregion
  1107. }
  1108. }