using Yw.Service.Basic;
|
using Yw.Service.Monitor;
|
|
namespace Yw.Server
|
{
|
/// <summary>
|
/// 设备运行分析单任务
|
/// </summary>
|
[DisallowConcurrentExecution]//此特性标识 必须等待这次任务执行完成后,才能执行下次任务
|
public class EquipmentRunAnalySingleJob : IJob
|
{
|
|
internal const string Instance = "Instance";
|
|
private Yw.Model.RunAnalyConfigure _configure;
|
|
/// <summary>
|
///
|
/// </summary>
|
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 allEquipmentList = new Yw.Service.Equipment().GetChildAndSelfByID(configure.ObjectID);
|
if (allEquipmentList == null || allEquipmentList.Count < 1)
|
{
|
LogHelper.Info($"设备运行分析单任务中,设备id:{configure.ObjectID} 检索设备失败!");
|
return;
|
}
|
var allEquipmentIds = allEquipmentList.Select(x => x.ID).Distinct().ToList();
|
|
#endregion
|
|
#region 获取测点
|
|
var allMonitorMappingList = new Yw.Service.EquipmentMonitorMapping().GetByEquipmentIds(allEquipmentIds);
|
if (allMonitorMappingList == null || allMonitorMappingList.Count < 1)
|
{
|
LogHelper.Info($"设备运行分析单任务中,设备id:{configure.ObjectID} 检索测点关联失败!");
|
return;
|
}
|
var allMonitorIds = allMonitorMappingList.Select(x => x.MonitorPointID).Distinct().ToList();
|
var allMonitorList = new Yw.Service.MonitorPoint().GetExSignalWithSignalTypeByIds(allMonitorIds);
|
allMonitorList = allMonitorList?.Where(x => x.UseStatus == Yw.Model.eUseStatus.Enable).ToList();
|
if (allMonitorList == null || allMonitorList.Count < 1)
|
{
|
LogHelper.Info($"设备运行分析单任务中,设备id:{configure.ObjectID} 检索测点失败!");
|
return;
|
}
|
|
#endregion
|
|
#region 运行测点
|
|
var signal = allMonitorList.Matching(Yw.Monitor.SignalType.运行状态, new List<string>() { 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);
|
EquipmentRunAnalyChangeTimesHelper.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<bool?>();
|
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 = new Yw.Service.RunRealRecord().InsertLastRecord(runRecord);
|
if (bol)
|
{
|
LogHelper.Info($"设备运行分析单任务中,设备id:{configure.ObjectID} 运行记录分析成功!");
|
}
|
else
|
{
|
LogHelper.Info($"设备运行分析单任务中,设备id:{configure.ObjectID} 运行记录保存失败!");
|
}
|
|
#endregion
|
}
|
catch (Exception ex)
|
{
|
LogHelper.Error($"设备运行分析单任务中, 数据id:{_configure.ObjectID}, 执行出错", ex);
|
var e = new JobExecutionException(ex);
|
throw e;
|
}
|
|
});
|
|
|
}
|
|
|
|
|
|
|
}
|
}
|