AutoTestViewModel.cs 43 KB

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