Пример #1
0
        public static List <Balance> GetBalances(string fileName)
        {
            List <Source> sources = GetSources(fileName);
            XDocument     xml;

            try
            {
                xml = XDocument.Load(fileName);
            }
            catch (Exception ex)
            {
                formErrorMessage frm = new formErrorMessage("Операция: получение описаний балансов",
                                                            new Tuple <string, string>("Невозможно загрузить файл " + fileName, ex.Message));
                frm.ShowDialog();
                return(null);
            }
            List <Balance> result = new List <Balance>();

            try
            {
                foreach (XElement element in xml.Descendants("balance"))
                {
                    List <BalanceComponent> components = new List <BalanceComponent>();
                    foreach (XElement component in element.Descendants("component"))
                    {
                        components.Add(new BalanceComponent(component.Attribute("sign").Value,
                                                            (BalanceSides)Enum.Parse(typeof(BalanceSides), component.Attribute("side").Value, true),
                                                            sources.First(s => s.Id == component.Attribute("source").Value),
                                                            component.Attribute("channel").Value,
                                                            component.Attribute("method").Value == "показания" ?
                                                            CalculateMethods.integral :
                                                            CalculateMethods.interval));
                    }
                    result.Add(new Balance(element.Attribute("name").Value,
                                           components));
                }
            }
            catch (Exception ex)
            {
                formErrorMessage frm = new formErrorMessage("Операция: получение описаний балансов",
                                                            new Tuple <string, string>("Невозможно прочитать файл " + fileName,
                                                                                       ex.Message + Environment.NewLine + "Elements added by this point: " + result.Count));
                frm.ShowDialog();
                return(null);
            }
            return(result);
        }
Пример #2
0
        public static string ChannelName(Source source, string channelID)
        {
            object result;

            using (SqlConnection cn = new SqlConnection(ConnectionString(source)))
            {
                try
                {
                    cn.Open();
                }
                catch (Exception ex)
                {
                    formErrorMessage dlg = new formErrorMessage("Операция: получение имени канала",
                                                                new Tuple <string, string>("Не удалось подключиться к базе данных:",
                                                                                           ex.Message + Environment.NewLine + "Connection string = " +
                                                                                           cn.ConnectionString));
                    dlg.ShowDialog();
                    return("");
                }
                SqlCommand cmd = cn.CreateCommand();
                cmd.CommandText = source.SQL.Replace("{x}", channelID);
                try
                {
                    result = cmd.ExecuteScalar();
                }
                catch (Exception ex)
                {
                    formErrorMessage dlg = new formErrorMessage("Операция: получение имени канала",
                                                                new Tuple <string, string>("Ошибка при выполнении запроса к БД",
                                                                                           ex.Message + Environment.NewLine + Environment.NewLine + cmd.CommandText));
                    dlg.ShowDialog();
                    return("");
                }
            }
            if (result == null || Convert.IsDBNull(result))
            {
                formErrorMessage dlg = new formErrorMessage("Операция: получение имени канала",
                                                            new Tuple <string, string>("Запрос вернул пустое значение",
                                                                                       source.SQL.Replace("{x}", channelID)));
                dlg.ShowDialog();
                return("");
            }
            return(result.ToString());
        }
Пример #3
0
        public static List <Source> GetSources(string fileName)
        {
            XDocument xml;

            try
            {
                xml = XDocument.Load(fileName);
            }
            catch (Exception ex)
            {
                formErrorMessage frm = new formErrorMessage("Операция: получение источников",
                                                            new Tuple <string, string>("Невозможно загрузить файл " + fileName, ex.Message));
                frm.ShowDialog();
                return(null);
            }
            List <Source> result = new List <Source>();

            try
            {
                foreach (XElement element in xml.Descendants("source"))
                {
                    result.Add(new Source(element.Attribute("id").Value,
                                          element.Descendants("server").First().Value,
                                          element.Descendants("database").First().Value,
                                          element.Descendants("user").First().Value,
                                          element.Descendants("password").First().Value,
                                          element.Descendants("component").First().Value));
                }
            }
            catch (Exception ex)
            {
                formErrorMessage frm = new formErrorMessage("Операция: получение источников",
                                                            new Tuple <string, string>("Невозможно прочитать файл " + fileName,
                                                                                       ex.Message + Environment.NewLine + "Elements added by this point: " + result.Count));
                frm.ShowDialog();
                return(null);
            }
            return(result);
        }
Пример #4
0
        private void BtnCalc_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            int    leftRow     = 2;
            int    rightRow    = 2;
            double totalIN     = 0;
            double totalOUT    = 0;
            double consumption = 0;

            if (lstBalances.SelectedIndex >= 0)
            {
                dgvResult.Rows.Clear();
                Balance b = c.Balances[lstBalances.SelectedIndex];
                dgvResult.RowCount = Math.Max(
                    b.Components.Count(comp => comp.Side == BalanceSides.IN),
                    b.Components.Count(comp => comp.Side == BalanceSides.OUT)) + 2;
                foreach (BalanceComponent component in b.Components)
                {
                    try
                    {
                        consumption = DataProvider.GetConsumption(component.Source,
                                                                  component, dtpFrom.Value, dtpTill.Value);
                    }
                    catch (Exception ex)
                    {
                        formErrorMessage dlg = new formErrorMessage("Опреация: вычисление потребления",
                                                                    new Tuple <string, string>(
                                                                        ex.Message,
                                                                        (ex.InnerException != null) ?
                                                                        ex.InnerException.Message :
                                                                        "No additional info"));
                        dlg.ShowDialog();
                        this.Cursor = Cursors.Default;
                        return;
                    }
                    switch (component.Side)
                    {
                    case BalanceSides.IN:
                        dgvResult.Rows[leftRow].Cells[0].Value = component.Name;
                        dgvResult.Rows[leftRow].Cells[1].Value = consumption.ToString("N0",
                                                                                      System.Globalization.CultureInfo.CurrentCulture.NumberFormat);
                        totalIN += consumption;
                        leftRow++;
                        break;

                    case BalanceSides.OUT:
                        dgvResult.Rows[rightRow].Cells[3].Value = component.Name;
                        dgvResult.Rows[rightRow].Cells[2].Value = consumption.ToString("N0",
                                                                                       System.Globalization.CultureInfo.CurrentCulture.NumberFormat);
                        totalOUT += consumption;
                        rightRow++;
                        break;
                    }
                    dgvResult.Refresh();
                }
                dgvResult.Rows[1].DefaultCellStyle.Font =
                    new Font(dgvResult.DefaultCellStyle.Font, FontStyle.Bold);
                dgvResult.Rows[1].Cells[0].Value = "ИТОГО:";
                dgvResult.Rows[1].Cells[1].Value = totalIN.ToString("N0",
                                                                    System.Globalization.CultureInfo.CurrentCulture.NumberFormat);
                dgvResult.Rows[1].Cells[2].Value = totalOUT.ToString("N0",
                                                                     System.Globalization.CultureInfo.CurrentCulture.NumberFormat);
                if (totalIN == 0 && totalOUT == 0)
                {
                    formErrorMessage dlg = new formErrorMessage("Опреация: вычисление небаланса",
                                                                "На ноль делить нельзя!");
                    dlg.ShowDialog();
                    this.Cursor = Cursors.Default;
                    return;
                }
                dgvResult.Rows[0].DefaultCellStyle.Font =
                    new Font(dgvResult.DefaultCellStyle.Font, FontStyle.Bold);
                dgvResult.Rows[0].DefaultCellStyle.BackColor = dgvResult.DefaultCellStyle.BackColor;
                dgvResult.Rows[0].Cells[0].Value             = "Небаланс=";
                double disbalance = Math.Abs(totalIN - totalOUT);
                dgvResult.Rows[0].Cells[1].Value = disbalance.ToString("N0",
                                                                       System.Globalization.CultureInfo.CurrentCulture.NumberFormat) +
                                                   " кВт-ч";
                disbalance = 2 * disbalance / (totalIN + totalOUT);
                dgvResult.Rows[0].Cells[2].Value = disbalance.ToString("P2",
                                                                       System.Globalization.CultureInfo.CurrentCulture.NumberFormat);
                this.Cursor = Cursors.Default;
            }
        }