ManualTestViewModel.cs 64 KB

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