lixiaojun
2022-08-03 aa5aa380cb4e3f63b2e98c5e686b69becefce332
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
214
215
216
217
218
219
220
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace IStation.Calculation
{
    /// <summary>
    /// 监测报警计算辅助类
    /// </summary>
    public class MonitorAlarmCalcuHelper
    {
 
        /// <summary>
        /// 验证是否有报警
        /// </summary>
        public static bool Verify
            (
                Model.MonitorAlarmConfigure configure,//报警配置
                Model.MonitorBasicRecord record,//信号记录(可能为空)
                Func<Model.MonitorBasicRecord> getLastRecord,//获取最后一条记录
                Func<Model.MonitorBasicRecord> getFirstUnChangedRecord//获取第一条数据不变的记录
            )
        {
            var result = false;
            switch (configure.AlarmType)
            {
                case Alarm.Type_Upper://数据越上限
                    {
                        if (record != null)
                        {
                            if (double.TryParse(record.DataValue, out double dataValue))
                            {
                                if (!string.IsNullOrEmpty(configure.ThresholdValue))
                                {
                                    if (double.TryParse(configure.ThresholdValue, out double thresholdValue))
                                    {
                                        if (dataValue > thresholdValue)
                                        {
                                            result = true;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    break;
                case Alarm.Type_Lower://数据越下限
                    {
                        if (record != null)
                        {
                            if (double.TryParse(record.DataValue, out double dataValue))
                            {
                                if (!string.IsNullOrEmpty(configure.ThresholdValue))
                                {
                                    if (double.TryParse(configure.ThresholdValue, out double thresholdValue))
                                    {
                                        if (dataValue < thresholdValue)
                                        {
                                            result = true;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    break;
                case Alarm.Type_Lacked://缺数据
                    {
                        if (!string.IsNullOrEmpty(configure.ThresholdValue))
                        {
                            if (double.TryParse(configure.ThresholdValue, out double thresholdValue))
                            {
                                var lastRecord = getLastRecord();
                                if (lastRecord != null)
                                {
                                    var dataTime = record == null ? DateTime.Now : record.DataTime;
                                    var fValue = (int)Math.Floor(Math.Abs(dataTime.Subtract(lastRecord.DataTime).TotalSeconds));
                                    if (fValue > thresholdValue)
                                    {
                                        return true;
                                    }
                                }
                            }
                        }
                    }
                    break;
                case Alarm.Type_UnChanged://数据不变
                    {
                        if (record != null)
                        {
                            if (!string.IsNullOrEmpty(configure.ThresholdValue))
                            {
                                if (double.TryParse(configure.ThresholdValue, out double thresholdValue))
                                {
                                    var firstUnChangedRecord = getFirstUnChangedRecord();
                                    if (firstUnChangedRecord != null)
                                    {
                                        if (record.DataValue == firstUnChangedRecord.DataValue)
                                        {
                                            var fValue = (int)Math.Floor(Math.Abs(record.DataTime.Subtract(firstUnChangedRecord.DataTime).TotalSeconds));
                                            if (fValue > thresholdValue)
                                            {
                                                return true;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    break;
                case Alarm.Type_Mutation://数据突变
                    {
                        if (record != null)
                        {
                            if (double.TryParse(record.DataValue, out double dataValue))
                            {
                                if (!string.IsNullOrEmpty(configure.ThresholdValue))
                                {
                                    if (double.TryParse(configure.ThresholdValue, out double thresholdValue))
                                    {
                                        var lastRecord = getLastRecord();
                                        if (lastRecord != null)
                                        {
                                            if (double.TryParse(lastRecord.DataValue, out double lastDataValue))
                                            {
                                                var fValue = Math.Abs(dataValue - lastDataValue);
                                                var sub = fValue / lastDataValue * 100;
                                                if (sub > thresholdValue)
                                                {
                                                    return true;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    break;
                case Alarm.Type_UnReasonable://数据不合理
                    {
                        if (record != null)
                        {
                            if (double.TryParse(record.DataValue, out double dataValue))
                            {
                                if (!string.IsNullOrEmpty(configure.ThresholdValue))
                                {
                                    var thresholdModel = Model.MonitorAlarmConfigure.ReasonableSpace.ToModel(configure.ThresholdValue);
                                    if (thresholdModel != null)
                                    {
                                        if (!thresholdModel.Verify(dataValue))
                                        {
                                            result = true;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    break;
                case Alarm.Type_Interrupted://数据中断
                    {
                        if (record == null)
                        {
                            if (!string.IsNullOrEmpty(configure.ThresholdValue))
                            {
                                if (double.TryParse(configure.ThresholdValue, out double thresholdValue))
                                {
                                    var lastRecord = getLastRecord();
                                    if (lastRecord != null)
                                    {
                                        var fValue = (int)Math.Floor(Math.Abs(DateTime.Now.Subtract(lastRecord.DataTime).TotalSeconds));
                                        if (fValue > thresholdValue)
                                        {
                                            return true;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    break;
                default: break;
            }
            return result;
        }
 
        /// <summary>
        /// 创建
        /// </summary>
        public static Model.MonitorAlarmRecord Create
            (
                Model.MonitorPoint monitor,//测点
                Model.Signal signal,//信号(可能为空)
                Model.MonitorAlarmConfigure configure,//报警配置
                Model.MonitorBasicRecord record//记录(可能为空)
            )
        {
            var alarm = new Model.MonitorAlarmRecord();
            alarm.CorpID = configure.CorpID;
            alarm.ConfigureID = configure.ID;
            alarm.MonitorPointID = monitor.ID;
            alarm.SignalID = signal?.ID;
            alarm.AlarmType=configure.AlarmType;
            alarm.AlarmLevel= configure.AlarmLevel;
            alarm.ThresholdValue = configure.ThresholdValue;
            alarm.AlarmTime = DateTime.Now;
            alarm.AlarmContent = configure.AlarmType;
            alarm.DataTime = record?.DataTime;
            alarm.DataValue = record?.DataValue;
            alarm.HandleStatus = Alarm.HandleStatus_None;
            return alarm;
        }
 
 
    }
}