public Dictionary <int, ObservableCollection <HistoricalData> > GetHistoricalData(HistoricalDataCommand command, string sessionMsg, params int[] rids) { if (m_factory == null || rids.Length <= 0) { return(new Dictionary <int, ObservableCollection <HistoricalData> >()); } Dictionary <int, ObservableCollection <HistoricalData> > dic = new Dictionary <int, ObservableCollection <HistoricalData> >(); lock (this) { try { HashSet <int> set = new HashSet <int>(); foreach (int rid in rids) { set.Add(rid); } //查询开始打开连接 m_factory.DbConnection.Open(); string sql; DbDataReader reader;//snapshot sql = string.Format("select Rid,history(member =Eng,function ={0} ,stime = {1},etime ={2},interval = {3} msec,output = (date,value,all)) from Point where ", command.Func.ToString().ToLower(), command.StartTime.ToString("yyyy/MM/dd HH:mm:ss.fff"), command.EndTime.ToString("yyyy/MM/dd HH:mm:ss.fff"), command.Interval); bool isFirst = true; foreach (int rid in set) { if (isFirst) { sql += string.Format("Rid = {0} ", rid); isFirst = false; } else { sql += string.Format("or Rid = {0} ", rid); } } sql.TrimEnd(' '); Console.WriteLine(sql); m_factory.DbCommand.CommandText = sql; reader = m_factory.DbCommand.ExecuteReader(); while (reader.Read()) { int rid; if (!int.TryParse(reader["Rid"].ToString(), out rid)) { continue; } //创建存储该Pid点的集合 ObservableCollection <HistoricalData> list; if (!dic.TryGetValue(rid, out list)) { list = new ObservableCollection <HistoricalData>(); dic.Add(rid, list); } if (command.DataType == SampleDataType.Ana) { AnaHistoricalData data = new AnaHistoricalData(); data.Time = DateTime.Parse(reader["date"].ToString()); ushort s = (ushort)short.Parse(reader["status"].ToString()); data.State = (SCHistStatus)s; data.Value = (double)Convert.ChangeType(reader["value"], typeof(double)); list.Add(data); } else if (command.DataType == SampleDataType.Dig) { DigHistoricalData data = new DigHistoricalData(); data.Time = DateTime.Parse(reader["date"].ToString()); ushort s = (ushort)short.Parse(reader["status"].ToString()); data.State = (SCHistStatus)s; data.Value = (ushort)Convert.ChangeType(reader["value"], typeof(ushort)); list.Add(data); } else { break; } } reader.Close(); } catch (Exception ex) { Console.WriteLine(ex.Message); //查询完了关闭连接 m_factory.DbConnection.Close(); } finally { //查询完了关闭连接 m_factory.DbConnection.Close(); } } return(dic); }
public void LoadHistoricalData(HistoricalDataCommand command, string sessionMsg) { if (m_factory == null) { return; } lock (this) { try { //查询开始打开连接 m_factory.DbConnection.Open(); Console.WriteLine("Open hist database--------------------"); string field = "Eng"; if (command.DataType == SampleDataType.Ana) { field = "Eng"; } else if (command.DataType == SampleDataType.Dig) { field = "Cs"; } string sql = string.Format("select history(member ={0},function ={1} ,stime = {2},etime ={3},interval = {4} msec,output = (date,value,all)) from Point where Rid={5}", field, command.Func.ToString().ToLower(), command.StartTime.ToString("yyyy/MM/dd HH:mm:ss.fff"), command.EndTime.ToString("yyyy/MM/dd HH:mm:ss.fff"), command.Interval, command.Rid); Console.WriteLine(sql); m_factory.DbCommand.CommandText = sql; DbDataReader reader = m_factory.DbCommand.ExecuteReader(); int count = SystemConfig.MaxArrayLength; List <HistoricalData> list = new List <HistoricalData>(); while (reader.Read()) { if (command.DataType == SampleDataType.Ana) { AnaHistoricalData data = new AnaHistoricalData(); data.Time = DateTime.Parse(reader["date"].ToString()); ushort s = (ushort)short.Parse(reader["status"].ToString()); data.State = (SCHistStatus)s; data.Value = (double)Convert.ChangeType(reader["value"], typeof(double)); list.Add(data); } else if (command.DataType == SampleDataType.Dig) { DigHistoricalData data = new DigHistoricalData(); data.Time = DateTime.Parse(reader["date"].ToString()); ushort s = (ushort)short.Parse(reader["status"].ToString()); data.State = (SCHistStatus)s; data.Value = (ushort)Convert.ChangeType(reader["value"], typeof(ushort)); list.Add(data); } else { break; } if (count > 0) { count--; } else { //达到最大数组数量时发送事件 count = SystemConfig.MaxArrayLength; if (HistoricalDataLoaded != null) { HistoricalDataLoaded(this, new HistoricalDataEventArgs( command, list.ToArray(), sessionMsg, true)); Console.WriteLine(string.Format("send data, count = {0}", list.Count)); } list.Clear(); } } reader.Close(); //剩余数据发送事件 if (HistoricalDataLoaded != null) { HistoricalDataLoaded(this, new HistoricalDataEventArgs( command, list.ToArray(), sessionMsg, false)); Console.WriteLine(string.Format("send data, count = {0}", list.Count)); } } catch (Exception ex) { Console.WriteLine(ex.Message); //查询完了关闭连接 m_factory.DbConnection.Close(); Console.WriteLine("Close hist database--------------------"); } finally { //查询完了关闭连接 m_factory.DbConnection.Close(); Console.WriteLine("Close hist database--------------------"); } } }
public HistoricalData[] GetHistoricalData(HistoricalDataCommand command, string sessionMsg, string pid) { if (m_factory == null) { return(new HistoricalData[0]); } List <HistoricalData> list = new List <HistoricalData>(); lock (this) { try { //查询开始打开连接 m_factory.DbConnection.Open(); uint rid = 0; string sql; DbDataReader reader; if (pid.Trim() != string.Empty) { if (!m_pidDic.TryGetValue(pid.Trim(), out rid)) { rid = 0; } } else { rid = command.Rid; } if (rid > 0) { sql = string.Format("select history(member =Eng,function ={0} ,stime = {1},etime ={2},interval = {3} msec,output = (date,value,all)) from Ana where Rid={4}", command.Func.ToString().ToLower(), command.StartTime.ToString("yyyy/MM/dd HH:mm:ss.fff"), command.EndTime.ToString("yyyy/MM/dd HH:mm:ss.fff"), command.Interval, rid); Console.WriteLine(sql); m_factory.DbCommand.CommandText = sql; reader = m_factory.DbCommand.ExecuteReader(); while (reader.Read()) { if (command.DataType == SampleDataType.Ana) { AnaHistoricalData data = new AnaHistoricalData(); data.Time = DateTime.Parse(reader["date"].ToString()); ushort s = (ushort)short.Parse(reader["status"].ToString()); data.State = (SCHistStatus)s; data.Value = (double)Convert.ChangeType(reader["value"], typeof(double)); list.Add(data); } else if (command.DataType == SampleDataType.Dig) { DigHistoricalData data = new DigHistoricalData(); data.Time = DateTime.Parse(reader["date"].ToString()); ushort s = (ushort)short.Parse(reader["status"].ToString()); data.State = (SCHistStatus)s; //data.Value = (ushort)Convert.ChangeType(reader["value"], typeof(ushort)); data.Value = (ushort)double.Parse(reader["value"].ToString()); list.Add(data); } else { break; } } reader.Close(); } else { Console.WriteLine("无效的Pid或Rid:Pid={0},Rid={1}", pid, rid); } } catch (Exception ex) { Console.WriteLine(ex.Message); //查询完了关闭连接 m_factory.DbConnection.Close(); } finally { //查询完了关闭连接 m_factory.DbConnection.Close(); } } return(list.ToArray()); }