duheng
8 天以前 4de480ec624d3b79ca690dc906e10cd2fbdc9be7
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
 
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace IStation.BLL
{
    /// <summary>
    /// 月监测数据集
    /// </summary>
    public class MonthSignalRecordPacket_Analy
    {
        #region Basic
 
        /// <summary>
        /// 一天秒数之和
        /// </summary>
        const int TotalSecondsOfDay = 86400;
 
        /// <summary>
        /// 计算容量
        /// </summary> 
        private int CalcuCapacity(int days, int frequency)
        {
            var monthlyTotalSeconds = days * TotalSecondsOfDay;
            var capacity = monthlyTotalSeconds / frequency + 1;
            return capacity;
        }
 
        /// <summary>
        /// 获取数据字典 (将符合当前时间的测点数据汇总到一个字典里 )
        /// </summary> 
        private Dictionary<long, double?> GetRecordDict(DateTime time, List<Model.SignalRecordPacket> signalRecordPackets, Dictionary<long, int> findIndexDict, Dictionary<long, double?> recordDict, int frequency, out bool success)
        {
            success = false;
            if (signalRecordPackets == null || signalRecordPackets.Count < 1)
                return default;
 
            foreach (var packet in signalRecordPackets)
            {
                var signalId = packet.SignalID;
                var records = packet.RecordList;
                var count = packet.RecordList.Count;
                recordDict[signalId] = null;
                for (int index = findIndexDict[signalId]; index < count; index++)
                {
                    var afterRecord = records[index];
                    if (afterRecord.Time > time)
                    {
                        if (index == 0)
                        {
                            success = true;
                            recordDict[signalId] = afterRecord.Value;
                        }
                        else
                        {
                            var frontRecord = records[index - 1];
                            var diffFrontSecond = (time - frontRecord.Time).TotalSeconds;
                            var diffAfterSecond = (afterRecord.Time - time).TotalSeconds;
                            if (diffFrontSecond <= diffAfterSecond)
                            {
                                if (Math.Abs(diffFrontSecond) <= frequency)
                                {
                                    success = true;
                                    recordDict[signalId] = frontRecord.Value;
                                }
                                findIndexDict[signalId] = index;
                                break;
                            }
                            else
                            {
                                if (Math.Abs(diffAfterSecond) <= frequency)
                                {
                                    success = true;
                                    recordDict[signalId] = afterRecord.Value;
                                }
                                findIndexDict[signalId] = index;
                                break;
 
                            }
                        }
                    }
                    findIndexDict[signalId] = index;
 
                }
            }
            return recordDict;
        }
 
 
        #endregion
 
 
 
        /// <summary>
        /// 获取月数据集合
        /// </summary>
        /// <param name="frequency">数据步长</param>
        /// <returns></returns>
        public List<Model.MonthSignalRecordPacket> Get(int frequency = 300)
        {
            var bllSummay = new BLL.MonitorDataSetSummary();
            var yearMonths = bllSummay.GetYearMonth();
            if (yearMonths == null || !yearMonths.Any())
                return default;
 
            var bllMonitorPoint = new BLL.MonitorPoint();
            var bllMonitorDataSet = new BLL.MonitorDataSet();
 
            var station = new BLL.Station().GetAll().First();
            var enginePumps = new BLL.Product().GetEnginePumpListByBelongTypeAndBelongID(IStation.ObjectType.Station, station.ID);
 
 
            var signalIds = new List<long>();
            var stationMonitorPoints = bllMonitorPoint.GetExSignalExSignalTypeByMappingTypeAndMappingID(IStation.ObjectType.Station, station.ID);
            if (stationMonitorPoints == null || !stationMonitorPoints.Any())
                return default;
            signalIds.AddRange(stationMonitorPoints.Select(x => x.SignalID));
 
            var pumpMonitorPointDict = new Dictionary<long, List<Model.MonitorPointExSignalExSignalType>>();
            foreach (var enginePump in enginePumps)
            {
                var pumpMonitorPoints = bllMonitorPoint.GetExSignalExSignalTypeByMappingTypeAndMappingID(IStation.ObjectType.Product, enginePump.ID);
                if (pumpMonitorPoints == null || !pumpMonitorPoints.Any())
                    continue;
                pumpMonitorPointDict.Add(enginePump.ID, pumpMonitorPoints);
                signalIds.AddRange(pumpMonitorPoints.Select(x => x.SignalID));
            }
 
 
            var changJiangWL = stationMonitorPoints.Find(x => x.TagName == "_0402010205002020001");
            var quShuiKouWL = stationMonitorPoints.Find(x => x.TagName == "_0402010204089903001");
            var qianChiWL = stationMonitorPoints.Find(x => x.TagName == "_0402010204089904009");
            var shuiKuWL = stationMonitorPoints.Find(x => x.TagName == "_0402010201030103006");
            var totalActiveEnergy1 = stationMonitorPoints.Find(x => x.TagName == "_0402010204040193043");
            var totalActiveEnergy2 = stationMonitorPoints.Find(x => x.TagName == "_0402010204040293027");
 
 
            var monthSignalRecordPackets = new List<Model.MonthSignalRecordPacket>(yearMonths.Count);
            foreach (var yearMonth in yearMonths)
            {
                var year = yearMonth.Year;
                var month = yearMonth.Month;
 
                var summaries = bllSummay.GetAllByDate(year, month);
                if (summaries == null || !summaries.Any())
                    continue;
 
                var minTime = summaries.Min(x => x.MinTime);
                var maxTime = summaries.Max(x => x.MaxTime);
                var totalSeconds = (int)(maxTime - minTime).TotalSeconds;
 
                var startDay = minTime;
                var days = (maxTime - minTime).Days;
                var capacity = CalcuCapacity(days, frequency);// 每个月的数据量
 
                var monthSignalRecordPacket = new Model.MonthSignalRecordPacket();
                monthSignalRecordPacket.Year = year;
                monthSignalRecordPacket.Month = month;
                monthSignalRecordPacket.StationSignalRecords = new List<Model.StationSignalRecord>(capacity);
 
                var findIndexDict = new Dictionary<long, int>();// 查找数据下标
                var recordDict = new Dictionary<long, double?>();// 数据字典
                signalIds.ForEach(x =>
                {
                    findIndexDict.Add(x, 0);
                    recordDict.Add(x, null);
                });
 
                var signalRecordPackets = bllMonitorDataSet.GetSignalRecordPacket(signalIds, year, month);
                if (signalRecordPackets == null || !signalRecordPackets.Any())
                    continue;
 
                for (int i = 0; i <= days; i++)  //按天整理数据
                {
                    var day = startDay.AddDays(i);
                    for (int seconds = 0; seconds < TotalSecondsOfDay; seconds += frequency) //00点的数据不要
                    {
                        var realTime = day.AddSeconds(seconds);
                        GetRecordDict(realTime, signalRecordPackets, findIndexDict, recordDict, frequency, out bool existRecord);
                        if (!existRecord)
                            continue;
 
                        var stationSignalRecord = new Model.StationSignalRecord();
                        stationSignalRecord.Time = realTime;
                        stationSignalRecord.ChangJiangWL = recordDict[changJiangWL.SignalID];
                        stationSignalRecord.QuShuiKouWL = recordDict[quShuiKouWL.SignalID];
                        stationSignalRecord.QianChiWL = recordDict[qianChiWL.SignalID];
                        stationSignalRecord.ShuiKuWL = recordDict[shuiKuWL.SignalID];
                        stationSignalRecord.TotalActiveEnergy1 = recordDict[totalActiveEnergy1.SignalID];
                        stationSignalRecord.TotalActiveEnergy2 = recordDict[totalActiveEnergy2.SignalID];
 
                        if (stationSignalRecord.ChangJiangWL == null && stationSignalRecord.QuShuiKouWL == null &&
                            stationSignalRecord.QianChiWL == null && stationSignalRecord.ShuiKuWL == null)
                            continue;
 
                        stationSignalRecord.PumpSignalRecords = new List<Model.PumpSignalRecord>();
                        foreach (var pumpMonitorPointItem in pumpMonitorPointDict)
                        {
                            var pumpMonitorPoint = pumpMonitorPointItem.Value;
                            var runStatus = pumpMonitorPoint.Find(x => x.Identifier == IStation.SignalType.运行状态);
                            var FlowRate = pumpMonitorPoint.Find(x => x.Identifier == IStation.SignalType.瞬时流量);
                            var TotalFlow = pumpMonitorPoint.Find(x => x.Identifier == IStation.SignalType.累积流量);
 
                            var InletPressure = pumpMonitorPoint.Find(x => x.Flags != null && x.Identifier == IStation.SignalType.压力 && x.Flags.Contains(IStation.LogicFlags.进口));
                            var OutletPressure = pumpMonitorPoint.Find(x => x.Flags != null && x.Identifier == IStation.SignalType.压力 && x.Flags.Contains(IStation.LogicFlags.出口));
                            var InstantaneousPower = pumpMonitorPoint.Find(x => x.Identifier == IStation.SignalType.有功功率);
                            var TotalActiveEnergy = pumpMonitorPoint.Find(x => x.Identifier == IStation.SignalType.有功电度);
 
 
                            var pumpSignalRecord = new Model.PumpSignalRecord();
                            pumpSignalRecord.EnginePumpID = pumpMonitorPointItem.Key;
                            pumpSignalRecord.RunStatus = (int?)recordDict[runStatus.SignalID] ?? 0;
                            pumpSignalRecord.FlowRate = recordDict[FlowRate.SignalID];
                            pumpSignalRecord.TotalFlow = recordDict[TotalFlow.SignalID];
                            pumpSignalRecord.InletPressure = recordDict[InletPressure.SignalID];
                            pumpSignalRecord.OutletPressure = recordDict[OutletPressure.SignalID];
                            pumpSignalRecord.InstantaneousPower = recordDict[InstantaneousPower.SignalID];
                            pumpSignalRecord.TotalActiveEnergy = recordDict[TotalActiveEnergy.SignalID];
 
                            stationSignalRecord.PumpSignalRecords.Add(pumpSignalRecord);
 
                            monthSignalRecordPacket.StationSignalRecords.Add(stationSignalRecord);
                        }
                        stationSignalRecord.PumpRunCount = stationSignalRecord.PumpSignalRecords.Where(x => x.RunStatus == IStation.RunStatus.Run).Count();
                    }
                }
 
                monthSignalRecordPackets.Add(monthSignalRecordPacket);
            }
 
 
            return monthSignalRecordPackets;
        }
 
        /// <summary>
        /// 分析月数据集合并保存
        /// </summary>
        /// <param name="frequency">数据步长</param>
        /// <returns></returns>
        public bool AnalysisAndSave(int frequency = 300)
        {
            var bll = new DAL.MonthSignalRecordPacket();
            bll.Clear();
 
            var monthSignalRecordPackets = Get(frequency);
            return bll.Save(monthSignalRecordPackets);
        }
 
    }
}