using Yw.LCache.Medis; using Yw.Service.Basic; using Yw.Service.Monitor; namespace Yw.Server { /// /// 工位运行分析单任务 /// [DisallowConcurrentExecution]//此特性标识 必须等待这次任务执行完成后,才能执行下次任务 public class EMWSRunAnalySingleJob : IJob { internal const string Instance = "Instance"; private Yw.Model.RunAnalyConfigure _configure; /// /// /// public Task Execute(IJobExecutionContext context) { return Task.Run(() => { try { #region 任务数据 var dataMap = context.MergedJobDataMap; var jobModel = (Yw.Model.RunAnalyConfigure)dataMap[Instance]; if (jobModel == null) { LogHelper.Info($"工位运行分析单任务中,任务数据传输失败!"); return; } var configure = jobModel; _configure = configure; #endregion #region 服务准备 var service_work_site = new Lazy(() => new Yw.Service.EMWSWorkSite());//工位服务 var service_work_mapping = new Lazy(() => new Yw.Service.EMWSWorkMapping());//工位与测点映射服务 var service_monitor = new Lazy(() => new Yw.Service.MonitorPoint());//测点服务 var service_monitor_record = new Lazy(() => new Yw.Service.MonitorRealRecord());//测点记录服务 var service_run_record = new Lazy(() => new Yw.Service.RunRealRecord());//运行分析记录服务 #endregion #region 获取工位 var allWorKSiteList = service_work_site.Value.GetChildAndSelfByID(configure.ObjectID); if (allWorKSiteList == null || allWorKSiteList.Count < 1) { LogHelper.Info($"工位运行分析单任务中,获取工位数据失败!"); return; } var allWorkSiteIds = allWorKSiteList.Select(x => x.ID).Distinct().ToList(); var currentWorkSite = allWorKSiteList.Find(x => x.ID == configure.ObjectID); #endregion #region 获取测点 var allMonitorMappingList = service_work_mapping.Value.GetByWorkIdsAndMappingType(allWorkSiteIds, Yw.Monitor.DataType.MonitorPoint); if (allMonitorMappingList == null || allMonitorMappingList.Count < 1) { LogHelper.Info($"工位运行分析单任务中,工位名称:{currentWorkSite.Name},工位id:{currentWorkSite.ID},获取关联测点信息失败!"); return; } var allMonitorIds = allMonitorMappingList.Select(x => x.MappingID).Distinct().ToList(); var allMonitorList = service_monitor.Value.GetExSignalWithSignalTypeByIds(allMonitorIds); allMonitorList = allMonitorList?.Where(x => x.UseStatus == Yw.Model.eUseStatus.Enable).ToList(); allMonitorList = allMonitorList?.Where(x => x.CronType == Yw.Monitor.eCronType.Real).ToList(); if (allMonitorList == null || allMonitorList.Count < 1) { LogHelper.Info($"工位运行分析单任务中,工位名称:{currentWorkSite.Name},工位id:{currentWorkSite.ID},获取有效测点信息失败!"); return; } #endregion #region 运行测点 var signal = allMonitorList.Matching(Yw.Monitor.SignalType.运行状态, new List() { Yw.Run.Flags.默认 }); if (signal == null) { signal = allMonitorList.Matching(Yw.Monitor.SignalType.运行状态, null); } if (signal == null) { LogHelper.Info($"工位运行分析单任务中,设备id:{configure.ObjectID} 检索运行状态测点失败!"); return; } var monitor = allMonitorList.Find(x => x.ID == signal.MonitorPointID); #endregion #region 连续缓存 var lastRecord = new Yw.Service.MonitorRealRecord().GetLastRecord(signal.ID); EMWSRunAnalyChangeTimesHelper.Set(configure.ID, configure.ChangeTimes, monitor, lastRecord); #endregion #region 运行分析 var runRecord = new Yw.Model.RunRealRecord() { ObjectType = configure.ObjectType, ObjectID = configure.ObjectID, DataTime = DateTime.Now, RSa = Yw.Run.RunStatus.Shut, ContinueTime = 0, TotalShutTime = 0, TotalRunTime = 0, BootTimes = 0, AnalyStatus = Yw.Run.AnalyStatus.Normal, AnalyInfo = null }; var lastRunRecord = new Yw.Service.RunRealRecord().GetLastRecord(configure.ObjectType, configure.ObjectID); if (lastRunRecord == null) { runRecord.AnalyInfo = "首次分析"; } else { runRecord.RSa = lastRunRecord.RSa; runRecord.ContinueTime = lastRunRecord.ContinueTime; runRecord.TotalShutTime = lastRunRecord.TotalShutTime; runRecord.TotalRunTime = lastRunRecord.TotalRunTime; runRecord.BootTimes = lastRunRecord.BootTimes; } var rsa = Yw.Run.RunStatus.Shut; if (lastRecord == null) { runRecord.AnalyStatus = Yw.Run.AnalyStatus.Missing; } else { if (Yw.Monitor.DataStatus.HasError(lastRecord.DataStatus)) { runRecord.AnalyStatus = Yw.Run.AnalyStatus.Abnormal; } var enable_interrupt = Yw.Run.SysParas.EnableMonitorInterruptJudgement.GetPValue(); if (enable_interrupt.HasValue && enable_interrupt.Value) { if (monitor.IsInterrupt(lastRecord)) { runRecord.AnalyStatus = Yw.Run.AnalyStatus.Interrupt; } } if (int.TryParse(lastRecord.DataValue, out int intDataValue)) { rsa = intDataValue > Yw.Monitor.RunStatus.Shut ? Yw.Run.RunStatus.Run : Yw.Run.RunStatus.Shut; } } //发生改变 if (runRecord.RSa != rsa) { if (EquipmentRunAnalyChangeTimesHelper.HasChanged(configure.ID, configure.ChangeTimes)) { runRecord.RSa = rsa; runRecord.ContinueTime = 0; if (runRecord.RSa == Yw.Run.RunStatus.Run) { runRecord.BootTimes += 1; } } } //持续时间 runRecord.ContinueTime += configure.Frequency; //总关机时间 if (runRecord.RSa == Yw.Run.RunStatus.Shut) { runRecord.TotalShutTime += configure.Frequency; } //总运行时间 if (runRecord.RSa == Yw.Run.RunStatus.Run) { runRecord.TotalRunTime += configure.Frequency; } #endregion #region 数据存储 var bol = service_run_record.Value.InsertLastRecord(runRecord); if (bol) { LogHelper.Info($"工位运行分析单任务中,工位名称:{currentWorkSite.Name},工位id:{currentWorkSite.ID}, 运行记录分析成功!"); } else { LogHelper.Info($"工位运行分析单任务中,工位名称:{currentWorkSite.Name},工位id:{currentWorkSite.ID},运行记录保存失败!"); } #endregion } catch (Exception ex) { LogHelper.Error($"工位运行分析单任务中, 工位id:{_configure.ObjectID}, 执行出错", ex); var e = new JobExecutionException(ex); throw e; } }); } } }