public Dictionary <Tuple <long, DateTime>, DERMSCommon.SCADACommon.DayItem> ReadFromCollectTable(string connectionString) { DERMSCommon.SCADACommon.DayItem itemDay = null; Dictionary <Tuple <long, DateTime>, DERMSCommon.SCADACommon.DayItem> dayItems = new Dictionary <Tuple <long, DateTime>, DERMSCommon.SCADACommon.DayItem>(); Tuple <long, DateTime> key = null; Dictionary <Tuple <long, DateTime>, DERMSCommon.SCADACommon.CollectItem> collectItemsData = new Dictionary <Tuple <long, DateTime>, DERMSCommon.SCADACommon.CollectItem>(); using (System.Data.SqlClient.SqlConnection _con = new System.Data.SqlClient.SqlConnection(connectionString)) { using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("SELECT Timestamp, Gid, Production FROM dbo.Collect", _con)) { _con.Open(); using (System.Data.SqlClient.SqlDataReader reader = cmd.ExecuteReader()) { // Check is the reader has any rows at all before starting to read. if (reader.HasRows) { // Read advances to the next row. while (reader.Read()) { DERMSCommon.SCADACommon.CollectItem c = new DERMSCommon.SCADACommon.CollectItem(); // To avoid unexpected bugs access columns by name. try { c.Timestamp = reader.GetDateTime(reader.GetOrdinal("Timestamp")); c.Gid = reader.GetInt64(reader.GetOrdinal("Gid")); c.P = reader.GetDouble(reader.GetOrdinal("Production")); key = new Tuple <long, DateTime>(c.Gid, c.Timestamp); collectItemsData.Add(key, c); } catch (Exception e) { } } } } _con.Close(); } } foreach (var d in collectItemsData) { //itemDay = new DERMSCommon.SCADACommon.DayItem(d.Key.Item1, d.Key.Item2.Date.AddHours(d.Key.Item2.Hour), MinProductionPerHour(d.Key.Item2.Hour, d.Key.Item2.DayOfYear, collectItemsData), MaxProductionPerHour(d.Key.Item2.Hour, d.Key.Item2.DayOfYear, collectItemsData), AvgProductionPerHour(d.Key.Item2.Hour, d.Key.Item2.DayOfYear, collectItemsData), 0, 0); itemDay = new DERMSCommon.SCADACommon.DayItem(d.Key.Item1, d.Key.Item2.Date.AddHours(d.Key.Item2.Hour), MinProductionPerHour(d.Key.Item2.Hour, d.Key.Item2.DayOfYear, collectItemsData, d.Key.Item1), MaxProductionPerHour(d.Key.Item2.Hour, d.Key.Item2.DayOfYear, collectItemsData, d.Key.Item1), AvgProductionPerHour(d.Key.Item2.Hour, d.Key.Item2.DayOfYear, collectItemsData, d.Key.Item1), 0, d.Value.P); key = new Tuple <long, DateTime>(itemDay.Gid, itemDay.Timestamp); if (!dayItems.ContainsKey(key)) { dayItems.Add(key, itemDay); } } return(dayItems); }
private Dictionary <long, DERMSCommon.SCADACommon.CollectItem> ConvertDataPoints(List <DERMSCommon.SCADACommon.DataPoint> datapoints) { Dictionary <long, DERMSCommon.SCADACommon.CollectItem> collectItems = new Dictionary <long, DERMSCommon.SCADACommon.CollectItem>(); DERMSCommon.SCADACommon.CollectItem item = null; foreach (var dataPoint in datapoints) { item = new DERMSCommon.SCADACommon.CollectItem(dataPoint.Gid, 0, dataPoint.Timestamp); collectItems.Add(item.Gid, item); } return(collectItems); }
//private double MinProductionPerHour(int hour, int day, Dictionary<Tuple<long, DateTime>, DERMSCommon.SCADACommon.CollectItem> dayItems) //{ // double minPerHour = double.MaxValue; // foreach (var d in dayItems) // { // if (d.Key.Item2.DayOfYear.Equals(day) && d.Key.Item2.Hour.Equals(hour) && d.Value.P < minPerHour) // minPerHour = d.Value.P; // } // return minPerHour; //} //private double MaxProductionPerHour(int hour, int day, Dictionary<Tuple<long, DateTime>, DERMSCommon.SCADACommon.CollectItem> dayItems) //{ // double maxPerHour = double.MinValue; // foreach (var d in dayItems) // { // if (d.Key.Item2.DayOfYear.Equals(day) && d.Key.Item2.Hour.Equals(hour) && d.Value.P > maxPerHour) // maxPerHour = d.Value.P; // } // return maxPerHour; //} //private double AvgProductionPerHour(int hour, int day, Dictionary<Tuple<long, DateTime>, DERMSCommon.SCADACommon.CollectItem> dayItems) //{ // int counter = 0; // double sumPerHour = 0; // foreach (var d in dayItems) // { // if (d.Key.Item2.DayOfYear.Equals(day) && d.Key.Item2.Hour.Equals(hour)) // { // counter++; // sumPerHour += d.Value.P; // } // } // return sumPerHour / counter; //} public Dictionary <Tuple <long, DateTime>, DERMSCommon.SCADACommon.CollectItem> ConvertDataPoints(List <DataPoint> pointTypeToConfiguration) { Dictionary <Tuple <long, DateTime>, DERMSCommon.SCADACommon.CollectItem> collectItems = new Dictionary <Tuple <long, DateTime>, DERMSCommon.SCADACommon.CollectItem>(); Tuple <long, DateTime> key = null; DERMSCommon.SCADACommon.CollectItem item = null; foreach (var dataPoint in pointTypeToConfiguration) { if (dataPoint.Name == "Aqusition") { item = new DERMSCommon.SCADACommon.CollectItem(dataPoint.GidGeneratora, dataPoint.RawValue, dataPoint.Timestamp /*, dataPoint.Value.GidGeneratora*/); key = new Tuple <long, DateTime>(item.Gid, item.Timestamp); collectItems.Add(key, item); } } return(collectItems); }