public List<BathLevel> GetBathLevel(int fusionId)
 {
     List<BathLevel> result = new List<BathLevel>();
     string sql = "SELECT BATHLEVEL_ID,HEAT_ID, to_char(INSERTTIME,'dd.mm.yyyy HH24:MI:SS'),VALUE FROM HEAT_BATHLEVEL WHERE HEAT_ID=" + fusionId.ToString();
     OracleDataReader reader = Execute(sql);
     while (reader.Read())
     {
         BathLevel bathLevel = new BathLevel();
         bathLevel.Id = int.Parse(CheckNubmerForNull(reader[0].ToString()));
         bathLevel.FusionId = int.Parse(CheckNubmerForNull(reader[1].ToString()));
         bathLevel.Date = DateTime.Parse(CheckDateForNull(reader[2].ToString()));
         bathLevel.Value = int.Parse(CheckNubmerForNull(reader[3].ToString()));
         result.Add(bathLevel);
     }
     return result;
 }
        public void LoadFusionFromFile(string fileName)
        {
            try
            {
                BinaryFormatter bf = new BinaryFormatter();
                FileInfo file = new FileInfo(fileName);
                FileStream fs = file.OpenRead();

                string heatNumber = file.Name.Replace(".dat", "");
                var deserializedObject = new object();
                try
                {
                    deserializedObject = bf.Deserialize(fs);
                }
                catch
                {
                }
                finally
                {
                    fs.Close();
                    fs.Dispose();
                }

                _heat = new Heat {Number = int.Parse(heatNumber)};
                if (deserializedObject is Heat)
                {
                    _heat = deserializedObject as Heat;
                }
                if (deserializedObject is Dictionary<DateTime, BaseEvent>)
                {
                    _heat.LanceHistory.Clear();
                    _heat.OffGasAnalysisHistory.Clear();
                    _heat.OffGasHistory.Clear();
                    _heat.BoilerWaterCoolingHistory.Clear();
                    _heat.IgnitionHistory.Clear();
                    _heat.SlagOutburstHistory.Clear();
                    Dictionary<DateTime, BaseEvent> events = deserializedObject as Dictionary<DateTime, BaseEvent>;
                    foreach (DateTime key in events.Keys)
                    {

                        switch (events[key].GetType().ToString())
                        {
                            case "Converter.LanceEvent":
                                _heat.LanceHistory.Add((LanceEvent)events[key]);
                                break;
                            case "Converter.OffGasAnalysisEvent":
                                _heat.OffGasAnalysisHistory.Add((OffGasAnalysisEvent)events[key]);
                                break;
                            case "Converter.OffGasEvent":
                                _heat.OffGasHistory.Add((OffGasEvent)events[key]);
                                break;
                            case "Converter.BoilerWaterCoolingEvent":
                                _heat.BoilerWaterCoolingHistory.Add((BoilerWaterCoolingEvent)events[key]);
                                break;
                            case "Converter.IgnitionEvent":
                                _heat.IgnitionHistory.Add((IgnitionEvent)events[key]);
                                break;
                            case "Converter.SlagOutburstEvent":
                                _heat.SlagOutburstHistory.Add((SlagOutburstEvent)events[key]);
                                break;
                        }
                    }
                }
                else if ((deserializedObject is List<BaseEvent>))
                {
                    _heat.LanceHistory.Clear();
                    _heat.OffGasAnalysisHistory.Clear();
                    _heat.OffGasHistory.Clear();
                    _heat.BoilerWaterCoolingHistory.Clear();
                    _heat.IgnitionHistory.Clear();
                    _heat.SlagOutburstHistory.Clear();
                    List<BaseEvent> events = deserializedObject as List<BaseEvent>;
                    foreach (BaseEvent e in events)
                    {

                        switch (e.GetType().ToString())
                        {
                            case "Converter.LanceEvent":
                                _heat.LanceHistory.Add((LanceEvent)e);
                                break;
                            case "Converter.OffGasAnalysisEvent":
                                _heat.OffGasAnalysisHistory.Add((OffGasAnalysisEvent)e);
                                break;
                            case "Converter.OffGasEvent":
                                _heat.OffGasHistory.Add((OffGasEvent)e);
                                break;
                            case "Converter.BoilerWaterCoolingEvent":
                                _heat.BoilerWaterCoolingHistory.Add((BoilerWaterCoolingEvent)e);
                                break;
                            case "Converter.IgnitionEvent":
                                _heat.IgnitionHistory.Add((IgnitionEvent)e);
                                break;
                            case "Converter.SlagOutburstEvent":
                                _heat.SlagOutburstHistory.Add((SlagOutburstEvent)e);
                                break;
                        }
                    }

                }

                CurrentFussion = m_Db.GetFusion(_heat.Number.ToString().Insert(2, "0"));
                CurrentListOfGas.Clear();

                //if (false)
                //{
                //    CurrentListOfGas.Clear();
                //    DateTime? lastEventTime = null;
                //    double ArSum = 0;
                //    double COSum = 0;
                //    double CO2Sum = 0;
                //    double N2Sum = 0;
                //    double H2Sum = 0;
                //    double O2Sum = 0;
                //    int counter = 0;
                //    foreach (OffGasAnalysisEvent ogaEvent in _heat.OffGasAnalysisHistory)
                //    {
                //        ArSum += ogaEvent.Ar;
                //        COSum += ogaEvent.CO;
                //        CO2Sum += ogaEvent.CO2;
                //        H2Sum += ogaEvent.H2;
                //        N2Sum += ogaEvent.N2;
                //        O2Sum += ogaEvent.O2;
                //        counter++;
                //        if (lastEventTime == null || lastEventTime.Value.AddSeconds(1) < key)
                //        {
                //            OffGas og = new OffGas();
                //            og.Ar = ArSum / counter;
                //            og.CO = COSum / counter;
                //            og.CO2 = CO2Sum / counter;
                //            og.H2 = H2Sum / counter;
                //            og.N2 = N2Sum / counter;
                //            og.O2 = O2Sum / counter;
                //            og.Date = key;
                //            foreach (DateTime offGasHistoryKey in _heat.OffGasHistory.Keys)
                //            {
                //                if (offGasHistoryKey.Hour == key.Hour &&
                //                   offGasHistoryKey.Minute == key.Minute &&
                //                   offGasHistoryKey.Second == key.Second)
                //                {
                //                    og.Temp = _heat.OffGasHistory[offGasHistoryKey].OffGasTemp;
                //                    og.Flow = (int)_heat.OffGasHistory[offGasHistoryKey].OffGasFlow;
                //                    break;
                //                }
                //            }
                //            if (og.Flow != 0)
                //            {
                //                CurrentListOfGas.Add(og);
                //            }
                //            lastEventTime = key;
                //            ArSum = 0;
                //            COSum = 0;
                //            CO2Sum = 0;
                //            N2Sum = 0;
                //            H2Sum = 0;
                //            O2Sum = 0;
                //            counter = 0;
                //        }
                //    }

                //    lastEventTime = null;
                //    CurrentListLance.Clear();
                //    # region Объявление переменных
                //    int HeightSum = 0;
                //    double O2FlowSum = 0;
                //    double O2PressureSum = 0;
                //    double O2VolSum = 0;
                //    double O2LeftLanceGewBaerSum = 0;
                //    double O2LeftLanceGewWeightSum = 0;
                //    int O2LeftLanceLeckSum = 0;
                //    double O2LeftLanceWaterInputSum = 0;
                //    double O2LeftLanceWaterOutputSum = 0;
                //    double O2LeftLanceWaterPressureSum = 0;
                //    double O2LeftLanceWaterTempInputSum = 0;
                //    double O2LeftLanceWaterTempOutputSum = 0;
                //    double O2RightLanceGewBaerSum = 0;
                //    double O2RightLanceGewWeightSum = 0;
                //    int O2RightLanceLeckSum = 0;
                //    double O2RightLanceWaterInputSum = 0;
                //    double O2RightLanceWaterOutputSum = 0;
                //    double O2RightLanceWaterPressureSum = 0;
                //    double O2RightLanceWaterTempInputSum = 0;
                //    double O2RightLanceWaterTempOutputSum = 0;
                //    #endregion

                //    counter = 0;
                //    foreach (LanceEvent lEvent in  _heat.LanceHistory)
                //    {
                //        # region Суммирование
                //        HeightSum += lEvent.LanceHeight;
                //        O2FlowSum += lEvent.O2Flow;
                //        O2PressureSum += lEvent.O2Pressure;
                //        O2VolSum += lEvent.O2TotalVol;
                //        O2LeftLanceGewBaerSum += lEvent.O2LeftLanceGewBaer;
                //        O2LeftLanceGewWeightSum += lEvent.O2LeftLanceGewWeight;
                //        O2LeftLanceLeckSum += lEvent.O2LeftLanceLeck;
                //        O2LeftLanceWaterInputSum += lEvent.O2LeftLanceWaterInput;
                //        O2LeftLanceWaterOutputSum += lEvent.O2LeftLanceWaterOutput;
                //        O2LeftLanceWaterPressureSum += lEvent.O2LeftLanceWaterPressure;
                //        O2LeftLanceWaterTempInputSum += lEvent.O2LeftLanceWaterTempInput;
                //        O2LeftLanceWaterTempOutputSum += lEvent.O2LeftLanceWaterTempOutput;
                //        O2RightLanceGewBaerSum += lEvent.O2RightLanceGewBaer;
                //        O2RightLanceGewWeightSum += lEvent.O2RightLanceGewWeight;
                //        O2RightLanceLeckSum += lEvent.O2RightLanceLeck;
                //        O2RightLanceWaterInputSum += lEvent.O2RightLanceWaterInput;
                //        O2RightLanceWaterOutputSum += lEvent.O2RightLanceWaterOutput;
                //        O2RightLanceWaterPressureSum += lEvent.O2RightLanceWaterPressure;
                //        O2RightLanceWaterTempInputSum += lEvent.O2RightLanceWaterTempInput;
                //        O2RightLanceWaterTempOutputSum += lEvent.O2RightLanceWaterTempOutput;
                //        # endregion

                //        counter++;
                //        if (lastEventTime == null || lastEventTime.Value.AddSeconds(1) < key)
                //        {
                //            #region Создание объекта lance
                //            Lance lance = new Lance();
                //            lance.Date = key;
                //            lance.Height = HeightSum / counter;
                //            lance.O2Flow = O2FlowSum / counter;
                //            lance.O2Pressure = O2PressureSum / counter;
                //            lance.O2Vol = O2VolSum / counter;
                //            //lance.O2FlowMode = _heat.LanceHistory[key].O2FlowMode;
                //            lance.O2LeftLanceGewBaer = O2LeftLanceGewBaerSum / counter;
                //            lance.O2LeftLanceGewWeight = O2LeftLanceGewWeightSum / counter;
                //            lance.O2LeftLanceLeck = O2LeftLanceLeckSum / counter;
                //            lance.O2LeftLanceWaterInput = O2LeftLanceWaterInputSum / counter;
                //            lance.O2LeftLanceWaterOutput = O2LeftLanceWaterOutputSum / counter;
                //            lance.O2LeftLanceWaterPressure = O2LeftLanceWaterPressureSum / counter;
                //            lance.O2LeftLanceWaterTempInput = O2LeftLanceWaterTempInputSum / counter;
                //            lance.O2LeftLanceWaterTempOutput = O2LeftLanceWaterTempOutputSum / counter;
                //            lance.O2RightLanceGewBaer = O2RightLanceGewBaerSum / counter;
                //            lance.O2RightLanceGewWeight = O2RightLanceGewWeightSum / counter;
                //            lance.O2RightLanceLeck = O2RightLanceLeckSum / counter;
                //            lance.O2RightLanceWaterInput = O2RightLanceWaterInputSum / counter;
                //            lance.O2RightLanceWaterOutput = O2RightLanceWaterOutputSum / counter;
                //            lance.O2RightLanceWaterPressure = O2RightLanceWaterPressureSum / counter;
                //            lance.O2RightLanceWaterTempInput = O2RightLanceWaterTempInputSum / counter;
                //            lance.O2RightLanceWaterTempOutput = O2RightLanceWaterTempOutputSum / counter;
                //            #endregion
                //            //_heat.LanceHistory[key].O2LeftLanceWaterInput
                //            CurrentListLance.Add(lance);
                //            BathLevel bathLevel = new BathLevel();
                //            bathLevel.Date = key;
                //            bathLevel.Value = _heat.LanceHistory[key].BathLevel;
                //            CurrentListBathLevel.Add(bathLevel);
                //            lastEventTime = key;

                //            #region Обнуление суммарных значений и счетчика
                //            HeightSum = 0;
                //            O2FlowSum = 0;
                //            O2PressureSum = 0;
                //            O2VolSum = 0;
                //            O2LeftLanceGewBaerSum = 0;
                //            O2LeftLanceGewWeightSum = 0;
                //            O2LeftLanceLeckSum = 0;
                //            O2LeftLanceWaterInputSum = 0;
                //            O2LeftLanceWaterOutputSum = 0;
                //            O2LeftLanceWaterPressureSum = 0;
                //            O2LeftLanceWaterTempInputSum = 0;
                //            O2LeftLanceWaterTempOutputSum = 0;
                //            O2RightLanceGewBaerSum = 0;
                //            O2RightLanceGewWeightSum = 0;
                //            O2RightLanceLeckSum = 0;
                //            O2RightLanceWaterInputSum = 0;
                //            O2RightLanceWaterOutputSum = 0;
                //            O2RightLanceWaterPressureSum = 0;
                //            O2RightLanceWaterTempInputSum = 0;
                //            O2RightLanceWaterTempOutputSum = 0;
                //            counter = 0;
                //            #endregion
                //        }
                //    }
                //}
                //else
                {
                    DateTime first = _heat.OffGasAnalysisHistory.First().Time;
                    DateTime last = _heat.OffGasAnalysisHistory.Last().Time;

                    DateTime current = first.AddMilliseconds(-first.Millisecond).AddSeconds(-first.Second);
                    OffGas og = new OffGas();

                    SummaryIgnition = _heat.IgnitionHistory;
                    SummarySlagOutburst = _heat.SlagOutburstHistory;

                    foreach (OffGasAnalysisEvent ogaEvent in _heat.OffGasAnalysisHistory)
                    {
                        og = new OffGas();
                        og.Ar = ogaEvent.Ar;
                        og.CO = ogaEvent.CO;
                        og.CO2 = ogaEvent.CO2;
                        og.H2 = ogaEvent.H2;
                        og.N2 = ogaEvent.N2;
                        og.O2 = ogaEvent.O2;
                        og.Date = ogaEvent.Time;
                        ListOfAllGas.Add(og);
                    }

                    for (int i = 1; i < (last - first).TotalMinutes * 4; i++)
                    {
                        og.Date = current.AddSeconds(7);
                        var gasArray = _heat.OffGasAnalysisHistory.Where(x => x.Time >= current && x.Time < current.AddSeconds(15)).Select(x => x).ToArray();
                        if (gasArray.Length > 0)
                        {
                            og = new OffGas();
                            og.Ar = gasArray.Average(x => x.Ar);
                            og.CO = gasArray.Average(x => x.CO);
                            og.CO2 = gasArray.Average(x => x.CO2);
                            og.H2 = gasArray.Average(x => x.H2);
                            og.N2 = gasArray.Average(x => x.N2);
                            og.O2 = gasArray.Average(x => x.O2);
                            var gasFlowAndTempArray = _heat.OffGasHistory.Where(x => x.Time >= current && x.Time < current.AddSeconds(15)).Select(x => x).ToArray();
                            if (gasFlowAndTempArray.Length > 0)
                            {
                                og.Temperature = (int)gasFlowAndTempArray.Average(x => x.OffGasTemp);
                                og.Flow = (int)gasFlowAndTempArray.Average(x => x.OffGasFlow);
                            }
                            var gasTempsArray = _heat.BoilerWaterCoolingHistory.Where(x => x.Time >= current && x.Time < current.AddSeconds(15)).Select(x => x).ToArray();
                            if (gasTempsArray.Length > 0)
                            {
                                og.TemperatureOnExit = gasTempsArray.Average(x => x.GasTemperatureOnExit);
                                og.PrecollingTemperature = gasTempsArray.Average(x => x.PrecollingGasTemperature);
                                og.TemperatureAfter1Step = gasTempsArray.Average(x => x.GasTemperatureAfter1Step);
                                og.TemperatureAfter2Step = gasTempsArray.Average(x => x.GasTemperatureAfter2Step);
                            }
                        }
                        if (og.Flow != 0)
                        {
                            CurrentListOfGas.Add(og);
                        }
                        current = current.AddSeconds(15);
                    }

                    CurrentListLance.Clear();
                    CurrentListOfGas.Remove(CurrentListOfGas.Last());
                    current = CurrentListOfGas.First().Date.AddMilliseconds(-CurrentListOfGas.First().Date.Millisecond).AddSeconds(-CurrentListOfGas.First().Date.Second);
                    Lance lance = new Lance();
                    for (int i = 1; i < (CurrentListOfGas.Last().Date - CurrentListOfGas.First().Date).TotalMinutes * 4; i++)
                    {
                        current = current.AddSeconds(15);
                        var LanceEventArray = _heat.LanceHistory.Where(x => x.Time >= current && x.Time < current.AddSeconds(15)).Select(x => x).ToArray();
                        if (LanceEventArray.Length > 0)
                        {
                            lance = new Lance();
                            lance.Date = current.AddSeconds(7);
                            lance.Height = (int)LanceEventArray.Average(x => x.LanceHeight);
                            lance.O2Flow = LanceEventArray.Average(x => x.O2Flow);
                            lance.O2Pressure = LanceEventArray.Average(x => x.O2Pressure);
                            lance.O2Vol = LanceEventArray.Average(x => x.O2TotalVol);
                            lance.O2LeftLanceGewBaer = LanceEventArray.Average(x => x.O2LeftLanceGewBaer);
                            lance.O2LeftLanceGewWeight = LanceEventArray.Average(x => x.O2LeftLanceGewWeight);
                            lance.O2LeftLanceLeck = LanceEventArray.Average(x => x.O2LeftLanceLeck);
                            lance.O2LeftLanceWaterInput = LanceEventArray.Average(x => x.O2LeftLanceWaterInput);
                            lance.O2LeftLanceWaterOutput = LanceEventArray.Average(x => x.O2LeftLanceWaterOutput);
                            lance.O2LeftLanceWaterPressure = LanceEventArray.Average(x => x.O2LeftLanceWaterPressure);
                            lance.O2LeftLanceWaterTempInput = LanceEventArray.Average(x => x.O2LeftLanceWaterTempInput);
                            lance.O2LeftLanceWaterTempOutput = LanceEventArray.Average(x => x.O2LeftLanceWaterTempOutput);
                            lance.O2RightLanceGewBaer = LanceEventArray.Average(x => x.O2RightLanceGewBaer);
                            lance.O2RightLanceGewWeight = LanceEventArray.Average(x => x.O2RightLanceGewWeight);
                            lance.O2RightLanceLeck = LanceEventArray.Average(x => x.O2RightLanceLeck);
                            lance.O2RightLanceWaterInput = LanceEventArray.Average(x => x.O2RightLanceWaterInput);
                            lance.O2RightLanceWaterOutput = LanceEventArray.Average(x => x.O2RightLanceWaterOutput);
                            lance.O2RightLanceWaterPressure = LanceEventArray.Average(x => x.O2RightLanceWaterPressure);
                            lance.O2RightLanceWaterTempInput = LanceEventArray.Average(x => x.O2RightLanceWaterTempInput);
                            lance.O2RightLanceWaterTempOutput = LanceEventArray.Average(x => x.O2RightLanceWaterTempOutput);
                            if (CurrentListBathLevel.Count == 0)
                            {
                                BathLevel level = new BathLevel();
                                level.Value = (int)LanceEventArray.Average(x => x.BathLevel);
                                CurrentListBathLevel.Add(level);
                            }
                        }
                        if (lance.O2Flow > 0)
                        {
                            CurrentListLance.Add(lance);
                        }
                    }
                }
            }
            catch (Exception) { }
            CurrentListAddition.Clear();
            UpdateFusion();
        }