lixiaojun
2024-05-06 09e5635e174d3ee6e8b699f7d0d4f125d3e1c46f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
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;
                }
 
            });
 
 
        }
 
 
 
 
 
 
    }
}