コード例 #1
0
ファイル: EpaAnalogData.cs プロジェクト: rj128x/EPADB
        public void readData(string fn, GraphPane graph)
        {
            SqlConnection con = DBSettings.getConnection();

            con.Open();
            Int64 timeStart = EPADB.GetIntDate(DateStart);
            Int64 timeEnd   = EPADB.GetIntDate(DateEnd);
            Dictionary <Int64, Dictionary <string, double> > Data = new Dictionary <long, Dictionary <string, double> >();

            Int64 time = timeStart;

            while (time <= timeEnd)
            {
                Data.Add(time, new Dictionary <string, double>());
                foreach (SignalInfo si in epa.SelectedAnalogSignals)
                {
                    Data[time].Add(si.KKS, double.NaN);
                }
                time += Step;
            }

            List <string>         kksQList     = new List <string>();
            List <int>            numSignQList = new List <int>();
            List <int>            timesQList   = new List <int>();
            Dictionary <int, int> timesDict    = new Dictionary <int, int>();
            List <int>            timesList    = new List <int>();

            foreach (int ss in SignalsBySubSys.Keys)
            {
                foreach (string table in tables)
                {
                    if (subSystemsByTables[table] != ss)
                    {
                        continue;
                    }
                    Status.Text = "Обработка таблицы " + table;
                    SqlCommand com = con.CreateCommand();
                    com.CommandText = String.Format("Select * from {0} where time_page>={1} and time_page<={2}", table.Replace("state", "time"), timeStart, timeEnd);
                    List <Int32> times = new List <int>();

                    try {
                        Status.Text += "---|";
                        SqlDataReader reader = com.ExecuteReader();
                        while (reader.Read())
                        {
                            try {
                                int tm = reader.GetInt32(0);
                                times.Add(tm);
                            }
                            catch { }
                        }
                        times.Sort();
                        reader.Close();

                        foreach (SignalInfo si in SignalsBySubSys[ss])
                        {
                            kksQList.Add("'" + si.KKS + "'");
                            numSignQList.Add(si.numSign);
                            if (kksQList.Count() <= 10 && si != SignalsBySubSys[ss].Last())
                            {
                                continue;
                            }
                            string kksQ = String.Join(",", kksQList);
                            string numQ = String.Join(",", numSignQList);
                            kksQList.Clear();
                            timesDict.Clear();
                            numSignQList.Clear();
                            timesQList.Clear();

                            foreach (int t in Data.Keys)
                            {
                                if (t >= times.First() && t <= times.Last())
                                {
                                    int valT = times.First(tempT => { return(tempT >= t); });
                                    if (valT - t < Step)
                                    {
                                        timesQList.Add(valT);
                                        timesDict.Add(valT, t);
                                    }
                                }
                                if (timesQList.Count < 100 && t != Data.Keys.Last())
                                {
                                    continue;
                                }
                                string timesQ = String.Join(",", timesQList);
                                timesQList.Clear();

                                try {
                                    com = con.CreateCommand();
                                    //com.CommandText = String.Format("Select kks_id_signal,time_page,data from {0} where time_page in ({2}) and kks_id_signal in ({1})", table, kksQ, timesQ);
                                    //com.CommandText = String.Format("Select kks_id_signal,time_page,data from {0} where time_page={2} and kks_id_signal = '{1}'", table, kks, valT);
                                    if (!epa.UseNumSignals)
                                    {
                                        com.CommandText = String.Format("Select kks_id_signal,time_page,data from {0} where time_page in ({2}) and kks_id_signal in ({1})", table, kksQ, timesQ);
                                    }
                                    else
                                    {
                                        com.CommandText = String.Format("Select kks_id_signal,time_page,data from {0} where time_page in ({2}) and num_sign in ({1})", table, numQ, timesQ);
                                    }

                                    Status.Text += "---|";
                                    reader       = com.ExecuteReader();
                                    while (reader.Read())
                                    {
                                        try {
                                            int    timeRes = reader.GetInt32(1);
                                            string kksAsu  = reader.GetString(0);
                                            double val     = reader.GetFloat(2);

                                            long resultTime = timesDict[timeRes];
                                            Data[resultTime][kksAsu] = val;
                                        }
                                        catch (Exception e) { Logger.Info(e.ToString()); }
                                    }
                                    reader.Close();
                                }
                                catch (Exception e) { Logger.Info(e.ToString()); }
                            }
                        }
                    }
                    catch (Exception e) { Logger.Info(e.ToString()); }
                }
            }
            con.Close();

            Status.Text = "Чтение завершено";
            List <string> thAsuList  = new List <string>();
            List <string> thTechList = new List <string>();

            foreach (SignalInfo si in epa.SelectedAnalogSignals)
            {
                thAsuList.Add(String.Format("<th>{0}</th>", si.ShortName));
                try {
                    string     kksTech = epa.ASUTechDict[si.KKS];
                    SignalInfo tech    = epa.FindSignal(epa.TechRoot, kksTech, null);
                    thTechList.Add(String.Format("<th>{0}</th>", tech.ShortName));
                }
                catch {
                    thTechList.Add("<th>-</th>");
                }
            }
            OutputData.writeToOutput(fn, String.Format("<table border='1'><tr><th rowspan='2'>Дата</th>{0}</tr><tr>{1}</tr>", string.Join(" ", thAsuList), string.Join(" ", thTechList)));
            foreach (int tm in Data.Keys)
            {
                OutputData.writeToOutput(fn, String.Format("<tr><th>{0}</th><td>{1}</td></tr>", EPADB.GetDate(tm), String.Join("</td><td>", Data[tm].Values)));
            }
            OutputData.writeToOutput(fn, "</table>");

            graph.CurveList.Clear();
            graph.XAxis.Scale.Min           = Data.Keys.First();
            graph.XAxis.Scale.Max           = Data.Keys.Last();
            graph.XAxis.Scale.MinAuto       = false;
            graph.XAxis.Scale.MaxAuto       = false;
            graph.XAxis.Title.IsVisible     = false;
            graph.YAxis.IsVisible           = false;
            graph.YAxis.Title.IsVisible     = false;
            graph.Legend.FontSpec.Size      = 6;
            graph.Legend.Location.X         = 0;
            graph.Legend.Location.Y         = 0;
            graph.Title.IsVisible           = false;
            graph.YAxis.Scale.FontSpec.Size = 6;
            graph.YAxis.IsVisible           = false;
            graph.XAxis.Scale.FontSpec.Size = 6;
            int index = 0;

            foreach (SignalInfo si in epa.SelectedAnalogSignals)
            {
                try {
                    string name  = si.ShortName;
                    int    axInd = graph.AddYAxis("");
                    graph.YAxisList[axInd].Color = Colors[index % 8];
                    graph.YAxisList[axInd].Scale.FontSpec.Size      = 6;
                    graph.YAxisList[axInd].Scale.FontSpec.FontColor = Colors[index % 8];

                    try {
                        string     kksTech = epa.ASUTechDict[si.KKS];
                        SignalInfo tech    = epa.FindSignal(epa.TechRoot, kksTech, null);
                        name = name + " (" + tech.ShortName + ")";
                    }
                    catch { }
                    PointPairList list = new PointPairList();
                    foreach (int tm in Data.Keys)
                    {
                        list.Add(new PointPair(tm, Data[tm][si.KKS]));
                    }
                    graph.AddCurve(name, list, Colors[index % 8], SymbolType.None);
                    graph.CurveList[index].YAxisIndex = axInd;
                }
                catch (Exception e) {
                    Logger.Info(e.ToString());
                }
                graph.AxisChange();
                index++;
            }
        }
コード例 #2
0
        public void readData(string fn)
        {
            OutputData.writeToOutput(fn, "<table border='1'><tr><th>Дата</th><th>kks_asu</th><th>kks_tech</th><th>name_asu</th><th>name_tech</th><th>state</th><th>source</th><th>subsys</th></tr>");
            SqlConnection con = DBSettings.getConnection();

            con.Open();
            Int64 timeStart = EPADB.GetIntDate(DateStart);
            Int64 timeEnd   = EPADB.GetIntDate(DateEnd);
            Dictionary <int, string> colors = new Dictionary <int, string>();

            colors.Add(0, "white");
            colors.Add(1, "PaleGreen");
            colors.Add(2, "LightSkyBlue");
            colors.Add(3, "LightCoral");
            SortedList <DateTime, string> outputData = new SortedList <DateTime, string>();

            foreach (string tab in tables)
            {
                SqlCommand com = con.CreateCommand();
                Status.Text = "Чтение данных из таблицы " + tab;
                int index = 0;
                try {
                    com.CommandText = String.Format("Select time,mcs,data,kks_id_signal,bsrc from {0} where time>={1} and time<={2}", tab, timeStart, timeEnd);
                    if (epa.SelectedDiscrSignals.Count > 0)
                    {
                        int           ss       = subSystemsByTables[tab];
                        List <string> kksList  = new List <string>();
                        List <int>    numsList = new List <int>();
                        foreach (SignalInfo si in SignalsBySubSys[ss])
                        {
                            kksList.Add("'" + si.KKS + "'");
                            numsList.Add(si.numSign);
                        }
                        if (!epa.UseNumSignals)
                        {
                            com.CommandText += String.Format(" and kks_id_signal in ({0})", String.Join(",", kksList));
                        }
                        else
                        {
                            com.CommandText += String.Format(" and num_sign in ({0})", String.Join(",", numsList));
                        }
                    }
                    SqlDataReader reader = com.ExecuteReader();
                    while (reader.Read())
                    {
                        try {
                            index++;
                            if (index % 1000 == 0)
                            {
                                Status.Text += "...|";
                            }
                            DateTime dt    = EPADB.GetDate(reader.GetInt32(0));
                            string   kks   = reader.GetString(3);
                            int      state = Int32.Parse(reader[2].ToString());
                            int      src   = Int32.Parse(reader[4].ToString());
                            int      mcs   = Int32.Parse(reader[1].ToString());
                            dt = dt.AddMilliseconds(mcs / 1000.0);
                            SignalInfo siAsu  = epa.FindSignal(epa.ASURoot, kks, null);
                            SignalInfo siTech = null;
                            try {
                                string kksTech = epa.ASUTechDict[kks];
                                siTech = epa.FindSignal(epa.TechRoot, kksTech, null);
                            }
                            catch { }
                            string color = "white";
                            try {
                                color = colors[state];
                            }
                            catch { }
                            string outStr = String.Format("<tr bgColor='{0}'><th>{1}</th><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td><td>{6}</td><td>{7}</td><td>{8}</td></tr>", color,
                                                          dt.ToString("dd.MM.yyyy HH:mm:ss,fff"), kks, siTech == null ? "-" : siTech.KKS, siAsu.ShortName, siTech == null ? "-" : siTech.ShortName,
                                                          EPADB.getSignalState(state, siAsu), EPADB.getSignalSrc(src), siAsu.subSys);
                            if (!outputData.ContainsKey(dt))
                            {
                                outputData.Add(dt, "");
                            }
                            outputData[dt] += "\r\n" + outStr;
                        }
                        catch (Exception e) {
                            Logger.Info(e.ToString());
                        }
                    }
                    reader.Close();
                }
                catch { }
            }
            con.Close();
            foreach (string str in outputData.Values)
            {
                OutputData.writeToOutput(fn, str);
            }
            OutputData.writeToOutput(fn, "</table>");
            Status.Text = "Отчет сформирован";
        }