You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

328 lines
12 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Mesnac.Action.Base;
using Mesnac.Controls.Base;
using System.Windows.Forms;
using Mesnac.Codd.Session;
using Mesnac.Equips;
using Mesnac.Equips.BaseInfo;
namespace Mesnac.Action.Default.SynchroData
{
#region 数据回放类
/// <summary>
/// 按倍速进行数据回放
/// </summary>
public class EquipReadDataSpeed : DatabaseAction, IAction
{
private class SelectParameters
{
public SelectParameters()
{
this.StartTime = null;
this.EndTime = null;
this.PlaySpeed = 1;
this.SearchCount = 0;
}
public DateTime? StartTime { get; set; }
public DateTime? EndTime { get; set; }
private double _playspeed = 1;
public double PlaySpeed
{
get
{
return _playspeed;
}
set
{
_playspeed = value;
if (_playspeed <= 0)
{
_playspeed = 0;
}
}
}
public int SearchCount { get; set; }
public object[] ToData()
{
object[] Result = new object[3];
Result[0] = this.StartTime;
Result[1] = this.EndTime;
Result[2] = this.PlaySpeed;
return Result;
}
}
public void Run(RuntimeParameter runtime)
{
base.RunIni(runtime);
SelectParameters par = new SelectParameters();
IBaseControl StartDate = this.GetAllFormMCControlByKey("StartDate").FirstOrDefault();
IBaseControl EndDate = this.GetAllFormMCControlByKey("EndDate").FirstOrDefault();
IBaseControl StartTime = this.GetAllFormMCControlByKey("StartTime").FirstOrDefault();
IBaseControl EndTime = this.GetAllFormMCControlByKey("EndTime").FirstOrDefault();
if (StartTime != null && EndTime != null && StartDate != null && EndDate != null)
{
par.StartTime = Convert.ToDateTime(Convert.ToDateTime(StartDate.MCValue).ToString("yyyy-MM-dd") + " " + Convert.ToDateTime(StartTime.MCValue).ToString("HH:mm:ss"));
par.EndTime = Convert.ToDateTime(Convert.ToDateTime(EndDate.MCValue).ToString("yyyy-MM-dd") + " " + Convert.ToDateTime(EndTime.MCValue).ToString("HH:mm:ss"));
}
double speed = 1;
if (double.TryParse(runtime.Parameters, out speed))
{
par.PlaySpeed = speed;
foreach (BaseEquip equip in Factory.Instance.AllEquips.Values)
{
if (equip.GetType().FullName.StartsWith("Mesnac.Equip.Display"))
{
equip.Write(0, 0, par.ToData());
}
}
}
}
}
#endregion
#region 回放数据存储
/// <summary>
/// 设备数据存储
/// </summary>
public class EquipAutoSaveData : DatabaseAction, IAction
{
private DbHelper dbHelper = null;
private DateTime dt_lastSave = DateTime.Now;
private int KeyMaxIndex = 0;
private Dictionary<string, int> KeyIndex = new Dictionary<string, int>();
private void RefreshKeyIndex(int ideep)
{
#region 添加所有的信息
if (KeyIndex.Count == 0)
{
foreach (BaseEquip equip in Mesnac.Equips.Factory.Instance.AllEquips.Values)
{
foreach (Group group in equip.Group.Values)
{
foreach (Data data in group.Data.Values)
{
if (data.IsSave)
{
KeyIndex.Add(data.KeyName, 0);
}
}
}
}
}
#endregion
#region 配置数据位置
//数据查询
string sqlstr = "SELECT * FROM PptVariable";
dbHelper.CommandType = CommandType.Text;
dbHelper.CommandText = sqlstr;
DataTable dt = dbHelper.ToDataTable();
foreach (DataRow dr in dt.Rows)
{
string key = dr["VariableName"].ToString();
int idx = Convert.ToInt32(dr["Seq"].ToString());
if (KeyIndex.ContainsKey(key))
{
KeyIndex[key] = idx;
if (idx > KeyMaxIndex)
{
KeyMaxIndex = idx;
}
}
}
List<string> noTableKey = new List<string>();
foreach (KeyValuePair<string, int> d in KeyIndex)
{
if (d.Value == 0)
{
noTableKey.Add(d.Key);
}
}
#endregion
#region 生成数据位置
if (ideep < 1 && noTableKey.Count > 0)
{
foreach (string keyName in noTableKey)
{
//插入语句
dbHelper.ClearParameter();
dbHelper.CommandText = "INSERT INTO PptVariable (VariableName) VALUES (@VariableName)";
dbHelper.AddParameter("@VariableName", keyName);
if (dbHelper.DbSession.ProviderFactory.GetType().FullName.Contains("OracleClientFactory"))
{
dbHelper.CommandText = "INSERT INTO PptVariable (VariableName) VALUES (:VariableName)";
dbHelper.AddParameter("VariableName", keyName);
}
dbHelper.ExecuteNonQuery();
}
ideep++;
RefreshKeyIndex(ideep);
}
#endregion
}
private string DataValue(Dictionary<string, object> data)
{
string[] dataList = new string[KeyMaxIndex + 1];
foreach (KeyValuePair<string, object> d in data)
{
int idx = 0;
if (d.Value != null && d.Value != DBNull.Value && KeyIndex.TryGetValue(d.Key, out idx))
{
if (idx > 0)
{
if (d.Value is DataTable)
{
dataList[idx] = (d.Value as DataTable).TableName;
}
else
{
dataList[idx] = d.Value.ToString();
}
}
}
}
string ss = ":";
StringBuilder Result = new StringBuilder();
for (int i = 0; i < dataList.Length; i++)
{
if (string.IsNullOrWhiteSpace(dataList[i]))
{
Result.Append(string.Empty).Append(ss);
}
else
{
Result.Append(dataList[i].Replace(ss, string.Empty)).Append(ss);
}
}
return Result.ToString();
}
private class RunningData
{
public RunningData(string data)
{
this.RecordTime = DateTime.Now;
this.Data = data;
}
public DateTime RecordTime { get; private set; }
public string Data { get; private set; }
}
private List<RunningData> SaveRunningDataList = new List<RunningData>();
private void ReadData(object sender, Mesnac.Equips.ReadEventArgs e)
{
//if (dt_lastSave.AddSeconds(1) < DateTime.Now)
{
dt_lastSave = DateTime.Now;
#region 数据转存
Dictionary<string, object> datas = new Dictionary<string, object>();
foreach (KeyValuePair<string, object> data in e.Data)
{
datas.Add(data.Key, data.Value);
}
string value = DataValue(datas);
//base.LogDebug(value);
SaveRunningDataList.Add(new RunningData(value));
if (SaveRunningDataList.Count < 20)
{
return;
}
#endregion
#region 数据转存
List<RunningData> saveData = new List<RunningData>();
foreach (RunningData d in SaveRunningDataList)
{
saveData.Add(d);
}
SaveRunningDataList.Clear();
#endregion
#region 组合执行语句
StringBuilder sqlstr = new StringBuilder("INSERT INTO PptRunningData(RecordTime,Data) VALUES ");
dbHelper.ClearParameter();
for (int i = 0; i < saveData.Count; i++)
{
RunningData d = saveData[i];
#region 过滤掉同一秒的重复记录
if (i > 0)
{
if (String.Format("{0:yyyyMMddHHmmss}", saveData[i - 1].RecordTime) == String.Format("{0:yyyyMMddHHmmss}", d.RecordTime))
{
continue;
}
}
#endregion
string key1 = "@RecordTime" + i.ToString();
string key2 = "@Data" + i.ToString();
if (dbHelper.DbSession.ProviderFactory.GetType().FullName.Contains("OracleClientFactory"))
{
key1 = ":RecordTime" + i.ToString();
key2 = ":Data" + i.ToString();
}
sqlstr.Append(" (").Append(key1).Append(",").Append(key2).Append(")");
if (i < saveData.Count - 1)
{
sqlstr.AppendLine(",");
}
dbHelper.AddParameter(key1, d.RecordTime);
dbHelper.AddParameter(key2, d.Data);
}
//插入语句
dbHelper.CommandText = sqlstr.ToString();
dbHelper.ExecuteNonQuery();
#endregion
}
}
private static bool OnlyRun = false;
public void Run(RuntimeParameter runtime)
{
if (OnlyRun)
{
return;
}
OnlyRun = true;
dbHelper = NewDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local);
dbHelper.OpenConnection(false);
RefreshKeyIndex(0);
#region 查找读取频率最小的设备
BaseEquip minRZEquip = null;
foreach (BaseEquip equip in Mesnac.Equips.Factory.Instance.AllEquips.Values)
{
if (minRZEquip == null)
{
minRZEquip = equip;
}
else
{
if (equip.Main.ReadHz < minRZEquip.Main.ReadHz)
{
minRZEquip = equip;
}
}
}
#endregion
//订阅读取频率最小设备的读取事件
if (minRZEquip != null)
{
minRZEquip.EquipReadData += ReadData;
}
}
}
#endregion
}