123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314 |
- using AutoMapper;
- using BizService;
- using Microsoft.Extensions.Logging;
- using Model.Dto;
- using Model.Entities;
- using Newtonsoft.Json;
- using PLCTool.Common;
- using Prism.Commands;
- using Prism.Events;
- using Prism.Mvvm;
- using Prism.Services.Dialogs;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Xaml;
- using static System.Windows.Forms.VisualStyles.VisualStyleElement.ListView;
- namespace PLCTool.ViewModels.BusinessManageViewModel
- {
- public class AutoTestViewModel : BindableBase, IDialogAware
- {
- private readonly IDialogService _dialog;
- private readonly IEventAggregator _aggregator;
- private readonly IOptionConfigService _optionConfigService;
- private readonly IBasicPlcTestSchemeService _basicPlcTestSchemeService;
- private readonly IBasicPlcTestSchemeDtlService _basicPlcTestSchemeDtlService;
- private readonly IBizTestRecordService _iBizTestRecordService;
- private readonly IBizTestRecordDtlService _iBizTestRecordDtlService;
- private readonly IMapper _mapper;
- private readonly ILogger _logger;
- private List<OptionConfigDto> _optionConfigs;
- private DateTime startTime = DateTime.Now;
- private DateTime endTime = DateTime.Now;
- private int testStatus = 0; //测试状态
- private int testResult = 0; //测试结果
- private long schId = 0; //方案ID
- private long globalSchDetailId = 0; //测试方案明细ID
- private long selctStartDetailId = 0; //当前选择的第一个方案明细id
- private BizTestRecordDtlDto bizTestRecordDtlDto = new BizTestRecordDtlDto(); //测试方案明细表
- private const string TestMode = "自动测试";
- private int delayTime = 20;
- ManualResetEvent m = new ManualResetEvent(true); //实例化阻塞事件
- CancellationTokenSource tokensource = new CancellationTokenSource(); //声明令牌
- public AutoTestViewModel(IDialogService dialog, IEventAggregator aggregator, IOptionConfigService optionConfigService, IBasicPlcTestSchemeService basicPlcTestSchemeService, IBasicPlcTestSchemeDtlService basicPlcTestSchemeDtlService, IBizTestRecordService iBizTestRecordService, IBizTestRecordDtlService iBizTestRecordDtlService, IMapper mapper, ILogger logger)
- {
- _dialog = dialog;
- _aggregator = aggregator;
- _optionConfigService = optionConfigService;
- _basicPlcTestSchemeService = basicPlcTestSchemeService;
- _basicPlcTestSchemeDtlService = basicPlcTestSchemeDtlService;
- _iBizTestRecordDtlService = iBizTestRecordDtlService;
- _iBizTestRecordService = iBizTestRecordService;
- _mapper = mapper;
- _logger = logger;
- CloseCommand = new DelegateCommand(Close);
- StartCommand = new DelegateCommand(Start);
- StopOrContinueCommand = new DelegateCommand<object>(StopOrContinueMethod);
- CancelCommand = new DelegateCommand<object>(Cancel);
- BeforeConList = new ObservableCollection<BasPlcItemConfigDto>();
- InConList = new ObservableCollection<BasPlcItemConfigDto>();
- OutConList = new ObservableCollection<BasPlcItemConfigDto>();
- GetConfigOption();
- }
- private void Cancel(object obj)
- {
- tokensource.Cancel();
- }
- private void StopOrContinueMethod(object obj)
- {
- if(StopOrContinueCont=="暂停")
- {
- m.Reset(); //阻塞线程
- StopOrContinueCont = "继续";
- }
- else
- {
- if (StopOrContinueCont == "继续")
- {
- m.Set(); //继续线程
- StopOrContinueCont = "暂停";
- }
- }
-
- }
- #region idialog接口实现
- public string Title { set; get; } = "自动测试";
- public event Action<IDialogResult> RequestClose;
- public bool CanCloseDialog()
- {
- return true;
- }
- public void OnDialogClosed()
- {
- }
- public void OnDialogOpened(IDialogParameters parameters)
- {
- //编辑
- var getMsg = parameters.GetValues<long>("Key");
- ///值不为空,表示修改
- if (getMsg != null)
- {
- foreach (var item in getMsg)
- {
-
- selctStartDetailId= item;//测试方案明细ID
- int id = Convert.ToInt32(item);
- // 根据 测试方案明细主键ID 查找测试方案id 及方案名和设备名
- GetDetailInfo(id);
- }
- }
- }
- /// <summary>
- /// //根据 测试方案明细主键ID 查找测试方案id 及方案名和设备名
- /// </summary>
- /// <param name="id"></param>
- private void GetDetailInfo(int schDetailId)
- {
- //根据 测试方案明细主键ID 查找测试方案id 及方案名和设备名
- var findEntity = _basicPlcTestSchemeDtlService.Find(schDetailId);
- var findresult = _mapper.Map<bas_plc_test_scheme_dtl, BasicPlcTestSchemeDtlDto>(findEntity);
- if (findresult != null)
- {
- //根据方案id 在方案表中查找方案名 设备名
- schId = findresult.SchemeId.Value; //方案id,方便下一步查找
- int intSchId = Convert.ToInt32(findresult.SchemeId);
- var findSch = _basicPlcTestSchemeService.Find(intSchId);
- if (findSch != null)
- {
- ScheduleName = findSch.scheme_name;
- DeviceName = findSch.device_name;
- }
- TestName = findresult.ItemName;
- SelectTest = findresult.ItemType;
- //前置项解析
- string preconStr = findresult.Precondition.ToString();
- if (!string.IsNullOrEmpty(preconStr))
- {
- JsonModel preconditionModel = JsonConvert.DeserializeObject<JsonModel>(preconStr);
- BeforeSelectJudge = preconditionModel.ItemType;
- SelectLogic = preconditionModel.ItemLogical;
- BeforeDetail = preconditionModel.Description;
- BeforeConList.Clear();
- foreach (var detail in preconditionModel.DetailInfo)
- {
- BasPlcItemConfigDto basPlcItemConfigDto = new BasPlcItemConfigDto();
- basPlcItemConfigDto.Id = detail.Id;
- basPlcItemConfigDto.PlcItem = detail.PlcItem;
- basPlcItemConfigDto.PlcAddress = detail.PlcAddress;
- basPlcItemConfigDto.PlcAddType = detail.PlcAddType;
- basPlcItemConfigDto.PlcValue = detail.PlcValue;
- basPlcItemConfigDto.Remark = detail.Remark;
- BeforeConList.Add(basPlcItemConfigDto);
- }
- }
- //输入项解析
- string inStr = findresult.Action.ToString();
- if (!string.IsNullOrEmpty(inStr))
- {
- JsonModel inModel = JsonConvert.DeserializeObject<JsonModel>(inStr);
- SelectInJudge = inModel.ItemType;
- InSelectLogic = inModel.ItemLogical;
- InDetail = inModel.Description;
- InConList.Clear();
- foreach (var detail in inModel.DetailInfo)
- {
- BasPlcItemConfigDto basPlcItemConfigDto = new BasPlcItemConfigDto();
- basPlcItemConfigDto.Id = detail.Id;
- basPlcItemConfigDto.PlcItem = detail.PlcItem;
- basPlcItemConfigDto.PlcAddress = detail.PlcAddress;
- basPlcItemConfigDto.PlcAddType = detail.PlcAddType;
- basPlcItemConfigDto.PlcValue = detail.PlcValue;
- basPlcItemConfigDto.Remark = detail.Remark;
- InConList.Add(basPlcItemConfigDto);
- }
- }
- //输出项解析
- string outStr = findresult.JudgementResult.ToString();
- if (!string.IsNullOrEmpty(outStr))
- {
- JsonModel outModel = JsonConvert.DeserializeObject<JsonModel>(outStr);
- SelectOutJudge = outModel.ItemType;
- OutSelectLogic = outModel.ItemLogical;
- OutDetail = outModel.Description;
- OutConList.Clear();
- foreach (var detail in outModel.DetailInfo)
- {
- BasPlcItemConfigDto basPlcItemConfigDto = new BasPlcItemConfigDto();
- basPlcItemConfigDto.Id = detail.Id;
- basPlcItemConfigDto.PlcItem = detail.PlcItem;
- basPlcItemConfigDto.PlcAddress = detail.PlcAddress;
- basPlcItemConfigDto.PlcAddType = detail.PlcAddType;
- basPlcItemConfigDto.PlcValue = detail.PlcValue;
- basPlcItemConfigDto.Remark = detail.Remark;
- OutConList.Add(basPlcItemConfigDto);
- }
- }
- }
- }
- #endregion
- #region 私有发方法
- /// <summary>
- /// 增加或更新测试记录
- /// </summary>
- private void AddTestRecord()
- {
- //记录记录主表中是否有这个方案的记录,没有则添加
- var findRecord = _iBizTestRecordService.FindRecorddBySchname(ScheduleName);
- if (findRecord == null)
- {
- //状态status 没有赋值
- BizTestRecordDto bizTestRecordDto = new BizTestRecordDto();
- bizTestRecordDto.RecordName = ScheduleName + "_Record";
- bizTestRecordDto.SchemeName = ScheduleName;
- bizTestRecordDto.Tester = Appsession.UserName;
- bizTestRecordDto.StartTestTime = startTime;
- bizTestRecordDto.FinishTestTime = endTime;
- bizTestRecordDto.CreateBy = Appsession.UserName;
- bizTestRecordDto.CreateTime = DateTime.Now;
- bizTestRecordDto.UpdateBy = Appsession.UserName;
- bizTestRecordDto.UpdateTime = DateTime.Now;
- var testRecord = _mapper.Map<BizTestRecordDto, biz_test_record>(bizTestRecordDto);
- _iBizTestRecordService.Add(testRecord);
- }
- }
- /// <summary>
- /// 增加测试记录明细
- /// </summary>
- private void AddOrEditTesDtltRecord(long schDetailId)
- {
- //查找record ID
- long recordId = 0;
- var findRecordID = _iBizTestRecordService.FindRecorddBySchname(ScheduleName);
- if (findRecordID != null)
- {
- recordId = findRecordID.record_id;
- }
- testStatus = 99;//测试状态赋值为99
- //状态status 没有赋值
- //记录记录明细表中是否有这个方案明细ID的记录,没有则添加,有则更新记录状态
- var findRecordDetail = _iBizTestRecordDtlService.FindRecordDetailBySchDtlID(schDetailId);
- if (findRecordDetail == null)
- {
- BizTestRecordDtlDto newBizTestEwDetail = new BizTestRecordDtlDto();
- newBizTestEwDetail.RecordId = recordId;
- newBizTestEwDetail.SchemeDtlId = schDetailId;
- newBizTestEwDetail.StartTestTime = startTime;
- newBizTestEwDetail.FinishTestTime = endTime;
- newBizTestEwDetail.TestMode = TestMode;
- newBizTestEwDetail.Status = testStatus;
- newBizTestEwDetail.TestResult = GetTestResult();
- newBizTestEwDetail.CreateBy = Appsession.UserName;
- newBizTestEwDetail.CreateTime = startTime;
- newBizTestEwDetail.UpdateBy = Appsession.UserName;
- newBizTestEwDetail.UpdateTime = DateTime.Now;
- var testRecordDtl = _mapper.Map<BizTestRecordDtlDto, biz_test_record_dtl>(newBizTestEwDetail);
- _iBizTestRecordDtlService.Add(testRecordDtl);
- }
- else
- {
- bizTestRecordDtlDto.RecordDtlId = findRecordDetail.record_dtl_id;
- bizTestRecordDtlDto.RecordId = recordId;
- bizTestRecordDtlDto.SchemeDtlId = schDetailId;
- bizTestRecordDtlDto.StartTestTime = startTime;
- bizTestRecordDtlDto.FinishTestTime = endTime;
- bizTestRecordDtlDto.TestMode = TestMode;
- bizTestRecordDtlDto.Status = testStatus;
- bizTestRecordDtlDto.TestResult = GetTestResult();
- bizTestRecordDtlDto.CreateBy = Appsession.UserName;
- bizTestRecordDtlDto.CreateTime = startTime;
- bizTestRecordDtlDto.UpdateBy = Appsession.UserName;
- bizTestRecordDtlDto.UpdateTime = DateTime.Now;
- //更新时间
- bizTestRecordDtlDto.UpdateBy = Appsession.UserName;
- bizTestRecordDtlDto.UpdateTime = DateTime.Now;
- UpdateTesDtltRecord();
- }
- }
- /// <summary>
- /// 获取测试结果
- /// </summary>
- /// <returns></returns>
- private int GetTestResult()
- {
- //前置项
- int beforeResult = 0;
- int inResult = 0;
- int outResult = 0;
- if (SelectLogic == "OR")
- {
- var find = BeforeConList.FirstOrDefault(x => x.TestResult == "合格");
- if (find != null)
- {
- beforeResult = 1;
- }
- }
- else
- {
- if (SelectLogic == "AND" || SelectLogic == "ONLY")
- {
- var find = BeforeConList.FirstOrDefault(x => x.TestResult == "不合格");
- if (find != null)
- {
- beforeResult = 0;
- }
- }
- else
- {
- if (SelectLogic == "NULL")
- {
- beforeResult = 1;
- }
- }
- }
- //输入项
- if (InSelectLogic == "OR")
- {
- var find = InConList.FirstOrDefault(x => x.TestResult == "合格");
- if (find != null)
- {
- inResult = 1;
- }
- }
- else
- {
- if (InSelectLogic == "AND" || InSelectLogic == "ONLY")
- {
- var find = InConList.FirstOrDefault(x => x.TestResult == "不合格");
- if (find != null)
- {
- inResult = 0;
- }
- }
- else
- {
- if (InSelectLogic == "NULL")
- {
- inResult = 1;
- }
- }
- }
- //输出项
- if (OutSelectLogic == "OR")
- {
- var find = OutConList.FirstOrDefault(x => x.TestResult == "合格");
- if (find != null)
- {
- outResult = 1;
- }
- }
- else
- {
- if (OutSelectLogic == "AND" || OutSelectLogic == "ONLY")
- {
- var find = OutConList.FirstOrDefault(x => x.TestResult == "不合格");
- if (find != null)
- {
- outResult = 0;
- }
- }
- else
- {
- if (OutSelectLogic == "NULL")
- {
- outResult = 1;
- }
- }
- }
- if (beforeResult == 1 && inResult == 1 && outResult == 1)
- {
- return 1;
- }
- else
- {
- return 0;
- }
- }
- /// <summary>
- /// 更新测试记录
- /// </summary>
- private void UpdateTesDtltRecord()
- {
- var testRecordDtl = _mapper.Map<BizTestRecordDtlDto, biz_test_record_dtl>(bizTestRecordDtlDto);
- _iBizTestRecordDtlService.Edit(testRecordDtl);
- }
-
-
- /// <summary>
- /// 开始检测
- /// </summary>
- /// <param name="obj"></param>
- private void Start()
- {
-
- CancellationToken cancellationToken = tokensource.Token;
- StartEnalbe = false;
- Task.Run(async() => //模拟耗时任务
- {
- var basicSchDtls = _basicPlcTestSchemeDtlService.FindAllBySchId(schId)?.OrderBy(x => x.scheme_dtl_id);
- foreach (var item in basicSchDtls)
- {
-
- if (cancellationToken.IsCancellationRequested)
- {
- return;
- }
- m.WaitOne(); //当m等于true,才会往下执行,否则一直在此等待
- long schDetailId = item.scheme_dtl_id;
- //编号小于当前选择的就不测试了,
- if (schDetailId < selctStartDetailId)
- {
- continue;
- }
- globalSchDetailId = schDetailId;
- //当前选择的项不在更新显示,更新后面的显示
- if (selctStartDetailId!=schDetailId)
- {
-
- //更新下一个界面显示
- int id = Convert.ToInt32(globalSchDetailId);
- //界面显示
- System.Windows.Application.Current.Dispatcher.Invoke((delegate
- {
- GetDetailInfo(id);
- }));
- }
- _logger.LogInformation($"开始自动测试。方案名{ScheduleName},测试项{TestName}");
- await ExecTest();
- _logger.LogInformation($"自动测试完成。方案名{ScheduleName},测试项{TestName}");
- //最后一项必须要测试
- if (globalSchDetailId == basicSchDtls.Max(x => x.scheme_dtl_id))
- {
-
- MessageBox.Show("已是此测试方案最后一个测试项!", "确认", MessageBoxButton.OK, MessageBoxImage.Information);
- }
- }
- }, cancellationToken);//绑定令牌到多线程
- ///查找方案下的所有方案明细,并排序,
-
- }
- private async Task ExecTest()
- { //开始时间
- startTime = DateTime.Now;
- //增加测试记录主表
- AddTestRecord();
- //前置项
- bool preBool = TestPreceditionMethod();
- //输入项
- bool isAction = TestActionMethod();
- //输出项
- bool isJudgeResult = await TestJudgementMethod();
- //下一项
- StepIndex = 0;
- }
- /// <summary>
- /// 更新测试结果
- /// </summary>
- private void UpdateTestRecord()
- {
- }
- /// <summary>
- /// 测试前置项目
- /// </summary>
- private bool TestPreceditionMethod()
- {
- switch (BeforeSelectJudge)
- {
- case "人工判定":
- //弹出确认的对话框
- //前置项测试结果json转化
- string prefixJsonStr = ModelToJsonToStr(BeforeSelectJudge, SelectLogic, BeforeDetail, BeforeConList);
- //json字符串
- bizTestRecordDtlDto.PreconditionFinal = prefixJsonStr;
- bizTestRecordDtlDto.PreconditionResult = "合格";
- StepIndex = 1;
- //MessageBoxResult boxResult = MessageBox.Show(BeforeDetail, "确认", MessageBoxButton.OKCancel, MessageBoxImage.Information);
- //if (boxResult == MessageBoxResult.OK)
- //{
- // //前置项测试结果json转化
- // string prefixJsonStr = ModelToJsonToStr(BeforeSelectJudge, SelectLogic, BeforeDetail, BeforeConList);
- // //json字符串
- // bizTestRecordDtlDto.PreconditionFinal = prefixJsonStr;
- // bizTestRecordDtlDto.PreconditionResult = "合格";
- // StepIndex = 1;
- //}
- //else
- //{
- // return false;
- //}
- break;
- case "自动判定":
- //读取plc的值
- switch (SelectLogic)
- {
- case "NULL":
- //弹出确认的对话框
- //前置项测试结果json转化
- string prefixJsonStrAuto = ModelToJsonToStr(BeforeSelectJudge, SelectLogic, BeforeDetail, BeforeConList);
- //json字符串
- bizTestRecordDtlDto.PreconditionFinal = prefixJsonStrAuto;
- bizTestRecordDtlDto.PreconditionResult = "合格";
- StepIndex = 1;
- //MessageBoxResult boxResultAuto = MessageBox.Show(BeforeDetail, "确认", MessageBoxButton.OKCancel, MessageBoxImage.Information);
- //if (boxResultAuto == MessageBoxResult.OK)
- //{
- // //前置项测试结果json转化
- // string prefixJsonStr = ModelToJsonToStr(BeforeSelectJudge, SelectLogic, BeforeDetail, BeforeConList);
- // //json字符串
- // bizTestRecordDtlDto.PreconditionFinal = prefixJsonStr;
- // bizTestRecordDtlDto.PreconditionResult = "合格";
- // StepIndex = 1;
- //}
- //else
- //{
- // return false;
- //}
- break;
- case "AND":
- case "ONLY":
- case "OR":
- ReadPredice(SelectLogic);
- break;
- case "ORDER":
- break;
- }
- break;
- }
- //更新测试记录
- AddOrEditTesDtltRecord(globalSchDetailId);
- return true;
- }
- private void ReadPredice(string strLogic)
- {
- int countCond = 0;
- foreach (var item in BeforeConList.OrderBy(x => x.Id))
- {
- string plcAddress = item.PlcAddress;
- string plcAddType = item.PlcAddType;
- string plcValue = item.PlcValue;
- switch (plcAddType)
- {
- case "bool":
- string readResult = PLCCom.GetInstance().ReadPlcObject(plcAddress, VarType.Bit);
- ///实时值记录
- item.RealValue = readResult;
- item.TestTime = DateTime.Now;
- if (readResult.Trim() == plcValue.Trim())
- {
- item.TestResult = "合格";
- countCond++;
- }
- else
- {
- item.TestResult = "不合格";
- }
- break;
- case "word":
- break;
- }
- }
- //测试记录存入数据库
- //前置项测试结果json转化
- string prefixJsonStr = ModelToJsonToStr(BeforeSelectJudge, SelectLogic, BeforeDetail, BeforeConList);
- //json字符串
- bizTestRecordDtlDto.PreconditionFinal = prefixJsonStr;
- //条件满足
- switch (strLogic)
- {
- case "AND":
- case "ONLY":
- if ((countCond == BeforeConList.Count) && (countCond != 0))
- {
- bizTestRecordDtlDto.PreconditionResult = "合格";
- }
- else
- {
- bizTestRecordDtlDto.PreconditionResult = "不合格";
- }
- break;
- case "OR":
- if ((countCond != 0))
- {
- bizTestRecordDtlDto.PreconditionResult = "合格";
- }
- else
- {
- bizTestRecordDtlDto.PreconditionResult = "不合格";
- }
- break;
- }
- StepIndex = 1;
- }
- /// <summary>
- /// 输入项写入
- /// </summary>
- private bool TestActionMethod()
- {
- bool isActionResult = true;
- switch (SelectInJudge)
- {
- case "人工判定":
- //弹出确认的对话框
- MessageBoxResult boxResult = MessageBox.Show(InDetail, "确认", MessageBoxButton.OKCancel, MessageBoxImage.Information);
- if (boxResult == MessageBoxResult.OK)
- {
- //前置项测试结果json转化
- string actionJsonStr = ModelToJsonToStr(SelectInJudge, InSelectLogic, InDetail, InConList);
- //json字符串
- bizTestRecordDtlDto.ActionFinal = actionJsonStr;
- bizTestRecordDtlDto.ActionResult = "合格";
- StepIndex = 2;
- }
- else
- {
- isActionResult = false;
- }
- break;
- case "自动判定":
- //读取plc的值
- switch (InSelectLogic)
- {
- case "NULL":
- //弹出确认的对话框
- MessageBoxResult boxResultAction = MessageBox.Show(InDetail, "确认", MessageBoxButton.OKCancel, MessageBoxImage.Information);
- if (boxResultAction == MessageBoxResult.OK)
- {
- //前置项测试结果json转化
- string actionJsonStr = ModelToJsonToStr(SelectInJudge, InSelectLogic, InDetail, InConList);
- //json字符串
- bizTestRecordDtlDto.ActionFinal = actionJsonStr;
- bizTestRecordDtlDto.ActionResult = "合格";
- StepIndex = 2;
- }
- else
- {
- isActionResult = false;
- }
- break;
- case "AND":
- case "ONLY":
- case "OR":
- isActionResult = SendActionToPLC(InSelectLogic);
- break;
- case "ORDER":
- break;
- }
- break;
- }
- //更新测试记录
- //更新测试记录
- AddOrEditTesDtltRecord(globalSchDetailId);
- return isActionResult;
- }
- /// <summary>
- /// 发送命令给PLC
- /// </summary>
- private bool SendActionToPLC(string strActionLogic)
- {
- bool isAction = true;
- int countCond = 0;
- foreach (var item in InConList.OrderBy(x => x.Id))
- {
- string plcAddress = item.PlcAddress;
- string plcAddType = item.PlcAddType;
- string plcValue = item.PlcValue;
- switch (plcAddType)
- {
- case "bool":
- bool writeResult = PLCCom.GetInstance().WritePlcObject(plcAddress, VarType.Bit, plcValue);
- ///实时值记录
- item.RealValue = plcValue;
- item.TestTime = DateTime.Now;
- if (writeResult)
- {
- ///单个测试项合格
- item.TestResult = "合格";
- countCond++;
- }
- else
- {
- item.TestResult = "不合格";
- }
-
- break;
- case "word":
-
- bool writeWrordResult = PLCCom.GetInstance().WritePlcObject(plcAddress, VarType.Word, plcValue);
- ///实时值记录
- item.RealValue = plcValue;
- item.TestTime = DateTime.Now;
- if (writeWrordResult)
- {
- ///单个测试项合格
- item.TestResult = "合格";
- countCond++;
- }
- else
- {
- item.TestResult = "不合格";
- }
-
-
- break;
- }
- }
- if (!isAction)
- {
- return isAction;
- }
- //测试记录存入数据库
- //前置项测试结果json转化
- string actionJsonStr = ModelToJsonToStr(SelectInJudge, InSelectLogic, InDetail, InConList);
- //json字符串
- bizTestRecordDtlDto.ActionFinal = actionJsonStr;
- //条件满足
- //条件满足
- switch (strActionLogic)
- {
- case "AND":
- case "ONLY":
- if ((countCond == BeforeConList.Count) && (countCond != 0))
- {
- bizTestRecordDtlDto.ActionResult = "合格";
- }
- else
- {
- bizTestRecordDtlDto.ActionResult = "不合格";
- }
- break;
- case "OR":
- if ((countCond != 0))
- {
- bizTestRecordDtlDto.ActionResult = "合格";
- }
- else
- {
- bizTestRecordDtlDto.ActionResult = "不合格";
- }
- break;
- }
- StepIndex = 2;
- return isAction;
- }
- /// <summary>
- /// 结果项判定
- /// </summary>
- private async Task<bool> TestJudgementMethod()
- {
- bool isJudge = true;
- switch (SelectOutJudge)
- {
- case "人工判定":
- //弹出确认的对话框
- MessageBoxResult boxResult = MessageBox.Show(OutDetail, "确认", MessageBoxButton.OKCancel, MessageBoxImage.Information);
- if (boxResult == MessageBoxResult.OK)
- {
- bizTestRecordDtlDto.JudgementResult = "合格";
- StepIndex = 3;
- }
- else
- {
- isJudge = false;
- }
- break;
- case "自动判定":
- //读取plc的值
- switch (OutSelectLogic)
- {
- case "NULL":
- bizTestRecordDtlDto.JudgementResult = "合格";
- StepIndex = 3;
- break;
- case "AND":
- case "ONLY":
- case "OR":
- await ReadJudge(OutSelectLogic);
- break;
- case "ORDER":
- break;
- }
- break;
- }
- //更新测试记录
- //更新测试记录
- AddOrEditTesDtltRecord(globalSchDetailId);
- //判定是否为最后一项
- //long maxDtlId = _basicPlcTestSchemeDtlService.FindAllBySchId(schId).Select(x => x.scheme_dtl_id).Max();
- //if (globalSchDetailId == maxDtlId)
- //{
- // MessageBox.Show("已是此测试方案最后一个测试项!", "确认", MessageBoxButton.OK, MessageBoxImage.Information);
- //}
- return isJudge;
- }
- /// <summary>
- /// 读取结果
- /// </summary>
- private async Task ReadJudge(String strLogic)
- {
- int countCond = 0;
- foreach (var item in OutConList.OrderBy(x => x.Id))
- {
- string plcAddress = item.PlcAddress;
- string plcAddType = item.PlcAddType;
- string plcValue = item.PlcValue;
- switch (plcAddType)
- {
- case "bool":
- DateTime entryTime = DateTime.Now;
- string readResult = string.Empty;
- //一直读
- await Task.Run(async () =>
- {
- while (entryTime.AddMilliseconds(delayTime * 1000) > DateTime.Now)
- {
- //显示进度条
- System.Windows.Application.Current.Dispatcher.Invoke((delegate
- {
- ProVisibility = Visibility.Visible;
- }));
-
- //if (readResult == item.PlcValue)
- //{
- // break;
- //}
- await Task.Delay(1000);
- }
- });
- readResult = PLCCom.GetInstance().ReadPlcObject(plcAddress, VarType.Bit);
- ProVisibility = Visibility.Hidden;
- ///实时值记录
- item.RealValue = readResult;
- item.TestTime = DateTime.Now;
- if (readResult.Trim() == plcValue?.Trim())
- {
- item.TestResult = "合格";
- countCond++;
- }
- else
- {
- item.TestResult = "不合格";
- }
- break;
- case "word":
- DateTime entryTime2 = DateTime.Now;
- string readResult2 = string.Empty;
- //一直读
- await Task.Run(async () =>
- {
- while (entryTime2.AddMilliseconds(delayTime * 1000) > DateTime.Now)
- {
- //显示进度条
- System.Windows.Application.Current.Dispatcher.Invoke((delegate
- {
- ProVisibility = Visibility.Visible;
- }));
- readResult = PLCCom.GetInstance().ReadPlcObject(plcAddress, VarType.Word);
- if (readResult == item.PlcValue)
- {
- break;
- }
- await Task.Delay(1000);
- }
- });
- ProVisibility = Visibility.Hidden;
- ///实时值记录
- item.RealValue = readResult2;
- item.TestTime = DateTime.Now;
- if (readResult2.Trim() == plcValue?.Trim())
- {
- item.TestResult = "合格";
- countCond++;
- }
- else
- {
- item.TestResult = "不合格";
- }
- break;
-
- }
- }
- //测试记录存入数据库
- //输出项测试结果json转化
- string prefixJsonStr = ModelToJsonToStr(SelectOutJudge, OutSelectLogic, OutDetail, OutConList);
- //json字符串
- bizTestRecordDtlDto.JudgementResultFinal = prefixJsonStr;
- //条件满足
- switch (strLogic)
- {
- case "AND":
- case "ONLY":
- if ((countCond == OutConList.Count) && (countCond != 0))
- {
- bizTestRecordDtlDto.JudgementResult = "合格";
- }
- else
- {
- bizTestRecordDtlDto.JudgementResult = "不合格";
- }
- break;
- case "OR":
- if (countCond != 0)
- {
- bizTestRecordDtlDto.JudgementResult = "合格";
- }
- else
- {
- bizTestRecordDtlDto.JudgementResult = "不合格";
- }
- break;
- }
- StepIndex = 3;
- }
- /// <summary>
- /// 获取配置
- /// </summary>
- private void GetConfigOption()
- {
- var configList = _optionConfigService.QueryList();
- _optionConfigs = _mapper.Map<List<OptionConfig>, List<OptionConfigDto>>(configList);
- var tests = _optionConfigs.FindAll(x => x.TypeID == 1);
- TestKinds = new List<string>();
- foreach (var test in tests)
- {
- TestKinds.Add(test.ContentOption);
- }
- //测试项类型
- var judges = _optionConfigs.FindAll(x => x.TypeID == 2);
- foreach (var judge in judges)
- {
- BeforeJudgeKinds.Add(judge.ContentOption);
- InJudgeKinds.Add(judge.ContentOption);
- OutJudgeKinds.Add(judge.ContentOption);
- }
- //判定逻辑
- var logics = _optionConfigs.FindAll(x => x.TypeID == 3);
- foreach (var logic in logics)
- {
- JudgeLogicKinds.Add(logic.ContentOption);
- InJudgeLogicKinds.Add(logic.ContentOption);
- OutJudgeLogicKinds.Add(logic.ContentOption);
- }
- }
- /// <summary>
- /// 关闭按钮
- /// </summary>
- private void Close()
- {
- RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel));
- }
- /// <summary>
- /// mode转json
- /// </summary>
- /// <param name="itemType"></param>
- /// <param name="itemLogical"></param>
- /// <param name="description"></param>
- /// <param name="conList"></param>
- /// <returns></returns>
- private string ModelToJsonToStr(string itemType, string itemLogical, string description, ObservableCollection<BasPlcItemConfigDto> conList)
- {
- JsonModel prefixJsonModel = new JsonModel();
- prefixJsonModel.ItemType = itemType;
- prefixJsonModel.ItemLogical = itemLogical;
- prefixJsonModel.Description = description;
- prefixJsonModel.DetailInfo = new List<DetailPLC>();
- foreach (var item in conList)
- {
- DetailPLC plcItem = new DetailPLC();
- plcItem.Id = item.Id;
- plcItem.PlcItem = item.PlcItem;
- plcItem.PlcAddress = item.PlcAddress;
- plcItem.PlcAddType = item.PlcAddType;
- plcItem.PlcValue = item.PlcValue;
- plcItem.RealValue = item.RealValue;
- plcItem.TestTime = item.TestTime;
- plcItem.TestResult = item.TestResult;
- plcItem.Remark = item.Remark;
- prefixJsonModel.DetailInfo.Add(plcItem);
- }
- string prefixJsonStr = JsonConvert.SerializeObject(prefixJsonModel);
- return prefixJsonStr;
- }
- #endregion
- #region 命令绑定
- public DelegateCommand CloseCommand { set; get; }
- public DelegateCommand StartCommand { set; get; }
- public DelegateCommand<object> StopOrContinueCommand { set; get; }
- public DelegateCommand<object> CancelCommand { set; get; }
-
- #endregion
- #region 变量绑定
- /// <summary>
- /// 测试方案编码
- /// </summary>
- private string scheduleName;
- public string ScheduleName
- {
- get { return scheduleName; }
- set { scheduleName = value; RaisePropertyChanged(); }
- }
- /// <summary>
- /// 设备名称
- /// </summary>
- private string deviceName;
- public string DeviceName
- {
- get { return deviceName; }
- set { deviceName = value; RaisePropertyChanged(); }
- }
- /// <summary>
- /// 测试项名称
- /// </summary>
- private string testName;
- public string TestName
- {
- get { return testName; }
- set { testName = value; RaisePropertyChanged(); }
- }
- /// <summary>
- /// 测试项类型
- /// </summary>
- private List<string> testKinds = new List<string>();
- public List<string> TestKinds
- {
- get { return testKinds; }
- set { testKinds = value; RaisePropertyChanged(); }
- }
- /// <summary>
- ///
- /// </summary>
- private string selectTest;
- public string SelectTest
- {
- get { return selectTest; }
- set { selectTest = value; RaisePropertyChanged(); }
- }
- /// <summary>
- /// 判定类型(前置项)
- /// </summary>
- private List<string> beforeJudgeKinds = new List<string>();
- public List<string> BeforeJudgeKinds
- {
- get { return beforeJudgeKinds; }
- set { beforeJudgeKinds = value; RaisePropertyChanged(); }
- }
- private string beforeSelectJudge;
- public string BeforeSelectJudge
- {
- get { return beforeSelectJudge; }
- set { beforeSelectJudge = value; RaisePropertyChanged(); }
- }
- /// <summary>
- /// 逻辑判断
- /// </summary>
- private List<string> judgeLogicKinds = new List<string>();
- public List<string> JudgeLogicKinds
- {
- get { return judgeLogicKinds; }
- set { judgeLogicKinds = value; RaisePropertyChanged(); }
- }
- private string selectLogic;
- public string SelectLogic
- {
- get { return selectLogic; }
- set { selectLogic = value; RaisePropertyChanged(); }
- }
- private string beforeDetail;
- public string BeforeDetail
- {
- get { return beforeDetail; }
- set { beforeDetail = value; RaisePropertyChanged(); }
- }
- private ObservableCollection<BasPlcItemConfigDto> beforeConList;
- public ObservableCollection<BasPlcItemConfigDto> BeforeConList
- {
- get { return beforeConList; }
- set { beforeConList = value; RaisePropertyChanged(); }
- }
- /// <summary>
- /// 判定类型(输入项)
- /// </summary>
- private List<string> inJudgeKinds = new List<string>();
- public List<string> InJudgeKinds
- {
- get { return inJudgeKinds; }
- set { inJudgeKinds = value; RaisePropertyChanged(); }
- }
- private string selectInJudge;
- public string SelectInJudge
- {
- get { return selectInJudge; }
- set { selectInJudge = value; RaisePropertyChanged(); }
- }
- /// <summary>
- /// 逻辑判断
- /// </summary>
- private List<string> inJudgeLogicKinds = new List<string>();
- public List<string> InJudgeLogicKinds
- {
- get { return inJudgeLogicKinds; }
- set { inJudgeLogicKinds = value; RaisePropertyChanged(); }
- }
- private string inSelectLogic;
- public string InSelectLogic
- {
- get { return inSelectLogic; }
- set { inSelectLogic = value; RaisePropertyChanged(); }
- }
- private string inDetail;
- public string InDetail
- {
- get { return inDetail; }
- set { inDetail = value; RaisePropertyChanged(); }
- }
- private ObservableCollection<BasPlcItemConfigDto> inConList;
- public ObservableCollection<BasPlcItemConfigDto> InConList
- {
- get { return inConList; }
- set { inConList = value; RaisePropertyChanged(); }
- }
- /// <summary>
- /// 判定类型(结果项)
- /// </summary>
- private List<string> outJudgeKinds = new List<string>();
- public List<string> OutJudgeKinds
- {
- get { return outJudgeKinds; }
- set { outJudgeKinds = value; RaisePropertyChanged(); }
- }
- private string selectOutJudge;
- public string SelectOutJudge
- {
- get { return selectOutJudge; }
- set { selectOutJudge = value; RaisePropertyChanged(); }
- }
- /// <summary>
- /// 逻辑判断
- /// </summary>
- private List<string> outJudgeLogicKinds = new List<string>();
- public List<string> OutJudgeLogicKinds
- {
- get { return outJudgeLogicKinds; }
- set { outJudgeLogicKinds = value; RaisePropertyChanged(); }
- }
- private string outSelectLogic;
- public string OutSelectLogic
- {
- get { return outSelectLogic; }
- set { outSelectLogic = value; RaisePropertyChanged(); }
- }
- private string outDetail;
- public string OutDetail
- {
- get { return outDetail; }
- set { outDetail = value; RaisePropertyChanged(); }
- }
- private ObservableCollection<BasPlcItemConfigDto> outConList;
- public ObservableCollection<BasPlcItemConfigDto> OutConList
- {
- get { return outConList; }
- set { outConList = value; RaisePropertyChanged(); }
- }
- private int stepIndex = 0;
- public int StepIndex
- {
- get { return stepIndex; }
- set { stepIndex = value; RaisePropertyChanged(); }
- }
- private Visibility proVisibility = Visibility.Hidden;
- public Visibility ProVisibility
- {
- get { return proVisibility; }
- set { proVisibility = value; RaisePropertyChanged(); }
- }
- private string stopOrContinueCont ="暂停";
- public string StopOrContinueCont
- {
- get { return stopOrContinueCont; }
- set { stopOrContinueCont = value; RaisePropertyChanged(); }
- }
- private bool startEnalbe = true;
- public bool StartEnalbe
- {
- get { return startEnalbe; }
- set { startEnalbe = value; RaisePropertyChanged(); }
- }
-
- #endregion
- }
- }
|