duheng
2024-03-26 11c7908bcc2ca699e88b7a0a17bc32e45521c349
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
using DevExpress.XtraEditors; 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace IStation.WinFrmUI.Monitor
{
    public partial class WaitMonitorDataImportDlg : DevExpress.XtraEditors.XtraForm
    {
        public WaitMonitorDataImportDlg()
        {
            InitializeComponent();
            this.layoutControl1.SetupLayoutControl();
        }
 
        private List<MonitorDataImportModel> _dockList = null;//对接列表
        private List<string> _fileNameList = null;//文件列表
 
        /// <summary>
        /// 
        /// </summary>
        public void SetBindingData(List<MonitorDataImportModel> allDockingList, List<string> fileNameList)
        {
            if (allDockingList == null || allDockingList.Count < 1)
                return;
            if (fileNameList == null || fileNameList.Count < 1)
                return;
            _dockList = allDockingList;
            _fileNameList = fileNameList;
            this.pbcTotal.Properties.Minimum = 0;
            this.pbcTotal.Properties.Maximum = fileNameList.Count;
            this.pbcTotal.Position = 0;
            var task = Task.Run(() =>
            {
                var dict = new Dictionary<long, List<Model.SignalRecord>>();
                foreach (var item in allDockingList)
                {
                    dict.Add(item.MonitorPointID, new List<Model.SignalRecord>());
                }
                var tagName = Settings.DataImport.TagName;
                var dataTime = Settings.DataImport.DataTime;
                var dataValue = Settings.DataImport.DataValue;
                var bll_MonitorDataSet = new BLL.MonitorDataSet();
                foreach (var fileName in fileNameList)
                {
                    var tagNameIndex = Settings.DataImport.TagNameIndex;
                    var dataTimeIndex = Settings.DataImport.DataTimeIndex;
                    var dataValueIndex = Settings.DataImport.DataValueIndex;
                    try
                    {
                        using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
                        using (var sr = new StreamReader(fs, Encoding.UTF8))
                        {
                            var strLine = sr.ReadLine();
                            if (!string.IsNullOrEmpty(strLine))
                            {
                                var colStrList = strLine.Split(',').ToList();
                                var index = colStrList.IndexOf(tagName);
                                if (index > -1)
                                {
                                    tagNameIndex = index;
                                }
                                index = colStrList.IndexOf(dataTime);
                                if (index > -1)
                                {
                                    dataTimeIndex = index;
                                }
                                index = colStrList.IndexOf(dataValue);
                                if (index > -1)
                                {
                                    dataValueIndex = index;
                                }
 
                                while (!string.IsNullOrEmpty(strLine = sr.ReadLine()))
                                {
                                    var strList = strLine.Split(',');
                                    var dockingList = allDockingList.FindAll(x => x.TagName == strList[tagNameIndex]);
                                    if (dockingList == null || dockingList.Count < 1)
                                        continue;
                                    if (!DateTime.TryParse(strList[dataTimeIndex], out DateTime dataTimeValue))
                                        continue;
                                    if (!double.TryParse(strList[dataValueIndex], out double dataValueValue))
                                    {
                                        dataValueValue = IStation.Error.Default;
                                    }
                                    foreach (var docking in dockingList)
                                    {
                                        var content = new Model.SignalRecord();
                                        content.Time = dataTimeValue;
                                        content.Value = HandleDataValue(docking, dataValueValue);
                                        dict[docking.MonitorPointID].Add(content);
                                    }
                                }
                            }
                        }
 
                        SpinWait.SpinUntil(() => this.IsHandleCreated);
                        this.Invoke(new Action(() =>
                        {
                            this.pbcTotal.Position += 1;
                        }));
                    }
                    catch (Exception ex)
                    {
                        SpinWait.SpinUntil(() => this.IsHandleCreated);
                        this.Invoke(new Action(() =>
                        {
                            AlertTool.ShowInfo(this, "提示", ex.Message);
                        }));
                    }
                }
 
                if (dict.Values.Max(x => x.Count) < 1)
                {
                    SpinWait.SpinUntil(() => this.IsHandleCreated);
                    this.Invoke(new Action(() =>
                    {
                        AlertTool.ShowInfo(this, "提示", "未检索到有效监测记录!");
                        this.DialogResult = DialogResult.Cancel;
                        this.Close();
                    }));
                    return;
                }
 
                for (int i = 0; i < dict.Count;)
                {
                    var item = dict.ElementAt(i);
                    if (item.Value.Count > 0)
                    {
                        var monitorDataSetList = new List<Model.MonitorDataSet>();
 
                        var docking = allDockingList.Find(t => t.MonitorPointID == item.Key);
                        var monitorDataSetGroup = item.Value.GroupBy(x => new { x.Time.Year, x.Time.Month }).ToList();
                        foreach (var month in monitorDataSetGroup)
                        {
                            var monitorDataSet = new Model.MonitorDataSet();
                            monitorDataSet.Year = month.Key.Year;
                            monitorDataSet.Month = month.Key.Month;
                            monitorDataSet.MonitorPointID = docking.MonitorPointID;
                            monitorDataSet.PacketList = new List<Model.SignalRecordPacket>() {
                            new Model.SignalRecordPacket(){
                                    SignalID=docking.SignalId,
                                    RecordList=month.ToList()
                                }
                            };
                            monitorDataSetList.Add(monitorDataSet);
                        }
                        var bol = new BLL.MonitorDataSet().Save(monitorDataSetList);
                        if (!bol)
                        {
                            SpinWait.SpinUntil(() => this.IsHandleCreated);
                            this.Invoke(new Action(() =>
                            {
                                AlertTool.ShowInfo(this, "提示", $"[{item.Key}]保存失败!");
                            }));
                        }
                    }
                    dict.Remove(item.Key);
                }
 
                SpinWait.SpinUntil(() => this.IsHandleCreated);
                this.Invoke(new Action(() =>
                {
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }));
            });
        }
 
 
        private class DynamicExpresso
        {
            public string Expression { get; set; }
            //public Interpreter Interpreter { get; set; }
 
            //public double Out()
            //{
            //    return (double)this.Interpreter.Eval(this.Expression);
            //}
        }
 
        Dictionary<long, DynamicExpresso> _dic = new Dictionary<long, DynamicExpresso>();
 
        /// <summary>
        /// 处理数据
        /// </summary> 
        private double HandleDataValue(MonitorDataImportModel docking, double value)
        {
            if (value == IStation.Error.Default)
                return value;
 
            if (docking.Coefficients != null)
            {
                value = value * docking.Coefficients.A;
                /*if (_dic.ContainsKey(docking.MonitorPointID))
                {
                    value = _dic[docking.MonitorPointID].Out();
                }
                else
                {
                    docking.Coefficients.OutExpression(value, out string expression, out Dictionary<string, double> args);
                    var dynamic = new DynamicExpresso();
                    dynamic.Expression = expression;
                    dynamic.Interpreter = new Interpreter();
                    foreach (var item in args)
                    {
                        dynamic.Interpreter.SetVariable(item.Key, item.Value);
                    }
                    value = dynamic.Out();
 
                    _dic.Add(docking.MonitorPointID, dynamic);
                }*/
 
            }
            return value;
        }
 
 
 
    }
}