AutoTestViewModel.cs 48 KB

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