using IStation.Unit;
|
using System;
|
using System.Collections.Generic;
|
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();
|
layoutControl1.SetupLayoutControl();
|
}
|
|
|
private class DataDocking
|
{
|
public Model.eFormulaType FormulaType { get; set; }
|
public string FormulaParas { get; set; }
|
public List<Model.SignalRecord> Records { get; set; }
|
|
public void HandleDataValue()
|
{
|
if (Records.Count < 1)
|
return;
|
if (string.IsNullOrEmpty(FormulaParas))
|
return;
|
if (FormulaType == Model.eFormulaType.None)
|
return;
|
switch (FormulaType)
|
{
|
case Model.eFormulaType.Unit:
|
{
|
var paras = Model.UnitRatioMatrix.ToModel(FormulaParas);
|
var helper = UnitHelper.GetUnitHelper(paras.UnitType);
|
if (helper == null)
|
return;
|
for (int i = 0; i < Records.Count; i++)
|
{
|
var r = Records[i];
|
if (r.Value == IStation.Error.Default)
|
continue;
|
r.Value = helper.Convert(paras.FromUnit, paras.ToUnit, r.Value);
|
Records[i] = r;
|
}
|
}
|
break;
|
case Model.eFormulaType.Once:
|
{
|
var paras = Model.OnceRatioMatrix.ToModel(FormulaParas);
|
for (int i = 0; i < Records.Count; i++)
|
{
|
var r = Records[i];
|
if (r.Value == IStation.Error.Default)
|
continue;
|
r.Value = paras.Matrix(r.Value);
|
Records[i] = r;
|
}
|
}
|
break;
|
case Model.eFormulaType.Twice:
|
{
|
var paras = Model.OnceRatioMatrix.ToModel(FormulaParas);
|
for (int i = 0; i < Records.Count; i++)
|
{
|
var r = Records[i];
|
if (r.Value == IStation.Error.Default)
|
continue;
|
r.Value = paras.Matrix(r.Value);
|
Records[i] = r;
|
}
|
}
|
break;
|
case Model.eFormulaType.Single:
|
{
|
//待补充
|
}
|
break;
|
case Model.eFormulaType.Compare:
|
{
|
//待补充
|
}
|
break;
|
case Model.eFormulaType.Range:
|
{
|
//待补充
|
}
|
break;
|
default:
|
break;
|
}
|
}
|
}
|
|
/// <summary>
|
///
|
/// </summary>
|
public void SetBindingData(long monitorDataSourcesId, List<MonitorDataImportModel> allDockingList, List<string> fileNameList)
|
{
|
if (allDockingList == null || allDockingList.Count < 1)
|
return;
|
if (fileNameList == null || fileNameList.Count < 1)
|
return;
|
pbcTotal.Properties.Minimum = 0;
|
pbcTotal.Properties.Maximum = fileNameList.Count;
|
pbcTotal.Position = 0;
|
var task = Task.Run(() =>
|
{
|
var dict = new Dictionary<long, DataDocking>();
|
foreach (var item in allDockingList)
|
{
|
var dk = new DataDocking
|
{
|
FormulaType = item.FormulaType,
|
FormulaParas = item.FormulaParas,
|
Records = new List<Model.SignalRecord>()
|
};
|
dict.Add(item.MonitorPointID, dk);
|
}
|
var tagName = Settings.Scada.TagName;
|
var dataTime = Settings.Scada.DataTime;
|
var dataValue = Settings.Scada.DataValue;
|
var bll_MonitorDataSet = new BLL.MonitorDataSet();
|
foreach (var fileName in fileNameList)
|
{
|
var tagNameIndex = Settings.Scada.TagNameIndex;
|
var dataTimeIndex = Settings.Scada.DataTimeIndex;
|
var dataValueIndex = Settings.Scada.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
|
{
|
Time = dataTimeValue,
|
// content.Value = HandleDataValue(docking, dataValueValue);
|
Value = dataValueValue
|
};
|
dict[docking.MonitorPointID].Records.Add(content);
|
}
|
}
|
}
|
}
|
|
SpinWait.SpinUntil(() => IsHandleCreated);
|
Invoke(new Action(() =>
|
{
|
pbcTotal.Position += 1;
|
}));
|
}
|
catch (Exception ex)
|
{
|
SpinWait.SpinUntil(() => IsHandleCreated);
|
Invoke(new Action(() =>
|
{
|
AlertTool.ShowInfo(this, "提示", ex.Message);
|
}));
|
}
|
}
|
|
|
if (dict.Values.Max(x => x.Records.Count) < 1)
|
{
|
SpinWait.SpinUntil(() => IsHandleCreated);
|
Invoke(new Action(() =>
|
{
|
AlertTool.ShowInfo(this, "提示", "未检索到有效监测记录!");
|
DialogResult = DialogResult.Cancel;
|
Close();
|
}));
|
return;
|
}
|
|
for (int i = 0; i < dict.Count;)
|
{
|
var item = dict.ElementAt(i);
|
if (item.Value.Records.Count > 0)
|
{
|
item.Value.HandleDataValue();
|
var monitorDataSetList = new List<Model.MonitorDataSet>();
|
var docking = allDockingList.Find(t => t.MonitorPointID == item.Key);
|
|
|
var monitorDataSetGroup = item.Value.Records.GroupBy(x => new { x.Time.Year, x.Time.Month }).ToList();
|
foreach (var month in monitorDataSetGroup)
|
{
|
var monitorDataSet = new Model.MonitorDataSet
|
{
|
Year = month.Key.Year,
|
Month = month.Key.Month,
|
MonitorPointID = docking.MonitorPointID,
|
PacketList = new List<Model.SignalRecordPacket>()
|
{
|
new Model.SignalRecordPacket()
|
{
|
SignalID=docking.SignalId,
|
RecordList=month.ToList()
|
}
|
}
|
};
|
monitorDataSetList.Add(monitorDataSet);
|
}
|
var bol = new BLL.MonitorDataSet().Save(monitorDataSourcesId, monitorDataSetList);
|
if (!bol)
|
{
|
SpinWait.SpinUntil(() => IsHandleCreated);
|
Invoke(new Action(() =>
|
{
|
AlertTool.ShowInfo(this, "提示", $"[{item.Key}]保存失败!");
|
}));
|
}
|
}
|
dict.Remove(item.Key);
|
}
|
|
SpinWait.SpinUntil(() => IsHandleCreated);
|
Invoke(new Action(() =>
|
{
|
DialogResult = DialogResult.OK;
|
Close();
|
}));
|
});
|
}
|
|
|
|
/// <summary>
|
/// 处理数据
|
/// </summary>
|
private double HandleDataValue(MonitorDataImportModel docking, double value)
|
{
|
if (value == IStation.Error.Default)
|
return value;
|
|
if (docking.FormulaType != Model.eFormulaType.None && !string.IsNullOrEmpty(docking.FormulaParas))
|
{
|
|
|
}
|
return value;
|
}
|
|
|
}
|
}
|