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 _dockList = null;//对接列表 private List _fileNameList = null;//文件列表 /// /// /// public void SetBindingData(List allDockingList, List 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>(); foreach (var item in allDockingList) { dict.Add(item.MonitorPointID, new List()); } 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(); 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() { 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 _dic = new Dictionary(); /// /// 处理数据 /// 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 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; } } }