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.
256 lines
8.9 KiB
C#
256 lines
8.9 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Data;
|
|
using Mesnac.Equips;
|
|
using Mesnac.Equips.BaseInfo;
|
|
using System.Runtime.InteropServices;
|
|
using System.Data.SqlClient;
|
|
using System.Collections.Generic;
|
|
using Mesnac.Codd.Session;
|
|
|
|
namespace Mesnac.Equip.Display.MSSQL.Database
|
|
{
|
|
public class Equip : BaseEquip
|
|
{
|
|
public Equip()
|
|
{
|
|
Factory.Instance.OnConfigChanged += new EventHandler(Instance_OnConfigChanged);
|
|
}
|
|
|
|
void Instance_OnConfigChanged(object sender, EventArgs e)
|
|
{
|
|
this.selectParameters = new SelectParameters(this);
|
|
this.selectParameters.SetReadHz = base.Main.ReadHz;
|
|
foreach (Group group in this.Group.Values)
|
|
{
|
|
foreach (Data data in group.Data.Values)
|
|
{
|
|
data.KeyName = data.Name;
|
|
}
|
|
}
|
|
}
|
|
|
|
private DbHelper dbHelper = null;
|
|
public override bool Open()
|
|
{
|
|
lock (this)
|
|
{
|
|
if (dbHelper == null)
|
|
{
|
|
string constr = ((Mesnac.Equips.Connection.Database.ConnType)this.Main.ConnType).ConnectionString;
|
|
DbSession dbsession = new DbSession(SqlClientFactory.Instance, constr);
|
|
dbHelper = new DbHelper(dbsession);
|
|
System.Data.Common.DbConnection conn = dbsession.ProviderFactory.CreateConnection();
|
|
conn.ConnectionString = constr;
|
|
this.Main.ConnType.Connection = conn;
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
private DataTable dtShowDisply = new DataTable();
|
|
private int RowIndex = 0;
|
|
private Dictionary<string, int> KeyIndex = new Dictionary<string, int>();
|
|
|
|
DateTime date_lasttable = DateTime.MinValue;
|
|
private object[] DisplyInfo()
|
|
{
|
|
if (RowIndex >= dtShowDisply.Rows.Count)
|
|
{
|
|
base.Main.ReadHz = selectParameters.SetReadHz;
|
|
return null;
|
|
}
|
|
object[] Result = new object[0];
|
|
string[] ssValues = new string[0];
|
|
|
|
for (int i = RowIndex; i < dtShowDisply.Rows.Count; i++)
|
|
{
|
|
DataRow drShowDisply = dtShowDisply.Rows[RowIndex];
|
|
RowIndex++;
|
|
string ss = ":";
|
|
DateTime date_table = (DateTime)drShowDisply["RecordTime"];
|
|
|
|
if ((date_table - date_lasttable).TotalMilliseconds > selectParameters.SetReadHz)
|
|
{
|
|
date_lasttable = date_table;
|
|
ssValues = drShowDisply["Data"].ToString().Split(ss.ToCharArray());
|
|
break;
|
|
}
|
|
}
|
|
if (ssValues.Length < 1)
|
|
return null;
|
|
#region 初始化数据
|
|
DataTable dt_value = new DataTable();
|
|
DataColumn dc_key = new DataColumn();
|
|
dc_key.ColumnName = "ssKey";
|
|
dc_key.DataType = typeof(string);
|
|
dt_value.Columns.Add(dc_key);
|
|
DataColumn dc_value = new DataColumn();
|
|
dc_value.ColumnName = "ssValue";
|
|
dc_value.DataType = typeof(string);
|
|
dt_value.Columns.Add(dc_value);
|
|
#endregion
|
|
|
|
foreach (KeyValuePair<string, int> key in KeyIndex)
|
|
{
|
|
DataRow dr = dt_value.NewRow();
|
|
dr["ssKey"] = key.Key.Split('.')[2];
|
|
dr["ssValue"] = ssValues[key.Value];
|
|
dt_value.Rows.Add(dr);
|
|
}
|
|
Result = new object[dt_value.Rows.Count];
|
|
for (int i = 0; i < dt_value.Rows.Count; i++)
|
|
{
|
|
Result[i] = dt_value.Rows[i];
|
|
}
|
|
return Result;
|
|
}
|
|
public override bool Read(string block, int start, int len, out object[] buff)
|
|
{
|
|
lock (this)
|
|
{
|
|
if (KeyIndex.Count == 0)
|
|
{
|
|
buff = new object[len];
|
|
if (!this.Open())
|
|
{
|
|
return false;
|
|
}
|
|
dbHelper.CommandType = CommandType.Text;
|
|
dbHelper.ClearParameter();
|
|
dbHelper.CommandText = "SELECT * FROM PptVariable";
|
|
DataTable dt = dbHelper.ToDataTable();
|
|
foreach (DataRow dr in dt.Rows)
|
|
{
|
|
string key = dr["VariableName"].ToString();
|
|
int idx = Convert.ToInt32(dr["Seq"].ToString());
|
|
KeyIndex.Add(key, idx);
|
|
}
|
|
this.RowIndex = 0;
|
|
}
|
|
if (selectParameters.SearchCount == 0)
|
|
{
|
|
buff = new object[len];
|
|
if (!this.Open())
|
|
{
|
|
return false;
|
|
}
|
|
dbHelper.CommandType = CommandType.Text;
|
|
dbHelper.ClearParameter();
|
|
string sqlstr = "";
|
|
selectParameters.SearchCount++;
|
|
if (selectParameters.StartTime != null && selectParameters.EndTime != null)
|
|
{
|
|
sqlstr = "SELECT * FROM dbo.PptRunningData WHERE RecordTime>=@begintime AND RecordTime<=@endtime";
|
|
dbHelper.CommandText = sqlstr;
|
|
dbHelper.AddParameter("@begintime", selectParameters.StartTime);
|
|
dbHelper.AddParameter("@endtime", selectParameters.EndTime);
|
|
}
|
|
else
|
|
{
|
|
int iTop = 1000;
|
|
sqlstr = "SELECT TOP " + iTop.ToString() + " * FROM PptRunningData WHERE 1=1 ";
|
|
if (selectParameters.StartTime != null)
|
|
{
|
|
sqlstr = sqlstr + " AND RecordTime>=@begintime";
|
|
dbHelper.AddParameter("@begintime", selectParameters.StartTime);
|
|
}
|
|
dbHelper.CommandText = sqlstr;
|
|
}
|
|
this.dtShowDisply = dbHelper.ToDataTable();
|
|
this.RowIndex = 0;
|
|
}
|
|
buff = DisplyInfo();
|
|
if (buff == null)
|
|
{
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
|
|
private SelectParameters selectParameters = null;
|
|
private class SelectParameters
|
|
{
|
|
private Equip equip = null;
|
|
public SelectParameters(Equip equip)
|
|
{
|
|
this.StartTime = null;
|
|
this.EndTime = null;
|
|
this.PlaySpeed = 1;
|
|
this.SearchCount = 0;
|
|
this.equip = equip;
|
|
}
|
|
public DateTime? StartTime { get; set; }
|
|
public DateTime? EndTime { get; set; }
|
|
public int SetReadHz { get; set; }
|
|
private double _playspeed = 1;
|
|
public double PlaySpeed
|
|
{
|
|
get
|
|
{
|
|
return _playspeed;
|
|
}
|
|
set
|
|
{
|
|
if (this.equip != null)
|
|
{
|
|
if (value == 0)
|
|
{
|
|
this.equip.Main.ReadHz = int.MaxValue;
|
|
}
|
|
else
|
|
{
|
|
this.equip.Main.ReadHz = (int)(this.SetReadHz / value);
|
|
}
|
|
}
|
|
_playspeed = value;
|
|
}
|
|
}
|
|
public int SearchCount { get; set; }
|
|
}
|
|
|
|
public override bool Write(int block, int start, object[] buff)
|
|
{
|
|
if (buff.Length < 3)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (buff[0] == null || buff[0] == DBNull.Value)
|
|
{
|
|
selectParameters.StartTime = null;
|
|
}
|
|
else
|
|
{
|
|
selectParameters.StartTime = Convert.ToDateTime(buff[0].ToString());
|
|
}
|
|
|
|
if (buff[1] == null || buff[1] == DBNull.Value)
|
|
{
|
|
selectParameters.EndTime = null;
|
|
}
|
|
else
|
|
{
|
|
selectParameters.EndTime = Convert.ToDateTime(buff[1].ToString());
|
|
}
|
|
|
|
if (buff[2] == null || buff[2] == DBNull.Value)
|
|
{
|
|
selectParameters.PlaySpeed = 1;
|
|
}
|
|
else
|
|
{
|
|
selectParameters.PlaySpeed = Convert.ToDouble(buff[2].ToString());
|
|
}
|
|
selectParameters.SearchCount = 0;
|
|
RowIndex = 0;
|
|
date_lasttable = DateTime.MinValue;
|
|
return true;
|
|
}
|
|
public override void Close()
|
|
{
|
|
|
|
}
|
|
}
|
|
}
|